js-apis-net-connection.md 39.9 KB
Newer Older
S
shawn_he 已提交
1 2
# Network Connection Management

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 10
>
> 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 已提交
11
```js
S
shawn_he 已提交
12 13 14 15 16 17 18
import connection from '@ohos.net.connection'
```

## connection.getDefaultNet

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

S
shawn_he 已提交
19
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 已提交
20

S
shawn_he 已提交
21
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
22 23 24 25 26 27 28 29 30 31 32

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

**Parameters**

| Name  | Type                                   | Mandatory| Description      |
| -------- | --------------------------------------- | ---- | ---------- |
| callback | AsyncCallback\<[NetHandle](#nethandle)> | Yes  | Callback used to return the result.|

**Example**

S
shawn_he 已提交
33
```js
S
shawn_he 已提交
34 35 36 37 38 39 40 41 42 43
connection.getDefaultNet(function (error, netHandle) {
    console.log(JSON.stringify(error))
    console.log(JSON.stringify(netHandle))
})
```

## connection.getDefaultNet

getDefaultNet(): Promise\<NetHandle>

S
shawn_he 已提交
44
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 已提交
45

S
shawn_he 已提交
46
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
47 48 49

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

S
shawn_he 已提交
50
**Return value**
S
shawn_he 已提交
51 52 53 54 55 56 57

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

**Example**

S
shawn_he 已提交
58
```js
S
shawn_he 已提交
59 60 61 62 63
connection.getDefaultNet().then(function (netHandle) {
    console.log(JSON.stringify(netHandle))
})
```

S
shawn_he 已提交
64 65 66 67
## connection.getDefaultNetSync

getDefaultNetSync(): NetHandle;

S
shawn_he 已提交
68
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 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

**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.|

**Example**

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


S
shawn_he 已提交
87 88 89 90
## connection.hasDefaultNet

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

S
shawn_he 已提交
91
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.
S
shawn_he 已提交
92 93

**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
94 95 96 97 98 99 100 101 102 103 104

**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 that the default data network is activated.|

**Example**

S
shawn_he 已提交
105
```js
S
shawn_he 已提交
106 107
connection.hasDefaultNet(function (error, has) {
    console.log(JSON.stringify(error))
S
shawn_he 已提交
108
    console.log('has: ' + has)
S
shawn_he 已提交
109 110 111 112 113 114 115
})
```

## connection.hasDefaultNet

hasDefaultNet(): Promise\<boolean>

S
shawn_he 已提交
116
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.
S
shawn_he 已提交
117 118

**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
119 120 121

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

S
shawn_he 已提交
122
**Return value**
S
shawn_he 已提交
123 124 125 126 127 128 129

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

**Example**

S
shawn_he 已提交
130
```js
S
shawn_he 已提交
131
connection.hasDefaultNet().then(function (has) {
S
shawn_he 已提交
132
    console.log('has: ' + has)
S
shawn_he 已提交
133 134 135 136 137 138 139
})
```

## connection.getAllNets

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

S
shawn_he 已提交
140
Obtains the list of all active data networks. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
141

S
shawn_he 已提交
142
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
143 144 145 146

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

**Parameters**
S
shawn_he 已提交
147

S
shawn_he 已提交
148 149 150 151 152 153
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;Array&lt;[NetHandle](#nethandle)&gt;&gt; | Yes| Callback used to return the result.|

**Example**

S
shawn_he 已提交
154
```js
S
shawn_he 已提交
155 156 157 158 159 160 161 162 163 164 165
connection.getAllNets(function (error, nets) {
    console.log(JSON.stringify(error))
    console.log(JSON.stringify(nets))
});
```


## connection.getAllNets

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

S
shawn_he 已提交
166
Obtains the list of all active data networks. This API uses a promise to return the result.
S
shawn_he 已提交
167

S
shawn_he 已提交
168
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
169 170 171

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

S
shawn_he 已提交
172 173
**Return value**

S
shawn_he 已提交
174 175 176 177 178 179
| Type| Description|
| -------- | -------- |
| Promise&lt;Array&lt;[NetHandle](#nethandle)&gt;&gt; | Promise used to return the result.|

**Example**

S
shawn_he 已提交
180
```js
S
shawn_he 已提交
181 182 183 184 185 186 187 188 189
connection.getAllNets().then(function (nets) {
    console.log(JSON.stringify(nets))
});
```

## connection.getConnectionProperties

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

S
shawn_he 已提交
190
Obtains connection properties of the network corresponding to the given network handle. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
191

S
shawn_he 已提交
192
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
193 194 195 196 197 198 199

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

**Parameters**

| Name   | Type                                                        | Mandatory| Description            |
| --------- | ------------------------------------------------------------ | ---- | ---------------- |
S
shawn_he 已提交
200
| netHandle | [NetHandle](#nethandle)                                      | Yes  | Handle of the data network.|
S
shawn_he 已提交
201 202 203 204
| callback  | AsyncCallback\<[ConnectionProperties](#connectionproperties)> | Yes  | Callback used to return the result.      |

**Example**

S
shawn_he 已提交
205
```js
S
shawn_he 已提交
206 207 208 209 210 211 212 213 214 215 216 217
connection.getDefaultNet().then(function (netHandle) {
    connection.getConnectionProperties(netHandle, function (error, info) {
        console.log(JSON.stringify(error))
        console.log(JSON.stringify(info))
    })
})
```

## connection.getConnectionProperties

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

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

S
shawn_he 已提交
220
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
221 222 223 224 225 226 227

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

**Parameters**

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

S
shawn_he 已提交
230
**Return value**
S
shawn_he 已提交
231 232 233 234 235 236 237

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

**Example**

S
shawn_he 已提交
238
```js
S
shawn_he 已提交
239 240 241 242 243 244 245 246 247 248 249
connection.getDefaultNet().then(function (netHandle) {
    connection.getConnectionProperties(netHandle).then(function (info) {
        console.log(JSON.stringify(info))
    })
})
```

## connection.getNetCapabilities

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

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

S
shawn_he 已提交
252
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
253 254 255 256 257 258 259

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

**Parameters**

| Name   | Type                                               | Mandatory| Description            |
| --------- | --------------------------------------------------- | ---- | ---------------- |
S
shawn_he 已提交
260
| netHandle | [NetHandle](#nethandle)                             | Yes  | Handle of the data network.|
S
shawn_he 已提交
261 262 263 264
| callback  | AsyncCallback\<[NetCapabilities](#netcapabilities)> | Yes  | Callback used to return the result.      |

**Example**

S
shawn_he 已提交
265
```js
S
shawn_he 已提交
266 267 268 269 270 271 272 273 274 275 276 277
connection.getDefaultNet().then(function (netHandle) {
    connection.getNetCapabilities(netHandle, function (error, info) {
        console.log(JSON.stringify(error))
        console.log(JSON.stringify(info))
    })
})
```

## connection.getNetCapabilities

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

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

S
shawn_he 已提交
280
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
281 282 283 284 285 286 287

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

**Parameters**

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

S
shawn_he 已提交
290
**Return value**
S
shawn_he 已提交
291 292 293 294 295 296 297

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

**Example**

S
shawn_he 已提交
298
```js
S
shawn_he 已提交
299 300 301 302 303 304 305 306 307 308 309
connection.getDefaultNet().then(function (netHandle) {
    connection.getNetCapabilities(netHandle).then(function (info) {
        console.log(JSON.stringify(info))
    })
})
```

## connection.reportNetConnected

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

S
shawn_he 已提交
310
Reports connection of the data network. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
311

S
shawn_he 已提交
312 313
If this API is called, the application considers that the network connection state (**ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED**) is inconsistent with that in the network management module.

S
shawn_he 已提交
314
**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET
S
shawn_he 已提交
315 316 317 318

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

**Parameters**
S
shawn_he 已提交
319

S
shawn_he 已提交
320 321 322 323 324 325 326
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|

**Example**

S
shawn_he 已提交
327
```js
S
shawn_he 已提交
328 329 330 331 332 333 334 335 336 337 338 339
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 已提交
340
Reports connection of the data network. This API uses a promise to return the result.
S
shawn_he 已提交
341

S
shawn_he 已提交
342 343
If this API is called, the application considers that the network connection state (**ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED**) is inconsistent with that in the network management module.

S
shawn_he 已提交
344
**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET
S
shawn_he 已提交
345 346 347 348

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

**Parameters**
S
shawn_he 已提交
349

S
shawn_he 已提交
350 351 352 353
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).|

S
shawn_he 已提交
354 355
**Return value**

S
shawn_he 已提交
356 357
| Type| Description|
| -------- | -------- |
S
shawn_he 已提交
358
| Promise&lt;void&gt; | Promise that returns no value.|
S
shawn_he 已提交
359 360 361

**Example**

S
shawn_he 已提交
362
```js
S
shawn_he 已提交
363 364 365 366 367 368 369 370 371 372 373 374
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 已提交
375
Reports disconnection of the data network. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
376

S
shawn_he 已提交
377 378
If this API is called, the application considers that the network connection state (**ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED**) is inconsistent with that in the network management module.

S
shawn_he 已提交
379
**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET
S
shawn_he 已提交
380 381 382 383

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

**Parameters**
S
shawn_he 已提交
384

S
shawn_he 已提交
385 386 387 388 389 390 391
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|

**Example**

S
shawn_he 已提交
392
```js
S
shawn_he 已提交
393 394 395 396 397 398 399 400 401 402 403 404
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 已提交
405
Reports disconnection of the data network. This API uses a promise to return the result.
S
shawn_he 已提交
406

S
shawn_he 已提交
407 408
If this API is called, the application considers that the network connection state (**ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED**) is inconsistent with that in the network management module.

S
shawn_he 已提交
409
**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET
S
shawn_he 已提交
410 411 412 413

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

**Parameters**
S
shawn_he 已提交
414

S
shawn_he 已提交
415 416 417 418
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).|

S
shawn_he 已提交
419 420
**Return value**

S
shawn_he 已提交
421 422
| Type| Description|
| -------- | -------- |
S
shawn_he 已提交
423
| Promise&lt;void&gt; | Promise that returns no value.|
S
shawn_he 已提交
424 425 426

**Example**

S
shawn_he 已提交
427
```js
S
shawn_he 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440
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 已提交
441
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
442 443 444 445 446 447 448

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

**Parameters**

| Name  | Type                                             | Mandatory| Description              |
| -------- | ------------------------------------------------- | ---- | ------------------ |
S
shawn_he 已提交
449
| host     | string                                            | Yes  | Host name to be resolved.|
S
shawn_he 已提交
450 451 452 453
| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | Yes  | Callback used to return the result.        |

**Example**

S
shawn_he 已提交
454
```js
S
shawn_he 已提交
455 456 457 458 459 460 461 462 463 464 465 466 467
let host = "xxxx";
connection.getAddressesByName(host, function (error, addresses) {
    console.log(JSON.stringify(error))
    console.log(JSON.stringify(addresses))
})
```

## connection.getAddressesByName

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

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 已提交
468
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
469 470 471 472 473 474 475

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

**Parameters**

| Name| Type  | Mandatory| Description              |
| ------ | ------ | ---- | ------------------ |
S
shawn_he 已提交
476
| host   | string | Yes  | Host name to be resolved.|
S
shawn_he 已提交
477

S
shawn_he 已提交
478
**Return value**
S
shawn_he 已提交
479 480 481

| Type                                       | Description                         |
| ------------------------------------------- | ----------------------------- |
S
shawn_he 已提交
482
| Promise\<Array\<[NetAddress](#netaddress)>> | Promise used to return the result.|
S
shawn_he 已提交
483 484 485

**Example**

S
shawn_he 已提交
486
```js
S
shawn_he 已提交
487 488 489 490 491 492
let host = "xxxx";
connection.getAddressesByName(host).then(function (addresses) {
    console.log(JSON.stringify(addresses))
})
```

S
shawn_he 已提交
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531

## connection.enableAirplaneMode

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

Enables the airplane mode. This API uses an asynchronous callback to return the result.

This is a system API.

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

**Parameters**

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

**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.

This is a system API.

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

**Return value**

| Type                                       | Description                         |
| ------------------------------------------- | ----------------------------- |
S
shawn_he 已提交
532
| Promise\<void> | Promise that returns no value.|
S
shawn_he 已提交
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580

**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.

This is a system API.

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

**Parameters**

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

**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.

This is a system API.

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

**Return value**

| Type                                       | Description                         |
| ------------------------------------------- | ----------------------------- |
S
shawn_he 已提交
581
| Promise\<void> | Promise that returns no value.|
S
shawn_he 已提交
582 583 584 585 586 587 588 589 590 591

**Example**

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


S
shawn_he 已提交
592 593 594 595
## connection.createNetConnection

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

S
shawn_he 已提交
596
Obtains the handle of the network specified by **netSpecifier**.
S
shawn_he 已提交
597 598 599 600 601 602 603 604 605 606

**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.|

S
shawn_he 已提交
607
**Return value**
S
shawn_he 已提交
608 609 610 611 612 613 614

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

**Example**

S
shawn_he 已提交
615
```js
S
shawn_he 已提交
616 617 618 619 620 621
// Default network
let netConnection = connection.createNetConnection()

// Cellular network
let netConnectionCellular = connection.createNetConnection({
    netCapabilities: {
S
shawn_he 已提交
622
        bearerTypes: [connection.NetBearType.BEARER_CELLULAR]
S
shawn_he 已提交
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
    }
})
```

## NetConnection

Represents the network connection handle.

### on('netAvailable')

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

Registers a listener for **netAvailable** events.

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

**Parameters**

| Name  | Type                              | Mandatory| Description                                                        |
| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
643
| type     | string                             | Yes  | Event type. The value is fixed at **netAvailable**.<br>**netAvailable**: event indicating that the data network is available.|
S
shawn_he 已提交
644 645 646 647
| callback | Callback\<[NetHandle](#nethandle)> | Yes  | Callback used to return the result.                                                  |

**Example**

S
shawn_he 已提交
648
```js
S
shawn_he 已提交
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
netConnection.on('netAvailable', function (data) {
    console.log(JSON.stringify(data))
})
```

### on('netCapabilitiesChange')

on(type: 'netCapabilitiesChange', callback: Callback<{ netHandle: NetHandle, netCap: NetCapabilities }>): void

Registers a listener for **netCapabilitiesChange** events.

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

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
666
| type     | string                                                       | Yes  | Event type. The value is fixed at **netCapabilitiesChange**.<br>**netCapabilitiesChange**: event indicating that network capabilities have changed.|
S
shawn_he 已提交
667 668 669 670
| callback | Callback<{ netHandle: [NetHandle](#nethandle), netCap: [NetCapabilities](#netcapabilities) }> | Yes  | Callback used to return the result.                                                  |

**Example**

S
shawn_he 已提交
671
```js
S
shawn_he 已提交
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
netConnection.on('netCapabilitiesChange', function (data) {
    console.log(JSON.stringify(data))
})
```

### on('netConnectionPropertiesChange')

on(type: 'netConnectionPropertiesChange', callback: Callback<{ netHandle: NetHandle, connectionProperties: ConnectionProperties }>): void

Registers a listener for **netConnectionPropertiesChange** events.

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

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
689
| type     | string                                                       | Yes  | Event type. The value is fixed at **netConnectionPropertiesChange**.<br>**netConnectionPropertiesChange**: event indicating that network connection properties have changed.|
S
shawn_he 已提交
690 691 692 693
| callback | Callback<{ netHandle: [NetHandle](#nethandle), connectionProperties: [ConnectionProperties](#connectionproperties) }> | Yes  | Callback used to return the result.                                                  |

**Example**

S
shawn_he 已提交
694
```js
S
shawn_he 已提交
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
netConnection.on('netConnectionPropertiesChange', function (data) {
    console.log(JSON.stringify(data))
})
```

### on('netBlockStatusChange')

on(type: 'netBlockStatusChange', callback: Callback&lt;{ netHandle: NetHandle, blocked: boolean }&gt;): void

Registers a listener for **netBlockStatusChange** events.

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

**Parameters**

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

**Example**

S
shawn_he 已提交
717
```js
S
shawn_he 已提交
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
netConnection.on('netBlockStatusChange', function (data) {
    console.log(JSON.stringify(data))
})
```

### on('netLost')

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

Registers a listener for **netLost** events.

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

**Parameters**

| Name  | Type                              | Mandatory| Description                                                        |
| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
735
| type     | string                             | Yes  | Event type. The value is fixed at **netLost**.<br>netLost: event indicating that the network is interrupted or normally disconnected.|
S
shawn_he 已提交
736 737 738 739
| callback | Callback\<[NetHandle](#nethandle)> | Yes  | Callback used to return the result.                                                  |

**Example**

S
shawn_he 已提交
740
```js
S
shawn_he 已提交
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
let netConnection1 = connection.createNetConnection()
netConnection1.on('netLost', function (data) {
    console.log(JSON.stringify(data))
})
```

### on('netUnavailable')

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

Registers a listener for **netUnavailable** events.

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

**Parameters**

| Name  | Type           | Mandatory| Description                                                        |
| -------- | --------------- | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
759
| type     | string          | Yes  | Event type. The value is fixed at **netUnavailable**.<br>**netUnavailable**: event indicating that the network is unavailable.|
S
shawn_he 已提交
760 761 762 763
| callback | Callback\<void> | Yes  | Callback used to return the result.                                                  |

**Example**

S
shawn_he 已提交
764
```js
S
shawn_he 已提交
765 766 767 768 769 770 771 772 773 774 775
netConnection.on('netUnavailable', function (data) {
    console.log(JSON.stringify(data))
})
```

### register

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

Registers a listener for network status changes.

S
shawn_he 已提交
776
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
777 778 779 780 781 782 783 784 785 786 787

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

**Parameters**

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

**Example**

S
shawn_he 已提交
788
```js
S
shawn_he 已提交
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
netConnection.register(function (error) {
    console.log(JSON.stringify(error))
})
```

### unregister

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

Unregisters the listener for network status changes.

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

**Parameters**

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

**Example**

S
shawn_he 已提交
810
```js
S
shawn_he 已提交
811 812 813 814 815 816 817 818 819
netConnection.unregister(function (error) {
    console.log(JSON.stringify(error))
})
```

## NetHandle

Defines the handle of the data network.

S
shawn_he 已提交
820
Before invoking NetHandle APIs, call **getNetHandle** to obtain a **NetHandle** object.
S
shawn_he 已提交
821 822 823

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

S
shawn_he 已提交
824
### Parameters
S
shawn_he 已提交
825 826 827

| Name| Type  | Description                     |
| ------ | ------ | ------------------------- |
S
shawn_he 已提交
828
| netId  | number | Network ID. The value must be greater than or equal to 100.|
S
shawn_he 已提交
829

S
shawn_he 已提交
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
### bindSocket

bindSocket(socketParam: TCPSocket \| UDPSocket, callback: AsyncCallback\<void>): void;

Binds a **TCPSocket** or **UDPSocket** object to the data network. 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                           |
| ----------- | ------------------------ | ---- | -------------------------------|
| socketParam | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | Yes| **TCPSocket** or **UDPSocket** object.|
| callback    | AsyncCallback\<void>      | Yes  | Callback used to return the result.                       |

**Example**

```js
connection.getDefaultNet().then(function (netHandle) {
    var tcp = socket.constructTCPSocketInstance();
    var udp = socket.constructUDPSocketInstance();
    let socketType = "xxxx";
    if (socketType == "TCPSocket") {
        tcp.bind({
            address: "xxxx", port: xxxx, family: xxxx
        }, err => {
            netHandle.bindSocket(tcp, function (error, data) {
            console.log(JSON.stringify(error))
            console.log(JSON.stringify(data))
        })
    } else {
        udp.on('message', callback);
        udp.bind({
            address: "xxxx", port: xxxx, family: xxxx
        }, err => {
            udp.on('message', (data) => {
            console.log(JSON.stringify(data))
            });
            netHandle.bindSocket(udp, function (error, data) {
            console.log(JSON.stringify(error))
            console.log(JSON.stringify(data))
            });
        })
     }
}
```

### bindSocket

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.

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

**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 已提交
899
| Promise\<void> | Promise that returns no value.|
S
shawn_he 已提交
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 931

**Example**

```js
connection.getDefaultNet().then(function (netHandle) {
    var tcp = socket.constructTCPSocketInstance();
    var udp = socket.constructUDPSocketInstance();
    let socketType = "xxxx";
    if(socketType == "TCPSocket") {
        tcp.bind({
            address: "xxxx", port: xxxx, family: xxxx
        }, err => {
            netHandle.bindSocket(tcp).then(err, data) {
            console.log(JSON.stringify(data))
        })
    } else {
        udp.on('message', callback);
        udp.bind({
            address: "xxxx", port: xxxx, family: xxxx
        }, err => {
            udp.on('message', (data) => {
            console.log(JSON.stringify(data))
            });
            netHandle.bindSocket(tcp).then(err, data) {
            console.log(JSON.stringify(data))
            });
        })
     }
}
```


S
shawn_he 已提交
932 933 934 935 936 937
### 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 已提交
938 939
**Required permission**: ohos.permission.GET_NETWORK_INFO

S
shawn_he 已提交
940 941 942 943 944 945
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                                             | Mandatory| Description              |
| -------- | ------------------------------------------------- | ---- | ------------------ |
S
shawn_he 已提交
946
| host     | string                                            | Yes  | Host name to be resolved.|
S
shawn_he 已提交
947 948 949 950
| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | Yes  | Callback used to return the result.          |

**Example**

S
shawn_he 已提交
951
```js
S
shawn_he 已提交
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
connection.getDefaultNet().then(function (netHandle) {
    let host = "xxxx";
    netHandle.getAddressesByName(host, function (error, addresses) {
        console.log(JSON.stringify(error))
        console.log(JSON.stringify(addresses))
    })
})
```

### 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 已提交
967 968
**Required permission**: ohos.permission.GET_NETWORK_INFO

S
shawn_he 已提交
969 970 971 972 973 974
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name| Type  | Mandatory| Description              |
| ------ | ------ | ---- | ------------------ |
S
shawn_he 已提交
975
| host   | string | Yes  | Host name to be resolved.|
S
shawn_he 已提交
976

S
shawn_he 已提交
977
**Return value**
S
shawn_he 已提交
978 979 980 981 982 983 984

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

**Example**

S
shawn_he 已提交
985
```js
S
shawn_he 已提交
986 987 988 989 990 991 992 993 994 995 996 997 998 999
connection.getDefaultNet().then(function (netHandle) {
    let host = "xxxx";
    netHandle.getAddressesByName(host).then(function (addresses) {
        console.log(JSON.stringify(addresses))
    })
})
```

### 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 已提交
1000 1001
**Required permission**: ohos.permission.GET_NETWORK_INFO

S
shawn_he 已提交
1002 1003 1004 1005 1006 1007
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                                     | Mandatory| Description              |
| -------- | ----------------------------------------- | ---- | ------------------ |
S
shawn_he 已提交
1008
| host     | string                                    | Yes  | Host name to be resolved.|
S
shawn_he 已提交
1009 1010 1011 1012
| callback | AsyncCallback\<[NetAddress](#netaddress)> | Yes  | Callback used to return the result.        |

**Example**

S
shawn_he 已提交
1013
```js
S
shawn_he 已提交
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
connection.getDefaultNet().then(function (netHandle) {
    let host = "xxxx";
    netHandle.getAddressByName(host, function (error, address) {
        console.log(JSON.stringify(error))
        console.log(JSON.stringify(address))
    })
})
```

### 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 已提交
1029 1030
**Required permission**: ohos.permission.GET_NETWORK_INFO

S
shawn_he 已提交
1031 1032 1033 1034 1035 1036
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name| Type  | Mandatory| Description              |
| ------ | ------ | ---- | ------------------ |
S
shawn_he 已提交
1037
| host   | string | Yes  | Host name to be resolved.|
S
shawn_he 已提交
1038

S
shawn_he 已提交
1039
**Return value**
S
shawn_he 已提交
1040 1041 1042 1043 1044 1045 1046

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

**Example**

S
shawn_he 已提交
1047
```js
S
shawn_he 已提交
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
connection.getDefaultNet().then(function (netHandle) {
    let host = "xxxx";
    netHandle.getAddressByName(host).then(function (address) {
        console.log(JSON.stringify(address))
    })
})
```

## NetSpecifier

Provides an instance that bears data network capabilities.

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

| Name                 | Type                               | Description                                                        |
| ----------------------- | ----------------------------------- | ------------------------------------------------------------ |
| netCapabilities         | [NetCapabilities](#netcapabilities) | Network transmission capabilities and bearer types of the data network.                          |
| bearerPrivateIdentifier | string                              | 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                              | Description                    |
| --------------------- | ---------------------------------- | ------------------------ |
| linkUpBandwidthKbps   | number                             | Uplink (from the device to the network) bandwidth.|
| linkDownBandwidthKbps | number                             | Downlink (from the network to the device) bandwidth.|
| networkCap            | Array<[NetCap](#netcap)>           | Network capability.          |
S
shawn_he 已提交
1078
| bearerTypes           | Array<[NetBearType](#netbeartype)> | Network type.              |
S
shawn_he 已提交
1079 1080 1081 1082 1083 1084 1085

## NetCap

Defines the network capability.

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

S
shawn_he 已提交
1086
| Name                 | Value  | Description                  |
S
shawn_he 已提交
1087 1088 1089
| ------------------------ | ---- | ---------------------- |
| 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.|
S
shawn_he 已提交
1090
| NET_CAPABILITY_INTERNET  | 12   | The network can connect to the Internet.|
S
shawn_he 已提交
1091
| NET_CAPABILITY_NOT_VPN | 15 | The network does not use a Virtual Private Network (VPN).|
S
shawn_he 已提交
1092
| NET_CAPABILITY_VALIDATED | 16   | The network is available.            |
S
shawn_he 已提交
1093 1094 1095 1096 1097 1098 1099

## NetBearType

Defines the network type.

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

S
shawn_he 已提交
1100
| Name        | Value  | Description       |
S
shawn_he 已提交
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
| --------------- | ---- | ----------- |
| BEARER_CELLULAR | 0    | Cellular network |
| BEARER_WIFI     | 1    | Wi-Fi network|
| BEARER_ETHERNET | 3 | Ethernet network|

## ConnectionProperties

Defines the network connection properties.

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

| Name      | Type                              | Description            |
| ------------- | ---------------------------------- | ---------------- |
| interfaceName | string                             | NIC card name.      |
| domains       | string                             | Domain. The default value is **""**.|
| linkAddresses | Array<[LinkAddress](#linkaddress)> | Link information.      |
| routes        | Array<[RouteInfo](#routeinfo)>     | Route information.      |
| dnses | Array&lt;[NetAddress](#netaddress)&gt; | Network address. For details, see [NetAddress](#netaddress).|
| mtu           | number                             | Maximum transmission unit (MTU).  |

## LinkAddress

S
shawn_he 已提交
1123
Network link information.
S
shawn_he 已提交
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133

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

| Name      | Type                     | Description                |
| ------------ | ------------------------- | -------------------- |
| address      | [NetAddress](#netaddress) | Link address.          |
| prefixLength | number                    | Length of the link address prefix.|

## RouteInfo

S
shawn_he 已提交
1134
Network route information.
S
shawn_he 已提交
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155

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

| Name        | Type                       | Description            |
| -------------- | --------------------------- | ---------------- |
| interface      | string                      | NIC card name.      |
| destination    | [LinkAddress](#linkaddress) | Destination IP address.      |
| gateway        | [NetAddress](#netaddress)   | Gateway address.      |
| hasGateway     | boolean                     | Whether a gateway is present.    |
| isDefaultRoute | boolean                     | Whether the route is the default route.|

## NetAddress

Defines the network address.

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

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