harmony 鸿蒙Ethernet Connection (For System Applications Only)

  • 2025-06-12
  • 浏览 (5)

Ethernet Connection (For System Applications Only)

Overview

The Ethernet Connection module allows a device to access the Internet through a network cable. After a device is connected to the Ethernet through a network cable, the device can obtain a series of network attributes, such as the dynamically allocated IP address, subnet mask, gateway, and DNS. You can manually configure and obtain the network attributes of the device in static mode.

NOTE To maximize the application running efficiency, most API calls are called asynchronously in callback or promise mode. The following code examples use the promise mode. For details about the APIs, see API Reference.

Constraints

  • Programming language: JS
  • The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Scenario

Typical application scenarios of Ethernet connection are as follows:

  • Dynamically assigning a series of network attributes, such as the IP address, subnet mask, gateway, and DNS in DHCP mode to enable network access
  • Configuring a series of network attributes, such as the IP address, subnet mask, gateway, and DNS, in static mode to enable network access.

The following describes the development procedure specific to each application scenario.

Available APIs

For the complete list of APIs and example code, see Ethernet Connection.

Type API Description
setIfaceConfig(iface: string, ic: InterfaceConfiguration, callback: AsyncCallback<void>): void Configures the network attributes of the specified Ethernet network. This API uses an asynchronous callback to return the result. Sets the network interface configuration.
getIfaceConfig(iface: string, callback: AsyncCallback<InterfaceConfiguration>): void Obtains the network attributes of the specified Ethernet network. This API uses an asynchronous callback to return the result. Obtains the network interface configuration.
isIfaceActive(iface: string, callback: AsyncCallback<number>): void Checks whether the specified network port is active. This API uses an asynchronous callback to return the result. Checks whether the specified network port is active.
getAllActiveIfaces(callback: AsyncCallback<Array<string>>): void Obtains the list of all active network ports. This API uses an asynchronous callback to return the result. Obtains the list of all active network ports.
on(type: ‘interfaceStateChange’, callback: Callback<{ iface: string, active: boolean }>): void Subscribes to interface state change events. Subscribes to NIC hot swap events.
off(type: ‘interfaceStateChange’, callback?: Callback<{ iface: string, active: boolean }>): void Unsubscribes from interface state change events. Unsubscribes from NIC hot swap events.

Ethernet Connection – DHCP Mode

  1. Use a network cable to connect the device to a network port.
  2. Import the ethernet namespace from @kit.NetworkKit.
  3. Call getAllActiveIfaces to obtain the list of all active network ports, for example, eth0 and eth1.
  4. Call isIfaceActive in user mode to check whether the eth0 port is active.
  5. Call getIfaceConfig in user mode to obtain the static network attributes of the eth0 port. By default, an unconfigured Ethernet network uses the DHCP mode, in which the Ethernet network obtains the automatically assigned network attributes.
// Import the ethernet namespace from @kit.NetworkKit.
import { ethernet } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

// Call getAllActiveIfaces to obtain the list of all active network ports.
ethernet.getAllActiveIfaces().then((data: string[]) => {
  console.log("getAllActiveIfaces promise data.length = " + JSON.stringify(data.length));
  for (let i = 0; i < data.length; i++) {
    console.log("getAllActiveIfaces promise  = " + JSON.stringify(data[i]));
  }
}).catch((error:BusinessError) => {
  console.log("getAllActiveIfaces promise error = " + JSON.stringify(error));
});

// Call isIfaceActive to check whether the specified network port is active.
ethernet.isIfaceActive("eth0").then((data: number) => {
  console.log("isIfaceActive promise = " + JSON.stringify(data));
}).catch((error: BusinessError) => {
  console.log("isIfaceActive promise error = " + JSON.stringify(error));
});

// Call getIfaceConfig to obtain the network attributes of the specified Ethernet network.
ethernet.getIfaceConfig("eth0").then((data: ethernet.InterfaceConfiguration) => {
  console.log("getIfaceConfig promise mode = " + JSON.stringify(data.mode));
  console.log("getIfaceConfig promise ipAddr = " + JSON.stringify(data.ipAddr));
  console.log("getIfaceConfig promise route = " + JSON.stringify(data.route));
  console.log("getIfaceConfig promise gateway = " + JSON.stringify(data.gateway));
  console.log("getIfaceConfig promise netMask = " + JSON.stringify(data.netMask));
  console.log("getIfaceConfig promise dnsServers = " + JSON.stringify(data.dnsServers));
}).catch((error: BusinessError) => {
  console.log("getIfaceConfig promise error = " + JSON.stringify(error));
});

