openharmony 鸿蒙 js-apis-net-ethernet-sys

2025-06-12 浏览 (1)

@ohos.net.ethernet (Ethernet Connection Management) (System API)

The ethernet module provides wired network capabilities, which allow users to set the IP address, subnet mask, gateway, and Domain Name System (DNS) server, and HTTP proxy of a wired network.

NOTE 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. The APIs provided by this module are system APIs.

Modules to Import

import { ethernet } from '@kit.NetworkKit';

ethernet.setIfaceConfig9+

setIfaceConfig(iface: string, ic: InterfaceConfiguration, callback: AsyncCallback<void>): void

Sets the network interface configuration. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.Ethernet

Parameters

NameTypeMandatoryDescription
ifacestringYesInterface name.
icInterfaceConfigurationYesNetwork interface configuration to set.
callbackAsyncCallback<void>YesCallback used to return the result. If the operation is successful, the return result is empty. If the operation fails, an error code is returned.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2200001Invalid parameter value.
2200002Failed to connect to the service.
2200003System internal error.
2201004Invalid Ethernet profile.
2201005Device information does not exist.
2201006Ethernet device not connected.
2201007Ethernet failed to write user configuration information.

Example

import { ethernet } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

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"
};

ethernet.setIfaceConfig("eth0", config, (error: BusinessError) => {
  if (error) {
    console.log("setIfaceConfig callback error = " + JSON.stringify(error));
  } else {
    console.log("setIfaceConfig callback ok");
  }
});

ethernet.setIfaceConfig9+

setIfaceConfig(iface: string, ic: InterfaceConfiguration): Promise<void>

Sets the network interface configuration. This API uses a promise to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.Ethernet

Parameters

NameTypeMandatoryDescription
ifacestringYesInterface name.
icInterfaceConfigurationYesNetwork interface configuration to set.

Return value

TypeDescription
Promise<void>Promise used to return the result. If the operation is successful, the return result is empty. If the operation fails, an error code is returned.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2200001Invalid parameter value.
2200002Failed to connect to the service.
2200003System internal error.
2201004Invalid Ethernet profile.
2201005Device information does not exist.
2201006Ethernet device not connected.
2201007Ethernet failed to write user configuration information.

Example

import { ethernet } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

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));
});

ethernet.getIfaceConfig9+

getIfaceConfig(iface: string, callback: AsyncCallback<InterfaceConfiguration>): void

Obtains the configuration of a network interface. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.GET_NETWORK_INFO

System capability: SystemCapability.Communication.NetManager.Ethernet

Parameters

NameTypeMandatoryDescription
ifacestringYesInterface name.
callbackAsyncCallback<InterfaceConfiguration>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2200001Invalid parameter value.
2200002Failed to connect to the service.
2200003System internal error.
2201005Device information does not exist.

Example

import { ethernet } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

ethernet.getIfaceConfig("eth0", (error: BusinessError, value: ethernet.InterfaceConfiguration) => {
  if (error) {
    console.log("getIfaceConfig  callback error = " + JSON.stringify(error));
  } else {
    console.log("getIfaceConfig callback mode = " + JSON.stringify(value.mode));
    console.log("getIfaceConfig callback ipAddr = " + JSON.stringify(value.ipAddr));
    console.log("getIfaceConfig callback route = " + JSON.stringify(value.route));
    console.log("getIfaceConfig callback gateway = " + JSON.stringify(value.gateway));
    console.log("getIfaceConfig callback netMask = " + JSON.stringify(value.netMask));
    console.log("getIfaceConfig callback dnsServers = " + JSON.stringify(value.dnsServers));
  }
});

ethernet.getIfaceConfig9+

getIfaceConfig(iface: string): Promise<InterfaceConfiguration>

Obtains the configuration of a network interface. This API uses a promise to return the result.

System API: This is a system API.

Required permission: ohos.permission.GET_NETWORK_INFO

System capability: SystemCapability.Communication.NetManager.Ethernet

Parameters

NameTypeMandatoryDescription
ifacestringYesInterface name.

Return value

TypeDescription
Promise<InterfaceConfiguration>Promise used to return the result.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2200001Invalid parameter value.
2200002Failed to connect to the service.
2200003System internal error.
2201005Device information does not exist.

Example

import { ethernet } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

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.isIfaceActive9+

isIfaceActive(iface: string, callback: AsyncCallback<number>): void

Checks whether a network interface is active. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.GET_NETWORK_INFO

System capability: SystemCapability.Communication.NetManager.Ethernet

Parameters

NameTypeMandatoryDescription
ifacestringYesInterface name. If this parameter is left empty, the API checks for any active network interface.
callbackAsyncCallback<number>YesCallback used to return the result. The value 1 means that the network interface is active, 0 means that the network interface is inactive, and any other value means that an error has occurred.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2200001Invalid parameter value.
2200002Failed to connect to the service.
2200003System internal error.
2201005Device information does not exist.

Example

import { ethernet } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

ethernet.isIfaceActive("eth0", (error: BusinessError, value: number) => {
  if (error) {
    console.log("whether2Activate callback error = " + JSON.stringify(error));
  } else {
    console.log("whether2Activate callback = " + JSON.stringify(value));
  }
});

ethernet.isIfaceActive9+

isIfaceActive(iface: string): Promise<number>

Checks whether a network interface is active. This API uses a promise to return the result.

System API: This is a system API.

Required permission: ohos.permission.GET_NETWORK_INFO

System capability: SystemCapability.Communication.NetManager.Ethernet

Parameters

NameTypeMandatoryDescription
ifacestringYesInterface name. If this parameter is left empty, the API checks for any active network interface.

Return value

