js-apis-net-connection.md 68.8 KB
Newer Older
S
shawn_he 已提交
1
# @ohos.net.connection (Network Connection Management)
S
shawn_he 已提交
2

S
shawn_he 已提交
3
The network connection management module provides basic network management capabilities. You can obtain the default active data network or the list of all active data networks, enable or disable the airplane mode, and obtain network capability information.
S
shawn_he 已提交
4

S
shawn_he 已提交
5
> **NOTE**
S
shawn_he 已提交
6 7 8 9
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.

## Modules to Import

S
shawn_he 已提交
10
```js
S
shawn_he 已提交
11 12
import connection from '@ohos.net.connection'
```
S
shawn_he 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
## connection.createNetConnection

createNetConnection(netSpecifier?: NetSpecifier, timeout?: number): NetConnection

Creates a **NetConnection** object. **netSpecifier** specifies the network, and **timeout** specifies the timeout interval in ms. **timeout** is configurable only when **netSpecifier** is specified. If neither of them is present, the default network is used.

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name      | Type                         | Mandatory| Description                                                        |
| ------------ | ----------------------------- | ---- | ------------------------------------------------------------ |
| netSpecifier | [NetSpecifier](#netspecifier) | No  | Network specifier. If this parameter is not set, the default network is used.                  |
| timeout      | number                        | No  | Timeout interval for obtaining the network specified by **netSpecifier**. This parameter is valid only when **netSpecifier** is set.|

**Return value**

| Type                           | Description                |
| ------------------------------- | -------------------- |
| [NetConnection](#netconnection) | Handle of the network specified by **netSpecifier**.|

**Example**

```js
// Default network
let netConnection = connection.createNetConnection()

// Cellular network
let netConnectionCellular = connection.createNetConnection({
    netCapabilities: {
        bearerTypes: [connection.NetBearType.BEARER_CELLULAR]
    }
})
```
S
shawn_he 已提交
47 48 49 50 51

## connection.getDefaultNet

getDefaultNet(callback: AsyncCallback\<NetHandle>): void

S
shawn_he 已提交
52
Obtains the default active data network. This API uses an asynchronous callback to return the result. You can use [getNetCapabilities](#connectiongetnetcapabilities) to obtain information such as the network type and capabilities.
S
shawn_he 已提交
53

S
shawn_he 已提交
54
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
55 56 57 58 59 60 61

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                                   | Mandatory| Description      |
| -------- | --------------------------------------- | ---- | ---------- |
S
shawn_he 已提交
62 63 64 65 66 67 68 69 70
| callback | AsyncCallback\<[NetHandle](#nethandle)> | Yes  | Callback used to return the result. If the default activated data network is obtained successfully, err is undefined and data is the default activated data network. Otherwise, err is an error object.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
71 72 73

**Example**

S
shawn_he 已提交
74
```js
S
shawn_he 已提交
75
connection.getDefaultNet(function (error, data) {
S
shawn_he 已提交
76
    console.log(JSON.stringify(error))
S
shawn_he 已提交
77
    console.log(JSON.stringify(data))
S
shawn_he 已提交
78 79 80 81 82 83 84
})
```

## connection.getDefaultNet

getDefaultNet(): Promise\<NetHandle>

S
shawn_he 已提交
85
Obtains the default active data network. This API uses a promise to return the result. You can use [getNetCapabilities](#connectiongetnetcapabilities) to obtain information such as the network type and capabilities.
S
shawn_he 已提交
86

S
shawn_he 已提交
87
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
88 89 90

**System capability**: SystemCapability.Communication.NetManager.Core

S
shawn_he 已提交
91
**Return value**
S
shawn_he 已提交
92 93 94 95 96

| Type                             | Description                                 |
| --------------------------------- | ------------------------------------- |
| Promise\<[NetHandle](#nethandle)> | Promise used to return the result.|

S
shawn_he 已提交
97 98 99 100 101 102 103 104
**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

S
shawn_he 已提交
105 106
**Example**

S
shawn_he 已提交
107
```js
S
shawn_he 已提交
108 109
connection.getDefaultNet().then(function (data) {
    console.log(JSON.stringify(data))
S
shawn_he 已提交
110 111 112
})
```

S
shawn_he 已提交
113
## connection.getDefaultNetSync<sup>9+</sup>
S
shawn_he 已提交
114

S
shawn_he 已提交
115
getDefaultNetSync(): NetHandle
S
shawn_he 已提交
116

S
shawn_he 已提交
117
Obtains the default active data network in synchronous mode. You can use [getNetCapabilities](#connectiongetnetcapabilities) to obtain information such as the network type and capabilities.
S
shawn_he 已提交
118 119 120 121 122 123 124 125 126 127 128

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Communication.NetManager.Core

**Return value**

| Type     | Description                              |
| --------- | ---------------------------------- |
| NetHandle | Handle of the default active data network.|

S
shawn_he 已提交
129 130 131 132 133 134 135 136
**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

S
shawn_he 已提交
137 138 139 140 141 142
**Example**

```js
let netHandle = connection.getDefaultNetSync();
```

S
shawn_he 已提交
143
## connection.getGlobalHttpProxy<sup>10+</sup>
S
shawn_he 已提交
144

S
shawn_he 已提交
145
getGlobalHttpProxy(callback: AsyncCallback\<HttpProxy>): void
S
shawn_he 已提交
146

S
shawn_he 已提交
147
Obtains the global HTTP proxy configuration of the network. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
148

S
shawn_he 已提交
149
**System API**: This is a system API.
S
shawn_he 已提交
150

S
shawn_he 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name   | Type                                                        | Mandatory| Description            |
| --------- | ------------------------------------------------------------ | ---- | ---------------- |
| callback  | AsyncCallback\<[HttpProxy](#httpproxy)> | Yes  | Callback used to return the result. If the global HTTP proxy configuration of the network is obtained successfully, **err** is **undefined** and **data** is the global HTTP proxy configuration. Otherwise, **err** is an error object.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
connection.getGlobalHttpProxy((error,data) => {
    console.info(JSON.stringify(error));
    console.info(JSON.stringify(data));
})
```

## connection.getGlobalHttpProxy<sup>10+</sup>

getGlobalHttpProxy(): Promise\<HttpProxy>;

Obtains the global HTTP proxy configuration of the network. This API uses a promise to return the result.

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

**System capability**: SystemCapability.Communication.NetManager.Core

**Return value**

| Type                             | Description                                 |
| --------------------------------- | ------------------------------------- |
| Promise\<[HttpProxy](#httpproxy)> | Promise used to return the result.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
connection.getGlobalHttpProxy().then((data) => {
   console.info(JSON.stringify(data));
}).catch(error => {
   console.info(JSON.stringify(error));
})
```

## connection.setGlobalHttpProxy<sup>10+</sup>

setGlobalHttpProxy(httpProxy: HttpProxy, callback: AsyncCallback\<void>): void

Sets the global HTTP proxy configuration of the network. This API uses an asynchronous callback to return the result.

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

**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
S
shawn_he 已提交
217 218 219 220 221

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

S
shawn_he 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235
| Name   | Type                                                        | Mandatory| Description            |
| --------- | ------------------------------------------------------------ | ---- | ---------------- |
| httpProxy | [HttpProxy](#httpproxy)                                      | Yes  | Global HTTP proxy configuration of the network.|
| callback  | AsyncCallback\<void> | Yes  | Callback used to return the result. If the global HTTP proxy configuration of the network is set successfully, **err** is **undefined**. Otherwise, **err** is an error object.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
236 237 238

**Example**

S
shawn_he 已提交
239
```js
S
shawn_he 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
let exclusionStr="192.168,baidu.com"
let exclusionArray = exclusionStr.split(',');
let httpProxy = {
    host: "192.168.xx.xxx",
    port: 8080,
    exclusionList: exclusionArray
}
connection.setGlobalHttpProxy(httpProxy, (error, data) => {
   console.info(JSON.stringify(error));
   console.info(JSON.stringify(data));
});
```

## connection.setGlobalHttpProxy<sup>10+</sup>

setGlobalHttpProxy(httpProxy: HttpProxy): Promise\<void>;

Sets the global HTTP proxy configuration of the network. 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.Core

**Parameters**

| Name   | Type                                                        | Mandatory| Description            |
| --------- | ------------------------------------------------------------ | ---- | ---------------- |
| httpProxy | [HttpProxy](#httpproxy)                                      | Yes  | Global HTTP proxy configuration of the network.|

**Return value**

| Type                                       | Description                         |
| ------------------------------------------- | ----------------------------- |
| Promise\<void> | Promise that returns no value.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
let exclusionStr="192.168,baidu.com"
let exclusionArray = exclusionStr.split(',');
let httpProxy = {
    host: "192.168.xx.xxx",
    port: 8080,
    exclusionList: exclusionArray
}
connection.setGlobalHttpProxy(httpProxy).then((error, data) => {
   console.info(JSON.stringify(data));
}).catch(error=>{
   console.info(JSON.stringify(error));
S
shawn_he 已提交
301 302 303
})
```

S
shawn_he 已提交
304
## connection.getAppNet<sup>9+</sup>
S
shawn_he 已提交
305

S
shawn_he 已提交
306
getAppNet(callback: AsyncCallback\<NetHandle>): void
S
shawn_he 已提交
307

S
shawn_he 已提交
308
Obtains information about the network bound to an application. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
309

S
shawn_he 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name   | Type                                                        | Mandatory| Description            |
| --------- | ------------------------------------------------------------ | ---- | ---------------- |
| callback  | AsyncCallback\<[NetHandle](#nethandle)> | Yes  | Callback used to return the result. If information about the network bound to the application is successfully obtained, **err** is **undefined** and **data** is the obtained network information. Otherwise, **err** is an error object.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
connection.getAppNet(function(error, data) {
   console.log(JSON.stringify(error))
   console.log(JSON.stringify(data))
})
```

## connection.getAppNet<sup>9+</sup>

getAppNet(): Promise\<NetHandle>;

Obtains information about the network bound to an application. This API uses a promise to return the result.
S
shawn_he 已提交
339 340 341

**System capability**: SystemCapability.Communication.NetManager.Core

S
shawn_he 已提交
342
**Return value**
S
shawn_he 已提交
343

S
shawn_he 已提交
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
| Type                             | Description                                 |
| --------------------------------- | ------------------------------------- |
| Promise\<[NetHandle](#nethandle)> | Promise used to return the result.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
connection.getAppNet().then((data) => {
   console.info(JSON.stringify(data));
}).catch(error => {
   console.info(JSON.stringify(error));
})
```

## connection.SetAppNet<sup>9+</sup>

setAppNet(netHandle: NetHandle, callback: AsyncCallback\<void>): void

Binds an application to the specified network, so that the application can access the external network only through this network. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.INTERNET

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name   | Type                                                        | Mandatory| Description            |
| --------- | ------------------------------------------------------------ | ---- | ---------------- |
| netHandle | [NetHandle](#nethandle)                                      | Yes  | Handle of the data network.|
| callback  | AsyncCallback\<void> | Yes  | Callback used to return the result. If the application is successfully bound to the specified network, **err** is **undefined**. Otherwise, **err** is an error object.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
391 392 393

**Example**

S
shawn_he 已提交
394
```js
S
shawn_he 已提交
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 436 437 438 439 440 441 442 443
connection.getDefaultNet(function (error, netHandle) {
   connection.setAppNet(netHandle, (error, data) => {
       console.log(JSON.stringify(error))
       console.log(JSON.stringify(data))
   });
})
```

## connection.SetAppNet<sup>9+</sup>

setAppNet(netHandle: NetHandle): Promise\<void>;

Binds an application to the specified network, so that the application can access the external network only through this network. This API uses a promise to return the result.

**Required permissions**: ohos.permission.INTERNET

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name   | Type                                                        | Mandatory| Description            |
| --------- | ------------------------------------------------------------ | ---- | ---------------- |
| netHandle | [NetHandle](#nethandle)                                      | Yes  | Handle of the data network.|

**Return value**

| Type                                       | Description                         |
| ------------------------------------------- | ----------------------------- |
| Promise\<void> | Promise that returns no value.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
connection.getDefaultNet().then(function (netHandle) {
   connection.setAppNet(netHandle).then((error, data) => {
        console.log(JSON.stringify(data))
   }).catch(error => {
        console.log(JSON.stringify(error))
   })
S
shawn_he 已提交
444 445 446 447 448 449 450
})
```

## connection.getAllNets

getAllNets(callback: AsyncCallback&lt;Array&lt;NetHandle&gt;&gt;): void

S
shawn_he 已提交
451
Obtains the list of all connected networks. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
452

S
shawn_he 已提交
453
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
454 455 456 457

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**
S
shawn_he 已提交
458

S
shawn_he 已提交
459 460
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
S
shawn_he 已提交
461 462 463 464 465 466 467 468 469
| callback | AsyncCallback&lt;Array&lt;[NetHandle](#nethandle)&gt;&gt; | Yes| Callback used to return the result. If the list of all connected networks is obtained successfully, **err** is **undefined** and **data** is the list of activated data networks. Otherwise, **err** is an error object.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
470 471 472

**Example**

S
shawn_he 已提交
473
```js
S
shawn_he 已提交
474
connection.getAllNets(function (error, data) {
S
shawn_he 已提交
475
    console.log(JSON.stringify(error))
S
shawn_he 已提交
476
    console.log(JSON.stringify(data))
S
shawn_he 已提交
477 478 479 480 481 482 483
});
```

## connection.getAllNets

getAllNets(): Promise&lt;Array&lt;NetHandle&gt;&gt;

S
shawn_he 已提交
484
Obtains the list of all connected networks. This API uses a promise to return the result.
S
shawn_he 已提交
485

S
shawn_he 已提交
486
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
487 488 489

**System capability**: SystemCapability.Communication.NetManager.Core

S
shawn_he 已提交
490 491
**Return value**

S
shawn_he 已提交
492 493 494 495
| Type| Description|
| -------- | -------- |
| Promise&lt;Array&lt;[NetHandle](#nethandle)&gt;&gt; | Promise used to return the result.|

S
shawn_he 已提交
496 497 498 499 500 501 502 503
**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

S
shawn_he 已提交
504 505
**Example**

S
shawn_he 已提交
506
```js
S
shawn_he 已提交
507 508
connection.getAllNets().then(function (data) {
    console.log(JSON.stringify(data))
S
shawn_he 已提交
509 510 511 512 513 514 515
});
```

## connection.getConnectionProperties

getConnectionProperties(netHandle: NetHandle, callback: AsyncCallback\<ConnectionProperties>): void

S
shawn_he 已提交
516
Obtains connection properties of the network corresponding to the **netHandle**. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
517

S
shawn_he 已提交
518
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
519 520 521 522 523 524 525

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name   | Type                                                        | Mandatory| Description            |
| --------- | ------------------------------------------------------------ | ---- | ---------------- |
S
shawn_he 已提交
526
| netHandle | [NetHandle](#nethandle)                                      | Yes  | Handle of the data network.|
S
shawn_he 已提交
527 528 529 530 531 532 533 534 535 536 537
| callback  | AsyncCallback\<[ConnectionProperties](#connectionproperties)> | Yes  | Callback used to return the result. If the connection properties of the network corresponding to the **netHandle** is obtained successfully, **err** is **undefined** and **data** is the obtained network connection information. Otherwise, **err** is an error object.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
538 539 540

**Example**

S
shawn_he 已提交
541
```js
S
shawn_he 已提交
542
connection.getDefaultNet().then(function (netHandle) {
S
shawn_he 已提交
543
    connection.getConnectionProperties(netHandle, function (error, data) {
S
shawn_he 已提交
544
        console.log(JSON.stringify(error))
S
shawn_he 已提交
545
        console.log(JSON.stringify(data))
S
shawn_he 已提交
546 547 548 549 550 551 552 553
    })
})
```

## connection.getConnectionProperties

getConnectionProperties(netHandle: NetHandle): Promise\<ConnectionProperties>

S
shawn_he 已提交
554
Obtains connection properties of the network corresponding to the **netHandle**. This API uses a promise to return the result.
S
shawn_he 已提交
555

S
shawn_he 已提交
556
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
557 558 559 560 561 562 563

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name   | Type                   | Mandatory| Description            |
| --------- | ----------------------- | ---- | ---------------- |
S
shawn_he 已提交
564
| netHandle | [NetHandle](#nethandle) | Yes  | Handle of the data network.|
S
shawn_he 已提交
565

S
shawn_he 已提交
566
**Return value**
S
shawn_he 已提交
567 568 569 570 571

| Type                                                   | Description                             |
| ------------------------------------------------------- | --------------------------------- |
| Promise\<[ConnectionProperties](#connectionproperties)> | Promise used to return the result.|

S
shawn_he 已提交
572 573 574 575 576 577 578 579 580 581
**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

S
shawn_he 已提交
582 583
**Example**

S
shawn_he 已提交
584
```js
S
shawn_he 已提交
585
connection.getDefaultNet().then(function (netHandle) {
S
shawn_he 已提交
586 587
    connection.getConnectionProperties(netHandle).then(function (data) {
        console.log(JSON.stringify(data))
S
shawn_he 已提交
588 589 590 591 592 593 594 595
    })
})
```

## connection.getNetCapabilities

getNetCapabilities(netHandle: NetHandle, callback: AsyncCallback\<NetCapabilities>): void

S
shawn_he 已提交
596
Obtains capability information of the network corresponding to the **netHandle**. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
597

S
shawn_he 已提交
598
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
599 600 601 602 603 604 605

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name   | Type                                               | Mandatory| Description            |
| --------- | --------------------------------------------------- | ---- | ---------------- |
S
shawn_he 已提交
606
| netHandle | [NetHandle](#nethandle)                             | Yes  | Handle of the data network.|
S
shawn_he 已提交
607 608 609 610 611 612 613 614 615 616 617
| callback  | AsyncCallback\<[NetCapabilities](#netcapabilities)> | Yes  | Callback used to return the result. If the capability information of the network corresponding to the **netHandle** is obtained successfully, **err** is **undefined** and **data** is the obtained network capability information. Otherwise, **err** is an error object.      |

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
618 619 620

**Example**

S
shawn_he 已提交
621
```js
S
shawn_he 已提交
622
connection.getDefaultNet().then(function (netHandle) {
S
shawn_he 已提交
623
    connection.getNetCapabilities(netHandle, function (error, data) {
S
shawn_he 已提交
624
        console.log(JSON.stringify(error))
S
shawn_he 已提交
625
        console.log(JSON.stringify(data))
S
shawn_he 已提交
626 627 628 629 630 631 632 633
    })
})
```

## connection.getNetCapabilities

getNetCapabilities(netHandle: NetHandle): Promise\<NetCapabilities>

S
shawn_he 已提交
634
Obtains capability information of the network corresponding to the **netHandle**. This API uses a promise to return the result.
S
shawn_he 已提交
635

S
shawn_he 已提交
636
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
637 638 639 640 641 642 643

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name   | Type                   | Mandatory| Description            |
| --------- | ----------------------- | ---- | ---------------- |
S
shawn_he 已提交
644
| netHandle | [NetHandle](#nethandle) | Yes  | Handle of the data network.|
S
shawn_he 已提交
645

S
shawn_he 已提交
646
**Return value**
S
shawn_he 已提交
647 648 649 650 651

| Type                                         | Description                             |
| --------------------------------------------- | --------------------------------- |
| Promise\<[NetCapabilities](#netcapabilities)> | Promise used to return the result.|

S
shawn_he 已提交
652 653 654 655 656 657 658 659 660 661
**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

S
shawn_he 已提交
662 663
**Example**

S
shawn_he 已提交
664
```js
S
shawn_he 已提交
665
connection.getDefaultNet().then(function (netHandle) {
S
shawn_he 已提交
666 667
    connection.getNetCapabilities(netHandle).then(function (data) {
        console.log(JSON.stringify(data))
S
shawn_he 已提交
668 669 670 671
    })
})
```

S
shawn_he 已提交
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
## connection.isDefaultNetMetered<sup>9+</sup>

isDefaultNetMetered(callback: AsyncCallback\<boolean>): void

Checks whether the data traffic usage on the current network is metered. This API uses an asynchronous callback to return the result.

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
| callback | AsyncCallback\<boolean> | Yes  | Callback used to return the result. The value **true** indicates the data traffic usage is metered.|

S
shawn_he 已提交
688 689 690 691 692 693 694 695 696
**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**
S
shawn_he 已提交
697 698

```js
S
shawn_he 已提交
699
connection.isDefaultNetMetered(function (error, data) {
S
shawn_he 已提交
700
    console.log(JSON.stringify(error))
S
shawn_he 已提交
701
    console.log('data: ' + data)
S
shawn_he 已提交
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
})
```

## connection.isDefaultNetMetered<sup>9+</sup>

isDefaultNetMetered(): Promise\<boolean>

Checks whether the data traffic usage on the current network is metered. This API uses a promise to return the result.

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Communication.NetManager.Core

**Return value**

| Type             | Description                                           |
| ----------------- | ----------------------------------------------- |
| Promise\<boolean> | Promise used to return the result. The value **true** indicates the data traffic usage is metered.|

S
shawn_he 已提交
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
connection.isDefaultNetMetered().then(function (data) {
    console.log('data: ' + data)
})
```

## connection.hasDefaultNet

hasDefaultNet(callback: AsyncCallback\<boolean>): void

Checks whether the default data network is activated. This API uses an asynchronous callback to return the result. You can use [getDefaultNet](#connectiongetdefaultnet) to obtain the default data network, if any.

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
| callback | AsyncCallback\<boolean> | Yes  | Callback used to return the result. The value **true** indicates the default data network is activated.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
connection.hasDefaultNet(function (error, data) {
    console.log(JSON.stringify(error))
    console.log('data: ' + data)
})
```

## connection.hasDefaultNet

hasDefaultNet(): Promise\<boolean>

Checks whether the default data network is activated. This API uses a promise to return the result. You can use [getDefaultNet](#connectiongetdefaultnet) to obtain the default data network, if any.

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Communication.NetManager.Core

**Return value**

| Type             | Description                                           |
| ----------------- | ----------------------------------------------- |
| Promise\<boolean> | Promise used to return the result. The value **true** indicates that the default data network is activated.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**
S
shawn_he 已提交
795 796

```js
S
shawn_he 已提交
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
connection.hasDefaultNet().then(function (data) {
    console.log('data: ' + data)
})
```

## connection.enableAirplaneMode

enableAirplaneMode(callback: AsyncCallback\<void>): void

Enables the airplane mode. 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.Core

**Parameters**

| Name  | Type                                             | Mandatory| Description              |
| -------- | ------------------------------------------------- | ---- | ------------------ |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.        |

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
connection.enableAirplaneMode(function (error) {
    console.log(JSON.stringify(error))
})
```

## connection.enableAirplaneMode

enableAirplaneMode(): Promise\<void>

Enables the airplane mode. 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.Core

**Return value**

| Type                                       | Description                         |
| ------------------------------------------- | ----------------------------- |
| Promise\<void> | Promise that returns no value.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
connection.enableAirplaneMode().then(function (error) {
    console.log(JSON.stringify(error))
})
```

## connection.disableAirplaneMode

disableAirplaneMode(callback: AsyncCallback\<void>): void

Disables the airplane mode. 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.Core

**Parameters**

| Name  | Type                                             | Mandatory| Description              |
| -------- | ------------------------------------------------- | ---- | ------------------ |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result. If the airplane mode is disabled successfully, **err** is **undefined**. Otherwise, **err** is an error object.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
connection.disableAirplaneMode(function (error) {
    console.log(JSON.stringify(error))
})
```

## connection.disableAirplaneMode

disableAirplaneMode(): Promise\<void>

Disables the airplane mode. 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.Core

**Return value**

| Type                                       | Description                         |
| ------------------------------------------- | ----------------------------- |
| Promise\<void> | Promise that returns no value.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
connection.disableAirplaneMode().then(function (error) {
    console.log(JSON.stringify(error))
S
shawn_he 已提交
931 932 933
})
```

S
shawn_he 已提交
934 935 936 937
## connection.reportNetConnected

reportNetConnected(netHandle: NetHandle, callback: AsyncCallback&lt;void&gt;): void

S
shawn_he 已提交
938 939
Reports a **netAavailable** event to NetManager. If this API is called, the application considers that its network status (ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED) is inconsistent with that of NetManager.
This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
940

S
shawn_he 已提交
941
**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET
S
shawn_he 已提交
942 943 944 945

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**
S
shawn_he 已提交
946

S
shawn_he 已提交
947 948 949
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).|
S
shawn_he 已提交
950 951 952 953 954 955 956 957 958 959 960
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result. If the network status is reported successfully, **err** is **undefined**. Otherwise, **err** is an error object.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
961 962 963

**Example**

S
shawn_he 已提交
964
```js
S
shawn_he 已提交
965 966 967 968 969 970 971 972 973 974 975
connection.getDefaultNet().then(function (netHandle) {
    connection.reportNetConnected(netHandle, function (error) {
        console.log(JSON.stringify(error))
    });
});
```

## connection.reportNetConnected

reportNetConnected(netHandle: NetHandle): Promise&lt;void&gt;

S
shawn_he 已提交
976 977
Reports a **netAavailable** event to NetManager. If this API is called, the application considers that its network status (ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED) is inconsistent with that of NetManager.
This API uses a promise to return the result.
S
shawn_he 已提交
978

S
shawn_he 已提交
979
**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET
S
shawn_he 已提交
980 981 982 983

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**
S
shawn_he 已提交
984

S
shawn_he 已提交
985 986 987 988
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).|

S
shawn_he 已提交
989
**Return value**
S
shawn_he 已提交
990 991
| Type| Description|
| -------- | -------- |
S
shawn_he 已提交
992
| Promise&lt;void&gt; | Promise that returns no value.|
S
shawn_he 已提交
993

S
shawn_he 已提交
994 995 996 997 998 999 1000 1001 1002 1003
**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

S
shawn_he 已提交
1004 1005
**Example**

S
shawn_he 已提交
1006
```js
S
shawn_he 已提交
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
connection.getDefaultNet().then(function (netHandle) {
    connection.reportNetConnected(netHandle).then(function () {
        console.log(`report success`)
    });
});
```

## connection.reportNetDisconnected

reportNetDisconnected(netHandle: NetHandle, callback: AsyncCallback&lt;void&gt;): void

S
shawn_he 已提交
1018 1019
Reports a **netAavailable** event to NetManager. If this API is called, the application considers that its network status (ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED) is inconsistent with that of NetManager.
This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
1020

S
shawn_he 已提交
1021
**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET
S
shawn_he 已提交
1022 1023 1024 1025

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**
S
shawn_he 已提交
1026

S
shawn_he 已提交
1027 1028 1029
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).|
S
shawn_he 已提交
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result. If the network status is reported successfully, **err** is **undefined**. Otherwise, **err** is an error object.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
1041 1042 1043

**Example**

S
shawn_he 已提交
1044
```js
S
shawn_he 已提交
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
connection.getDefaultNet().then(function (netHandle) {
    connection.reportNetDisconnected(netHandle, function (error) {
        console.log(JSON.stringify(error))
    });
});
```

## connection.reportNetDisconnected

reportNetDisconnected(netHandle: NetHandle): Promise&lt;void&gt;

S
shawn_he 已提交
1056 1057
Reports a **netAavailable** event to NetManager. If this API is called, the application considers that its network status (ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED) is inconsistent with that of NetManager.
This API uses a promise to return the result.
S
shawn_he 已提交
1058

S
shawn_he 已提交
1059
**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET
S
shawn_he 已提交
1060 1061 1062 1063

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**
S
shawn_he 已提交
1064

S
shawn_he 已提交
1065 1066 1067 1068
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).|

S
shawn_he 已提交
1069
**Return value**
S
shawn_he 已提交
1070 1071
| Type| Description|
| -------- | -------- |
S
shawn_he 已提交
1072
| Promise&lt;void&gt; | Promise that returns no value.|
S
shawn_he 已提交
1073

S
shawn_he 已提交
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

S
shawn_he 已提交
1084 1085
**Example**

S
shawn_he 已提交
1086
```js
S
shawn_he 已提交
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
connection.getDefaultNet().then(function (netHandle) {
    connection.reportNetDisconnected(netHandle).then(function () {
        console.log(`report success`)
    });
});
```

## connection.getAddressesByName

getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>>): void

Resolves the host name by using the default network to obtain all IP addresses. This API uses an asynchronous callback to return the result.

S
shawn_he 已提交
1100
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
1101 1102 1103 1104 1105 1106 1107

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                                             | Mandatory| Description              |
| -------- | ------------------------------------------------- | ---- | ------------------ |
S
shawn_he 已提交
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
| host     | string                                            | Yes  | Host name to resolve.|
| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | Yes  | Callback used to return the result. If all IP addresses are successfully obtained, **err** is **undefined**, and **data** is the list of all obtained IP addresses. Otherwise, **err** is an error object.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
1120 1121 1122 1123

**Example**

```js
S
shawn_he 已提交
1124 1125
let host = "xxxx";
connection.getAddressesByName(host, function (error, data) {
S
shawn_he 已提交
1126
    console.log(JSON.stringify(error))
S
shawn_he 已提交
1127
    console.log(JSON.stringify(data))
S
shawn_he 已提交
1128 1129 1130
})
```

S
shawn_he 已提交
1131
## connection.getAddressesByName
S
shawn_he 已提交
1132

S
shawn_he 已提交
1133
getAddressesByName(host: string): Promise\<Array\<NetAddress>>
S
shawn_he 已提交
1134

S
shawn_he 已提交
1135
Resolves the host name by using the default network to obtain all IP addresses. This API uses a promise to return the result.
S
shawn_he 已提交
1136

S
shawn_he 已提交
1137 1138 1139
**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Communication.NetManager.Core
S
shawn_he 已提交
1140

S
shawn_he 已提交
1141 1142 1143 1144 1145
**Parameters**

| Name| Type  | Mandatory| Description              |
| ------ | ------ | ---- | ------------------ |
| host   | string | Yes  | Host name to resolve.|
S
shawn_he 已提交
1146 1147 1148 1149 1150

**Return value**

| Type                                       | Description                         |
| ------------------------------------------- | ----------------------------- |
S
shawn_he 已提交
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
| Promise\<Array\<[NetAddress](#netaddress)>> | Promise used to return the result.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
1162 1163 1164 1165

**Example**

```js
S
shawn_he 已提交
1166 1167 1168
let host = "xxxx";
connection.getAddressesByName(host).then(function (data) {
    console.log(JSON.stringify(data))
S
shawn_he 已提交
1169 1170 1171
})
```

S
shawn_he 已提交
1172
## NetConnection
S
shawn_he 已提交
1173

S
shawn_he 已提交
1174
Represents the network connection handle.
S
shawn_he 已提交
1175

S
shawn_he 已提交
1176
### register
S
shawn_he 已提交
1177

S
shawn_he 已提交
1178
register(callback: AsyncCallback\<void>): void
S
shawn_he 已提交
1179

S
shawn_he 已提交
1180
Registers a listener for network status changes.
S
shawn_he 已提交
1181

S
shawn_he 已提交
1182
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
1183

S
shawn_he 已提交
1184
**System capability**: SystemCapability.Communication.NetManager.Core
S
shawn_he 已提交
1185

S
shawn_he 已提交
1186
**Parameters**
S
shawn_he 已提交
1187

S
shawn_he 已提交
1188 1189 1190
| Name  | Type                | Mandatory| Description      |
| -------- | -------------------- | ---- | ---------- |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result. If a listener for network status changes is registered successfully, **err** is **undefined**. Otherwise, **err** is an error object.|
S
shawn_he 已提交
1191

S
shawn_he 已提交
1192
**Error codes**
S
shawn_he 已提交
1193

S
shawn_he 已提交
1194 1195 1196 1197 1198 1199 1200
| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
| 2101008 | The callback is not exists.      |
| 2101022 | The number of requests exceeded the maximum. |
S
shawn_he 已提交
1201 1202 1203 1204 1205


**Example**

```js
S
shawn_he 已提交
1206
netConnection.register(function (error) {
S
shawn_he 已提交
1207 1208 1209 1210
    console.log(JSON.stringify(error))
})
```

S
shawn_he 已提交
1211
### unregister
S
shawn_he 已提交
1212

S
shawn_he 已提交
1213
unregister(callback: AsyncCallback\<void>): void
S
shawn_he 已提交
1214

S
shawn_he 已提交
1215
Unregisters the listener for network status changes.
S
shawn_he 已提交
1216 1217 1218 1219 1220

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

S
shawn_he 已提交
1221 1222 1223
| Name  | Type                | Mandatory| Description      |
| -------- | -------------------- | ---- | ---------- |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result. If a listener for network status changes is unregistered successfully, **err** is **undefined**. Otherwise, **err** is an error object.|
S
shawn_he 已提交
1224

S
shawn_he 已提交
1225
**Error codes**
S
shawn_he 已提交
1226

S
shawn_he 已提交
1227 1228 1229 1230 1231
| ID| Error Message                       |
| ------- | -----------------------------  |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
| 2101007 | The same callback exists.      |
S
shawn_he 已提交
1232 1233 1234

**Example**

S
shawn_he 已提交
1235
```js
S
shawn_he 已提交
1236 1237
netConnection.unregister(function (error) {
    console.log(JSON.stringify(error))
S
shawn_he 已提交
1238 1239 1240 1241 1242 1243 1244 1245 1246
})
```

### on('netAvailable')

on(type: 'netAvailable', callback: Callback\<NetHandle>): void

Registers a listener for **netAvailable** events.

S
shawn_he 已提交
1247 1248
**Model restriction**: Before you call this API, make sure that you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network.

S
shawn_he 已提交
1249 1250 1251 1252 1253 1254
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                              | Mandatory| Description                                                        |
| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
1255 1256
| type     | string                             | Yes  | Event type. The value is fixed to **netAvailable**.<br>**netAvailable**: event indicating that the data network is available.|
| callback | Callback\<[NetHandle](#nethandle)> | Yes  | Callback used to return the network handle.|
S
shawn_he 已提交
1257 1258 1259

**Example**

S
shawn_he 已提交
1260
```js
S
shawn_he 已提交
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
// Create a NetConnection object.
let netCon = connection.createNetConnection()

// Call register to register a listener.
netCon.register(function (error) {
    console.log(JSON.stringify(error))
})

// Subscribe to netAvailable events. Event notifications can be received only after register is called.
netCon.on('netAvailable', function (data) {
S
shawn_he 已提交
1271 1272
    console.log(JSON.stringify(data))
})
S
shawn_he 已提交
1273 1274 1275 1276 1277

// Call unregister to unregister the listener.
netCon.unregister(function (error) {
    console.log(JSON.stringify(error))
})
S
shawn_he 已提交
1278 1279
```

S
shawn_he 已提交
1280
### on('netBlockStatusChange')
S
shawn_he 已提交
1281

S
shawn_he 已提交
1282
on(type: 'netBlockStatusChange', callback: Callback&lt;{ netHandle: NetHandle, blocked: boolean }&gt;): void
S
shawn_he 已提交
1283

S
shawn_he 已提交
1284 1285 1286
Registers a listener for **netBlockStatusChange** events.

**Model restriction**: Before you call this API, make sure tat you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network.
S
shawn_he 已提交
1287 1288 1289 1290 1291 1292 1293

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
1294 1295
| type     | string                                                       | Yes  | Event type. The value is fixed to **netBlockStatusChange**.<br>**netBlockStatusChange**: event indicating a change in the network blocking status.|
| callback | Callback&lt;{&nbsp;netHandle:&nbsp;[NetHandle](#nethandle),&nbsp;blocked:&nbsp;boolean&nbsp;}&gt; | Yes  | Callback used to return the network handle (**netHandle**) and network status (**blocked**).|
S
shawn_he 已提交
1296 1297 1298

**Example**

S
shawn_he 已提交
1299
```js
S
shawn_he 已提交
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
// Create a NetConnection object.
let netCon = connection.createNetConnection()

// Call register to register a listener.
netCon.register(function (error) {
    console.log(JSON.stringify(error))
})

// Subscribe to netBlockStatusChange events. Event notifications can be received only after register is called.
netCon.on('netBlockStatusChange', function (data) {
S
shawn_he 已提交
1310 1311
    console.log(JSON.stringify(data))
})
S
shawn_he 已提交
1312 1313 1314 1315 1316

// Call unregister to unregister the listener.
netCon.unregister(function (error) {
    console.log(JSON.stringify(error))
})
S
shawn_he 已提交
1317 1318
```

S
shawn_he 已提交
1319
### on('netCapabilitiesChange')
S
shawn_he 已提交
1320

S
shawn_he 已提交
1321
on(type: 'netCapabilitiesChange', callback: Callback<{ netHandle: NetHandle, netCap: NetCapabilities }>): void
S
shawn_he 已提交
1322

S
shawn_he 已提交
1323 1324 1325
Registers a listener for **netCapabilitiesChange** events.

**Model restriction**: Before you call this API, make sure tat you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network.
S
shawn_he 已提交
1326 1327 1328 1329 1330 1331 1332

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
1333 1334
| type     | string                                                       | Yes  | Event type. The value is fixed to **netCapabilitiesChange**.<br>**netCapabilitiesChange**: event indicating that the network capabilities have changed.|
| callback | Callback<{ netHandle: [NetHandle](#nethandle), netCap: [NetCapabilities](#netcapabilities) }> | Yes  | Callback used to return the network handle (**netHandle**) and capability information (**netCap**).|
S
shawn_he 已提交
1335 1336 1337

**Example**

S
shawn_he 已提交
1338
```js
S
shawn_he 已提交
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
// Create a NetConnection object.
let netCon = connection.createNetConnection()

// Call register to register a listener.
netCon.register(function (error) {
    console.log(JSON.stringify(error))
})

// Subscribe to netCapabilitiesChange events. Event notifications can be received only after register is called.
netCon.on('netCapabilitiesChange', function (data) {
S
shawn_he 已提交
1349 1350
    console.log(JSON.stringify(data))
})
S
shawn_he 已提交
1351 1352 1353 1354 1355

// Call unregister to unregister the listener.
netCon.unregister(function (error) {
    console.log(JSON.stringify(error))
})
S
shawn_he 已提交
1356 1357
```

S
shawn_he 已提交
1358
### on('netConnectionPropertiesChange')
S
shawn_he 已提交
1359

S
shawn_he 已提交
1360
on(type: 'netConnectionPropertiesChange', callback: Callback<{ netHandle: NetHandle, connectionProperties: ConnectionProperties }>): void
S
shawn_he 已提交
1361

S
shawn_he 已提交
1362 1363 1364
Registers a listener for **netConnectionPropertiesChange** events.

**Model restriction**: Before you call this API, make sure tat you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network.
S
shawn_he 已提交
1365 1366 1367 1368 1369 1370 1371

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
1372 1373
| type     | string                                                       | Yes  | Event type. The value is fixed to **netConnectionPropertiesChange**.<br>**netConnectionPropertiesChange**: event indicating that network connection properties have changed.|
| callback | Callback<{ netHandle: [NetHandle](#nethandle), connectionProperties: [ConnectionProperties](#connectionproperties) }> | Yes  | Callback used to return the network handle (**netHandle**) and capability information (**netCap**).|
S
shawn_he 已提交
1374 1375 1376

**Example**

S
shawn_he 已提交
1377
```js
S
shawn_he 已提交
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
// Create a NetConnection object.
let netCon = connection.createNetConnection()

// Call register to register a listener.
netCon.register(function (error) {
    console.log(JSON.stringify(error))
})

// Subscribe to netConnectionPropertiesChange events. Event notifications can be received only after register is called.
netCon.on('netConnectionPropertiesChange', function (data) {
S
shawn_he 已提交
1388 1389
    console.log(JSON.stringify(data))
})
S
shawn_he 已提交
1390 1391 1392 1393 1394

// Call unregister to unregister the listener.
netCon.unregister(function (error) {
    console.log(JSON.stringify(error))
})
S
shawn_he 已提交
1395 1396 1397 1398 1399 1400 1401 1402
```

### on('netLost')

on(type: 'netLost', callback: Callback\<NetHandle>): void

Registers a listener for **netLost** events.

S
shawn_he 已提交
1403 1404
**Model restriction**: Before you call this API, make sure tat you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network.

S
shawn_he 已提交
1405 1406 1407 1408 1409 1410
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                              | Mandatory| Description                                                        |
| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
1411 1412
| type     | string                             | Yes  | Event type. The value is fixed to **netLost**.<br>netLost: event indicating that the network is interrupted or normally disconnected.|
| callback | Callback\<[NetHandle](#nethandle)> | Yes  | Callback used to return the network handle (**netHandle**).|
S
shawn_he 已提交
1413 1414 1415

**Example**

S
shawn_he 已提交
1416
```js
S
shawn_he 已提交
1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
// Create a NetConnection object.
let netCon = connection.createNetConnection()

// Call register to register a listener.
netCon.register(function (error) {
    console.log(JSON.stringify(error))
})

// Subscribe to netLost events. Event notifications can be received only after register is called.
netCon.on('netLost', function (data) {
S
shawn_he 已提交
1427 1428
    console.log(JSON.stringify(data))
})
S
shawn_he 已提交
1429 1430 1431 1432 1433

// Call unregister to unregister the listener.
netCon.unregister(function (error) {
    console.log(JSON.stringify(error))
})
S
shawn_he 已提交
1434 1435 1436 1437 1438 1439 1440 1441
```

### on('netUnavailable')

on(type: 'netUnavailable', callback: Callback\<void>): void

Registers a listener for **netUnavailable** events.

S
shawn_he 已提交
1442 1443
**Model restriction**: Before you call this API, make sure tat you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network.

S
shawn_he 已提交
1444 1445 1446 1447 1448 1449
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type           | Mandatory| Description                                                        |
| -------- | --------------- | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
1450 1451
| type     | string          | Yes  | Event type. The value is fixed to **netUnavailable**.<br>**netUnavailable**: event indicating that the network is unavailable.|
| callback | Callback\<void> | Yes  | Callback used to return the result, which is empty.|
S
shawn_he 已提交
1452 1453 1454

**Example**

S
shawn_he 已提交
1455
```js
S
shawn_he 已提交
1456 1457
// Create a NetConnection object.
let netCon = connection.createNetConnection()
S
shawn_he 已提交
1458

S
shawn_he 已提交
1459 1460
// Call register to register a listener.
netCon.register(function (error) {
S
shawn_he 已提交
1461 1462 1463
    console.log(JSON.stringify(error))
})

S
shawn_he 已提交
1464 1465 1466 1467
// Subscribe to netUnavailable events. Event notifications can be received only after register is called.
netCon.on('netUnavailable', function (data) {
    console.log(JSON.stringify(data))
})
S
shawn_he 已提交
1468

S
shawn_he 已提交
1469 1470
// Call unregister to unregister the listener.
netCon.unregister(function (error) {
S
shawn_he 已提交
1471 1472 1473 1474 1475 1476 1477 1478
    console.log(JSON.stringify(error))
})
```

## NetHandle

Defines the handle of the data network.

S
shawn_he 已提交
1479
Before invoking **NetHandle** APIs, call **getNetHandle** to obtain a **NetHandle** object.
S
shawn_he 已提交
1480 1481 1482

**System capability**: SystemCapability.Communication.NetManager.Core

S
shawn_he 已提交
1483
### Attributes
S
shawn_he 已提交
1484

S
shawn_he 已提交
1485 1486 1487
| Name   | Type  | Mandatory| Description                     |
| ------ | ------ | --- |------------------------- |
| netId  | number | Yes |  Network ID. The value **0** indicates no default network. Any other value must be greater than or equal to 100.|
S
shawn_he 已提交
1488

S
shawn_he 已提交
1489
### bindSocket<sup>9+</sup>
S
shawn_he 已提交
1490

S
shawn_he 已提交
1491
bindSocket(socketParam: TCPSocket \| UDPSocket, callback: AsyncCallback\<void>): void
S
shawn_he 已提交
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501

Binds a **TCPSocket** or **UDPSocket** object to the data network. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name     | Type                    | Mandatory| Description                           |
| ----------- | ------------------------ | ---- | -------------------------------|
| socketParam | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | Yes| **TCPSocket** or **UDPSocket** object.|
S
shawn_he 已提交
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
| callback    | AsyncCallback\<void>      | Yes  | Callback used to return the result. If the **TCPSocket** or **UDPSocket** object is successfully bound to the current network, **err** is **undefined**. Otherwise, **err** is an error object.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
1512 1513 1514 1515

**Example**

```js
S
shawn_he 已提交
1516
import socket from "@ohos.net.socket";
S
shawn_he 已提交
1517
connection.getDefaultNet().then((netHandle) => {
S
shawn_he 已提交
1518 1519
    var tcp = socket.constructTCPSocketInstance();
    var udp = socket.constructUDPSocketInstance();
S
shawn_he 已提交
1520
    let socketType = "TCPSocket";
S
shawn_he 已提交
1521 1522
    if (socketType == "TCPSocket") {
        tcp.bind({
S
shawn_he 已提交
1523 1524 1525
            address: '192.168.xx.xxx', port: 8080, family: 1
        }, error => {
            if (error) {
S
shawn_he 已提交
1526 1527
                console.log('bind fail');
            }
S
shawn_he 已提交
1528
            netHandle.bindSocket(tcp, (error, data) => {
S
shawn_he 已提交
1529 1530 1531 1532 1533 1534
                if (error) {
                    console.log(JSON.stringify(error));
                } else {
                    console.log(JSON.stringify(data));
                }
            })
S
shawn_he 已提交
1535 1536
        })
    } else {
S
shawn_he 已提交
1537
        let callback = value => {
S
shawn_he 已提交
1538
            console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo);
S
shawn_he 已提交
1539
        }
S
shawn_he 已提交
1540 1541
        udp.on('message', callback);
        udp.bind({
S
shawn_he 已提交
1542 1543 1544
            address: '192.168.xx.xxx', port: 8080, family: 1
        }, error => {
            if (error) {
S
shawn_he 已提交
1545 1546
                console.log('bind fail');
            }
S
shawn_he 已提交
1547
            udp.on('message', (data) => {
S
shawn_he 已提交
1548
                console.log(JSON.stringify(data))
S
shawn_he 已提交
1549
            });
S
shawn_he 已提交
1550
            netHandle.bindSocket(udp, (error, data) => {
S
shawn_he 已提交
1551 1552 1553 1554 1555 1556
                if (error) {
                    console.log(JSON.stringify(error));
                } else {
                    console.log(JSON.stringify(data));
                }
            })
S
shawn_he 已提交
1557
        })
S
shawn_he 已提交
1558 1559
    }
})
S
shawn_he 已提交
1560 1561
```

S
shawn_he 已提交
1562
### bindSocket<sup>9+</sup>
S
shawn_he 已提交
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579

bindSocket(socketParam: TCPSocket \| UDPSocket): Promise\<void>;

Binds a **TCPSocket** or **UDPSocket** object to the data network. This API uses a promise to return the result.

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name         | Type                 | Mandatory | Description                          |
| --------------- | --------------------- | ---- | ------------------------------ |
| socketParam     | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | Yes  | **TCPSocket** or **UDPSocket** object.|

**Return value**

| Type          | Description                  |
| -------------- | ---------------------- |
S
shawn_he 已提交
1580
| Promise\<void> | Promise that returns no value.|
S
shawn_he 已提交
1581

S
shawn_he 已提交
1582 1583 1584 1585 1586 1587 1588 1589 1590
**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

S
shawn_he 已提交
1591 1592 1593
**Example**

```js
S
shawn_he 已提交
1594
import socket from "@ohos.net.socket";
S
shawn_he 已提交
1595
connection.getDefaultNet().then((netHandle) => {
S
shawn_he 已提交
1596 1597
    var tcp = socket.constructTCPSocketInstance();
    var udp = socket.constructUDPSocketInstance();
S
shawn_he 已提交
1598 1599
    let socketType = "TCPSocket";
    if (socketType == "TCPSocket") {
S
shawn_he 已提交
1600
        tcp.bind({
S
shawn_he 已提交
1601 1602 1603
            address: '192.168.xx.xxx', port: 8080, family: 1
        }, error => {
            if (error) {
S
shawn_he 已提交
1604 1605
                console.log('bind fail');
            }
S
shawn_he 已提交
1606 1607 1608 1609
            netHandle.bindSocket(tcp).then((data) => {
                console.log(JSON.stringify(data));
            }).catch(error => {
                console.log(JSON.stringify(error));
S
shawn_he 已提交
1610
            })
S
shawn_he 已提交
1611 1612
        })
    } else {
S
shawn_he 已提交
1613
        let callback = value => {
S
shawn_he 已提交
1614
            console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo);
S
shawn_he 已提交
1615
        }
S
shawn_he 已提交
1616 1617
        udp.on('message', callback);
        udp.bind({
S
shawn_he 已提交
1618 1619 1620
            address: '192.168.xx.xxx', port: 8080, family: 1
        }, error => {
            if (error) {
S
shawn_he 已提交
1621 1622
                console.log('bind fail');
            }
S
shawn_he 已提交
1623
            udp.on('message', (data) => {
S
shawn_he 已提交
1624 1625
                console.log(JSON.stringify(data));
            })
S
shawn_he 已提交
1626 1627 1628 1629
            netHandle.bindSocket(udp).then((data) => {
                console.log(JSON.stringify(data));
            }).catch(error => {
                console.log(JSON.stringify(error));
S
shawn_he 已提交
1630
            })
S
shawn_he 已提交
1631
        })
S
shawn_he 已提交
1632 1633
    }
})
S
shawn_he 已提交
1634 1635
```

S
shawn_he 已提交
1636 1637 1638 1639 1640 1641
### getAddressesByName

getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>>): void

Resolves the host name by using the corresponding network to obtain all IP addresses. This API uses an asynchronous callback to return the result.

S
shawn_he 已提交
1642 1643
**Required permission**: ohos.permission.GET_NETWORK_INFO

S
shawn_he 已提交
1644 1645 1646 1647 1648 1649
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                                             | Mandatory| Description              |
| -------- | ------------------------------------------------- | ---- | ------------------ |
S
shawn_he 已提交
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661
| host     | string                                            | Yes  | Host name to resolve.|
| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | Yes  | Callback used to return the result. If all IP addresses are successfully obtained, **err** is **undefined**, and **data** is the list of all obtained IP addresses. Otherwise, **err** is an error object.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
1662 1663 1664

**Example**

S
shawn_he 已提交
1665
```js
S
shawn_he 已提交
1666 1667
connection.getDefaultNet().then(function (netHandle) {
    let host = "xxxx";
S
shawn_he 已提交
1668
    netHandle.getAddressesByName(host, function (error, data) {
S
shawn_he 已提交
1669
        console.log(JSON.stringify(error))
S
shawn_he 已提交
1670
        console.log(JSON.stringify(data))
S
shawn_he 已提交
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
    })
})
```

### getAddressesByName

getAddressesByName(host: string): Promise\<Array\<NetAddress>>

Resolves the host name by using the corresponding network to obtain all IP addresses. This API uses a promise to return the result.

S
shawn_he 已提交
1681 1682
**Required permission**: ohos.permission.GET_NETWORK_INFO

S
shawn_he 已提交
1683 1684 1685 1686 1687 1688
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name| Type  | Mandatory| Description              |
| ------ | ------ | ---- | ------------------ |
S
shawn_he 已提交
1689
| host   | string | Yes  | Host name to resolve.|
S
shawn_he 已提交
1690

S
shawn_he 已提交
1691
**Return value**
S
shawn_he 已提交
1692 1693 1694 1695 1696

| Type                                       | Description                         |
| ------------------------------------------- | ----------------------------- |
| Promise\<Array\<[NetAddress](#netaddress)>> | Promise used to return the result.|

S
shawn_he 已提交
1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

S
shawn_he 已提交
1707 1708
**Example**

S
shawn_he 已提交
1709
```js
S
shawn_he 已提交
1710 1711
connection.getDefaultNet().then(function (netHandle) {
    let host = "xxxx";
S
shawn_he 已提交
1712 1713
    netHandle.getAddressesByName(host).then(function (data) {
        console.log(JSON.stringify(data))
S
shawn_he 已提交
1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
    })
})
```

### getAddressByName

getAddressByName(host: string, callback: AsyncCallback\<NetAddress>): void

Resolves the host name by using the corresponding network to obtain the first IP address. This API uses an asynchronous callback to return the result.

S
shawn_he 已提交
1724 1725
**Required permission**: ohos.permission.GET_NETWORK_INFO

S
shawn_he 已提交
1726 1727 1728 1729 1730 1731
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                                     | Mandatory| Description              |
| -------- | ----------------------------------------- | ---- | ------------------ |
S
shawn_he 已提交
1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
| host     | string                                    | Yes  | Host name to resolve.|
| callback | AsyncCallback\<[NetAddress](#netaddress)> | Yes  | Callback used to return the result. If the first IP address is obtained successfully, **err** is **undefined**, and **data** is the first obtained IP address. Otherwise, **err** is an error object.        |

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
1744 1745 1746

**Example**

S
shawn_he 已提交
1747
```js
S
shawn_he 已提交
1748 1749
connection.getDefaultNet().then(function (netHandle) {
    let host = "xxxx";
S
shawn_he 已提交
1750
    netHandle.getAddressByName(host, function (error, data) {
S
shawn_he 已提交
1751
        console.log(JSON.stringify(error))
S
shawn_he 已提交
1752
        console.log(JSON.stringify(data))
S
shawn_he 已提交
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
    })
})
```

### getAddressByName

getAddressByName(host: string): Promise\<NetAddress>

Resolves the host name by using the corresponding network to obtain the first IP address. This API uses a promise to return the result.

S
shawn_he 已提交
1763 1764
**Required permission**: ohos.permission.GET_NETWORK_INFO

S
shawn_he 已提交
1765 1766 1767 1768 1769 1770
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name| Type  | Mandatory| Description              |
| ------ | ------ | ---- | ------------------ |
S
shawn_he 已提交
1771
| host   | string | Yes  | Host name to resolve.|
S
shawn_he 已提交
1772

S
shawn_he 已提交
1773
**Return value**
S
shawn_he 已提交
1774 1775 1776 1777 1778

| Type                               | Description                           |
| ----------------------------------- | ------------------------------- |
| Promise\<[NetAddress](#netaddress)> | Promise used to return the result.|

S
shawn_he 已提交
1779 1780 1781 1782 1783 1784 1785 1786 1787 1788
**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

S
shawn_he 已提交
1789 1790
**Example**

S
shawn_he 已提交
1791
```js
S
shawn_he 已提交
1792 1793
connection.getDefaultNet().then(function (netHandle) {
    let host = "xxxx";
S
shawn_he 已提交
1794 1795
    netHandle.getAddressByName(host).then(function (data) {
        console.log(JSON.stringify(data))
S
shawn_he 已提交
1796 1797 1798 1799
    })
})
```

S
shawn_he 已提交
1800
## NetCap
S
shawn_he 已提交
1801

S
shawn_he 已提交
1802
Defines the network capability.
S
shawn_he 已提交
1803 1804 1805

**System capability**: SystemCapability.Communication.NetManager.Core

S
shawn_he 已提交
1806 1807 1808 1809 1810 1811 1812
| Name                 | Value  | Description                  |
| ------------------------ | ---- | ---------------------- |
| NET_CAPABILITY_MMS | 0 | The network can connect to the carrier's Multimedia Messaging Service Center (MMSC) to send and receive multimedia messages.|
| NET_CAPABILITY_NOT_METERED | 11 | The network traffic is not metered.|
| NET_CAPABILITY_INTERNET  | 12   | The network has the Internet access capability, which is set by the network provider.|
| NET_CAPABILITY_NOT_VPN | 15 | The network does not use a virtual private network (VPN).|
| NET_CAPABILITY_VALIDATED | 16   | The Internet access capability of the network is successfully verified by the connection management module.|
S
shawn_he 已提交
1813

S
shawn_he 已提交
1814
## NetBearType
S
shawn_he 已提交
1815

S
shawn_he 已提交
1816
Enumerates network types.
S
shawn_he 已提交
1817 1818 1819

**System capability**: SystemCapability.Communication.NetManager.Core

S
shawn_he 已提交
1820 1821 1822 1823 1824
| Name        | Value  | Description       |
| --------------- | ---- | ----------- |
| BEARER_CELLULAR | 0    | Cellular network. |
| BEARER_WIFI     | 1    | Wi-Fi network.|
| BEARER_ETHERNET | 3 | Ethernet network.|
S
shawn_he 已提交
1825

S
shawn_he 已提交
1826
## HttpProxy<sup>10+</sup>
S
shawn_he 已提交
1827

S
shawn_he 已提交
1828
Defines the global HTTP proxy configuration of the network.
S
shawn_he 已提交
1829 1830 1831

**System capability**: SystemCapability.Communication.NetManager.Core

S
shawn_he 已提交
1832 1833 1834 1835 1836
| Name   | Type  | Mandatory| Description                     |
| ------ | ------ | --- |------------------------- |
| host  | string | No |  Host name of the proxy server.|
| port  | number | No |  Host port.|
| exclusionList  | Array<string> | No |  List of hosts that do not use the proxy server.|
S
shawn_he 已提交
1837

S
shawn_he 已提交
1838
## NetSpecifier
S
shawn_he 已提交
1839

S
shawn_he 已提交
1840
Provides an instance that bears data network capabilities.
S
shawn_he 已提交
1841 1842 1843

**System capability**: SystemCapability.Communication.NetManager.Core

S
shawn_he 已提交
1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860
| Name                    | Type                               | Mandatory | Description                                                        |
| ----------------------- | ----------------------------------- | ---- | ------------------------------------------------------------ |
| netCapabilities         | [NetCapabilities](#netcapabilities) |  Yes | Network transmission capabilities and bearer types of the data network.                               |
| bearerPrivateIdentifier | string                              |  No |  Network identifier. The identifier of a Wi-Fi network is **wifi**, and that of a cellular network is **slot0** (corresponding to SIM card 1).|

## NetCapabilities

Defines the network capability set.

**System capability**: SystemCapability.Communication.NetManager.Core

| Name                 | Type                               | Mandatory| Description                    |
| --------------------- | ---------------------------------- | --- | ------------------------ |
| linkUpBandwidthKbps   | number                             |  No|  Uplink (from the device to the network) bandwidth. |
| linkDownBandwidthKbps | number                             |  No|  Downlink (from the network to the device) bandwidth.  |
| networkCap            | Array\<[NetCap](#netcap)>           |  No|  Network capability.          |
| bearerTypes           | Array\<[NetBearType](#netbeartype)> |  Yes|  Network type.              |
S
shawn_he 已提交
1861 1862 1863 1864 1865 1866 1867

## ConnectionProperties

Defines the network connection properties.

**System capability**: SystemCapability.Communication.NetManager.Core

S
shawn_he 已提交
1868 1869 1870 1871 1872 1873 1874 1875
| Name          | Type                              | Mandatory|  Description            |
| ------------- | ---------------------------------- | ----|---------------- |
| interfaceName | string                             | Yes|Network interface card (NIC) name.      |
| domains       | string                             | Yes|Domain. The default value is **""**.|
| linkAddresses | Array\<[LinkAddress](#linkaddress)> | Yes|Link information.      |
| routes        | Array\<[RouteInfo](#routeinfo)>     | Yes|Route information.      |
| dnses     | Array\<[NetAddress](#netaddress)> | Yes|Network address. For details, see [NetAddress](#netaddress).|
| mtu           | number                             | Yes|Maximum transmission unit (MTU).  |
S
shawn_he 已提交
1876

S
shawn_he 已提交
1877
## RouteInfo
S
shawn_he 已提交
1878

S
shawn_he 已提交
1879
Defines network route information.
S
shawn_he 已提交
1880 1881 1882

**System capability**: SystemCapability.Communication.NetManager.Core

S
shawn_he 已提交
1883 1884 1885 1886 1887 1888 1889
| Name          | Type                       | Mandatory|Description            |
| -------------- | --------------------------- | --- |---------------- |
| interface      | string                      | Yes|NIC name.      |
| destination    | [LinkAddress](#linkaddress) | Yes|Destination address.      |
| gateway        | [NetAddress](#netaddress)   | Yes|Gateway address.      |
| hasGateway     | boolean                     | Yes|Whether a gateway is present.    |
| isDefaultRoute | boolean                     | Yes|Whether the route is the default route.|
S
shawn_he 已提交
1890

S
shawn_he 已提交
1891
## LinkAddress
S
shawn_he 已提交
1892

S
shawn_he 已提交
1893
Defines network link information.
S
shawn_he 已提交
1894 1895 1896

**System capability**: SystemCapability.Communication.NetManager.Core

S
shawn_he 已提交
1897 1898 1899 1900
| Name       | Type                     | Mandatory|Description                |
| ------------ | ----------------------- |---- |-------------------- |
| address      | [NetAddress](#netaddress) | Yes| Link address.          |
| prefixLength | number                    | Yes|Length of the link address prefix.|
S
shawn_he 已提交
1901 1902 1903

## NetAddress

S
shawn_he 已提交
1904
Defines a network address.
S
shawn_he 已提交
1905 1906 1907

**System capability**: SystemCapability.Communication.NetManager.Core

S
shawn_he 已提交
1908 1909 1910 1911 1912
| Name   | Type  | Mandatory| Description                          |
| ------- | ------ | -- |------------------------------ |
| address | string | Yes|Network address.                        |
| family  | number | No|Address family identifier. The value is **1** for IPv4 and **2** for IPv6. The default value is **1**.|
| port    | number | No|Port number. The value ranges from **0** to **65535**.   |