Wi-Fi Information Acquisition Application Demo
1. Demo Function Overview
The core function of this demo is to implement dynamic/static IP mode switching and network configuration management for the Ethernet of the OpenHarmony device, as follows:
- Network interface information acquisition: automatically detects the device's active Ethernet interface (such as
eth0) and obtains the current IP, gateway, subnet mask, and other configuration of the interface. - IP mode switching: through interface buttons, switch between dynamic IP (IP automatically assigned by the router DHCP) and static IP (manually entered specified IP) modes.
- Configuration validation: after switching modes or modifying the static IP, the interface display is updated in real time and logs are output to verify whether the network configuration was applied successfully.
2. Demo Code Listing
import ethernet from '@ohos.net.ethernet'
import { BusinessError } from '@ohos.base';
@Entry
@Component
struct Index {
@State message: string = '以太网Demo';
private TAG : string = 'ent_Demo'
@State entModeTest : string = '当前动态Ip'
@State entName : string = "eth0"
@State entModeStatus : boolean = true;//动/静态Ip的判断
@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('点击切换动/静态IP')
.onClick(()=>{
if (this.entMode == 0) {
this.entMode = 1;
this.entModeTest = '当前动态Ip'
this.entModeStatus = true;
}else{
this.entMode = 0;
this.entModeTest = '当前静态Ip'
this.entModeStatus = false;
}
this.setIfaceConfig();
this.getIfaceConfig();
})
if (this.entModeStatus){
Column(){
TextInput({placeholder : '静态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('网口信息')
Blank()
Text(this.entMsg)
}
.height(80)
.padding(10)
}
.width('100%')
}
}3. Demo Source Code and HAP Package Acquisition
3.1 Demo Source Code Acquisition
- Download EntDemo_API12.zip from the Baidu Netdisk materials. Link: Extraction code:
Note:
Project path:ShimetaPi OpenHarmony Materials>SC-3568HA>05-Development Materials>01-OpenHarmory Development Materials>APP Demo>EntDemo_API12.zip
- After extracting the compressed package, open the project through DevEco Studio to view the source code.
3.2 Demo HAP Package Acquisition
- Download EntDemo_API12.hap from the Baidu Netdisk materials. Link: Extraction code:

Note:
File path:ShimetaPi OpenHarmony Materials>SC-3568HA>05-Development Materials>01-OpenHarmory Development Materials>APP Demo>Hap_Package>EntDemo_API12.hap
- The HAP installation process for this demo is the same as the previous serial-debug-assistant and writing-board demos, and will not be repeated here.
4. Demo Function Introduction
- Click the desktop icon to enter the application interface.

Note:
When using this application, the development board must be connected to a PC or router via a network cable.
- The application interface is as follows:

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

The network-interface information contains two parts: all currently active interfaces and the current interface's configuration information.
- data.length = 1 and eth0 indicate that the device has 1 active interface named eth0 (consistent with entName in the code; no need to modify).
- Exception: if data.length = 0, check whether the network cable is firmly plugged in, whether the router is powered on, or whether the device's network port is damaged.
- mode = 1 indicates that the current mode is dynamic IP (1 = dynamic, 0 = static), ipAddr is the IP assigned by the router via DHCP, and gateway is the router gateway. These values being non-empty indicates that initialization succeeded.
- The application also supports switching between dynamic and static IP, as follows: ① Fill in the interface IP at "static ip", taking "192.168.102.43" as an example. ② Click the switch button. At this point, the switch from dynamic IP to static IP is complete, and the network-interface information section at the bottom will be updated synchronously.

- You can also view this information in the log area of the DevEco Studio software.
Displaying "ent _Demo setIfaceconfig promise ok" indicates that the switch succeeded.

At the same time, the displayed network-interface information is also updated synchronously, and the "ipAddr" should be the IP you set yourself.

Note:
Common error interpretation:
- Permission denied: check whether SET_NETWORK_INFO and GET_NETWORK_INFO permissions are added in module.json5.
- Interface not found: the interface name may not be eth0. Use the device command line
ifconfigto view the actual interface name (such as eth1) and modify the entName variable in the code. - Invalid configuration: static IP format error (such as 192.168.1.256), or the gateway and IP are not in the same network segment.
