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

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

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

## Modules to Import

S
shawn_he 已提交
10
```js
S
shawn_he 已提交
11 12
import connection from '@ohos.net.connection'
```
S
shawn_he 已提交
13

S
shawn_he 已提交
14
## connection.createNetConnection<sup>8+</sup>
S
shawn_he 已提交
15 16 17

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

S
shawn_he 已提交
18
Creates a **NetConnection** object. **netSpecifier** specifies the network, and **timeout** specifies the timeout duration in ms. **timeout** is configurable only when **netSpecifier** is specified. If neither of them is present, the default network is used.
S
shawn_he 已提交
19 20 21 22 23 24 25

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

**Parameters**

| Name      | Type                         | Mandatory| Description                                                        |
| ------------ | ----------------------------- | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
26 27
| netSpecifier | [NetSpecifier](#netspecifier) | No  | Network specifier, which specifies the characteristics of a network. If this parameter is not set or is set to **undefined**, the default network is used.                  |
| timeout      | number                        | No  | Timeout duration for obtaining the network specified by **netSpecifier**. This parameter is valid only when **netSpecifier** is specified. The default value is **0** if **netSpecifier** is **undefined**.|
S
shawn_he 已提交
28 29 30 31 32 33 34 35 36 37

**Return value**

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

**Example**

```js
S
shawn_he 已提交
38
// For the default network, you do not need to pass in parameters.
S
shawn_he 已提交
39 40
let netConnection = connection.createNetConnection()

S
shawn_he 已提交
41
// For the cellular network, you need to pass in related network parameters. If the timeout parameter is not specified, the timeout value is 0 by default.
S
shawn_he 已提交
42
let netConnectionCellular = connection.createNetConnection({
S
shawn_he 已提交
43 44 45
  netCapabilities: {
    bearerTypes: [connection.NetBearType.BEARER_CELLULAR]
  }
S
shawn_he 已提交
46 47
})
```
S
shawn_he 已提交
48

S
shawn_he 已提交
49
## connection.getDefaultNet<sup>8+</sup>
S
shawn_he 已提交
50 51 52

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

S
shawn_he 已提交
53
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 已提交
54

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

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

**Parameters**

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

**Error codes**

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

**Example**

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

S
shawn_he 已提交
83
## connection.getDefaultNet<sup>8+</sup>
S
shawn_he 已提交
84 85 86

getDefaultNet(): Promise\<NetHandle>

S
shawn_he 已提交
87
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 已提交
88

S
shawn_he 已提交
89
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
90 91 92

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

S
shawn_he 已提交
93
**Return value**
S
shawn_he 已提交
94 95 96 97 98

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

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

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
S
shawn_he 已提交
104
| 401     | Parameter error.             |
S
shawn_he 已提交
105 106 107
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

S
shawn_he 已提交
108 109
**Example**

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

S
shawn_he 已提交
116
## connection.getDefaultNetSync<sup>9+</sup>
S
shawn_he 已提交
117

S
shawn_he 已提交
118
getDefaultNetSync(): NetHandle
S
shawn_he 已提交
119

S
shawn_he 已提交
120
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 已提交
121 122 123 124 125 126 127 128 129 130 131

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

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

**Return value**

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

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

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
S
shawn_he 已提交
137
| 401     | Parameter error.             |
S
shawn_he 已提交
138 139 140
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

S
shawn_he 已提交
141 142 143 144 145 146
**Example**

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

S
shawn_he 已提交
147
## connection.getGlobalHttpProxy<sup>10+</sup>
S
shawn_he 已提交
148

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

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

S
shawn_he 已提交
153
**System API**: This is a system API.
S
shawn_he 已提交
154

S
shawn_he 已提交
155 156 157 158
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

S
shawn_he 已提交
159 160 161
| Name  | Type                                   | Mandatory| Description                                                        |
| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
| callback | AsyncCallback\<[HttpProxy](#httpproxy)> | Yes  | Callback used to return the result. If the global HTTP proxy configuration of the network is obtained successfully, **error** is **undefined** and **data** is the global HTTP proxy configuration. Otherwise, **error** is an error object.|
S
shawn_he 已提交
162 163 164 165 166

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
S
shawn_he 已提交
167 168
| 401     | Parameter error.             |
| 202     | Non-system applications use system APIs.             |
S
shawn_he 已提交
169 170 171 172 173 174
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
S
shawn_he 已提交
175 176 177
connection.getGlobalHttpProxy((error, data) => {
  console.info(JSON.stringify(error));
  console.info(JSON.stringify(data));
S
shawn_he 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
})
```

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

getGlobalHttpProxy(): Promise\<HttpProxy>;

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

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

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

**Return value**

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

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
S
shawn_he 已提交
201 202
| 401     | Parameter error.             |
| 202     | Non-system applications use system APIs.             |
S
shawn_he 已提交
203 204 205 206 207 208 209
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
connection.getGlobalHttpProxy().then((data) => {
S
shawn_he 已提交
210
  console.info(JSON.stringify(data));
S
shawn_he 已提交
211
}).catch(error => {
S
shawn_he 已提交
212
  console.info(JSON.stringify(error));
S
shawn_he 已提交
213 214 215 216 217 218 219 220 221 222 223 224
})
```

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

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

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

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

**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
S
shawn_he 已提交
225 226 227 228 229

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

**Parameters**

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

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
S
shawn_he 已提交
241
| 202     | Non-system applications use system APIs.               |
S
shawn_he 已提交
242 243 244
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
245 246 247

**Example**

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

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

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

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

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

**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

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

**Parameters**

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

**Return value**

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

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
S
shawn_he 已提交
291
| 202     | Non-system applications use system APIs.               |
S
shawn_he 已提交
292 293 294 295 296 297 298
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
S
shawn_he 已提交
299
let exclusionStr = "192.168,baidu.com"
S
shawn_he 已提交
300 301
let exclusionArray = exclusionStr.split(',');
let httpProxy = {
S
shawn_he 已提交
302 303 304
  host: "192.168.xx.xxx",
  port: 8080,
  exclusionList: exclusionArray
S
shawn_he 已提交
305
}
S
shawn_he 已提交
306
connection.setGlobalHttpProxy(httpProxy).then(() => {
S
shawn_he 已提交
307 308 309
  console.info("success");
}).catch(error => {
  console.info(JSON.stringify(error));
S
shawn_he 已提交
310 311 312
})
```

S
shawn_he 已提交
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 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
## connection.getDefaultHttpProxy<sup>10+</sup>

getDefaultHttpProxy(callback: AsyncCallback\<HttpProxy>): void

Obtains the default HTTP proxy configuration of the network.
If the global proxy is set, the global HTTP proxy configuration is returned. If [setAppNet](#connectionsetappnet) is used to bind the application to the network specified by [NetHandle](#nethandle), the HTTP proxy configuration of this network is returned. In other cases, the HTTP proxy configuration of the default network is returned.
This API uses an asynchronous callback to return the result.

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

**Parameters**

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

**Error codes**

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

**Example**

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

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

getDefaultHttpProxy(): Promise\<HttpProxy>;

Obtains the default proxy configuration of the network.
If the global proxy is set, the global proxy configuration is returned. If [setAppNet](#connectionsetappnet) is used to bind the application to the network specified by [NetHandle](#nethandle), the proxy configuration of this network is returned. In other cases, the HTTP proxy configuration of the default network is returned.
This API uses a promise to return the result.

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

**Return value**

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

**Error codes**

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

**Example**

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

S
shawn_he 已提交
378
## connection.getAppNet<sup>9+</sup>
S
shawn_he 已提交
379

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

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

S
shawn_he 已提交
384 385 386 387
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

S
shawn_he 已提交
388 389 390
| Name  | Type                                   | Mandatory| Description                                                        |
| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
| callback | AsyncCallback\<[NetHandle](#nethandle)> | Yes  | Callback used to return the result. If information about the network bound to the application is successfully obtained, **error** is **undefined** and **data** is the obtained network information. Otherwise, **error** is an error object.|
S
shawn_he 已提交
391 392 393 394 395

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
S
shawn_he 已提交
396
| 401 | Parameter error.|
S
shawn_he 已提交
397 398 399 400 401 402
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
S
shawn_he 已提交
403 404 405
connection.getAppNet(function (error, data) {
  console.log(JSON.stringify(error))
  console.log(JSON.stringify(data))
S
shawn_he 已提交
406 407 408 409 410 411 412 413
})
```

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

getAppNet(): Promise\<NetHandle>;

Obtains information about the network bound to an application. This API uses a promise to return the result.
S
shawn_he 已提交
414 415 416

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

S
shawn_he 已提交
417
**Return value**
S
shawn_he 已提交
418

S
shawn_he 已提交
419 420 421 422 423 424 425 426
| Type                             | Description                                 |
| --------------------------------- | ------------------------------------- |
| Promise\<[NetHandle](#nethandle)> | Promise used to return the result.|

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
S
shawn_he 已提交
427
| 401 | Parameter error.|
S
shawn_he 已提交
428 429 430 431 432 433 434
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
connection.getAppNet().then((data) => {
S
shawn_he 已提交
435
  console.info(JSON.stringify(data));
S
shawn_he 已提交
436
}).catch(error => {
S
shawn_he 已提交
437
  console.info(JSON.stringify(error));
S
shawn_he 已提交
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
})
```

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

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

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

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

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

**Parameters**

S
shawn_he 已提交
453 454 455 456
| Name   | Type                   | Mandatory| Description                                                        |
| --------- | ----------------------- | ---- | ------------------------------------------------------------ |
| netHandle | [NetHandle](#nethandle) | Yes  | Handle of the data network.                                            |
| callback  | AsyncCallback\<void>    | Yes  | Callback used to return the result. If the application is successfully bound to the specified network, **error** is **undefined**. Otherwise, **error** is an error object.|
S
shawn_he 已提交
457 458 459 460 461 462 463 464 465 466

**Error codes**

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

**Example**

S
shawn_he 已提交
470
```js
S
shawn_he 已提交
471
connection.getDefaultNet(function (error, netHandle) {
S
shawn_he 已提交
472 473 474 475
  connection.setAppNet(netHandle, (error, data) => {
    console.log(JSON.stringify(error))
    console.log(JSON.stringify(data))
  });
S
shawn_he 已提交
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
})
```

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

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

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

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

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

**Parameters**

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

**Return value**

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

**Error codes**

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

**Example**

```js
connection.getDefaultNet().then(function (netHandle) {
S
shawn_he 已提交
515 516 517 518 519
  connection.setAppNet(netHandle).then(() => {
    console.log("success")
  }).catch(error => {
    console.log(JSON.stringify(error))
  })
S
shawn_he 已提交
520 521 522
})
```

S
shawn_he 已提交
523
## connection.getAllNets<sup>8+</sup>
S
shawn_he 已提交
524 525 526

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

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

S
shawn_he 已提交
529
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
530 531 532 533

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

**Parameters**
S
shawn_he 已提交
534

S
shawn_he 已提交
535 536
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
S
shawn_he 已提交
537
| callback | AsyncCallback&lt;Array&lt;[NetHandle](#nethandle)&gt;&gt; | Yes| Callback used to return the result. If the list of all connected networks is obtained successfully, **error** is **undefined** and **data** is the list of activated data networks. Otherwise, **error** is an error object.|
S
shawn_he 已提交
538 539 540 541 542 543

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
S
shawn_he 已提交
544
| 401     | Parameter error.             |
S
shawn_he 已提交
545 546
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
547 548 549

**Example**

S
shawn_he 已提交
550
```js
S
shawn_he 已提交
551
connection.getAllNets(function (error, data) {
S
shawn_he 已提交
552 553
  console.log(JSON.stringify(error))
  console.log(JSON.stringify(data))
S
shawn_he 已提交
554 555 556
});
```

S
shawn_he 已提交
557
## connection.getAllNets<sup>8+</sup>
S
shawn_he 已提交
558 559 560

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

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

S
shawn_he 已提交
563
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
564 565 566

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

S
shawn_he 已提交
567 568
**Return value**

S
shawn_he 已提交
569 570 571 572
| Type| Description|
| -------- | -------- |
| Promise&lt;Array&lt;[NetHandle](#nethandle)&gt;&gt; | Promise used to return the result.|

S
shawn_he 已提交
573 574 575 576 577
**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
S
shawn_he 已提交
578
| 401     | Parameter error.             |
S
shawn_he 已提交
579 580 581
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

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

S
shawn_he 已提交
584
```js
S
shawn_he 已提交
585
connection.getAllNets().then(function (data) {
S
shawn_he 已提交
586
  console.log(JSON.stringify(data))
S
shawn_he 已提交
587 588 589
});
```

S
shawn_he 已提交
590
## connection.getConnectionProperties<sup>8+</sup>
S
shawn_he 已提交
591 592 593

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

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

S
shawn_he 已提交
596
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
597 598 599 600 601

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

**Parameters**

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

**Error codes**

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

**Example**

S
shawn_he 已提交
619
```js
S
shawn_he 已提交
620
connection.getDefaultNet().then(function (netHandle) {
S
shawn_he 已提交
621 622 623 624
  connection.getConnectionProperties(netHandle, function (error, data) {
    console.log(JSON.stringify(error))
    console.log(JSON.stringify(data))
  })
S
shawn_he 已提交
625 626 627
})
```

S
shawn_he 已提交
628
## connection.getConnectionProperties<sup>8+</sup>
S
shawn_he 已提交
629 630 631

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

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

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

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

**Parameters**

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

S
shawn_he 已提交
644
**Return value**
S
shawn_he 已提交
645 646 647 648 649

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

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

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

S
shawn_he 已提交
660 661
**Example**

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

S
shawn_he 已提交
670
## connection.getNetCapabilities<sup>8+</sup>
S
shawn_he 已提交
671 672 673

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

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

S
shawn_he 已提交
676
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
677 678 679 680 681

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

**Parameters**

S
shawn_he 已提交
682 683 684 685
| Name   | Type                                               | Mandatory| Description                                                        |
| --------- | --------------------------------------------------- | ---- | ------------------------------------------------------------ |
| netHandle | [NetHandle](#nethandle)                             | Yes  | Handle of the data network.                                            |
| callback  | AsyncCallback\<[NetCapabilities](#netcapabilities)> | Yes  | Callback used to return the result. If the capability information of the network corresponding to the **netHandle** is obtained successfully, **error** is **undefined** and **data** is the obtained network capability information. Otherwise, **error** is an error object.|
S
shawn_he 已提交
686 687 688 689 690 691 692 693 694 695

**Error codes**

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

**Example**

S
shawn_he 已提交
699
```js
S
shawn_he 已提交
700
connection.getDefaultNet().then(function (netHandle) {
S
shawn_he 已提交
701 702 703 704
  connection.getNetCapabilities(netHandle, function (error, data) {
    console.log(JSON.stringify(error))
    console.log(JSON.stringify(data))
  })
S
shawn_he 已提交
705 706 707
})
```

S
shawn_he 已提交
708
## connection.getNetCapabilities<sup>8+</sup>
S
shawn_he 已提交
709 710 711

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

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

S
shawn_he 已提交
714
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
715 716 717 718 719 720 721

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

**Parameters**

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

S
shawn_he 已提交
724
**Return value**
S
shawn_he 已提交
725 726 727 728 729

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

S
shawn_he 已提交
730 731 732 733 734 735 736 737 738 739
**Error codes**

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

S
shawn_he 已提交
740 741
**Example**

S
shawn_he 已提交
742
```js
S
shawn_he 已提交
743
connection.getDefaultNet().then(function (netHandle) {
S
shawn_he 已提交
744 745 746
  connection.getNetCapabilities(netHandle).then(function (data) {
    console.log(JSON.stringify(data))
  })
S
shawn_he 已提交
747 748 749
})
```

S
shawn_he 已提交
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
## connection.isDefaultNetMetered<sup>9+</sup>

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

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

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

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

**Parameters**

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

S
shawn_he 已提交
766 767 768 769 770
**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
S
shawn_he 已提交
771
| 401     | Parameter error.               |
S
shawn_he 已提交
772 773 774 775
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**
S
shawn_he 已提交
776 777

```js
S
shawn_he 已提交
778
connection.isDefaultNetMetered(function (error, data) {
S
shawn_he 已提交
779 780
  console.log(JSON.stringify(error))
  console.log('data: ' + data)
S
shawn_he 已提交
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
})
```

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

isDefaultNetMetered(): Promise\<boolean>

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

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

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

**Return value**

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

S
shawn_he 已提交
800 801 802 803 804
**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
S
shawn_he 已提交
805
| 401     | Parameter error.               |
S
shawn_he 已提交
806 807 808 809 810 811 812
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
connection.isDefaultNetMetered().then(function (data) {
S
shawn_he 已提交
813
  console.log('data: ' + data)
S
shawn_he 已提交
814 815 816
})
```

S
shawn_he 已提交
817
## connection.hasDefaultNet<sup>8+</sup>
S
shawn_he 已提交
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837

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

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

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

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

**Parameters**

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

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
S
shawn_he 已提交
838
| 401     | Parameter error.               |
S
shawn_he 已提交
839 840 841 842 843 844 845
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
connection.hasDefaultNet(function (error, data) {
S
shawn_he 已提交
846 847
  console.log(JSON.stringify(error))
  console.log('data: ' + data)
S
shawn_he 已提交
848 849 850
})
```

S
shawn_he 已提交
851
## connection.hasDefaultNet<sup>8+</sup>
S
shawn_he 已提交
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871

hasDefaultNet(): Promise\<boolean>

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

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

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

**Return value**

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

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
S
shawn_he 已提交
872
| 401     | Parameter error.               |
S
shawn_he 已提交
873 874 875 876
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**
S
shawn_he 已提交
877 878

```js
S
shawn_he 已提交
879
connection.hasDefaultNet().then(function (data) {
S
shawn_he 已提交
880
  console.log('data: ' + data)
S
shawn_he 已提交
881 882 883
})
```

S
shawn_he 已提交
884
## connection.enableAirplaneMode<sup>8+</sup>
S
shawn_he 已提交
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905

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

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

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

**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

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

**Parameters**

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

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
S
shawn_he 已提交
906 907 908
| 201     | Permission denied.             |
| 202     | Non-system applications use system APIs.              |
| 401     | Parameter error.               |
S
shawn_he 已提交
909 910 911 912 913 914 915
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
connection.enableAirplaneMode(function (error) {
S
shawn_he 已提交
916
  console.log(JSON.stringify(error))
S
shawn_he 已提交
917 918 919
})
```

S
shawn_he 已提交
920
## connection.enableAirplaneMode<sup>8+</sup>
S
shawn_he 已提交
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941

enableAirplaneMode(): Promise\<void>

Enables the airplane mode. This API uses a promise to return the result.

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

**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

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

**Return value**

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

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
S
shawn_he 已提交
942 943 944
| 201     | Permission denied.             |
| 202     | Non-system applications use system APIs.              |
| 401     | Parameter error.               |
S
shawn_he 已提交
945 946 947 948 949 950 951
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
connection.enableAirplaneMode().then(function (error) {
S
shawn_he 已提交
952
  console.log(JSON.stringify(error))
S
shawn_he 已提交
953 954 955
})
```

S
shawn_he 已提交
956
## connection.disableAirplaneMode<sup>8+</sup>
S
shawn_he 已提交
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971

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

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

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

**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

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

**Parameters**

| Name  | Type                                             | Mandatory| Description              |
| -------- | ------------------------------------------------- | ---- | ------------------ |
S
shawn_he 已提交
972
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result. If the airplane mode is disabled successfully, **error** is **undefined**. Otherwise, **error** is an error object.|
S
shawn_he 已提交
973 974 975 976 977

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
S
shawn_he 已提交
978 979 980
| 201     | Permission denied.             |
| 202     | Non-system applications use system APIs.              |
| 401     | Parameter error.               |
S
shawn_he 已提交
981 982 983 984 985 986 987
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
connection.disableAirplaneMode(function (error) {
S
shawn_he 已提交
988
  console.log(JSON.stringify(error))
S
shawn_he 已提交
989 990 991
})
```

S
shawn_he 已提交
992
## connection.disableAirplaneMode<sup>8+</sup>
S
shawn_he 已提交
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013

disableAirplaneMode(): Promise\<void>

Disables the airplane mode. This API uses a promise to return the result.

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

**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

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

**Return value**

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

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
S
shawn_he 已提交
1014 1015 1016
| 201     | Permission denied.             |
| 202     | Non-system applications use system APIs.              |
| 401     | Parameter error.               |
S
shawn_he 已提交
1017 1018 1019 1020 1021 1022 1023
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**Example**

```js
connection.disableAirplaneMode().then(function (error) {
S
shawn_he 已提交
1024
  console.log(JSON.stringify(error))
S
shawn_he 已提交
1025 1026 1027
})
```

S
shawn_he 已提交
1028
## connection.reportNetConnected<sup>8+</sup>
S
shawn_he 已提交
1029 1030 1031

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

S
shawn_he 已提交
1032
Reports connection of the data network to the network management module. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
1033

S
shawn_he 已提交
1034
**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET
S
shawn_he 已提交
1035 1036 1037 1038

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

**Parameters**
S
shawn_he 已提交
1039

S
shawn_he 已提交
1040 1041 1042
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).|
S
shawn_he 已提交
1043
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result. If the network status is reported successfully, **error** is **undefined**. Otherwise, **error** is an error object.|
S
shawn_he 已提交
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053

**Error codes**

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

**Example**

S
shawn_he 已提交
1057
```js
S
shawn_he 已提交
1058
connection.getDefaultNet().then(function (netHandle) {
S
shawn_he 已提交
1059 1060 1061
  connection.reportNetConnected(netHandle, function (error) {
    console.log(JSON.stringify(error))
  });
S
shawn_he 已提交
1062 1063 1064
});
```

S
shawn_he 已提交
1065
## connection.reportNetConnected<sup>8+</sup>
S
shawn_he 已提交
1066 1067 1068

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

S
shawn_he 已提交
1069
Reports connection of the data network to the network management module. This API uses a promise to return the result.
S
shawn_he 已提交
1070

S
shawn_he 已提交
1071
**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET
S
shawn_he 已提交
1072 1073 1074 1075

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

**Parameters**
S
shawn_he 已提交
1076

S
shawn_he 已提交
1077 1078 1079 1080
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).|

S
shawn_he 已提交
1081
**Return value**
S
shawn_he 已提交
1082 1083
| Type| Description|
| -------- | -------- |
S
shawn_he 已提交
1084
| Promise&lt;void&gt; | Promise that returns no value.|
S
shawn_he 已提交
1085

S
shawn_he 已提交
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
**Error codes**

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

S
shawn_he 已提交
1096 1097
**Example**

S
shawn_he 已提交
1098
```js
S
shawn_he 已提交
1099
connection.getDefaultNet().then(function (netHandle) {
S
shawn_he 已提交
1100 1101 1102
  connection.reportNetConnected(netHandle).then(function () {
    console.log(`report success`)
  });
S
shawn_he 已提交
1103 1104 1105
});
```

S
shawn_he 已提交
1106
## connection.reportNetDisconnected<sup>8+</sup>
S
shawn_he 已提交
1107 1108 1109

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

S
shawn_he 已提交
1110
Reports disconnection of the data network to the network management module. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
1111

S
shawn_he 已提交
1112
**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET
S
shawn_he 已提交
1113 1114 1115 1116

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

**Parameters**
S
shawn_he 已提交
1117

S
shawn_he 已提交
1118 1119 1120
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).|
S
shawn_he 已提交
1121
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result. If the network status is reported successfully, **error** is **undefined**. Otherwise, **error** is an error object.|
S
shawn_he 已提交
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131

