js-apis-enterprise-networkManager.md 20.7 KB
Newer Older
A
Annie_wang 已提交
1 2
# @ohos.enterprise.networkManager (Network Management)

A
Annie_wang 已提交
3
The **networkManager** module provides APIs for network management of enterprise devices, including obtaining the device IP address and MAC address. Only the device administrator applications can call the APIs provided by this module.
A
Annie_wang 已提交
4 5 6

> **NOTE**
>
A
Annie_wang 已提交
7 8
> - The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> - The APIs of this module can be called only after a [device administrator application](js-apis-enterprise-adminManager.md#adminmanagerenableadmin) is enabled.
A
Annie_wang 已提交
9 10 11 12 13 14 15 16 17 18 19

## Modules to Import

```js
import networkManager from '@ohos.enterprise.networkManager';
```

## networkManager.getAllNetworkInterfaces

getAllNetworkInterfaces(admin: Want, callback: AsyncCallback<Array<string>>): void

A
Annie_wang 已提交
20
Obtains all active network interfaces through the specified device administrator application. This API uses an asynchronous callback to return the result.
A
Annie_wang 已提交
21

A
Annie_wang 已提交
22
**Required permissions**: ohos.permission.ENTERPRISE_GET_NETWORK_INFO
A
Annie_wang 已提交
23 24 25 26 27 28 29 30 31

**System capability**: SystemCapability.Customization.EnterpriseDeviceManager

**System API**: This is a system API.

**Parameters**

| Name     | Type                                      | Mandatory  | Description                      |
| -------- | ---------------------------------------- | ---- | ------------------------------- |
A
Annie_wang 已提交
32 33
| admin    | [Want](js-apis-app-ability-want.md)     | Yes   | Device administrator application that obtains the information.                 |
| callback | AsyncCallback<Array<string>>            | Yes   | Callback invoked to return the result. If the operation is successful, **err** is **null** and **data** is an array of network interfaces obtained. If the operation fails, **err** is an error object.    |
A
Annie_wang 已提交
34 35 36 37 38

**Error codes**

For details about the error codes, see [Enterprise Device Management Error Codes](../errorcodes/errorcode-enterpriseDeviceManager.md).

A
Annie_wang 已提交
39
| ID| Error Message                                                                      |
A
Annie_wang 已提交
40 41 42 43 44 45 46 47 48 49 50
| ------- | ---------------------------------------------------------------------------- |
| 9200001 | The application is not an administrator application of the device.                       |
| 9200002 | The administrator application does not have permission to manage the device.|

**Example**

```js
let wantTemp = {
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility",
};
A
Annie_wang 已提交
51
networkManager.getAllNetworkInterfaces(wantTemp, (error, result) => {
A
Annie_wang 已提交
52 53 54 55 56 57 58 59 60 61 62 63
    if (error != null) {
        console.log("error code:" + error.code + " error message:" + error.message);
        return;
    }
    console.log(JSON.stringify(result));
});
```

## networkManager.getAllNetworkInterfaces

getAllNetworkInterfaces(admin: Want): Promise<Array<string>>

A
Annie_wang 已提交
64
Obtains all active network interfaces through the specified device administrator application. This API uses a promise to return the result.
A
Annie_wang 已提交
65 66 67 68 69 70 71 72 73 74 75

**Required permissions**: ohos.permission.ENTERPRISE_GET_NETWORK_INFO

**System capability**: SystemCapability.Customization.EnterpriseDeviceManager

**System API**: This is a system API.

**Parameters**

| Name  | Type                                 | Mandatory  | Description     |
| ----- | ----------------------------------- | ---- | ------- |
A
Annie_wang 已提交
76
| admin | [Want](js-apis-app-ability-want.md) | Yes   | Device administrator application that obtains the information.|
A
Annie_wang 已提交
77 78 79 80 81

**Return value**

| Type                  | Description                     |
| --------------------- | ------------------------- |
A
Annie_wang 已提交
82
| Promise<Array<string>> | Promise used to return an array of network interfaces obtained. |
A
Annie_wang 已提交
83 84 85 86 87

**Error codes**

For details about the error codes, see [Enterprise Device Management Error Codes](../errorcodes/errorcode-enterpriseDeviceManager.md).

A
Annie_wang 已提交
88
| ID| Error Message                                                                    |
A
Annie_wang 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| ------- | ---------------------------------------------------------------------------- |
| 9200001 | The application is not an administrator application of the device.                       |
| 9200002 | The administrator application does not have permission to manage the device.|

**Example**

```js
let wantTemp = {
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility",
};
networkManager.getAllNetworkInterfaces(wantTemp).then((result) => {
    console.log(JSON.stringify(result));
}).catch(error => {
    console.log("error code:" + error.code + " error message:" + error.message);
});
```

## networkManager.getIpAddress

getIpAddress(admin: Want, networkInterface: string, callback: AsyncCallback<string>): void

A
Annie_wang 已提交
111
Obtains the device IP address based on the given network interface through the specified device administrator application. This API uses an asynchronous callback to return the result.
A
Annie_wang 已提交
112

A
Annie_wang 已提交
113
**Required permissions**: ohos.permission.ENTERPRISE_GET_NETWORK_INFO
A
Annie_wang 已提交
114 115 116 117 118 119 120 121 122

**System capability**: SystemCapability.Customization.EnterpriseDeviceManager

**System API**: This is a system API.

**Parameters**

| Name     | Type                                      | Mandatory  | Description                      |
| -------- | ---------------------------------------- | ---- | ------------------------------- |
A
Annie_wang 已提交
123
| admin    | [Want](js-apis-app-ability-want.md)     | Yes   | Device administrator application that obtains the information.                 |
A
Annie_wang 已提交
124
| networkInterface    | string     | Yes   | Network interface.                 |
A
Annie_wang 已提交
125
| callback | AsyncCallback<string>            | Yes   | Callback invoked to return the result. If the operation is successful, **err** is **null** and **data** is the IP address obtained. If the operation fails, **err** is an error object.      |
A
Annie_wang 已提交
126 127 128 129 130

**Error codes**

For details about the error codes, see [Enterprise Device Management Error Codes](../errorcodes/errorcode-enterpriseDeviceManager.md).

A
Annie_wang 已提交
131
| ID| Error Message                                                                      |
A
Annie_wang 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
| ------- | ---------------------------------------------------------------------------- |
| 9200001 | The application is not an administrator application of the device.                       |
| 9200002 | The administrator application does not have permission to manage the device.|

**Example**

```js
let wantTemp = {
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility",
};
networkManager.getIpAddress(wantTemp, "eth0", (error, result) => {
    if (error != null) {
        console.log("error code:" + error.code + " error message:" + error.message);
        return;
    }
    console.log(result);
});
```

## networkManager.getIpAddress

getIpAddress(admin: Want, networkInterface: string): Promise<string>

A
Annie_wang 已提交
156
Obtains the device IP address based on the given network interface through the specified device administrator application. This API uses a promise to return the result.
A
Annie_wang 已提交
157 158 159 160 161 162 163 164 165 166 167

**Required permissions**: ohos.permission.ENTERPRISE_GET_NETWORK_INFO

**System capability**: SystemCapability.Customization.EnterpriseDeviceManager

**System API**: This is a system API.

**Parameters**

| Name  | Type                                 | Mandatory  | Description     |
| ----- | ----------------------------------- | ---- | ------- |
A
Annie_wang 已提交
168
| admin | [Want](js-apis-app-ability-want.md) | Yes   | Device administrator application that obtains the information.|
A
Annie_wang 已提交
169 170 171 172 173 174 175 176 177 178 179 180
| networkInterface    | string     | Yes   | Network interface.                 |

**Return value**

| Type                  | Description                     |
| --------------------- | ------------------------- |
| Promise<string> | Promise used to return the device IP address obtained. |

**Error codes**

For details about the error codes, see [Enterprise Device Management Error Codes](../errorcodes/errorcode-enterpriseDeviceManager.md).

A
Annie_wang 已提交
181
| ID| Error Message                                                                    |
A
Annie_wang 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
| ------- | ---------------------------------------------------------------------------- |
| 9200001 | The application is not an administrator application of the device.                       |
| 9200002 | The administrator application does not have permission to manage the device.|

**Example**

```js
let wantTemp = {
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility",
};
networkManager.getIpAddress(wantTemp, "eth0").then((result) => {
    console.log(result);
}).catch(error => {
    console.log("error code:" + error.code + " error message:" + error.message);
});
```

## networkManager.getMac

getMac(admin: Want, networkInterface: string, callback: AsyncCallback<string>): void

A
Annie_wang 已提交
204
Obtains the device MAC address based on the given network interface through the specified device administrator application. This API uses an asynchronous callback to return the result.
A
Annie_wang 已提交
205 206 207 208 209 210 211 212 213 214 215

**Required permissions**: ohos.permission.ENTERPRISE_GET_NETWORK_INFO

**System capability**: SystemCapability.Customization.EnterpriseDeviceManager

**System API**: This is a system API.

**Parameters**

| Name     | Type                                      | Mandatory  | Description                      |
| -------- | ---------------------------------------- | ---- | ------------------------------- |
A
Annie_wang 已提交
216
| admin    | [Want](js-apis-app-ability-want.md)      | Yes   | Device administrator application that obtains the information.                 |
A
Annie_wang 已提交
217
| networkInterface    | string     | Yes   | Network interface.                 |
A
Annie_wang 已提交
218
| callback | AsyncCallback<string>            | Yes   | Callback invoked to return the result. If the operation is successful, **err** is **null** and **data** is the MAC address obtained. If the operation fails, **err** is an error object.      |
A
Annie_wang 已提交
219 220 221 222 223

**Error codes**

For details about the error codes, see [Enterprise Device Management Error Codes](../errorcodes/errorcode-enterpriseDeviceManager.md).

A
Annie_wang 已提交
224
| ID| Error Message                                                                      |
A
Annie_wang 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
| ------- | ---------------------------------------------------------------------------- |
| 9200001 | The application is not an administrator application of the device.                       |
| 9200002 | The administrator application does not have permission to manage the device.|

**Example**

```js
let wantTemp = {
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility",
};
networkManager.getMac(wantTemp, "eth0", (error, result) => {
    if (error != null) {
        console.log("error code:" + error.code + " error message:" + error.message);
        return;
    }
    console.log(result);
});
```

## networkManager.getMac

getIpAddress(admin: Want, networkInterface: string): Promise<string>

A
Annie_wang 已提交
249
Obtain the device MAC address based on the given network interface through the specified device administrator application. This API uses a promise to return the result.
A
Annie_wang 已提交
250 251 252 253 254 255 256 257 258 259 260

**Required permissions**: ohos.permission.ENTERPRISE_GET_NETWORK_INFO

**System capability**: SystemCapability.Customization.EnterpriseDeviceManager

**System API**: This is a system API.

**Parameters**

| Name  | Type                                 | Mandatory  | Description     |
| ----- | ----------------------------------- | ---- | ------- |
A
Annie_wang 已提交
261
| admin | [Want](js-apis-app-ability-want.md) | Yes   | Device administrator application that obtains the information.|
A
Annie_wang 已提交
262 263 264 265 266 267 268 269 270 271 272 273
| networkInterface    | string     | Yes   | Network interface.                 |

**Return value**

| Type                  | Description                     |
| --------------------- | ------------------------- |
| Promise<string> | Promise used to return the device MAC address obtained. |

**Error codes**

For details about the error codes, see [Enterprise Device Management Error Codes](../errorcodes/errorcode-enterpriseDeviceManager.md).

A
Annie_wang 已提交
274
| ID| Error Message                                                                    |
A
Annie_wang 已提交
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
| ------- | ---------------------------------------------------------------------------- |
| 9200001 | The application is not an administrator application of the device.                       |
| 9200002 | The administrator application does not have permission to manage the device.|

**Example**

```js
let wantTemp = {
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility",
};
networkManager.getMac(wantTemp, "eth0").then((result) => {
    console.log(result);
}).catch(error => {
    console.log("error code:" + error.code + " error message:" + error.message);
});
```
A
Annie_wang 已提交
292 293 294 295 296

## networkManager.isNetworkInterfaceDisabled

isNetworkInterfaceDisabled(admin: Want, networkInterface: string, callback: AsyncCallback<boolean>): void

A
Annie_wang 已提交
297
Checks whether a network interface is disabled through the specified device administrator application. This API uses an asynchronous callback to return the result.
A
Annie_wang 已提交
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316

**Required permissions**: ohos.permission.ENTERPRISE_GET_NETWORK_INFO

**System capability**: SystemCapability.Customization.EnterpriseDeviceManager

**System API**: This is a system API.

**Parameters**

| Name     | Type                                      | Mandatory  | Description                      |
| -------- | ---------------------------------------- | ---- | ------------------------------- |
| admin    | [Want](js-apis-app-ability-want.md)      | Yes   | Device administrator application that obtains the information.                 |
| networkInterface    | string     | Yes   | Network interface.                 |
| callback | AsyncCallback<boolean>            | Yes   | Callback invoked to return the result. If the operation is successful, **err** is **null**, and **data** indicates whether the network interface is disabled. The value **true** means the network interface is disabled; and **false** means the opposite. If the operation fails, **err** is an error object.      |

**Error codes**

For details about the error codes, see [Enterprise Device Management Error Codes](../errorcodes/errorcode-enterpriseDeviceManager.md).

A
Annie_wang 已提交
317
| ID| Error Message                                                                      |
A
Annie_wang 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
| ------- | ---------------------------------------------------------------------------- |
| 9200001 | The application is not an administrator application of the device.                       |
| 9200002 | The administrator application does not have permission to manage the device.|

**Example**

```js
let wantTemp = {
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility",
};
networkManager.isNetworkInterfaceDisabled(wantTemp, "eth0", (error, result) => {
    if (error != null) {
        console.log("error code:" + error.code + " error message:" + error.message);
        return;
    }
    console.log("result:" + result);
});
```

## networkManager.isNetworkInterfaceDisabled

isNetworkInterfaceDisabled(admin: Want, networkInterface: string): Promise<boolean>

A
Annie_wang 已提交
342
Checks whether a network interface is disabled through the specified device administrator application. This API uses a promise to return the result.
A
Annie_wang 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366

**Required permissions**: ohos.permission.ENTERPRISE_GET_NETWORK_INFO

**System capability**: SystemCapability.Customization.EnterpriseDeviceManager

**System API**: This is a system API.

**Parameters**

| Name  | Type                                 | Mandatory  | Description     |
| ----- | ----------------------------------- | ---- | ------- |
| admin | [Want](js-apis-app-ability-want.md) | Yes   | Device administrator application that obtains the information.|
| networkInterface    | string     | Yes   | Network interface.                 |

**Return value**

| Type                  | Description                     |
| --------------------- | ------------------------- |
| Promise<boolean> | Promise used to return the result. The value **true** means the network interface is disabled, and the value **false** means the opposite. |

**Error codes**

For details about the error codes, see [Enterprise Device Management Error Codes](../errorcodes/errorcode-enterpriseDeviceManager.md).

A
Annie_wang 已提交
367
| ID| Error Message                                                                    |
A
Annie_wang 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
| ------- | ---------------------------------------------------------------------------- |
| 9200001 | The application is not an administrator application of the device.                       |
| 9200002 | The administrator application does not have permission to manage the device.|

**Example**

```js
let wantTemp = {
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility",
};
networkManager.isNetworkInterfaceDisabled(wantTemp, "eth0").then((result) => {
    console.log("result:" + result);
}).catch(error => {
    console.log("error code:" + error.code + " error message:" + error.message);
});
```

## networkManager.setNetworkInterfaceDisabled

setNetworkInterfaceDisabled(admin: Want, networkInterface: string, isDisabled: boolean, callback: AsyncCallback<void>): void

A
Annie_wang 已提交
390
Sets a network interface through the specified device administrator application. This API uses an asynchronous callback to return the result.
A
Annie_wang 已提交
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435

**Required permissions**: ohos.permission.ENTERPRISE_SET_NETWORK

**System capability**: SystemCapability.Customization.EnterpriseDeviceManager

**System API**: This is a system API.

**Parameters**

| Name     | Type                                      | Mandatory  | Description                      |
| -------- | ---------------------------------------- | ---- | ------------------------------- |
| admin    | [Want](js-apis-app-ability-want.md)      | Yes   | Device administrator application that obtains the information.                 |
| networkInterface    | string     | Yes   | Network interface.                 |
| isDisabled    | boolean     | Yes   | Network interface status to set. The value **true** means to disable the network interface, and **false** means to enable the network interface.                 |
| callback | AsyncCallback<void>            | Yes   | Callback invoked to return the result. If the operation is successful, **err** is **null**. Otherwise, **err** is an error object.      |

**Error codes**

For details about the error codes, see [Enterprise Device Management Error Codes](../errorcodes/errorcode-enterpriseDeviceManager.md).

| ID| Error Message                                                                    |
| ------- | ---------------------------------------------------------------------------- |
| 9200001 | The application is not an administrator application of the device.                      |
| 9200002 | The administrator application does not have permission to manage the device.|

**Example**

```js
let wantTemp = {
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility",
};
networkManager.setNetworkInterfaceDisabled(wantTemp, "eth0", true, (error) => {
    if (error != null) {
        console.log("error code:" + error.code + " error message:" + error.message);
        return;
    }
    console.log("setNetworkInterfaceDisabled success!");
});
```

## networkManager.setNetworkInterfaceDisabled

setNetworkInterfaceDisabled(admin: Want, networkInterface: string, isDisabled: boolean): Promise<void>

A
Annie_wang 已提交
436
Sets a network interface through the specified device administrator application. This API uses a promise to return the result.
A
Annie_wang 已提交
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479

**Required permissions**: ohos.permission.ENTERPRISE_SET_NETWORK

**System capability**: SystemCapability.Customization.EnterpriseDeviceManager

**System API**: This is a system API.

**Parameters**

| Name  | Type                                 | Mandatory  | Description     |
| ----- | ----------------------------------- | ---- | ------- |
| admin | [Want](js-apis-app-ability-want.md) | Yes   | Device administrator application that obtains the information.|
| networkInterface    | string     | Yes   | Network interface.                 |
| isDisabled    | boolean     | Yes   | Network interface status to set. The value **true** means to disable the network interface, and **false** means to enable the network interface.                 |

**Return value**

| Type                  | Description                     |
| --------------------- | ------------------------- |
| Promise<void> | Promise that returns no value. An error object is thrown if the network interface fails to be disabled. |

**Error codes**

For details about the error codes, see [Enterprise Device Management Error Codes](../errorcodes/errorcode-enterpriseDeviceManager.md).

| ID| Error Message                                                                    |
| ------- | ---------------------------------------------------------------------------- |
| 9200001 | The application is not an administrator application of the device.                      |
| 9200002 | The administrator application does not have permission to manage the device.|

**Example**

```js
let wantTemp = {
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility",
};
networkManager.setNetworkInterfaceDisabled(wantTemp, "eth0", true).then(() => {
    console.log("setNetworkInterfaceDisabled success!");
}).catch(error => {
    console.log("error code:" + error.code + " error message:" + error.message);
});
```