TypeDescription
Promise<number>Promise used to return the result. The value 1 means that the network interface is active, 0 means that the network interface is inactive, and any other value means that an error has occurred.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2200001Invalid parameter value.
2200002Failed to connect to the service.
2200003System internal error.
2201005Device information does not exist.

Example

import { ethernet } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

ethernet.isIfaceActive("eth0").then((data: number) => {
  console.log("isIfaceActive promise = " + JSON.stringify(data));
}).catch((error: BusinessError) => {
  console.log("isIfaceActive promise error = " + JSON.stringify(error));
});

ethernet.getAllActiveIfaces9+

getAllActiveIfaces(callback: AsyncCallback<Array<string>>): void

Obtains the list of all active network interfaces. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.GET_NETWORK_INFO

System capability: SystemCapability.Communication.NetManager.Ethernet

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<Array<string>>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
2200002Failed to connect to the service.
2200003System internal error.

Example

import { ethernet } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

ethernet.getAllActiveIfaces((error: BusinessError, value: string[]) => {
  if (error) {
    console.log("getAllActiveIfaces callback error = " + JSON.stringify(error));
  } else {
    console.log("getAllActiveIfaces callback value.length = " + JSON.stringify(value.length));
    for (let i = 0; i < value.length; i++) {
      console.log("getAllActiveIfaces callback = " + JSON.stringify(value[i]));
    }
  }
});

ethernet.getAllActiveIfaces9+

getAllActiveIfaces(): Promise<Array<string>>

Obtains the list of all active network interfaces. This API uses a promise to return the result.

System API: This is a system API.

Required permission: ohos.permission.GET_NETWORK_INFO

System capability: SystemCapability.Communication.NetManager.Ethernet

Return value

TypeDescription
Promise<Array<string>>Promise used to return the result.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
2200002Failed to connect to the service.
2200003System internal error.

Example

import { ethernet } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

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));
});

ethernet.on('interfaceStateChange')10+

on(type: 'interfaceStateChange', callback: Callback<InterfaceStateInfo>): void

Registers an observer for NIC hot swap events. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.GET_NETWORK_INFO

System capability: SystemCapability.Communication.NetManager.Ethernet

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is interfaceStateChange.
callbackAsyncCallback<InterfaceStateInfo>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.

Example

import { ethernet } from '@kit.NetworkKit';

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

ethernet.off('interfaceStateChange')10+

off(type: 'interfaceStateChange', callback?: Callback<InterfaceStateInfo>): void

Unregisters the observer for NIC hot swap events. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.GET_NETWORK_INFO

System capability: SystemCapability.Communication.NetManager.Ethernet

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is interfaceStateChange.
callbackAsyncCallback<InterfaceStateInfo>NoCallback used to return the result.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.

Example

import { ethernet } from '@kit.NetworkKit';

ethernet.off('interfaceStateChange');

InterfaceConfiguration9+

Defines the network configuration for the Ethernet connection.

System API: This is a system API.

System capability: SystemCapability.Communication.NetManager.Ethernet

NameTypeMandatoryDescription
modeIPSetModeYesConfiguration mode of the Ethernet connection.
ipAddrstringYesStatic IP address of the Ethernet connection. The value must be an IPv4 address, which is a 32-bit number displayed in dotted decimal notation and each 8-bit field ranges from 0 to 255. This parameter does not need to be configured in Dynamic Host Configuration Protocol (DHCP) mode.
routestringYesRoute of the Ethernet connection. The value must be an IPv4 address, which is a 32-bit number displayed in dotted decimal notation and each 8-bit field ranges from 0 to 255. This parameter does not need to be configured in DHCP mode.
gatewaystringYesGateway of the Ethernet connection. The value must be an IPv4 address, which is a 32-bit number displayed in dotted decimal notation and each 8-bit field ranges from 0 to 255. This parameter does not need to be configured in DHCP mode.
netMaskstringYesSubnet mask of the Ethernet connection. The value must be an IPv4 address, which is a 32-bit number displayed in dotted decimal notation and each 8-bit field ranges from 0 to 255. This parameter does not need to be configured in DHCP mode.
dnsServersstringYesDNS server addresses of the Ethernet connection. The value must be an IPv4 address, which is a 32-bit number displayed in dotted decimal notation and each 8-bit field ranges from 0 to 255. This parameter does not need to be configured in DHCP mode. Multiple addresses are separated by commas (,).
httpProxy10+HttpProxyNoHTTP proxy of the Ethernet connection. By default, no proxy is configured.

InterfaceStateInfo11+

Listens for status changes of an Ethernet NIC.

System API: This is a system API.

System capability: SystemCapability.Communication.NetManager.Ethernet

NameTypeMandatoryDescription
ifacestringYesName of the Ethernet NIC.
activebooleanYesWhether the Ethernet NIC is activated. The value true indicates the NIC is activated, and the value false indicates the opposite.

IPSetMode9+

Defines the configuration mode of the Ethernet connection.

System API: This is a system API.

System capability: SystemCapability.Communication.NetManager.Ethernet

NameValueDescription
STATIC0Static network configuration for an Ethernet connection.
DHCP1Dynamic network configuration for an Ethernet connection.
LAN_STATIC11+2Static network configuration for a LAN connection.
LAN_DHCP11+3Dynamic network configuration for a LAN connection.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Network Kit

harmony 鸿蒙NetConn_ConnectionProperties

harmony 鸿蒙NetConn_HttpProxy

harmony 鸿蒙NetConn_NetAddr

harmony 鸿蒙NetConn_NetCapabilities

harmony 鸿蒙NetConn_NetConnCallback

harmony 鸿蒙NetConn_NetHandle

harmony 鸿蒙NetConn_NetHandleList

harmony 鸿蒙NetConn_NetSpecifier

harmony 鸿蒙NetConn_Route

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/8ujUHu