**Error codes**

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

**Example**

S
shawn_he 已提交
1135
```js
S
shawn_he 已提交
1136
connection.getDefaultNet().then(function (netHandle) {
S
shawn_he 已提交
1137 1138 1139
  connection.reportNetDisconnected(netHandle, function (error) {
    console.log(JSON.stringify(error))
  });
S
shawn_he 已提交
1140 1141 1142
});
```

S
shawn_he 已提交
1143
## connection.reportNetDisconnected<sup>8+</sup>
S
shawn_he 已提交
1144 1145 1146

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

S
shawn_he 已提交
1147
Reports disconnection of the data network to the network management module. This API uses a promise to return the result.
S
shawn_he 已提交
1148

S
shawn_he 已提交
1149
**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET
S
shawn_he 已提交
1150 1151 1152 1153

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

**Parameters**
S
shawn_he 已提交
1154

S
shawn_he 已提交
1155 1156 1157 1158
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).|

S
shawn_he 已提交
1159
**Return value**
S
shawn_he 已提交
1160 1161
| Type| Description|
| -------- | -------- |
S
shawn_he 已提交
1162
| Promise&lt;void&gt; | Promise that returns no value.|
S
shawn_he 已提交
1163

S
shawn_he 已提交
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
**Error codes**

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