Ethernet Connection – Static Mode

How to Develop

  1. Use a network cable to connect the device to a network port.
  2. Import the ethernet namespace from @kit.NetworkKit.
  3. Call getAllActiveIfaces in user mode to obtain the list of all active network ports, for example, eth0 and eth1.
  4. Call isIfaceActive in user mode to check whether the eth0 port is active.
  5. Call setIfaceConfig in user mode to set the eth0 port to the static mode, in which you need to manually assign the network attributes (including the IP address, subnet mask, gateway, and DNS).
  6. Call getIfaceConfig in user mode to obtain the static network attributes of the eth0 port.
// Import the ethernet namespace from @kit.NetworkKit.
import { ethernet } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

// Call getAllActiveIfaces to obtain the list of all active network ports.
ethernet.getAllActiveIfaces().then((data: string[]) => {
  console.log("getAllActiveIfaces promise data.length = " + JSON.stringify(data.length));
  for (let i = 0; i < data.length; i++) {
    console.log("getAllActiveIfaces promise  = " + JSON.stringify(data[i]));
  }
}).catch((error:BusinessError) => {
  console.log("getAllActiveIfaces promise error = " + JSON.stringify(error));
});

// Call isIfaceActive to check whether the specified network port is active.
ethernet.isIfaceActive("eth0").then((data: number) => {
  console.log("isIfaceActive promise = " + JSON.stringify(data));
}).catch((error: BusinessError) => {
  console.log("isIfaceActive promise error = " + JSON.stringify(error));
});

// Call setIfaceConfig to configure the network attributes of the specified Ethernet network.
let config: ethernet.InterfaceConfiguration = {
  mode: 0,
  ipAddr: "192.168.xx.xxx",
  route: "192.168.xx.xxx",
  gateway: "192.168.xx.xxx",
  netMask: "255.255.255.0",
  dnsServers: "1.1.1.1"
};

const setConfigPromise = ethernet.setIfaceConfig("eth0", config);

setConfigPromise.then(() => {
  console.log("setIfaceConfig promise ok");
}).catch((error: BusinessError)  => {
  console.log("setIfaceConfig promise error = " + JSON.stringify(error));
});

// Call getIfaceConfig to obtain the network attributes of the specified Ethernet network.
ethernet.getIfaceConfig("eth0").then((data: ethernet.InterfaceConfiguration) => {
  console.log("getIfaceConfig promise mode = " + JSON.stringify(data.mode));
  console.log("getIfaceConfig promise ipAddr = " + JSON.stringify(data.ipAddr));
  console.log("getIfaceConfig promise route = " + JSON.stringify(data.route));
  console.log("getIfaceConfig promise gateway = " + JSON.stringify(data.gateway));
  console.log("getIfaceConfig promise netMask = " + JSON.stringify(data.netMask));
  console.log("getIfaceConfig promise dnsServers = " + JSON.stringify(data.dnsServers));
}).catch((error: BusinessError) => {
  console.log("getIfaceConfig promise error = " + JSON.stringify(error));
});

Subscribes the status change of network device interfaces.

How to Develop

  1. Import the ethernet namespace from @kit.NetworkKit.
  2. Call the on() method to subscribe to interfaceStateChange events. It is up to you whether to listen for interfaceStateChange events.
  3. Check whether an interfaceStateChange event is triggered when the interface state changes.
  4. Call the off() method to unsubscribe from interfaceStateChange events.
// Import the ethernet namespace from @kit.NetworkKit.
import { ethernet } from '@kit.NetworkKit';

// Subscribe to interfaceStateChange events.
class EthernetData{
  iface: string = ""
  active: boolean = false
}

ethernet.on('interfaceStateChange', (data: EthernetData) => {
  console.log(JSON.stringify(data));
});

// Unsubscribe from interfaceStateChange events.
ethernet.off('interfaceStateChange');

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Network Kit

harmony 鸿蒙HTTP Data Request

harmony 鸿蒙Network Connection Development

harmony 鸿蒙WebSocket Connection (C/C++)

harmony 鸿蒙Network Connection Management

harmony 鸿蒙MDNS Management

harmony 鸿蒙Introduction to Network Kit

harmony 鸿蒙Network Firewall

harmony 鸿蒙Network Sharing (For System Applications Only)

harmony 鸿蒙Traffic Management

0  赞