js-apis-net-connection.md 34.5 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 19 20
import connection from '@ohos.net.connection'
```

## connection.getDefaultNet

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

Obtains the default active data network. This API uses an asynchronous callback to return the result.

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 44 45
connection.getDefaultNet(function (error, netHandle) {
    console.log(JSON.stringify(error))
    console.log(JSON.stringify(netHandle))
})
```

## connection.getDefaultNet

getDefaultNet(): Promise\<NetHandle>

Obtains the default active data network. This API uses a promise to return the result.

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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
connection.getDefaultNet().then(function (netHandle) {
    console.log(JSON.stringify(netHandle))
})
```

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

**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 已提交
80
```js
S
shawn_he 已提交
81 82
connection.hasDefaultNet(function (error, has) {
    console.log(JSON.stringify(error))
S
shawn_he 已提交
83
    console.log('has: ' + has)
S
shawn_he 已提交
84 85 86 87 88 89 90 91 92 93 94
})
```

## connection.hasDefaultNet

hasDefaultNet(): Promise\<boolean>

Checks whether the default data network is activated. This API uses a promise to return the result.

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

S
shawn_he 已提交
95
**Return Value**
S
shawn_he 已提交
96 97 98 99 100 101 102

| 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 已提交
103
```js
S
shawn_he 已提交
104
connection.hasDefaultNet().then(function (has) {
S
shawn_he 已提交
105
    console.log('has: ' + has)
S
shawn_he 已提交
106 107 108 109 110 111 112 113 114
})
```

## connection.getAllNets

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

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

S
shawn_he 已提交
115
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
116 117 118 119 120 121 122 123 124 125

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

**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;Array&lt;[NetHandle](#nethandle)&gt;&gt; | Yes| Callback used to return the result.|

**Example**

S
shawn_he 已提交
126
```js
S
shawn_he 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139
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;

Obtains the list of all active data networks. This API uses a promise to return the result.

S
shawn_he 已提交
140
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
141 142 143

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