S
shawn_he 已提交
1174 1175
**Example**

S
shawn_he 已提交
1176
```js
S
shawn_he 已提交
1177
connection.getDefaultNet().then(function (netHandle) {
S
shawn_he 已提交
1178 1179 1180
  connection.reportNetDisconnected(netHandle).then(function () {
    console.log(`report success`)
  });
S
shawn_he 已提交
1181 1182 1183
});
```

S
shawn_he 已提交
1184
## connection.getAddressesByName<sup>8+</sup>
S
shawn_he 已提交
1185 1186 1187 1188 1189

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 已提交
1190
**Required permissions**: ohos.permission.INTERNET
S
shawn_he 已提交
1191 1192 1193 1194 1195

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

**Parameters**

S
shawn_he 已提交
1196 1197 1198 1199
| Name  | Type                                             | Mandatory| Description                                                        |
| -------- | ------------------------------------------------- | ---- | ------------------------------------------------------------ |
| host     | string                                            | Yes  | Host name to resolve.                                          |
| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | Yes  | Callback used to return the result. If all IP addresses are successfully obtained, **error** is **undefined**, and **data** is the list of all obtained IP addresses. Otherwise, **error** is an error object.|
S
shawn_he 已提交
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209

**Error codes**

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

**Example**

```js
S
shawn_he 已提交
1214 1215
let host = "xxxx";
connection.getAddressesByName(host, function (error, data) {
S
shawn_he 已提交
1216 1217
  console.log(JSON.stringify(error))
  console.log(JSON.stringify(data))
S
shawn_he 已提交
1218 1219 1220
})
```

