• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.net.ethernet (以太网连接管理)
2
3以太网连接管理主要提供有线网络能力,提供设置有线网络的IP地址,子网掩码,网关,DNS等信息
4
5> **说明:**
6> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
7
8## 导入模块
9
10```js
11import ethernet from '@ohos.net.ethernet'
12```
13
14## ethernet.setIfaceConfig
15
16setIfaceConfig(iface: string, ic: InterfaceConfiguration, callback: AsyncCallback\<void>): void
17
18设置网络接口配置信息,使用callback方式作为异步方法。
19
20**系统接口**:此接口为系统接口。
21
22**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
23
24**系统能力**:SystemCapability.Communication.NetManager.Ethernet
25
26**参数:**
27
28| 参数名   | 类型                                              | 必填 | 说明                                       |
29| -------- | ------------------------------------------------- | ---- | ------------------------------------------ |
30| iface    | string                                            | 是   | 网络接口名                                     |
31| ic       | [InterfaceConfiguration](#interfaceconfiguration) | 是   | 要设置的网络接口配置信息                   |
32| callback | AsyncCallback\<void>                     | 是   | 回调函数,成功无返回,失败返回对应错误码。 |
33
34**错误码:**
35
36| 错误码ID | 错误信息                                 |
37| ------- | ----------------------------------------|
38| 201     | Permission denied.                      |
39| 401     | Parameter error.                        |
40| 2200001 | Invalid parameter value.                |
41| 2200002 | Operation failed. Cannot connect to service.|
42| 2200003 | System internal error.                  |
43| 2201005 | The device information does not exist.  |
44| 2201006 | Device disconnected.                    |
45| 2201007 | Failed to write the user configuration.    |
46
47**示例:**
48
49```js
50ethernet.setIfaceConfig("eth0", {
51    mode: 0,
52    ipAddr: "192.168.xx.xxx",
53    route: "192.168.xx.xxx",
54    gateway: "192.168.xx.xxx",
55    netMask: "255.255.255.0",
56    dnsServers: "1.1.1.1",
57    domain: "2.2.2.2"
58}, (error) => {
59    if (error) {
60        console.log("setIfaceConfig callback error = " + JSON.stringify(error));
61    } else {
62        console.log("setIfaceConfig callback ok ");
63    }
64});
65```
66
67## ethernet.setIfaceConfig
68
69setIfaceConfig(iface: string, ic: InterfaceConfiguration): Promise\<void>
70
71设置网络接口配置信息,使用Promise方式作为异步方法。
72
73**系统接口**:此接口为系统接口。
74
75**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
76
77**系统能力**:SystemCapability.Communication.NetManager.Ethernet
78
79**参数:**
80
81| 参数名 | 类型                                              | 必填 | 说明                     |
82| ------ | ------------------------------------------------- | ---- | ------------------------ |
83| iface  | string                                            | 是   | 接口名                   |
84| ic     | [InterfaceConfiguration](#interfaceconfiguration) | 是   | 要设置的网络接口配置信息 |
85
86**返回值:**
87
88| 类型                | 说明                                                        |
89| ------------------- | ----------------------------------------------------------- |
90| Promise\<void>       | 以Promise形式返回执行结果。成功无返回,失败返回对应错误码。 |
91
92**错误码:**
93
94| 错误码ID | 错误信息                                 |
95| ------- | ----------------------------------------|
96| 201     | Permission denied.                      |
97| 401     | Parameter error.                        |
98| 2200001 | Invalid parameter value.                |
99| 2200002 | Operation failed. Cannot connect to service.|
100| 2200003 | System internal error.                  |
101| 2201005 | The device information does not exist.  |
102| 2201006 | Device disconnected.                   |
103| 2201007 | Failed to write the user configuration.    |
104
105**示例:**
106
107```js
108ethernet.setIfaceConfig("eth0", {
109    mode: 0,
110    ipAddr: "192.168.xx.xxx",
111    route: "192.168.xx.xxx",
112    gateway: "192.168.xx.xxx",
113    netMask: "255.255.255.0",
114    dnsServers: "1.1.1.1",
115    domain: "2.2.2.2"
116}).then(() => {
117    console.log("setIfaceConfig promise ok ");
118}).catch(error => {
119    console.log("setIfaceConfig promise error = " + JSON.stringify(error));
120});
121```
122
123## ethernet.getIfaceConfig
124
125getIfaceConfig(iface: string, callback: AsyncCallback\<InterfaceConfiguration>): void
126
127获取指定网络接口信息,使用callback方式作为异步方法。
128
129**系统接口**:此接口为系统接口。
130
131**需要权限**:ohos.permission.GET_NETWORK_INFO
132
133**系统能力**:SystemCapability.Communication.NetManager.Ethernet
134
135**参数:**
136
137| 参数名   | 类型                                            | 必填  | 说明         |
138| -------- | ----------------------------------------------- | ----- | ------------ |
139| iface    | string                                          | 是    | 指定网络接口 |
140| callback | AsyncCallback\<[InterfaceConfiguration](#interfaceconfiguration)> | 是    | 回调函数,返回指定网络接口信息   |
141
142**错误码:**
143
144| 错误码ID | 错误信息                                 |
145| ------- | ----------------------------------------|
146| 201     | Permission denied.                      |
147| 401     | Parameter error.                        |
148| 2200001 | Invalid parameter value.                |
149| 2200002 | Operation failed. Cannot connect to service.|
150| 2200003 | System internal error.                  |
151| 2201005 | The device information does not exist.  |
152
153**示例:**
154
155```js
156ethernet.getIfaceConfig("eth0", (error, value) => {
157    if (error) {
158        console.log("getIfaceConfig  callback error = " + JSON.stringify(error));
159    } else {
160        console.log("getIfaceConfig callback mode = " + JSON.stringify(value.mode));
161        console.log("getIfaceConfig callback ipAddr = " + JSON.stringify(value.ipAddr));
162        console.log("getIfaceConfig callback route = " + JSON.stringify(value.route));
163        console.log("getIfaceConfig callback gateway = " + JSON.stringify(value.gateway));
164        console.log("getIfaceConfig callback netMask = " + JSON.stringify(value.netMask));
165        console.log("getIfaceConfig callback dnsServers = " + JSON.stringify(value.dnsServers));
166        console.log("getIfaceConfig callback domain = " + JSON.stringify(value.domain));
167    }
168});
169```
170
171## ethernet.getIfaceConfig
172
173getIfaceConfig(iface: string): Promise\<InterfaceConfiguration>
174
175获取指定网络接口信息,使用Promise方式作为异步方法。
176
177**系统接口**:此接口为系统接口。
178
179**需要权限**:ohos.permission.GET_NETWORK_INFO
180
181**系统能力**:SystemCapability.Communication.NetManager.Ethernet
182
183**参数:**
184
185| 参数名   | 类型                                    | 必填 | 说明         |
186| -------- | --------------------------------------- | ---- | ------------ |
187| iface    | string                                  | 是   | 指定网络接口 |
188
189**返回值:**
190
191| 类型                              | 说明                               |
192| --------------------------------- | ---------------------------------- |
193| Promise\<[InterfaceConfiguration](#interfaceconfiguration)>   | 以Promise形式返回接口信息。        |
194
195**错误码:**
196
197| 错误码ID | 错误信息                                 |
198| ------- | ----------------------------------------|
199| 201     | Permission denied.                      |
200| 401     | Parameter error.                        |
201| 2200001 | Invalid parameter value.                |
202| 2200002 | Operation failed. Cannot connect to service.|
203| 2200003 | System internal error.                  |
204| 2201005 | The device information does not exist.  |
205
206**示例:**
207
208```js
209ethernet.getIfaceConfig("eth0").then((data) => {
210    console.log("getIfaceConfig promise mode = " + JSON.stringify(data.mode));
211    console.log("getIfaceConfig promise ipAddr = " + JSON.stringify(data.ipAddr));
212    console.log("getIfaceConfig promise route = " + JSON.stringify(data.route));
213    console.log("getIfaceConfig promise gateway = " + JSON.stringify(data.gateway));
214    console.log("getIfaceConfig promise netMask = " + JSON.stringify(data.netMask));
215    console.log("getIfaceConfig promise dnsServers = " + JSON.stringify(data.dnsServers));
216    console.log("getIfaceConfig promise domain = " + JSON.stringify(data.domain));
217}).catch(error => {
218    console.log("getIfaceConfig promise error = " + JSON.stringify(error));
219});
220```
221
222## ethernet.isIfaceActive
223
224isIfaceActive(iface: string, callback: AsyncCallback\<number>): void
225
226判断接口是否已激活,使用callback方式作为异步方法。
227
228**系统接口**:此接口为系统接口。
229
230**需要权限**:ohos.permission.GET_NETWORK_INFO
231
232**系统能力**:SystemCapability.Communication.NetManager.Ethernet
233
234**参数:**
235
236| 参数名   | 类型                        | 必填 | 说明                                               |
237| -------- | --------------------------- | ---- | -------------------------------------------------- |
238| iface    | string                      | 是   | 接口名。为空时代表查询是否存在激活接口             |
239| callback | AsyncCallback\<number>       | 是   | 回调函数,已激活:1,未激活:0,其他为获取失败错误码。 |
240
241**错误码:**
242
243| 错误码ID | 错误信息                                 |
244| ------- | ----------------------------------------|
245| 201     | Permission denied.                      |
246| 401     | Parameter error.                        |
247| 2200001 | Invalid parameter value.                |
248| 2200002 | Operation failed. Cannot connect to service.|
249| 2200003 | System internal error.                  |
250| 2201005 | The device information does not exist.  |
251
252**示例:**
253
254```js
255ethernet.isIfaceActive("eth0", (error, value) => {
256    if (error) {
257        console.log("whether2Activate callback error = " + JSON.stringify(error));
258    } else {
259        console.log("whether2Activate callback = " + JSON.stringify(value));
260    }
261});
262```
263
264## ethernet.isIfaceActive
265
266isIfaceActive(iface: string): Promise\<number>
267
268判断接口是否已激活,使用Promise方式作为异步方法。
269
270**系统接口**:此接口为系统接口。
271
272**需要权限**:ohos.permission.GET_NETWORK_INFO
273
274**系统能力**:SystemCapability.Communication.NetManager.Ethernet
275
276**参数:**
277
278| 参数名 | 类型   | 必填 | 说明                                   |
279| ------ | ------ | ---- | -------------------------------------- |
280| iface  | string | 是   | 接口名。为空时代表查询是否存在激活接口 |
281
282**返回值:**
283
284| 类型            | 说明                                                               |
285| ----------------| ------------------------------------------------------------------ |
286| Promise\<number> | 以Promise形式返回获取结果。已激活:1,未激活:0,其他为获取失败错误码。|
287
288**错误码:**
289
290| 错误码ID | 错误信息                                 |
291| ------- | ----------------------------------------|
292| 201     | Permission denied.                      |
293| 401     | Parameter error.                        |
294| 2200001 | Invalid parameter value.                |
295| 2200002 | Operation failed. Cannot connect to service.|
296| 2200003 | System internal error.                  |
297| 2201005 | The device information does not exist.  |
298
299**示例:**
300
301```js
302ethernet.isIfaceActive("eth0").then((data) => {
303    console.log("isIfaceActive promise = " + JSON.stringify(data));
304}).catch(error => {
305    console.log("isIfaceActive promise error = " + JSON.stringify(error));
306});
307```
308
309## ethernet.getAllActiveIfaces
310
311getAllActiveIfaces(callback: AsyncCallback\<Array\<string>>): void
312
313获取活动的网络接口,使用callback方式作为异步方法。
314
315**系统接口**:此接口为系统接口。
316
317**需要权限**:ohos.permission.GET_NETWORK_INFO
318
319**系统能力**:SystemCapability.Communication.NetManager.Ethernet
320
321**参数:**
322
323| 参数名   | 类型                                 | 必填 | 说明                           |
324| -------- | ------------------------------------ | ---- | ------------------------------ |
325| callback | AsyncCallback\<Array\<string>>         | 是   | 回调函数,返回值为对应接口名。 |
326
327**错误码:**
328
329| 错误码ID | 错误信息                                 |
330| ------- | ----------------------------------------|
331| 201     | Permission denied.                      |
332| 2200002 | Operation failed. Cannot connect to service.|
333| 2200003 | System internal error.                  |
334
335**示例:**
336
337```js
338ethernet.getAllActiveIfaces((error, value) => {
339    if (error) {
340        console.log("getAllActiveIfaces callback error = " + JSON.stringify(error));
341    } else {
342        console.log("getAllActiveIfaces callback value.length = " + JSON.stringify(value.length));
343        for (let i = 0; i < value.length; i++) {
344            console.log("getAllActiveIfaces callback = " + JSON.stringify(value[i]));
345        }
346    }
347});
348```
349
350## ethernet.getAllActiveIfaces
351
352getAllActiveIfaces(): Promise\<Array\<string>>
353
354获取活动的网络接口,使用Promise方式作为异步方法。
355
356**系统接口**:此接口为系统接口。
357
358**需要权限**:ohos.permission.GET_NETWORK_INFO
359
360**系统能力**:SystemCapability.Communication.NetManager.Ethernet
361
362**返回值:**
363
364| 类型                           | 说明                                            |
365| ------------------------------ | ----------------------------------------------- |
366| Promise\<Array\<string>>         | 以Promise形式返回获取结果。返回值为对应接口名。 |
367
368**错误码:**
369
370| 错误码ID | 错误信息                                 |
371| ------- | ----------------------------------------|
372| 201     | Permission denied.                      |
373| 2200002 | Operation failed. Cannot connect to service.|
374| 2200003 | System internal error.                  |
375
376**示例:**
377
378```js
379ethernet.getAllActiveIfaces().then((data) => {
380    console.log("getAllActiveIfaces promise data.length = " + JSON.stringify(data.length));
381    for (let i = 0; i < data.length; i++) {
382        console.log("getAllActiveIfaces promise  = " + JSON.stringify(data[i]));
383    }
384}).catch(error => {
385    console.log("getAllActiveIfaces promise error = " + JSON.stringify(error));
386});
387```
388
389## InterfaceConfiguration
390
391以太网连接配置网络信息。
392
393**系统接口**:此接口为系统接口。
394
395**系统能力**:SystemCapability.Communication.NetManager.Ethernet
396
397| 名称          | 类型                    | 必填 | 说明                                                         |
398| ------------ | ----------------------- | ---|------------------------------------------------------------ |
399| mode         | [IPSetMode](#ipsetmode) | 是 | 以太网连接配置模式。 |
400| ipAddr       | string                  | 是 | 以太网连接静态配置ip信息,地址值范围0-255.0-255.0-255.0-255(DHCP模式无需配置)。 |
401| route        | string                  | 是 | 以太网连接静态配置路由信息,地址值范围0-255.0-255.0-255.0-255(DHCP模式无需配置)。 |
402| gateway      | string                  | 是 | 以太网连接配置网关信息,地址值范围0-255.0-255.0-255.0-255(DHCP模式无需配置)。 |
403| netMask      | string                  | 是 | 以太网连接配置子网掩码,地址值范围0-255.0-255.0-255.0-255(DHCP模式无需配置)。 |
404| dnsServers   | string                  | 是 | 以太网连接配置dns服务地址,地址值范围0-255.0-255.0-255.0-255(DHCP模式无需配置)多地址间用“,”隔开。 |
405
406## IPSetMode
407
408以太网连接模式。
409
410**系统接口**:此接口为系统接口。
411
412**系统能力**:SystemCapability.Communication.NetManager.Ethernet
413
414| 名称                  | 值   | 说明                   |
415| ------------------------ | ---- | ---------------------- |
416| STATIC | 0 | 以太网连接静态配置网络信息。 |
417| DHCP   | 1 | 以太网连接动态配置网络信息。 |
418