S
shawn_he 已提交
144
**Return Value**
S
shawn_he 已提交
145 146 147 148 149 150
| Type| Description|
| -------- | -------- |
| Promise&lt;Array&lt;[NetHandle](#nethandle)&gt;&gt; | Promise used to return the result.|

**Example**

S
shawn_he 已提交
151
```js
S
shawn_he 已提交
152 153 154 155 156 157 158 159 160
connection.getAllNets().then(function (nets) {
    console.log(JSON.stringify(nets))
});
```

## connection.getConnectionProperties

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

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

S
shawn_he 已提交
163
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
164 165 166 167 168 169 170

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

**Parameters**

| Name   | Type                                                        | Mandatory| Description            |
| --------- | ------------------------------------------------------------ | ---- | ---------------- |
S
shawn_he 已提交
171
| netHandle | [NetHandle](#nethandle)                                      | Yes  | Handle of the data network.|
S
shawn_he 已提交
172 173 174 175
| callback  | AsyncCallback\<[ConnectionProperties](#connectionproperties)> | Yes  | Callback used to return the result.      |

**Example**

S
shawn_he 已提交
176
```js
S
shawn_he 已提交
177 178 179 180 181 182 183 184 185 186 187 188
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 已提交
189
Obtains connection properties of the network corresponding to **netHandle**. This API uses a promise to return the result.
S
shawn_he 已提交
190

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

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

**Parameters**

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

S
shawn_he 已提交
201
**Return Value**
S
shawn_he 已提交
202 203 204 205 206 207 208

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

**Example**

S
shawn_he 已提交
209
```js
S
shawn_he 已提交
210 211 212 213 214 215 216 217 218 219 220
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 已提交
221
Obtains capability information of the network corresponding to **netHandle**. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
222

S
shawn_he 已提交
223
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
224 225 226 227 228 229 230

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

**Parameters**

| Name   | Type                                               | Mandatory| Description            |
| --------- | --------------------------------------------------- | ---- | ---------------- |
S
shawn_he 已提交
231
| netHandle | [NetHandle](#nethandle)                             | Yes  | Handle of the data network.|
S
shawn_he 已提交
232 233 234 235
| callback  | AsyncCallback\<[NetCapabilities](#netcapabilities)> | Yes  | Callback used to return the result.      |

**Example**

S
shawn_he 已提交
236
```js
S
shawn_he 已提交
237 238 239 240 241 242 243 244 245 246 247 248
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 已提交
249
Obtains capability information of the network corresponding to **netHandle**. This API uses a promise to return the result.
S
shawn_he 已提交
250

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

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

**Parameters**

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

S
shawn_he 已提交
261
**Return Value**
S
shawn_he 已提交
262 263 264 265 266 267 268

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

**Example**

S
shawn_he 已提交
269
```js
S
shawn_he 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282
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

Reports connection of the data network. This API uses an asynchronous callback to return the result.

S
shawn_he 已提交
283
**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET
S
shawn_he 已提交
284 285 286 287 288 289 290 291 292 293 294

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

**Parameters**
| 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 已提交
295
```js
S
shawn_he 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308 309
connection.getDefaultNet().then(function (netHandle) {
    connection.reportNetConnected(netHandle, function (error) {
        console.log(JSON.stringify(error))
    });
});
```


## connection.reportNetConnected

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

Reports connection of the data network. This API uses a promise to return the result.

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

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

**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).|

S
shawn_he 已提交
319
**Return Value**
S
shawn_he 已提交
320 321 322 323 324 325
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|

**Example**

S
shawn_he 已提交
326
```js
S
shawn_he 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339 340
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

Reports disconnection of the data network. This API uses an asynchronous callback to return the result.

S
shawn_he 已提交
341
**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET
S
shawn_he 已提交
342 343 344 345 346 347 348 349 350 351 352

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

**Parameters**
| 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 已提交
353
```js
S
shawn_he 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366 367
connection.getDefaultNet().then(function (netHandle) {
    connection.reportNetDisconnected(netHandle, function (error) {
        console.log(JSON.stringify(error))
    });
});
```


## connection.reportNetDisconnected

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

Reports disconnection of the data network. This API uses a promise to return the result.

S
shawn_he 已提交
368
**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET
S
shawn_he 已提交
369 370 371 372 373 374 375 376

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

**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).|

S
shawn_he 已提交
377
**Return Value**
S
shawn_he 已提交
378 379 380 381 382 383
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|

**Example**

S
shawn_he 已提交
384
```js
S
shawn_he 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397
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 已提交
398
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
399 400 401 402 403 404 405

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

**Parameters**

| Name  | Type                                             | Mandatory| Description              |
| -------- | ------------------------------------------------- | ---- | ------------------ |
S
shawn_he 已提交
406
| host     | string                                            | Yes  | Host name to be resolved.|
S
shawn_he 已提交
407 408 409 410
| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | Yes  | Callback used to return the result.        |

**Example**

S
shawn_he 已提交
411
```js
S
shawn_he 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424
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 已提交
425
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
426 427 428 429 430 431 432

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

**Parameters**

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

S
shawn_he 已提交
435
**Return Value**
S
shawn_he 已提交
436 437 438

| Type                                       | Description                         |
| ------------------------------------------- | ----------------------------- |
S
shawn_he 已提交
439
| Promise\<Array\<[NetAddress](#netaddress)>> | Promise used to return the result.|
S
shawn_he 已提交
440 441 442

**Example**

S
shawn_he 已提交
443
```js
S
shawn_he 已提交
444 445 446 447 448 449
let host = "xxxx";
connection.getAddressesByName(host).then(function (addresses) {
    console.log(JSON.stringify(addresses))
})
```

S
shawn_he 已提交
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 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 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548

## 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                         |
| ------------------------------------------- | ----------------------------- |
| Promise\<void> | Promise used to return the result.|

**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                         |
| ------------------------------------------- | ----------------------------- |
| Promise\<void> | Promise used to return the result.|

**Example**

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


S
shawn_he 已提交
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
## connection.createNetConnection

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

Obtains the handle of the network specified by **netSpecifier**.

**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 已提交
564
**Return Value**
S
shawn_he 已提交
565 566 567 568 569 570 571

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

**Example**

S
shawn_he 已提交
572
```js
S
shawn_he 已提交
573 574 575 576 577 578
// Default network
let netConnection = connection.createNetConnection()

// Cellular network
let netConnectionCellular = connection.createNetConnection({
    netCapabilities: {
S
shawn_he 已提交
579
        bearerTypes: [connection.NetBearType.BEARER_CELLULAR]
S
shawn_he 已提交
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
    }
})
```

## 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 已提交
600
| type     | string                             | Yes  | Event type. The value is fixed at **netAvailable**.<br>**netAvailable**: event indicating that the data network is available.|
S
shawn_he 已提交
601 602 603 604
| callback | Callback\<[NetHandle](#nethandle)> | Yes  | Callback used to return the result.                                                  |

**Example**

S
shawn_he 已提交
605
```js
S
shawn_he 已提交
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
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 已提交
623
| type     | string                                                       | Yes  | Event type. The value is fixed at **netCapabilitiesChange**.<br>**netCapabilitiesChange**: event indicating that network capabilities have changed.|
S
shawn_he 已提交
624 625 626 627
| callback | Callback<{ netHandle: [NetHandle](#nethandle), netCap: [NetCapabilities](#netcapabilities) }> | Yes  | Callback used to return the result.                                                  |

**Example**

S
shawn_he 已提交
628
```js
S
shawn_he 已提交
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
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 已提交
646
| type     | string                                                       | Yes  | Event type. The value is fixed at **netConnectionPropertiesChange**.<br>**netConnectionPropertiesChange**: event indicating that network connection properties have changed.|
S
shawn_he 已提交
647 648 649 650
| callback | Callback<{ netHandle: [NetHandle](#nethandle), connectionProperties: [ConnectionProperties](#connectionproperties) }> | Yes  | Callback used to return the result.                                                  |

**Example**

S
shawn_he 已提交
651
```js
S
shawn_he 已提交
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
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 已提交
669
| 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 已提交
670 671 672 673
| 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 已提交
674
```js
S
shawn_he 已提交
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
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 已提交
692
| 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 已提交
693 694 695 696
| callback | Callback\<[NetHandle](#nethandle)> | Yes  | Callback used to return the result.                                                  |

**Example**

S
shawn_he 已提交
697
```js
S
shawn_he 已提交
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
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 已提交
716
| type     | string          | Yes  | Event type. The value is fixed at **netUnavailable**.<br>**netUnavailable**: event indicating that the network is unavailable.|
S
shawn_he 已提交
717 718 719 720
| callback | Callback\<void> | Yes  | Callback used to return the result.                                                  |

**Example**

S
shawn_he 已提交
721
```js
S
shawn_he 已提交
722 723 724 725 726 727 728 729 730 731 732
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 已提交
733
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
734 735 736 737 738 739 740 741 742 743 744

**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 已提交
745
```js
S
shawn_he 已提交
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
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 已提交
767
```js
S
shawn_he 已提交
768 769 770 771 772 773 774 775 776
netConnection.unregister(function (error) {
    console.log(JSON.stringify(error))
})
```

## NetHandle

Defines the handle of the data network.

S
shawn_he 已提交
777
Before invoking NetHandle APIs, call **getNetHandle** to obtain a **NetHandle** object.
S
shawn_he 已提交
778 779 780

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

S
shawn_he 已提交
781
### Parameters
S
shawn_he 已提交
782 783 784

| Name| Type  | Description                     |
| ------ | ------ | ------------------------- |
S
shawn_he 已提交
785
| netId  | number | Network ID. The value must be greater than or equal to 100.|
S
shawn_he 已提交
786 787 788 789 790 791 792

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

S
shawn_he 已提交
795 796 797 798 799 800
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                                             | Mandatory| Description              |
| -------- | ------------------------------------------------- | ---- | ------------------ |
S
shawn_he 已提交
801
| host     | string                                            | Yes  | Host name to be resolved.|
S
shawn_he 已提交
802 803 804 805
| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | Yes  | Callback used to return the result.          |

**Example**

S
shawn_he 已提交
806
```js
S
shawn_he 已提交
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
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 已提交
822 823
**Required permission**: ohos.permission.GET_NETWORK_INFO

S
shawn_he 已提交
824 825 826 827 828 829
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

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

S
shawn_he 已提交
832
**Return Value**
S
shawn_he 已提交
833 834 835 836 837 838 839

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

**Example**

S
shawn_he 已提交
840
```js
S
shawn_he 已提交
841 842 843 844 845 846 847 848 849 850 851 852 853 854
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 已提交
855 856
**Required permission**: ohos.permission.GET_NETWORK_INFO

S
shawn_he 已提交
857 858 859 860 861 862
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                                     | Mandatory| Description              |
| -------- | ----------------------------------------- | ---- | ------------------ |
S
shawn_he 已提交
863
| host     | string                                    | Yes  | Host name to be resolved.|
S
shawn_he 已提交
864 865 866 867
| callback | AsyncCallback\<[NetAddress](#netaddress)> | Yes  | Callback used to return the result.        |

**Example**

S
shawn_he 已提交
868
```js
S
shawn_he 已提交
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
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 已提交
884 885
**Required permission**: ohos.permission.GET_NETWORK_INFO

S
shawn_he 已提交
886 887 888 889 890 891
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

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

S
shawn_he 已提交
894
**Return Value**
S
shawn_he 已提交
895 896 897 898 899 900 901

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

**Example**

S
shawn_he 已提交
902
```js
S
shawn_he 已提交
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 932
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 已提交
933
| bearerTypes           | Array<[NetBearType](#netbeartype)> | Network type.              |
S
shawn_he 已提交
934 935 936 937 938 939 940

## NetCap

Defines the network capability.

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

S
shawn_he 已提交
941
| Name                 | Value  | Description                  |
S
shawn_he 已提交
942 943 944 945 946 947 948 949 950 951 952 953 954
| ------------------------ | ---- | ---------------------- |
| 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 can connect to the Internet.|
| NET_CAPABILITY_NOT_VPN | 15 | The network does not use a Virtual Private Network (VPN).|
| NET_CAPABILITY_VALIDATED | 16   | The network is available.            |

## NetBearType

Defines the network type.

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

S
shawn_he 已提交
955
| Name        | Value  | Description       |
S
shawn_he 已提交
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
| --------------- | ---- | ----------- |
| 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 已提交
978
Network link information.
S
shawn_he 已提交
979 980 981 982 983 984 985 986 987 988

**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 已提交
989
Network route information.
S
shawn_he 已提交
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010

**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 已提交
1011
| port    | number | Port number. The value ranges from **0** to **65535**.   |