S
shawn_he 已提交
1221
## connection.getAddressesByName<sup>8+</sup>
S
shawn_he 已提交
1222

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

S
shawn_he 已提交
1225
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 已提交
1226

S
shawn_he 已提交
1227
**Required permissions**: ohos.permission.INTERNET
S
shawn_he 已提交
1228 1229

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

S
shawn_he 已提交
1231 1232 1233 1234 1235
**Parameters**

| Name| Type  | Mandatory| Description              |
| ------ | ------ | ---- | ------------------ |
| host   | string | Yes  | Host name to resolve.|
S
shawn_he 已提交
1236 1237 1238 1239 1240

**Return value**

| Type                                       | Description                         |
| ------------------------------------------- | ----------------------------- |
S
shawn_he 已提交
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
| Promise\<Array\<[NetAddress](#netaddress)>> | Promise used to return the result.|

**Error codes**

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

**Example**

```js
S
shawn_he 已提交
1256 1257
let host = "xxxx";
connection.getAddressesByName(host).then(function (data) {
S
shawn_he 已提交
1258
  console.log(JSON.stringify(data))
S
shawn_he 已提交
1259 1260 1261
})
```

S
shawn_he 已提交
1262
## NetConnection
S
shawn_he 已提交
1263

S
shawn_he 已提交
1264
Represents the network connection handle.
S
shawn_he 已提交
1265

S
shawn_he 已提交
1266 1267 1268 1269 1270
> **NOTE**
> When a device changes to the network connected state, the **netAvailable**, **netCapabilitiesChange**, and **netConnectionPropertiesChange** events will be triggered.
> When a device changes to the network disconnected state, the **netLost** event will be triggered.
> When a device switches from a Wi-Fi network to a cellular network, the **netLost** event will be first triggered to indicate that the Wi-Fi network is lost and then the **netAvaliable** event will be triggered to indicate that the cellular network is available.

S
shawn_he 已提交
1271
### register<sup>8+</sup>
S
shawn_he 已提交
1272

S
shawn_he 已提交
1273
register(callback: AsyncCallback\<void>): void
S
shawn_he 已提交
1274

S
shawn_he 已提交
1275
Registers a listener for network status changes.
S
shawn_he 已提交
1276

S
shawn_he 已提交
1277
**Required permission**: ohos.permission.GET_NETWORK_INFO
S
shawn_he 已提交
1278

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

S
shawn_he 已提交
1281
**Parameters**
S
shawn_he 已提交
1282

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

S
shawn_he 已提交
1287
**Error codes**
S
shawn_he 已提交
1288

S
shawn_he 已提交
1289 1290 1291
| ID| Error Message                       |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
S
shawn_he 已提交
1292
| 401     | Parameter error.             |
S
shawn_he 已提交
1293 1294
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
1295
| 2101008 | The same callback exists.     |
S
shawn_he 已提交
1296
| 2101022 | The number of requests exceeded the maximum. |
S
shawn_he 已提交
1297 1298 1299 1300

**Example**

```js
S
shawn_he 已提交
1301
netConnection.register(function (error) {
S
shawn_he 已提交
1302
  console.log(JSON.stringify(error))
S
shawn_he 已提交
1303 1304 1305
})
```

S
shawn_he 已提交
1306
### unregister<sup>8+</sup>
S
shawn_he 已提交
1307

S
shawn_he 已提交
1308
unregister(callback: AsyncCallback\<void>): void
S
shawn_he 已提交
1309

S
shawn_he 已提交
1310
Unregisters the listener for network status changes.
S
shawn_he 已提交
1311 1312 1313 1314 1315

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

**Parameters**

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

S
shawn_he 已提交
1320
**Error codes**
S
shawn_he 已提交
1321

S
shawn_he 已提交
1322 1323
| ID| Error Message                       |
| ------- | -----------------------------  |
S
shawn_he 已提交
1324 1325
| 201 | Permission denied.|
| 401 | Parameter error.         |
S
shawn_he 已提交
1326 1327
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
1328
| 2101007 | The callback is not exists.      |
S
shawn_he 已提交
1329 1330 1331

**Example**

S
shawn_he 已提交
1332
```js
S
shawn_he 已提交
1333
netConnection.unregister(function (error) {
S
shawn_he 已提交
1334
  console.log(JSON.stringify(error))
S
shawn_he 已提交
1335 1336 1337
})
```

S
shawn_he 已提交
1338
### on('netAvailable')<sup>8+</sup>
S
shawn_he 已提交
1339 1340 1341 1342 1343

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

Registers a listener for **netAvailable** events.

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

S
shawn_he 已提交
1346 1347 1348 1349 1350 1351
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

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

**Example**

S
shawn_he 已提交
1357
```js
S
shawn_he 已提交
1358 1359 1360 1361 1362
// Create a NetConnection object.
let netCon = connection.createNetConnection()

// Call register to register a listener.
netCon.register(function (error) {
S
shawn_he 已提交
1363
  console.log(JSON.stringify(error))
S
shawn_he 已提交
1364 1365 1366 1367
})

// Subscribe to netAvailable events. Event notifications can be received only after register is called.
netCon.on('netAvailable', function (data) {
S
shawn_he 已提交
1368
  console.log(JSON.stringify(data))
S
shawn_he 已提交
1369
})
S
shawn_he 已提交
1370 1371 1372

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

S
shawn_he 已提交
1377
### on('netBlockStatusChange')<sup>8+</sup>
S
shawn_he 已提交
1378

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

S
shawn_he 已提交
1381
Registers a listener for **netBlockStatusChange** events. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
1382

S
shawn_he 已提交
1383
**Model restriction**: Before you call this API, make sure that you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network.
S
shawn_he 已提交
1384 1385 1386 1387 1388 1389 1390

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

**Parameters**

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

**Example**

S
shawn_he 已提交
1396
```js
S
shawn_he 已提交
1397 1398 1399 1400 1401
// Create a NetConnection object.
let netCon = connection.createNetConnection()

// Call register to register a listener.
netCon.register(function (error) {
S
shawn_he 已提交
1402
  console.log(JSON.stringify(error))
S
shawn_he 已提交
1403 1404 1405 1406
})

// Subscribe to netBlockStatusChange events. Event notifications can be received only after register is called.
netCon.on('netBlockStatusChange', function (data) {
S
shawn_he 已提交
1407
  console.log(JSON.stringify(data))
S
shawn_he 已提交
1408
})
S
shawn_he 已提交
1409 1410 1411

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

S
shawn_he 已提交
1416
### on('netCapabilitiesChange')<sup>8+</sup>
S
shawn_he 已提交
1417

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

S
shawn_he 已提交
1420 1421 1422
Registers a listener for **netCapabilitiesChange** events.

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

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

**Parameters**

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

**Example**

S
shawn_he 已提交
1435
```js
S
shawn_he 已提交
1436 1437 1438 1439 1440
// Create a NetConnection object.
let netCon = connection.createNetConnection()

// Call register to register a listener.
netCon.register(function (error) {
S
shawn_he 已提交
1441
  console.log(JSON.stringify(error))
S
shawn_he 已提交
1442 1443 1444 1445
})

// Subscribe to netCapabilitiesChange events. Event notifications can be received only after register is called.
netCon.on('netCapabilitiesChange', function (data) {
S
shawn_he 已提交
1446
  console.log(JSON.stringify(data))
S
shawn_he 已提交
1447
})
S
shawn_he 已提交
1448 1449 1450

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

S
shawn_he 已提交
1455
### on('netConnectionPropertiesChange')<sup>8+</sup>
S
shawn_he 已提交
1456

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

S
shawn_he 已提交
1460 1461 1462
Registers a listener for **netConnectionPropertiesChange** events.

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

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

**Parameters**

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

**Example**

S
shawn_he 已提交
1475
```js
S
shawn_he 已提交
1476 1477 1478 1479 1480
// Create a NetConnection object.
let netCon = connection.createNetConnection()

// Call register to register a listener.
netCon.register(function (error) {
S
shawn_he 已提交
1481
  console.log(JSON.stringify(error))
S
shawn_he 已提交
1482 1483 1484 1485
})

// Subscribe to netConnectionPropertiesChange events. Event notifications can be received only after register is called.
netCon.on('netConnectionPropertiesChange', function (data) {
S
shawn_he 已提交
1486
  console.log(JSON.stringify(data))
S
shawn_he 已提交
1487
})
S
shawn_he 已提交
1488 1489 1490

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

S
shawn_he 已提交
1495
### on('netLost')<sup>8+</sup>
S
shawn_he 已提交
1496 1497 1498 1499 1500

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

Registers a listener for **netLost** events.

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

S
shawn_he 已提交
1503 1504 1505 1506 1507 1508
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

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

**Example**

S
shawn_he 已提交
1514
```js
S
shawn_he 已提交
1515 1516 1517 1518 1519
// Create a NetConnection object.
let netCon = connection.createNetConnection()

// Call register to register a listener.
netCon.register(function (error) {
S
shawn_he 已提交
1520
  console.log(JSON.stringify(error))
S
shawn_he 已提交
1521 1522 1523 1524
})

// Subscribe to netLost events. Event notifications can be received only after register is called.
netCon.on('netLost', function (data) {
S
shawn_he 已提交
1525
  console.log(JSON.stringify(data))
S
shawn_he 已提交
1526
})
S
shawn_he 已提交
1527 1528 1529

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

S
shawn_he 已提交
1534
### on('netUnavailable')<sup>8+</sup>
S
shawn_he 已提交
1535 1536 1537 1538 1539

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

Registers a listener for **netUnavailable** events.

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

S
shawn_he 已提交
1542 1543 1544 1545 1546 1547
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

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

**Example**

S
shawn_he 已提交
1553
```js
S
shawn_he 已提交
1554 1555
// Create a NetConnection object.
let netCon = connection.createNetConnection()
S
shawn_he 已提交
1556

S
shawn_he 已提交
1557 1558
// Call register to register a listener.
netCon.register(function (error) {
S
shawn_he 已提交
1559
  console.log(JSON.stringify(error))
S
shawn_he 已提交
1560 1561
})

S
shawn_he 已提交
1562 1563
// Subscribe to netUnavailable events. Event notifications can be received only after register is called.
netCon.on('netUnavailable', function (data) {
S
shawn_he 已提交
1564
  console.log(JSON.stringify(data))
S
shawn_he 已提交
1565
})
S
shawn_he 已提交
1566

S
shawn_he 已提交
1567 1568
// Call unregister to unregister the listener.
netCon.unregister(function (error) {
S
shawn_he 已提交
1569
  console.log(JSON.stringify(error))
S
shawn_he 已提交
1570 1571 1572
})
```

S
shawn_he 已提交
1573
## NetHandle<sup>8+</sup>
S
shawn_he 已提交
1574 1575 1576

Defines the handle of the data network.

S
shawn_he 已提交
1577
Before invoking **NetHandle** APIs, call **getNetHandle** to obtain a **NetHandle** object.
S
shawn_he 已提交
1578 1579 1580

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

S
shawn_he 已提交
1581
### Attributes
S
shawn_he 已提交
1582

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

S
shawn_he 已提交
1587
### bindSocket<sup>9+</sup>
S
shawn_he 已提交
1588

S
shawn_he 已提交
1589
bindSocket(socketParam: TCPSocket \| UDPSocket, callback: AsyncCallback\<void>): void
S
shawn_he 已提交
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599

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

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

**Parameters**

| Name     | Type                    | Mandatory| Description                           |
| ----------- | ------------------------ | ---- | -------------------------------|
| socketParam | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | Yes| **TCPSocket** or **UDPSocket** object.|
S
shawn_he 已提交
1600
| callback    | AsyncCallback\<void>      | Yes  | Callback used to return the result. If the **TCPSocket** or **UDPSocket** object is successfully bound to the current network, **error** is **undefined**. Otherwise, **error** is an error object.|
S
shawn_he 已提交
1601 1602 1603 1604 1605 1606 1607 1608 1609

**Error codes**

| ID| Error Message                       |
| ------- | -----------------------------  |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
S
shawn_he 已提交
1610 1611 1612 1613

**Example**

```js
S
shawn_he 已提交
1614
import socket from "@ohos.net.socket";
S
shawn_he 已提交
1615

S
shawn_he 已提交
1616
connection.getDefaultNet().then((netHandle) => {
S
shawn_he 已提交
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632
  var tcp = socket.constructTCPSocketInstance();
  var udp = socket.constructUDPSocketInstance();
  let socketType = "TCPSocket";
  if (socketType == "TCPSocket") {
    tcp.bind({
      address: '192.168.xx.xxx', port: 8080, family: 1
    }, error => {
      if (error) {
        console.log('bind fail');
        return;
      }
      netHandle.bindSocket(tcp, (error, data) => {
        if (error) {
          console.log(JSON.stringify(error));
        } else {
          console.log(JSON.stringify(data));
S
shawn_he 已提交
1633
        }
S
shawn_he 已提交
1634 1635 1636 1637 1638
      })
    })
  } else {
    let callback = value => {
      console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo);
S
shawn_he 已提交
1639
    }
S
shawn_he 已提交
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659
    udp.on('message', callback);
    udp.bind({
      address: '192.168.xx.xxx', port: 8080, family: 1
    }, error => {
      if (error) {
        console.log('bind fail');
        return;
      }
      udp.on('message', (data) => {
        console.log(JSON.stringify(data))
      });
      netHandle.bindSocket(udp, (error, data) => {
        if (error) {
          console.log(JSON.stringify(error));
        } else {
          console.log(JSON.stringify(data));
        }
      })
    })
  }
S
shawn_he 已提交
1660
})
S
shawn_he 已提交
1661 1662
```

S
shawn_he 已提交
1663
### bindSocket<sup>9+</sup>
S
shawn_he 已提交
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680

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

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

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

**Parameters**

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

**Return value**

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

S
shawn_he 已提交
1683 1684 1685 1686 1687 1688 1689 1690 1691
**Error codes**

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

S
shawn_he 已提交
1692 1693 1694
**Example**

```js
S
shawn_he 已提交
1695
import socket from "@ohos.net.socket";
S
shawn_he 已提交
1696

