js-apis-net-connection.md 32.9 KB
Newer Older
Z
zengyawen 已提交
1
# 网络连接管理
2

C
clevercong 已提交
3
网络连接管理提供管理网络一些基础能力,包括获取默认激活的数据网络、获取所有激活数据网络列表、开启关闭飞行模式、获取网络能力信息等功能。
C
clevercong 已提交
4

5 6
> **说明:**
>
Z
zengyawen 已提交
7
> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8 9 10

## 导入模块

Z
zengyawen 已提交
11
```js
12 13 14 15 16 17 18
import connection from '@ohos.net.connection'
```

## connection.getDefaultNet

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

Z
zengyawen 已提交
19
获取默认激活的数据网络,使用callback方式作为异步方法。
20 21 22 23 24 25 26

**需要权限**:ohos.permission.GET_NETWORK_INFO

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
27 28 29
| 参数名   | 类型                                    | 必填 | 说明       |
| -------- | --------------------------------------- | ---- | ---------- |
| callback | AsyncCallback\<[NetHandle](#nethandle)> | 是   | 回调函数。 |
30 31 32

**示例:**

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

## connection.getDefaultNet

getDefaultNet(): Promise\<NetHandle>

Z
zengyawen 已提交
44
获取默认激活的数据网络,使用Promise方式作为异步方法。
45 46 47 48 49 50 51

**需要权限**:ohos.permission.GET_NETWORK_INFO

**系统能力**:SystemCapability.Communication.NetManager.Core

**返回值:**

Z
zengyawen 已提交
52 53 54
| 类型                              | 说明                                  |
| --------------------------------- | ------------------------------------- |
| Promise\<[NetHandle](#nethandle)> | 以Promise形式返回默认激活的数据网络。 |
55 56 57

**示例:**

Z
zengyawen 已提交
58
```js
59 60 61 62 63 64 65 66 67
connection.getDefaultNet().then(function (netHandle) {
    console.log(JSON.stringify(netHandle))
})
```

## connection.hasDefaultNet

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

Z
zengyawen 已提交
68
检查默认数据网络是否被激活,使用callback方式作为异步方法。
69 70 71 72 73

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
74 75 76
| 参数名   | 类型                    | 必填 | 说明                                   |
| -------- | ----------------------- | ---- | -------------------------------------- |
| callback | AsyncCallback\<boolean> | 是   | 回调函数,默认数据网络被激活返回true。 |
77 78 79

**示例:**

Z
zengyawen 已提交
80
```js
81 82
connection.hasDefaultNet(function (error, has) {
    console.log(JSON.stringify(error))
C
clevercong 已提交
83
    console.log('has: ' + has)
84 85 86 87 88 89 90
})
```

## connection.hasDefaultNet

hasDefaultNet(): Promise\<boolean>

Z
zengyawen 已提交
91
检查默认数据网络是否被激活,使用Promise方式作为异步方法。
92 93 94 95 96

**系统能力**:SystemCapability.Communication.NetManager.Core

**返回值:**

Z
zengyawen 已提交
97 98 99
| 类型              | 说明                                            |
| ----------------- | ----------------------------------------------- |
| Promise\<boolean> | 以Promise形式返回,默认数据网络被激活返回true。 |
100 101 102

**示例:**

Z
zengyawen 已提交
103
```js
104
connection.hasDefaultNet().then(function (has) {
C
clevercong 已提交
105
    console.log('has: ' + has)
106 107 108
})
```

C
clevercong 已提交
109 110 111 112 113 114
## connection.getAllNets

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

获取全部激活的数据网络列表,使用callback方式作为异步方法。

C
clevercong 已提交
115 116 117
**需要权限**:ohos.permission.GET_NETWORK_INFO

**系统能力**:SystemCapability.Communication.NetManager.Core
C
clevercong 已提交
118 119 120 121 122 123 124 125

**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;Array&lt;[NetHandle](#nethandle)&gt;&gt; | 是 | 回调函数。 |

**示例:**

Z
zengyawen 已提交
126
```js
C
clevercong 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139
connection.getAllNets(function (error, nets) {
    console.log(JSON.stringify(error))
    console.log(JSON.stringify(nets))
});
```


## connection.getAllNets

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

获取全部激活的数据网络列表,使用promise方式作为异步方法。

C
clevercong 已提交
140 141 142
**需要权限**:ohos.permission.GET_NETWORK_INFO

**系统能力**:SystemCapability.Communication.NetManager.Core
C
clevercong 已提交
143 144 145 146 147 148 149 150

**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;Array&lt;[NetHandle](#nethandle)&gt;&gt; | 以Promise形式返回激活的数据网络列表。 |

**示例:**

Z
zengyawen 已提交
151
```js
C
clevercong 已提交
152 153 154 155 156
connection.getAllNets().then(function (nets) {
    console.log(JSON.stringify(nets))
});
```

157 158 159 160
## connection.getConnectionProperties

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

Z
zengyawen 已提交
161
获取netHandle对应的网络的连接信息,使用callback方式作为异步方法。
162 163 164 165 166 167 168

**需要权限**:ohos.permission.GET_NETWORK_INFO

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
169 170 171 172
| 参数名    | 类型                                                         | 必填 | 说明             |
| --------- | ------------------------------------------------------------ | ---- | ---------------- |
| netHandle | [NetHandle](#nethandle)                                      | 是   | 数据网络的句柄。 |
| callback  | AsyncCallback\<[ConnectionProperties](#connectionproperties)> | 是   | 回调函数。       |
173 174 175

**示例:**

Z
zengyawen 已提交
176
```js
177 178 179 180 181 182 183 184 185 186 187 188
connection.getDefaultNet().then(function (netHandle) {
    connection.getConnectionProperties(netHandle, function (error, info) {
        console.log(JSON.stringify(error))
        console.log(JSON.stringify(info))
    })
})
```

## connection.getConnectionProperties

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

Z
zengyawen 已提交
189
获取netHandle对应的网络的连接信息,使用Promise方式作为异步方法。
190 191 192 193 194 195 196

**需要权限**:ohos.permission.GET_NETWORK_INFO

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
197 198 199
| 参数名    | 类型                    | 必填 | 说明             |
| --------- | ----------------------- | ---- | ---------------- |
| netHandle | [NetHandle](#nethandle) | 是   | 数据网络的句柄。 |
200 201 202

**返回值:**

Z
zengyawen 已提交
203 204 205
| 类型                                                    | 说明                              |
| ------------------------------------------------------- | --------------------------------- |
| Promise\<[ConnectionProperties](#connectionproperties)> | 以Promise形式返回网络的连接信息。 |
206 207 208

**示例:**

Z
zengyawen 已提交
209
```js
210 211 212 213 214 215 216 217 218 219 220
connection.getDefaultNet().then(function (netHandle) {
    connection.getConnectionProperties(netHandle).then(function (info) {
        console.log(JSON.stringify(info))
    })
})
```

## connection.getNetCapabilities

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

Z
zengyawen 已提交
221
获取netHandle对应的网络的能力信息,使用callback方式作为异步方法。
222 223 224 225 226 227 228

**需要权限**:ohos.permission.GET_NETWORK_INFO

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
229 230 231 232
| 参数名    | 类型                                                | 必填 | 说明             |
| --------- | --------------------------------------------------- | ---- | ---------------- |
| netHandle | [NetHandle](#nethandle)                             | 是   | 数据网络的句柄。 |
| callback  | AsyncCallback\<[NetCapabilities](#netcapabilities)> | 是   | 回调函数。       |
233 234 235

**示例:**

Z
zengyawen 已提交
236
```js
237 238 239 240 241 242 243 244 245 246 247 248
connection.getDefaultNet().then(function (netHandle) {
    connection.getNetCapabilities(netHandle, function (error, info) {
        console.log(JSON.stringify(error))
        console.log(JSON.stringify(info))
    })
})
```

## connection.getNetCapabilities

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

Z
zengyawen 已提交
249
获取netHandle对应的网络的能力信息,使用Promise方式作为异步方法。
250 251 252 253 254 255 256

**需要权限**:ohos.permission.GET_NETWORK_INFO

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
257 258 259
| 参数名    | 类型                    | 必填 | 说明             |
| --------- | ----------------------- | ---- | ---------------- |
| netHandle | [NetHandle](#nethandle) | 是   | 数据网络的句柄。 |
260 261 262

**返回值:**

Z
zengyawen 已提交
263 264 265
| 类型                                          | 说明                              |
| --------------------------------------------- | --------------------------------- |
| Promise\<[NetCapabilities](#netcapabilities)> | 以Promise形式返回网络的能力信息。 |
266 267 268

**示例:**

Z
zengyawen 已提交
269
```js
270 271 272 273 274 275 276
connection.getDefaultNet().then(function (netHandle) {
    connection.getNetCapabilities(netHandle).then(function (info) {
        console.log(JSON.stringify(info))
    })
})
```

C
clevercong 已提交
277 278 279 280 281 282
## connection.reportNetConnected

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

报告网络状态已连接,使用callback方式作为异步方法。

C
clevercong 已提交
283 284 285
**需要权限**:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET

**系统能力**:SystemCapability.Communication.NetManager.Core
C
clevercong 已提交
286 287 288 289 290 291 292 293 294

**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 |

**示例:**

Z
zengyawen 已提交
295
```js
C
clevercong 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308 309
connection.getDefaultNet().then(function (netHandle) {
    connection.reportNetConnected(netHandle, function (error) {
        console.log(JSON.stringify(error))
    });
});
```


## connection.reportNetConnected

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

报告网络状态已连接,使用promise方式作为异步方法。

C
clevercong 已提交
310 311 312
**需要权限**:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET

**系统能力**:SystemCapability.Communication.NetManager.Core
C
clevercong 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325

**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 |

**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 以Promise形式返回执行结果。 |

**示例:**

Z
zengyawen 已提交
326
```js
C
clevercong 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339 340
connection.getDefaultNet().then(function (netHandle) {
    connection.reportNetConnected(netHandle).then(function () {
        console.log(`report success`)
    });
});
```


## connection.reportNetDisconnected

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

报告网络状态已断开,使用callback方式作为异步方法。

C
clevercong 已提交
341 342 343
**需要权限**:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET

**系统能力**:SystemCapability.Communication.NetManager.Core
C
clevercong 已提交
344 345 346 347 348 349 350 351 352

**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 |

**示例:**

Z
zengyawen 已提交
353
```js
C
clevercong 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366 367
connection.getDefaultNet().then(function (netHandle) {
    connection.reportNetDisconnected(netHandle, function (error) {
        console.log(JSON.stringify(error))
    });
});
```


## connection.reportNetDisconnected

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

报告网络状态已断开,使用promise方式作为异步方法。

C
clevercong 已提交
368 369 370
**需要权限**:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET

**系统能力**:SystemCapability.Communication.NetManager.Core
C
clevercong 已提交
371 372 373 374 375 376 377 378 379 380 381 382 383

**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 |

**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 以Promise形式返回执行结果。 |

**示例:**

Z
zengyawen 已提交
384
```js
C
clevercong 已提交
385 386 387 388 389 390 391
connection.getDefaultNet().then(function (netHandle) {
    connection.reportNetDisconnected(netHandle).then(function () {
        console.log(`report success`)
    });
});
```

392 393 394 395
## connection.getAddressesByName

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

Z
zengyawen 已提交
396
使用默认网络解析主机名以获取所有IP地址,使用callback方式作为异步方法。
397 398 399 400 401 402 403

**需要权限**:ohos.permission.GET_NETWORK_INFO

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
404 405 406 407
| 参数名   | 类型                                              | 必填 | 说明               |
| -------- | ------------------------------------------------- | ---- | ------------------ |
| host     | string                                            | 是   | 需要解析的主机名。 |
| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | 是   | 回调函数。         |
408 409 410

**示例:**

Z
zengyawen 已提交
411
```js
C
clevercong 已提交
412 413 414 415
let host = "xxxx";
connection.getAddressesByName(host, function (error, addresses) {
    console.log(JSON.stringify(error))
    console.log(JSON.stringify(addresses))
416 417 418 419 420
})
```

## connection.getAddressesByName

C
clevercong 已提交
421
getAddressesByName(host: string): Promise\<Array\<NetAddress>>
422

Z
zengyawen 已提交
423
使用默认网络解析主机名以获取所有IP地址,使用Promise方式作为异步方法。
424 425 426 427 428 429 430

**需要权限**:ohos.permission.GET_NETWORK_INFO

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
431 432 433
| 参数名 | 类型   | 必填 | 说明               |
| ------ | ------ | ---- | ------------------ |
| host   | string | 是   | 需要解析的主机名。 |
434 435 436

**返回值:**

Z
zengyawen 已提交
437 438 439
| 类型                                        | 说明                          |
| ------------------------------------------- | ----------------------------- |
| Promise\<Array\<[NetAddress](#netaddress)>> | 以Promise形式返回所有IP地址。 |
440 441 442

**示例:**

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

C
clevercong 已提交
450 451 452 453 454 455 456

## connection.enableAirplaneMode

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

开启飞行模式,使用callback方式作为异步方法。

C
clevercong 已提交
457 458
该接口为系统接口。

C
clevercong 已提交
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

| 参数名   | 类型                                              | 必填 | 说明               |
| -------- | ------------------------------------------------- | ---- | ------------------ |
| callback | AsyncCallback\<void> | 是   | 回调函数。         |

**示例:**

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

## connection.enableAirplaneMode

enableAirplaneMode(): Promise\<void>

开启飞行模式,使用Promise方式作为异步方法。

C
clevercong 已提交
481 482
该接口为系统接口。

C
clevercong 已提交
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
**系统能力**:SystemCapability.Communication.NetManager.Core

**返回值:**

| 类型                                        | 说明                          |
| ------------------------------------------- | ----------------------------- |
| Promise\<void> | 以Promise形式返回结果。 |

**示例:**

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


## connection.disableAirplaneMode

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

关闭飞行模式,使用callback方式作为异步方法。

C
clevercong 已提交
506 507
该接口为系统接口。

C
clevercong 已提交
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

| 参数名   | 类型                                              | 必填 | 说明               |
| -------- | ------------------------------------------------- | ---- | ------------------ |
| callback | AsyncCallback\<void> | 是   | 回调函数。         |

**示例:**

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

## connection.disableAirplaneMode

disableAirplaneMode(): Promise\<void>

关闭飞行模式,使用Promise方式作为异步方法。

C
clevercong 已提交
530 531
该接口为系统接口。

C
clevercong 已提交
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
**系统能力**:SystemCapability.Communication.NetManager.Core

**返回值:**

| 类型                                        | 说明                          |
| ------------------------------------------- | ----------------------------- |
| Promise\<void> | 以Promise形式返回结果。 |

**示例:**

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


549 550 551 552
## connection.createNetConnection

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

M
maosiping 已提交
553
获取一个netSpecifier指定的网络的句柄。
554 555 556 557 558

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
559 560 561 562
| 参数名       | 类型                          | 必填 | 说明                                                         |
| ------------ | ----------------------------- | ---- | ------------------------------------------------------------ |
| netSpecifier | [NetSpecifier](#netspecifier) | 否   | 指定网络的各项特征,不指定则关注默认网络。                   |
| timeout      | number                        | 否   | 获取netSpecifier指定的网络时的超时时间,仅netSpecifier存在时生效。 |
563 564 565

**返回值:**

Z
zengyawen 已提交
566 567 568
| 类型                            | 说明                 |
| ------------------------------- | -------------------- |
| [NetConnection](#netconnection) | 所关注的网络的句柄。 |
569 570 571

**示例:**

Z
zengyawen 已提交
572
```js
573
// 关注默认网络
C
clevercong 已提交
574
let netConnection = connection.createNetConnection()
575 576

// 关注蜂窝网络
C
clevercong 已提交
577
let netConnectionCellular = connection.createNetConnection({
578
    netCapabilities: {
C
clevercong 已提交
579
        bearerTypes: [connection.NetBearType.BEARER_CELLULAR]
580 581 582 583
    }
})
```

M
maosiping 已提交
584
## NetConnection
585

Z
zengyawen 已提交
586
网络连接的句柄。
587

M
maosiping 已提交
588
### on('netAvailable')
589 590 591

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

C
clevercong 已提交
592
订阅网络可用事件。
593 594 595 596 597

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
598 599
| 参数名   | 类型                               | 必填 | 说明                                                         |
| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ |
C
clevercong 已提交
600
| type     | string                             | 是   | 订阅事件,固定为'netAvailable'。<br>netAvailable:数据网络可用事件。 |
Z
zengyawen 已提交
601
| callback | Callback\<[NetHandle](#nethandle)> | 是   | 回调函数。                                                   |
602 603 604

**示例:**

Z
zengyawen 已提交
605
```js
C
clevercong 已提交
606
netConnection.on('netAvailable', function (data) {
607 608 609 610
    console.log(JSON.stringify(data))
})
```

M
maosiping 已提交
611
### on('netCapabilitiesChange')
612 613 614

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

C
clevercong 已提交
615
订阅网络能力变化事件。
616 617 618 619 620

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
621 622
| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
C
clevercong 已提交
623
| type     | string                                                       | 是   | 订阅事件,固定为'netCapabilitiesChange'。<br/>netCapabilitiesChange:网络能力变化事件。 |
Z
zengyawen 已提交
624
| callback | Callback<{ netHandle: [NetHandle](#nethandle), netCap: [NetCapabilities](#netcapabilities) }> | 是   | 回调函数。                                                   |
625 626 627

**示例:**

Z
zengyawen 已提交
628
```js
C
clevercong 已提交
629
netConnection.on('netCapabilitiesChange', function (data) {
630 631 632 633
    console.log(JSON.stringify(data))
})
```

M
maosiping 已提交
634
### on('netConnectionPropertiesChange')
635 636 637

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

C
clevercong 已提交
638
订阅网络连接信息变化事件。
639 640 641 642 643

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
644 645
| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
C
clevercong 已提交
646
| type     | string                                                       | 是   | 订阅事件,固定为'netConnectionPropertiesChange'。<br/>netConnectionPropertiesChange:网络连接信息变化事件。 |
Z
zengyawen 已提交
647
| callback | Callback<{ netHandle: [NetHandle](#nethandle), connectionProperties: [ConnectionProperties](#connectionproperties) }> | 是   | 回调函数。                                                   |
648 649 650

**示例:**

Z
zengyawen 已提交
651
```js
C
clevercong 已提交
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
netConnection.on('netConnectionPropertiesChange', function (data) {
    console.log(JSON.stringify(data))
})
```

### on('netBlockStatusChange')

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

订阅网络阻塞状态事件,使用callback方式作为异步方法。

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| type     | string                                                       | 是   | 订阅事件,固定为'netBlockStatusChange'。<br/>netBlockStatusChange:网络阻塞状态事件。 |
| callback | Callback&lt;{&nbsp;netHandle:&nbsp;[NetHandle](#nethandle),&nbsp;blocked:&nbsp;boolean&nbsp;}&gt; | 是   | 回调函数。                                                   |

**示例:**

Z
zengyawen 已提交
674
```js
C
clevercong 已提交
675
netConnection.on('netBlockStatusChange', function (data) {
676 677 678 679
    console.log(JSON.stringify(data))
})
```

M
maosiping 已提交
680
### on('netLost')
681 682 683

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

C
clevercong 已提交
684
订阅网络丢失事件。
685 686 687 688 689

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
690 691
| 参数名   | 类型                               | 必填 | 说明                                                         |
| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ |
C
clevercong 已提交
692
| type     | string                             | 是   | 订阅事件,固定为'netLost'。<br/>netLost:网络严重中断或正常断开事件。 |
Z
zengyawen 已提交
693
| callback | Callback\<[NetHandle](#nethandle)> | 是   | 回调函数。                                                   |
694 695 696

**示例:**

Z
zengyawen 已提交
697
```js
C
clevercong 已提交
698 699
let netConnection1 = connection.createNetConnection()
netConnection1.on('netLost', function (data) {
700 701 702 703
    console.log(JSON.stringify(data))
})
```

M
maosiping 已提交
704
### on('netUnavailable')
705 706 707

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

C
clevercong 已提交
708
订阅网络不可用事件。
709 710 711 712 713

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
714 715
| 参数名   | 类型            | 必填 | 说明                                                         |
| -------- | --------------- | ---- | ------------------------------------------------------------ |
C
clevercong 已提交
716
| type     | string          | 是   | 订阅事件,固定为'netUnavailable'。<br/>netUnavailable:网络不可用事件。 |
Z
zengyawen 已提交
717
| callback | Callback\<void> | 是   | 回调函数。                                                   |
718 719 720

**示例:**

Z
zengyawen 已提交
721
```js
C
clevercong 已提交
722
netConnection.on('netUnavailable', function (data) {
723 724 725 726
    console.log(JSON.stringify(data))
})
```

M
maosiping 已提交
727
### register
728 729 730

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

C
clevercong 已提交
731
订阅指定网络状态变化的通知。
732 733 734 735 736 737 738

**需要权限**:ohos.permission.GET_NETWORK_INFO

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
739 740 741
| 参数名   | 类型                 | 必填 | 说明       |
| -------- | -------------------- | ---- | ---------- |
| callback | AsyncCallback\<void> | 是   | 回调函数。 |
742 743 744

**示例:**

Z
zengyawen 已提交
745
```js
C
clevercong 已提交
746
netConnection.register(function (error) {
747 748 749 750
    console.log(JSON.stringify(error))
})
```

M
maosiping 已提交
751
### unregister
752 753 754

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

C
clevercong 已提交
755
取消订阅默认网络状态变化的通知。
756 757 758 759 760

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
761 762 763
| 参数名   | 类型                 | 必填 | 说明       |
| -------- | -------------------- | ---- | ---------- |
| callback | AsyncCallback\<void> | 是   | 回调函数。 |
764 765 766

**示例:**

Z
zengyawen 已提交
767
```js
C
clevercong 已提交
768
netConnection.unregister(function (error) {
769 770 771 772
    console.log(JSON.stringify(error))
})
```

M
maosiping 已提交
773
## NetHandle
774

Z
zengyawen 已提交
775
数据网络的句柄。
776

Z
zengyawen 已提交
777 778
在调用NetHandle的方法之前,需要先获取NetHandle对象。

C
clevercong 已提交
779 780
**系统能力**:SystemCapability.Communication.NetManager.Core

Z
zengyawen 已提交
781 782
### 属性

C
clevercong 已提交
783 784 785
| 参数名 | 类型   | 说明                      |
| ------ | ------ | ------------------------- |
| netId  | number | 网络ID,必须大于等于100。 |
786

M
maosiping 已提交
787
### getAddressesByName
788 789 790

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

Z
zengyawen 已提交
791
使用对应网络解析主机名以获取所有IP地址,使用callback方式作为异步方法。
792

C
clevercong 已提交
793 794
**需要权限**:ohos.permission.GET_NETWORK_INFO

795 796 797 798
**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
799 800 801 802
| 参数名   | 类型                                              | 必填 | 说明               |
| -------- | ------------------------------------------------- | ---- | ------------------ |
| host     | string                                            | 是   | 需要解析的主机名。 |
| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | 是   | 回调函数           |
803 804 805

**示例:**

Z
zengyawen 已提交
806
```js
807
connection.getDefaultNet().then(function (netHandle) {
C
clevercong 已提交
808 809
    let host = "xxxx";
    netHandle.getAddressesByName(host, function (error, addresses) {
810
        console.log(JSON.stringify(error))
C
clevercong 已提交
811
        console.log(JSON.stringify(addresses))
812 813 814 815
    })
})
```

M
maosiping 已提交
816
### getAddressesByName
817

C
clevercong 已提交
818
getAddressesByName(host: string): Promise\<Array\<NetAddress>>
819

Z
zengyawen 已提交
820
使用对应网络解析主机名以获取所有IP地址,使用Promise方式作为异步方法。
821

C
clevercong 已提交
822 823
**需要权限**:ohos.permission.GET_NETWORK_INFO

824 825 826 827
**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
828 829 830
| 参数名 | 类型   | 必填 | 说明               |
| ------ | ------ | ---- | ------------------ |
| host   | string | 是   | 需要解析的主机名。 |
831 832 833

**返回值:**

Z
zengyawen 已提交
834 835 836
| 类型                                        | 说明                          |
| ------------------------------------------- | ----------------------------- |
| Promise\<Array\<[NetAddress](#netaddress)>> | 以Promise形式返回所有IP地址。 |
837 838 839

**示例:**

Z
zengyawen 已提交
840
```js
841
connection.getDefaultNet().then(function (netHandle) {
C
clevercong 已提交
842 843 844
    let host = "xxxx";
    netHandle.getAddressesByName(host).then(function (addresses) {
        console.log(JSON.stringify(addresses))
845 846 847 848
    })
})
```

M
maosiping 已提交
849
### getAddressByName
850 851 852

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

Z
zengyawen 已提交
853
使用对应网络解析主机名以获取第一个IP地址,使用callback方式作为异步方法。
854

C
clevercong 已提交
855 856
**需要权限**:ohos.permission.GET_NETWORK_INFO

857 858 859 860
**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
861 862 863 864
| 参数名   | 类型                                      | 必填 | 说明               |
| -------- | ----------------------------------------- | ---- | ------------------ |
| host     | string                                    | 是   | 需要解析的主机名。 |
| callback | AsyncCallback\<[NetAddress](#netaddress)> | 是   | 回调函数。         |
865 866 867

**示例:**

Z
zengyawen 已提交
868
```js
869
connection.getDefaultNet().then(function (netHandle) {
C
clevercong 已提交
870 871
    let host = "xxxx";
    netHandle.getAddressByName(host, function (error, address) {
872
        console.log(JSON.stringify(error))
C
clevercong 已提交
873
        console.log(JSON.stringify(address))
874 875 876 877
    })
})
```

M
maosiping 已提交
878
### getAddressByName
879

C
clevercong 已提交
880
getAddressByName(host: string): Promise\<NetAddress>
881

Z
zengyawen 已提交
882
使用对应网络解析主机名以获取第一个IP地址,使用Promise方式作为异步方法。
883

C
clevercong 已提交
884 885
**需要权限**:ohos.permission.GET_NETWORK_INFO

886 887 888 889
**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
890 891 892
| 参数名 | 类型   | 必填 | 说明               |
| ------ | ------ | ---- | ------------------ |
| host   | string | 是   | 需要解析的主机名。 |
893 894 895

**返回值:**

Z
zengyawen 已提交
896 897 898
| 类型                                | 说明                            |
| ----------------------------------- | ------------------------------- |
| Promise\<[NetAddress](#netaddress)> | 以Promise形式返回第一个IP地址。 |
899 900 901

**示例:**

Z
zengyawen 已提交
902
```js
903
connection.getDefaultNet().then(function (netHandle) {
C
clevercong 已提交
904 905 906
    let host = "xxxx";
    netHandle.getAddressByName(host).then(function (address) {
        console.log(JSON.stringify(address))
907 908 909 910
    })
})
```

M
maosiping 已提交
911
## NetSpecifier
912

Z
zengyawen 已提交
913
提供承载数据网络能力的实例。
914

C
clevercong 已提交
915 916 917
**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.NetManager.Core。

| 参数名                  | 类型                                | 说明                                                         |
Z
zengyawen 已提交
918 919 920
| ----------------------- | ----------------------------------- | ------------------------------------------------------------ |
| netCapabilities         | [NetCapabilities](#netcapabilities) | 存储数据网络的传输能力和承载类型。                           |
| bearerPrivateIdentifier | string                              | 网络标识符,WIFI网络的标识符是"wifi",蜂窝网络的标识符是"slot0"(对应SIM卡1)。 |
921

M
maosiping 已提交
922
## NetCapabilities
923

M
maosiping 已提交
924
网络的能力集。
925

C
clevercong 已提交
926 927 928
**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.NetManager.Core。

| 参数名                | 类型                               | 说明                     |
Z
zengyawen 已提交
929 930 931 932
| --------------------- | ---------------------------------- | ------------------------ |
| linkUpBandwidthKbps   | number                             | 上行(设备到网络)带宽。 |
| linkDownBandwidthKbps | number                             | 下行(网络到设备)带宽。 |
| networkCap            | Array<[NetCap](#netcap)>           | 网络具体能力。           |
Z
zengyawen 已提交
933
| bearerTypes           | Array<[NetBearType](#netbeartype)> | 网络类型。               |
934

M
maosiping 已提交
935
## NetCap
936

M
maosiping 已提交
937
网络具体能力。
938

C
clevercong 已提交
939 940 941
**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.NetManager.Core。

| 参数名                  | 值   | 说明                   |
Z
zengyawen 已提交
942
| ------------------------ | ---- | ---------------------- |
C
clevercong 已提交
943 944
| NET_CAPABILITY_MMS | 0 | 表示网络可以访问运营商的MMSC(Multimedia&nbsp;Message&nbsp;Service,多媒体短信服务)发送和接收彩信。 |
| NET_CAPABILITY_NOT_METERED | 11 | 表示网络流量未被计费。 |
Z
zengyawen 已提交
945
| NET_CAPABILITY_INTERNET  | 12   | 网络可以访问Internet。 |
C
clevercong 已提交
946
| NET_CAPABILITY_NOT_VPN | 15 | 表示网络不使用VPN(Virtual&nbsp;Private&nbsp;Network,虚拟专用网络)。 |
Z
zengyawen 已提交
947
| NET_CAPABILITY_VALIDATED | 16   | 网络可用。             |
948

M
maosiping 已提交
949
## NetBearType
950

M
maosiping 已提交
951
网络类型。
952

C
clevercong 已提交
953 954 955
**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.NetManager.Core。

| 名称         | 值   | 说明        |
Z
zengyawen 已提交
956 957 958
| --------------- | ---- | ----------- |
| BEARER_CELLULAR | 0    | 蜂窝网络。  |
| BEARER_WIFI     | 1    | Wi-Fi网络。 |
C
clevercong 已提交
959
| BEARER_ETHERNET | 3 | 以太网网络。 |
960

M
maosiping 已提交
961
## ConnectionProperties
962

M
maosiping 已提交
963
网络连接信息。
964

C
clevercong 已提交
965 966 967
**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.NetManager.Core。

| 参数名       | 类型                               | 说明             |
Z
zengyawen 已提交
968 969 970 971 972
| ------------- | ---------------------------------- | ---------------- |
| interfaceName | string                             | 网卡名称。       |
| domains       | string                             | 所属域,默认""。 |
| linkAddresses | Array<[LinkAddress](#linkaddress)> | 链路信息。       |
| routes        | Array<[RouteInfo](#routeinfo)>     | 路由信息。       |
C
clevercong 已提交
973
| dnses | Array&lt;[NetAddress](#netaddress)&gt; | 网络地址,参考[NetAddress](#netaddress)。 |
Z
zengyawen 已提交
974
| mtu           | number                             | 最大传输单元。   |
975

M
maosiping 已提交
976
## LinkAddress
977

M
maosiping 已提交
978
网络链路信息。
979

C
clevercong 已提交
980 981 982
**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.NetManager.Core。

| 参数名       | 类型                      | 说明                 |
Z
zengyawen 已提交
983 984 985
| ------------ | ------------------------- | -------------------- |
| address      | [NetAddress](#netaddress) | 链路地址。           |
| prefixLength | number                    | 链路地址前缀的长度。 |
986

M
maosiping 已提交
987
## RouteInfo
988

M
maosiping 已提交
989
网络路由信息。
990

C
clevercong 已提交
991 992 993
**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.NetManager.Core。

| 参数名         | 类型                        | 说明             |
Z
zengyawen 已提交
994 995 996 997 998 999
| -------------- | --------------------------- | ---------------- |
| interface      | string                      | 网卡名称。       |
| destination    | [LinkAddress](#linkaddress) | 目的地址。       |
| gateway        | [NetAddress](#netaddress)   | 网关地址。       |
| hasGateway     | boolean                     | 是否有网关。     |
| isDefaultRoute | boolean                     | 是否为默认路由。 |
1000

M
maosiping 已提交
1001
## NetAddress
1002

Z
zengyawen 已提交
1003
网络地址。
1004

C
clevercong 已提交
1005 1006 1007
**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.NetManager.Core。

| 参数名  | 类型   | 说明                           |
Z
zengyawen 已提交
1008 1009 1010 1011
| ------- | ------ | ------------------------------ |
| address | string | 地址。                         |
| family  | number | IPv4 = 1,IPv6 = 2,默认IPv4。 |
| port    | number | 端口,取值范围\[0, 65535]。    |