04 WIFI Information Acquisition Application Case
1. Case Function Overview
The core function of this case is to implement dynamic/static IP mode switching and network configuration management for HarmonyOS device Ethernet, as follows:
- Ethernet Interface Information Acquisition: Automatically detect active Ethernet interfaces on the device (such as
eth0) and obtain the current interface's IP, gateway, subnet mask, and other configurations. - IP Mode Switching: Use interface buttons to switch between dynamic IP (automatically assigned by router DHCP) and static IP (manually entered) modes.
- Configuration Verification: After switching modes or modifying static IP, update the interface display in real-time and output logs to verify whether the network configuration was successfully applied.
2. Case Code Display
import ethernet from '@ohos.net.ethernet'
import { BusinessError } from '@ohos.base';
@Entry
@Component
struct Index {
@State message: string = 'EthernetDemo';
private TAG : string = 'ent_Demo'
@State entModeTest : string = 'Current Dynamic IP'
@State entName : string = "eth0"
@State entModeStatus : boolean = true;//Dynamic/Static IP judgment
@State entMsg : string = ''
@State entIp : string = ''
@State entRoute : string = ''
@State entGateway : string = ''
@State entMask : string = ''
@State entDNS : string = ''
@State entMode : number = 1;
aboutToAppear(): void {
this.getAllActiveIfaces();
this.getIfaceConfig();
}
setIfaceConfig(){
let config: ethernet.InterfaceConfiguration = {
mode: this.entMode,
ipAddr: this.entIp,
route: this.entRoute,
gateway: this.entGateway,
netMask: this.entMask,
dnsServers: this.entDNS
};
const setConfigPromise = ethernet.setIfaceConfig("eth0", config);
setConfigPromise.then(() => {
console.log(this.TAG,"setIfaceConfig promise ok");
}).catch((error: BusinessError) => {
console.error(this.TAG,"setIfaceConfig promise error = " + JSON.stringify(error));
});
}
getIfaceConfig(){
ethernet.getIfaceConfig(this.entName).then((data: ethernet.InterfaceConfiguration) => {
console.log(this.TAG,"getIfaceConfig promise mode = " + data.mode);
console.log(this.TAG,"getIfaceConfig promise ipAddr = " + JSON.stringify(data.ipAddr));
console.log(this.TAG,"getIfaceConfig promise route = " + JSON.stringify(data.route));
console.log(this.TAG,"getIfaceConfig promise gateway = " + JSON.stringify(data.gateway));
console.log(this.TAG,"getIfaceConfig promise netMask = " + JSON.stringify(data.netMask));
console.log(this.TAG,"getIfaceConfig promise dnsServers = " + JSON.stringify(data.dnsServers));
if (data.mode == 0) {
this.entModeStatus = false;
}else {
this.entModeStatus = true
}
this.entMode = data.mode
this.entMsg = JSON.stringify(data).toString();
this.entRoute = data.route.toString();
this.entGateway = data.gateway.toString();
this.entMask = data.netMask.toString();
this.entDNS = data.dnsServers.toString();
}).catch((error: BusinessError) => {
console.error(this.TAG,"getIfaceConfig promise error = " + JSON.stringify(error));
});
}
isIfaceActive(){
ethernet.isIfaceActive("eth0").then((data: number) => {
console.log(this.TAG,"isIfaceActive promise = " + JSON.stringify(data));
}).catch((error: BusinessError) => {
console.log(this.TAG,"isIfaceActive promise error = " + JSON.stringify(error));
});
}
getAllActiveIfaces(){
ethernet.getAllActiveIfaces().then((data: string[]) => {
console.log(this.TAG,"getAllActiveIfaces promise data.length = " + JSON.stringify(data.length));
if (JSON.stringify(data.length) == '1' ) {
console.log(this.TAG,'data.length')
}
for (let i = 0; i < data.length; i++) {
console.log(this.TAG,"getAllActiveIfaces promise = " + JSON.stringify(data[i]));
}
}).catch((error:BusinessError) => {
console.error(this.TAG,"getAllActiveIfaces promise error = " + JSON.stringify(error));
});
}
build() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.padding(20)
Button('Click to Switch Dynamic/Static IP')
.onClick(()=>{
if (this.entMode == 0) {
this.entMode = 1;
this.entModeTest = 'Current Dynamic IP'
this.entModeStatus = true;
}else{
this.entMode = 0;
this.entModeTest = 'Current Static IP'
this.entModeStatus = false;
}
this.setIfaceConfig();
this.getIfaceConfig();
})
if (this.entModeStatus){
Column(){
TextInput({placeholder : 'Static IP'})
.onChange((value : string)=>{
this.entIp = value
})
}
.height(80)
.width(300)
.padding(10)
.margin(10)
}
Text(this.entModeTest)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.padding(30)
Column(){
Text('Interface Information')
Blank()
Text(this.entMsg)
}
.height(80)
.padding(10)
}
.width('100%')
}
}3. Case Source Code and HAP Package Acquisition
3.1 Obtaining the Case Source Code
- Download EntDemo_API12.zip from Baidu Netdisk. Link:
Extraction code:
Note:
Project Path:
ShimetaPi OpenHarmony Resources>SC-3568HA>05-Development Resources>01-OpenHarmony Development Resources>APP Demo>EntDemo_API12.zip
- Extract the zip file and open the project in DevEco Studio to view the source code.
3.2 Obtaining the Case HAP Package
- Download EntDemo_API12.hap from Baidu Netdisk. Link:
Extraction code:

Note:
File Path:
ShimetaPi OpenHarmony Resources>SC-3568HA>05-Development Resources>01-OpenHarmony Development Resources>APP Demo>Hap_Package>EntDemo_API12.hap
- The process of installing the case through HAP is the same as the previous UART Assistant and Graphics Tablet cases, so it will not be repeated here.
4. Case Function Introduction
- Click the desktop icon to enter the application interface.

Note:
When using this application, connect the development board to the computer or router via Ethernet cable.
- The application interface is as follows:

At this point, it defaults to dynamic IP display, and the interface information section at the bottom shows the current interface configuration (IP, gateway, subnet mask, etc.).
- You can view this information in the DevEco Studio software log as follows:

The interface information contains two parts: all currently active interfaces and the current interface configuration information.
- data.length = 1 and eth0 indicate that the device has 1 active interface, named eth0 (consistent with entName in the code, no modification needed).
- Exception: If data.length = 0, check if the Ethernet cable is properly connected, the router is powered on, or the device interface is damaged.
- mode = 1 indicates the current is dynamic IP mode (1 = dynamic, 0 = static), ipAddr is the IP assigned by the router through DHCP, and gateway is the router gateway. These values being non-empty indicate successful initialization.
- The application also supports dynamic/static IP switching, as follows:
① Fill in the interface IP at "Static IP", using "192.168.102.43" as an example;
② Click the switch button, which completes the switch from dynamic IP to static IP. The interface information at the bottom will be synchronized and updated.

- You can also view this information in the DevEco Studio software log.
Displaying "ent_Demo setIfaceConfig promise ok" indicates successful switching

At the same time, the displayed interface information will also be synchronized and updated. The "ipAddr" should be the IP you set.

Note:
Common Error Explanations:
- Permission denied: Check if SET_NETWORK_INFO and GET_NETWORK_INFO permissions are added in module.json5.
- Interface not found: The interface name may not be eth0. Check the actual interface name (such as eth1) via the device command line ifconfig, and modify the entName variable in the code.
- Invalid configuration: Static IP format error (such as 192.168.1.256), or gateway and IP are not in the same network segment.
