js-apis-net-connection.md 42.5 KB
Newer Older
S
shawn_he 已提交
1
# # @ohos.net.connection
S
shawn_he 已提交
2

S
shawn_he 已提交
3
The **connection** 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**<br>
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 13 14 15 16 17
import connection from '@ohos.net.connection'
```

## connection.getDefaultNet

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

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

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

**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 已提交
32
```js
S
shawn_he 已提交
33 34 35 36 37 38 39 40 41 42
connection.getDefaultNet(function (error, netHandle) {
    console.log(JSON.stringify(error))
    console.log(JSON.stringify(netHandle))
})
```

## connection.getDefaultNet

getDefaultNet(): Promise\<NetHandle>

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

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

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

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

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

**Example**

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

S
shawn_he 已提交
63
## connection.getDefaultNetSync<sup>9+</sup>
S
shawn_he 已提交
64 65 66

getDefaultNetSync(): NetHandle;

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

**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 已提交
86 87 88 89
## connection.hasDefaultNet

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

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

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

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

## connection.hasDefaultNet

hasDefaultNet(): Promise\<boolean>

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

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

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

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

| 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 已提交
129
```js
S
shawn_he 已提交
130
connection.hasDefaultNet().then(function (has) {
S
shawn_he 已提交
131
    console.log('has: ' + has)
S
shawn_he 已提交
132 133 134 135 136 137 138
})
```

## connection.getAllNets

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

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

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

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

**Parameters**
S
shawn_he 已提交
146

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

**Example**

S
shawn_he 已提交
153
```js
S
shawn_he 已提交
154 155 156 157 158 159 160 161 162 163 164
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 已提交
165
Obtains the list of all active data networks. This API uses a promise to return the result.
S
shawn_he 已提交
166

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

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

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

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

**Example**

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

## connection.getConnectionProperties

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

S
shawn_he 已提交
189
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 已提交
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 201 202 203
| callback  | AsyncCallback\<[ConnectionProperties](#connectionproperties)> | Yes  | Callback used to return the result.      |

**Example**

S
shawn_he 已提交
204
```js
S
shawn_he 已提交
205 206 207 208 209 210 211 212 213 214 215 216
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 已提交
217
Obtains connection properties of the network corresponding to **netHandle**. This API uses a promise to return the result.
S
shawn_he 已提交
218

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

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

**Parameters**

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

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

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

**Example**

S
shawn_he 已提交
237
```js
S
shawn_he 已提交
238 239 240 241 242 243 244 245 246 247 248
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 已提交
249
Obtains capability information of the network corresponding to **netHandle**. This API uses an asynchronous callback 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 261 262 263
| callback  | AsyncCallback\<[NetCapabilities](#netcapabilities)> | Yes  | Callback used to return the result.      |

**Example**

S
shawn_he 已提交
264
```js
S
shawn_he 已提交
265 266 267 268 269 270 271 272 273 274 275 276
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 已提交
277
Obtains capability information of the network corresponding to **netHandle**. This API uses a promise to return the result.
S
shawn_he 已提交
278

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

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

**Parameters**

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

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

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

**Example**

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

S
shawn_he 已提交
305 306 307 308 309 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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
## 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.|

**Example**:

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

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

**Example**:

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

S
shawn_he 已提交
354 355 356 357
## connection.reportNetConnected

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

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

S
shawn_he 已提交
360 361
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 已提交
362
**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET
S
shawn_he 已提交
363 364 365 366

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

**Parameters**
S
shawn_he 已提交
367

S
shawn_he 已提交
368 369 370 371 372 373 374
| 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 已提交
375
```js
S
shawn_he 已提交
376 377 378 379 380 381 382 383 384 385 386
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 已提交
387
Reports connection of the data network. This API uses a promise to return the result.
S
shawn_he 已提交
388

S
shawn_he 已提交
389 390
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 已提交
391
**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET
S
shawn_he 已提交
392 393 394 395

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

**Parameters**
S
shawn_he 已提交
396

S
shawn_he 已提交
397 398 399 400
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).|

S
shawn_he 已提交
401 402
**Return value**

S
shawn_he 已提交
403 404
| Type| Description|
| -------- | -------- |
S
shawn_he 已提交
405
| Promise&lt;void&gt; | Promise that returns no value.|
S
shawn_he 已提交
406 407 408

**Example**

S
shawn_he 已提交
409
```js
S
shawn_he 已提交
410 411 412 413 414 415 416 417 418 419 420 421
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 已提交
422
Reports disconnection of the data network. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
423

S
shawn_he 已提交
424 425
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 已提交
426
**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET
S
shawn_he 已提交
427 428 429 430

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

**Parameters**
S
shawn_he 已提交
431

S
shawn_he 已提交
432 433 434 435 436 437 438
| 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 已提交
439
```js
S
shawn_he 已提交
440 441 442 443 444 445 446 447 448 449 450 451
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 已提交
452
Reports disconnection of the data network. This API uses a promise to return the result.
S
shawn_he 已提交
453

S
shawn_he 已提交
454 455
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 已提交
456
**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET
S
shawn_he 已提交
457 458 459 460

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

**Parameters**
S
shawn_he 已提交
461

S
shawn_he 已提交
462 463 464 465
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).|