S
shawn_he 已提交
1697
connection.getDefaultNet().then((netHandle) => {
S
shawn_he 已提交
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
  var tcp = socket.constructTCPSocketInstance();
  var udp = socket.constructUDPSocketInstance();
  let socketType = "TCPSocket";
  if (socketType == "TCPSocket") {
    tcp.bind({
      address: '192.168.xx.xxx', port: 8080, family: 1
    }, error => {
      if (error) {
        console.log('bind fail');
        return;
      }
      netHandle.bindSocket(tcp).then((data) => {
        console.log(JSON.stringify(data));
      }).catch(error => {
        console.log(JSON.stringify(error));
      })
    })
  } else {
    let callback = value => {
      console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo);
S
shawn_he 已提交
1718
    }
S
shawn_he 已提交
1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
    udp.on('message', callback);
    udp.bind({
      address: '192.168.xx.xxx', port: 8080, family: 1
    }, error => {
      if (error) {
        console.log('bind fail');
        return;
      }
      udp.on('message', (data) => {
        console.log(JSON.stringify(data));
      })
      netHandle.bindSocket(udp).then((data) => {
        console.log(JSON.stringify(data));
      }).catch(error => {
        console.log(JSON.stringify(error));
      })
    })
  }
S
shawn_he 已提交
1737
})
S
shawn_he 已提交
1738 1739
```

S
shawn_he 已提交
1740
### getAddressesByName<sup>8+</sup>
S
shawn_he 已提交
1741 1742 1743 1744 1745

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 已提交
1746
**Required permissions**: ohos.permission.INTERNET
S
shawn_he 已提交
1747

S
shawn_he 已提交
1748 1749 1750 1751
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

S
shawn_he 已提交
1752 1753 1754 1755
| Name  | Type                                             | Mandatory| Description                                                        |
| -------- | ------------------------------------------------- | ---- | ------------------------------------------------------------ |
| host     | string                                            | Yes  | Host name to resolve.                                          |
| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | Yes  | Callback used to return the result. If all IP addresses are successfully obtained, **error** is **undefined**, and **data** is the list of all obtained IP addresses. Otherwise, **error** is an error object.|
S
shawn_he 已提交
1756 1757 1758 1759 1760 1761 1762 1763 1764 1765

**Error codes**

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

**Example**

S
shawn_he 已提交
1769
```js
S
shawn_he 已提交
1770
connection.getDefaultNet().then(function (netHandle) {
S
shawn_he 已提交
1771 1772 1773 1774 1775
  let host = "xxxx";
  netHandle.getAddressesByName(host, function (error, data) {
    console.log(JSON.stringify(error))
    console.log(JSON.stringify(data))
  })
S
shawn_he 已提交
1776 1777 1778
})
```

S
shawn_he 已提交
1779
### getAddressesByName<sup>8+</sup>
S
shawn_he 已提交
1780 1781 1782 1783 1784

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 已提交
1785
**Required permissions**: ohos.permission.INTERNET
S
shawn_he 已提交
1786

S
shawn_he 已提交
1787 1788 1789 1790 1791 1792
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

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

S
shawn_he 已提交
1795
**Return value**
S
shawn_he 已提交
1796 1797 1798 1799 1800

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

S
shawn_he 已提交
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810
**Error codes**

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

S
shawn_he 已提交
1811 1812
**Example**

S
shawn_he 已提交
1813
```js
S
shawn_he 已提交
1814
connection.getDefaultNet().then(function (netHandle) {
S
shawn_he 已提交
1815 1816 1817 1818
  let host = "xxxx";
  netHandle.getAddressesByName(host).then(function (data) {
    console.log(JSON.stringify(data))
  })
S
shawn_he 已提交
1819 1820 1821
})
```

S
shawn_he 已提交
1822
### getAddressByName<sup>8+</sup>
S
shawn_he 已提交
1823 1824 1825 1826 1827

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 已提交
1828
**Required permissions**: ohos.permission.INTERNET
S
shawn_he 已提交
1829

S
shawn_he 已提交
1830 1831 1832 1833
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

S
shawn_he 已提交
1834 1835 1836 1837
| Name  | Type                                     | Mandatory| Description                                                        |
| -------- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
| host     | string                                    | Yes  | Host name to resolve.                                          |
| callback | AsyncCallback\<[NetAddress](#netaddress)> | Yes  | Callback used to return the result. If the first IP address is obtained successfully, **error** is **undefined**, and **data** is the first obtained IP address. Otherwise, **error** is an error object.|
S
shawn_he 已提交
1838 1839 1840 1841 1842 1843 1844 1845 1846 1847

**Error codes**

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

**Example**

S
shawn_he 已提交
1851
```js
S
shawn_he 已提交
1852
connection.getDefaultNet().then(function (netHandle) {
S
shawn_he 已提交
1853 1854 1855 1856 1857
  let host = "xxxx";
  netHandle.getAddressByName(host, function (error, data) {
    console.log(JSON.stringify(error))
    console.log(JSON.stringify(data))
  })
S
shawn_he 已提交
1858 1859 1860
})
```

S
shawn_he 已提交
1861
### getAddressByName<sup>8+</sup>
S
shawn_he 已提交
1862 1863 1864 1865 1866

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 已提交
1867
**Required permissions**: ohos.permission.INTERNET
S
shawn_he 已提交
1868

S
shawn_he 已提交
1869 1870 1871 1872 1873 1874
**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

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

S
shawn_he 已提交
1877
**Return value**
S
shawn_he 已提交
1878 1879 1880 1881 1882

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

S
shawn_he 已提交
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892
**Error codes**

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

S
shawn_he 已提交
1893 1894
**Example**

S
shawn_he 已提交
1895
```js
S
shawn_he 已提交
1896
connection.getDefaultNet().then(function (netHandle) {
S
shawn_he 已提交
1897 1898 1899 1900
  let host = "xxxx";
  netHandle.getAddressByName(host).then(function (data) {
    console.log(JSON.stringify(data))
  })
S
shawn_he 已提交
1901 1902 1903
})
```

S
shawn_he 已提交
1904
## NetCap<sup>8+</sup>
S
shawn_he 已提交
1905

S
shawn_he 已提交
1906
Defines the network capability.
S
shawn_he 已提交
1907 1908 1909

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

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

S
shawn_he 已提交
1918
## NetBearType<sup>8+</sup>
S
shawn_he 已提交
1919

S
shawn_he 已提交
1920
Enumerates network types.
S
shawn_he 已提交
1921 1922 1923

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

S
shawn_he 已提交
1924 1925 1926 1927 1928
| Name        | Value  | Description       |
| --------------- | ---- | ----------- |
| BEARER_CELLULAR | 0    | Cellular network. |
| BEARER_WIFI     | 1    | Wi-Fi network.|
| BEARER_ETHERNET | 3 | Ethernet network.|
S
shawn_he 已提交
1929

S
shawn_he 已提交
1930
## HttpProxy<sup>10+</sup>
S
shawn_he 已提交
1931

S
shawn_he 已提交
1932
Represents the HTTP proxy configuration.
S
shawn_he 已提交
1933 1934 1935

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

S
shawn_he 已提交
1936 1937 1938 1939
| Name   | Type  | Mandatory| Description                     |
| ------ | ------ | --- |------------------------- |
| host  | string | No |  Host name of the proxy server.|
| port  | number | No |  Host port.|
S
shawn_he 已提交
1940
| exclusionList  | Array<string> | No | List of the names of hosts that do not use a proxy. Host names can be domain names, IP addresses, or wildcards. The detailed matching rules are as follows:<br>- Domain name matching:<br>  - Exact match: The host name of the proxy server exactly matches any host name in the list.<br>  - Partial match: The host name of the proxy server contains any host name in the list.<br>For example, if **ample.com** is set in the host name list, **ample.com**, **www.ample.com**, and **ample.com:80** are matched, and **www.example.com** and **ample.com.org** are not matched.<br>- IP address matching: The host name of the proxy server exactly matches any IP address in the list.<br>- Both the domain name and IP address are added to the list for matching.<br>- A single asterisk (*) is the only valid wildcard. If the list contains only wildcards, the wildcards match all host names; that is, the HTTP proxy is disabled. A wildcard can only be added independently. It cannot be added to the list together with other domain names or IP addresses. Otherwise, the wildcard does not take effect.<br>- Host names are case insensitive.<br>- Protocol prefixes such as **http** and **https** are ignored during matching.|
S
shawn_he 已提交
1941

S
shawn_he 已提交
1942
## NetSpecifier<sup>8+</sup>
S
shawn_he 已提交
1943

S
shawn_he 已提交
1944
Provides an instance that bears data network capabilities.
S
shawn_he 已提交
1945 1946 1947

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

S
shawn_he 已提交
1948 1949 1950 1951 1952
| 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 已提交
1953
## NetCapabilities<sup>8+</sup>
S
shawn_he 已提交
1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964

Defines the network capability set.

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

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

S
shawn_he 已提交
1966
## ConnectionProperties<sup>8+</sup>
S
shawn_he 已提交
1967 1968 1969 1970 1971

Defines the network connection properties.

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

S
shawn_he 已提交
1972 1973 1974 1975 1976 1977 1978 1979
| Name          | Type                              | Mandatory|  Description            |
| ------------- | ---------------------------------- | ----|---------------- |
| interfaceName | string                             | Yes|Network interface card (NIC) name.      |
| domains       | string                             | Yes|Domain. The default value is **""**.|
| linkAddresses | Array\<[LinkAddress](#linkaddress)> | Yes|Link information.      |
| routes        | Array\<[RouteInfo](#routeinfo)>     | Yes|Route information.      |
| dnses     | Array\<[NetAddress](#netaddress)> | Yes|Network address. For details, see [NetAddress](#netaddress).|
| mtu           | number                             | Yes|Maximum transmission unit (MTU).  |
S
shawn_he 已提交
1980

S
shawn_he 已提交
1981
## RouteInfo<sup>8+</sup>
S
shawn_he 已提交
1982

S
shawn_he 已提交
1983
Defines network route information.
S
shawn_he 已提交
1984 1985 1986

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

S
shawn_he 已提交
1987 1988 1989 1990 1991 1992 1993
| Name          | Type                       | Mandatory|Description            |
| -------------- | --------------------------- | --- |---------------- |
| interface      | string                      | Yes|NIC name.      |
| destination    | [LinkAddress](#linkaddress) | Yes|Destination address.      |
| gateway        | [NetAddress](#netaddress)   | Yes|Gateway address.      |
| hasGateway     | boolean                     | Yes|Whether a gateway is present.    |
| isDefaultRoute | boolean                     | Yes|Whether the route is the default route.|
S
shawn_he 已提交
1994

S
shawn_he 已提交
1995
## LinkAddress<sup>8+</sup>
S
shawn_he 已提交
1996

S
shawn_he 已提交
1997
Defines network link information.
S
shawn_he 已提交
1998 1999 2000

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

S
shawn_he 已提交
2001 2002 2003 2004
| Name       | Type                     | Mandatory|Description                |
| ------------ | ----------------------- |---- |-------------------- |
| address      | [NetAddress](#netaddress) | Yes| Link address.          |
| prefixLength | number                    | Yes|Length of the link address prefix.|
S
shawn_he 已提交
2005

S
shawn_he 已提交
2006
## NetAddress<sup>8+</sup>
S
shawn_he 已提交
2007

S
shawn_he 已提交
2008
Defines a network address.
S
shawn_he 已提交
2009 2010 2011

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

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