S
shawn_he 已提交
466 467
**Return value**

S
shawn_he 已提交
468 469
| Type| Description|
| -------- | -------- |
S
shawn_he 已提交
470
| Promise&lt;void&gt; | Promise that returns no value.|
S
shawn_he 已提交
471 472 473

**Example**

S
shawn_he 已提交
474
```js
S
shawn_he 已提交
475 476 477 478 479 480 481 482 483 484 485 486 487
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 已提交
488
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
489 490 491 492 493 494 495

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

**Parameters**

| Name  | Type                                             | Mandatory| Description              |
| -------- | ------------------------------------------------- | ---- | ------------------ |
S
shawn_he 已提交
496
| host     | string                                            | Yes  | Host name to be resolved.|
S
shawn_he 已提交
497 498 499 500
| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | Yes  | Callback used to return the result.        |

**Example**

S
shawn_he 已提交
501
```js
S
shawn_he 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514
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 已提交
515
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
516 517 518 519 520 521 522

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

**Parameters**

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

S
shawn_he 已提交
525
**Return value**
S
shawn_he 已提交
526 527 528

| Type                                       | Description                         |
| ------------------------------------------- | ----------------------------- |
S
shawn_he 已提交
529
| Promise\<Array\<[NetAddress](#netaddress)>> | Promise used to return the result.|
S
shawn_he 已提交
530 531 532

**Example**

S
shawn_he 已提交
533
```js
S
shawn_he 已提交
534 535 536 537 538 539
let host = "xxxx";
connection.getAddressesByName(host).then(function (addresses) {
    console.log(JSON.stringify(addresses))
})
```

S
shawn_he 已提交
540 541 542 543 544 545
## connection.enableAirplaneMode

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

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

S
shawn_he 已提交
546
**System API**: This is a system API.
S
shawn_he 已提交
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569

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

S
shawn_he 已提交
570
**System API**: This is a system API.
S
shawn_he 已提交
571 572 573 574 575 576 577

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

**Return value**

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

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

S
shawn_he 已提交
594
**System API**: This is a system API.
S
shawn_he 已提交
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617

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

S
shawn_he 已提交
618
**System API**: This is a system API.
S
shawn_he 已提交
619 620 621 622 623 624 625

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

**Return value**

| Type                                       | Description                         |
| ------------------------------------------- | ----------------------------- |
S
shawn_he 已提交
626
| Promise\<void> | Promise that returns no value.|
S
shawn_he 已提交
627 628 629 630 631 632 633 634 635

**Example**

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

S
shawn_he 已提交
636 637 638 639
## connection.createNetConnection

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

S
shawn_he 已提交
640
Obtains the handle of the network specified by **netSpecifier**.
S
shawn_he 已提交
641 642 643 644 645 646 647 648 649 650

**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 已提交
651
**Return value**
S
shawn_he 已提交
652 653 654 655 656 657 658

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

**Example**

S
shawn_he 已提交
659
```js
S
shawn_he 已提交
660 661 662 663 664 665
// Default network
let netConnection = connection.createNetConnection()

// Cellular network
let netConnectionCellular = connection.createNetConnection({
    netCapabilities: {
S
shawn_he 已提交
666
        bearerTypes: [connection.NetBearType.BEARER_CELLULAR]
S
shawn_he 已提交
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
    }
})
```

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

**Example**

S
shawn_he 已提交
692
```js
S
shawn_he 已提交
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
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 已提交
710
| type     | string                                                       | Yes  | Event type. The value is fixed at **netCapabilitiesChange**.<br>**netCapabilitiesChange**: event indicating that network capabilities have changed.|
S
shawn_he 已提交
711 712 713 714
| callback | Callback<{ netHandle: [NetHandle](#nethandle), netCap: [NetCapabilities](#netcapabilities) }> | Yes  | Callback used to return the result.                                                  |

**Example**

S
shawn_he 已提交
715
```js
S
shawn_he 已提交
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
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 已提交
733
| type     | string                                                       | Yes  | Event type. The value is fixed at **netConnectionPropertiesChange**.<br>**netConnectionPropertiesChange**: event indicating that network connection properties have changed.|
S
shawn_he 已提交
734 735 736 737
| callback | Callback<{ netHandle: [NetHandle](#nethandle), connectionProperties: [ConnectionProperties](#connectionproperties) }> | Yes  | Callback used to return the result.                                                  |

**Example**

S
shawn_he 已提交
738
```js
S
shawn_he 已提交
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
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 已提交
756
| 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 已提交
757 758 759 760
| 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 已提交
761
```js
S
shawn_he 已提交
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
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 已提交
779
| 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 已提交
780 781 782 783
| callback | Callback\<[NetHandle](#nethandle)> | Yes  | Callback used to return the result.                                                  |

**Example**

S
shawn_he 已提交
784
```js
S
shawn_he 已提交
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
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 已提交
803
| type     | string          | Yes  | Event type. The value is fixed at **netUnavailable**.<br>**netUnavailable**: event indicating that the network is unavailable.|
S
shawn_he 已提交
804 805 806 807
| callback | Callback\<void> | Yes  | Callback used to return the result.                                                  |

**Example**

S
shawn_he 已提交
808
```js
S
shawn_he 已提交
809 810 811 812 813 814 815 816 817 818 819
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 已提交
820
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
821 822 823 824 825 826 827 828 829 830 831

**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 已提交
832
```js
S
shawn_he 已提交
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
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 已提交
854
```js
S
shawn_he 已提交
855 856 857 858 859 860 861 862 863
netConnection.unregister(function (error) {
    console.log(JSON.stringify(error))
})
```

## NetHandle

Defines the handle of the data network.

S
shawn_he 已提交
864
Before invoking NetHandle APIs, call **getNetHandle** to obtain a **NetHandle** object.
S
shawn_he 已提交
865 866 867

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

S
shawn_he 已提交
868
### Parameters
S
shawn_he 已提交
869 870 871

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

S
shawn_he 已提交
874
### bindSocket<sup>9+</sup>
S
shawn_he 已提交
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893

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
S
shawn_he 已提交
894 895
import socket from "@ohos.net.socket";
connection.getDefaultNet().then((netHandle)=>{
S
shawn_he 已提交
896 897
    var tcp = socket.constructTCPSocketInstance();
    var udp = socket.constructUDPSocketInstance();
S
shawn_he 已提交
898
    let socketType = "TCPSocket";
S
shawn_he 已提交
899 900
    if (socketType == "TCPSocket") {
        tcp.bind({
S
shawn_he 已提交
901
            address: '192.168.xx.xxx', port: xxxx, family: 1
S
shawn_he 已提交
902
        }, err => {
S
shawn_he 已提交
903 904 905 906 907 908 909 910 911 912
            if (err) {
                console.log('bind fail');
            }
            netHandle.bindSocket(tcp, (error, data)=>{
                if (error) {
                    console.log(JSON.stringify(error));
                } else {
                    console.log(JSON.stringify(data));
                }
            })
S
shawn_he 已提交
913 914
        })
    } else {
S
shawn_he 已提交
915 916 917
        let callback = value => {
            console.log(TAG + "on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo);
        }
S
shawn_he 已提交
918 919
        udp.on('message', callback);
        udp.bind({
S
shawn_he 已提交
920
            address: '192.168.xx.xxx', port: xxxx, family: 1
S
shawn_he 已提交
921
        }, err => {
S
shawn_he 已提交
922 923 924
            if (err) {
                console.log('bind fail');
            }
S
shawn_he 已提交
925
            udp.on('message', (data) => {
S
shawn_he 已提交
926
                console.log(JSON.stringify(data))
S
shawn_he 已提交
927
            });
S
shawn_he 已提交
928 929 930 931 932 933 934
            netHandle.bindSocket(udp,(error, data)=>{
                if (error) {
                    console.log(JSON.stringify(error));
                } else {
                    console.log(JSON.stringify(data));
                }
            })
S
shawn_he 已提交
935
        })
S
shawn_he 已提交
936 937
    }
})
S
shawn_he 已提交
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
```

### 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 已提交
960
| Promise\<void> | Promise that returns no value.|
S
shawn_he 已提交
961 962 963 964

**Example**

```js
S
shawn_he 已提交
965 966
import socket from "@ohos.net.socket";
connection.getDefaultNet().then((netHandle)=>{
S
shawn_he 已提交
967 968
    var tcp = socket.constructTCPSocketInstance();
    var udp = socket.constructUDPSocketInstance();
S
shawn_he 已提交
969 970
    let socketType = "TCPSocket";
    if (socketType == "TCPSocket") {
S
shawn_he 已提交
971
        tcp.bind({
S
shawn_he 已提交
972
            address: '192.168.xx.xxx', port: xxxx, family: 1
S
shawn_he 已提交
973
        }, err => {
S
shawn_he 已提交
974 975 976 977 978 979 980 981 982 983
            if (err) {
                console.log('bind fail');
            }
            netHandle.bindSocket(tcp).then((err, data) => {
                if (err) {
                    console.log(JSON.stringify(err));
                } else {
                    console.log(JSON.stringify(data));
                }
            })
S
shawn_he 已提交
984 985
        })
    } else {
S
shawn_he 已提交
986 987 988
        let callback = value => {
            console.log(TAG + "on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo);
        }
S
shawn_he 已提交
989 990
        udp.on('message', callback);
        udp.bind({
S
shawn_he 已提交
991
            address: '192.168.xx.xxx', port: xxxx, family: 1
S
shawn_he 已提交
992
        }, err => {
S
shawn_he 已提交
993 994 995
            if (err) {
                console.log('bind fail');
            }
S
shawn_he 已提交
996
            udp.on('message', (data) => {
S
shawn_he 已提交
997 998 999 1000 1001 1002 1003 1004 1005
                console.log(JSON.stringify(data));
            })
            netHandle.bindSocket(udp).then((err, data) => {
                if (err) {
                    console.log(JSON.stringify(err));
                } else {
                    console.log(JSON.stringify(data));
                }
            })
S
shawn_he 已提交
1006
        })
S
shawn_he 已提交
1007 1008
    }
})
S
shawn_he 已提交
1009 1010 1011
```


S
shawn_he 已提交
1012 1013 1014 1015 1016 1017
### 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 已提交
1018 1019
**Required permission**: ohos.permission.GET_NETWORK_INFO

S
shawn_he 已提交
1020 1021 1022 1023 1024 1025
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                                             | Mandatory| Description              |
| -------- | ------------------------------------------------- | ---- | ------------------ |
S
shawn_he 已提交
1026
| host     | string                                            | Yes  | Host name to be resolved.|
S
shawn_he 已提交
1027 1028 1029 1030
| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | Yes  | Callback used to return the result.          |

**Example**

S
shawn_he 已提交
1031
```js
S
shawn_he 已提交
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
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 已提交
1047 1048
**Required permission**: ohos.permission.GET_NETWORK_INFO

S
shawn_he 已提交
1049 1050 1051 1052 1053 1054
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

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

S
shawn_he 已提交
1057
**Return value**
S
shawn_he 已提交
1058 1059 1060 1061 1062 1063 1064

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

**Example**

S
shawn_he 已提交
1065
```js
S
shawn_he 已提交
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
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 已提交
1080 1081
**Required permission**: ohos.permission.GET_NETWORK_INFO

S
shawn_he 已提交
1082 1083 1084 1085 1086 1087
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                                     | Mandatory| Description              |
| -------- | ----------------------------------------- | ---- | ------------------ |
S
shawn_he 已提交
1088
| host     | string                                    | Yes  | Host name to be resolved.|
S
shawn_he 已提交
1089 1090 1091 1092
| callback | AsyncCallback\<[NetAddress](#netaddress)> | Yes  | Callback used to return the result.        |

**Example**

S
shawn_he 已提交
1093
```js
S
shawn_he 已提交
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
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 已提交
1109 1110
**Required permission**: ohos.permission.GET_NETWORK_INFO

S
shawn_he 已提交
1111 1112 1113 1114 1115 1116
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

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

S
shawn_he 已提交
1119
**Return value**
S
shawn_he 已提交
1120 1121 1122 1123 1124 1125 1126

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

**Example**

S
shawn_he 已提交
1127
```js
S
shawn_he 已提交
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
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

S
shawn_he 已提交
1142 1143 1144 1145
| 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).|
S
shawn_he 已提交
1146 1147 1148 1149 1150 1151 1152

## NetCapabilities

Defines the network capability set.

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

S
shawn_he 已提交
1153 1154 1155 1156 1157 1158
| 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 已提交
1159 1160 1161 1162 1163 1164 1165

## NetCap

Defines the network capability.

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

S
shawn_he 已提交
1166
| Name                 | Value  | Description                  |
S
shawn_he 已提交
1167 1168 1169
| ------------------------ | ---- | ---------------------- |
| 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 已提交
1170
| NET_CAPABILITY_INTERNET  | 12   | The network can connect to the Internet.|
S
shawn_he 已提交
1171
| NET_CAPABILITY_NOT_VPN | 15 | The network does not use a Virtual Private Network (VPN).|
S
shawn_he 已提交
1172
| NET_CAPABILITY_VALIDATED | 16   | The network is available.            |
S
shawn_he 已提交
1173 1174 1175 1176 1177 1178 1179

## NetBearType

Defines the network type.

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

S
shawn_he 已提交
1180
| Name        | Value  | Description       |
S
shawn_he 已提交
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
| --------------- | ---- | ----------- |
| 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

S
shawn_he 已提交
1192 1193 1194 1195 1196 1197 1198 1199
| Name     | Type    | Mandatory | Description |
| -------- | ------- | --------- | ----------- |
| interfaceName | string                             | Yes | NIC card 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 已提交
1200 1201 1202

## LinkAddress

S
shawn_he 已提交
1203
Network link information.
S
shawn_he 已提交
1204 1205 1206

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

S
shawn_he 已提交
1207 1208 1209 1210
| Name     | Type    | Mandatory | Description |
| -------- | ------- | --------- | ----------- |
| address      | [NetAddress](#netaddress) | Yes | Link address.          |
| prefixLength | number                    | Yes | Length of the link address prefix.|
S
shawn_he 已提交
1211 1212 1213

## RouteInfo

S
shawn_he 已提交
1214
Network route information.
S
shawn_he 已提交
1215 1216 1217

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

S
shawn_he 已提交
1218 1219 1220 1221 1222 1223 1224
| Name     | Type    | Mandatory | Description |
| -------- | ------- | --------- | ----------- |
| interface      | string                      | Yes | NIC card name.      |
| destination    | [LinkAddress](#linkaddress) | Yes | Destination IP 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 已提交
1225 1226 1227 1228 1229 1230 1231

## NetAddress

Defines the network address.

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

S
shawn_he 已提交
1232 1233 1234 1235 1236
| Name     | Type    | Mandatory | Description |
| -------- | ------- | --------- | ----------- |
| address | string | Yes | Network address.                        |
| family  | number | Yes | 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**.   |