js-apis-net-connection.md 38.2 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

M
maosiping 已提交
19
获取默认激活的数据网络,使用callback方式作为异步方法。可以使用[getNetCapabilities](#connectiongetnetcapabilities)去获取网络的类型、拥有的能力等信息。
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>

M
maosiping 已提交
44
获取默认激活的数据网络,使用Promise方式作为异步方法。可以使用[getNetCapabilities](#connectiongetnetcapabilities)去获取网络的类型、拥有的能力等信息。
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
connection.getDefaultNet().then(function (netHandle) {
    console.log(JSON.stringify(netHandle))
})
```

F
fuchao 已提交
64 65 66 67
## connection.getDefaultNetSync

getDefaultNetSync(): NetHandle;

M
maosiping 已提交
68
使用同步方法获取默认激活的数据网络。可以使用[getNetCapabilities](#connectiongetnetcapabilities)去获取网络的类型、拥有的能力等信息。
F
fuchao 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

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

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

**返回值:**

| 类型      | 说明                               |
| --------- | ---------------------------------- |
| NetHandle | 以同步方式返回默认激活的数据网络。 |

**示例:**

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

86 87 88 89
## connection.hasDefaultNet

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

M
maosiping 已提交
90
检查默认数据网络是否被激活,使用callback方式作为异步方法。如果有默认数据网路,可以使用[getDefaultNet](#connectiongetdefaultnet)去获取。
91

F
fuchao 已提交
92 93
**需要权限**:ohos.permission.GET_NETWORK_INFO

94 95 96 97
**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
98 99 100
| 参数名   | 类型                    | 必填 | 说明                                   |
| -------- | ----------------------- | ---- | -------------------------------------- |
| callback | AsyncCallback\<boolean> | 是   | 回调函数,默认数据网络被激活返回true。 |
101 102 103

**示例:**

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

## connection.hasDefaultNet

hasDefaultNet(): Promise\<boolean>

M
maosiping 已提交
115
检查默认数据网络是否被激活,使用Promise方式作为异步方法。如果有默认数据网路,可以使用[getDefaultNet](#connectiongetdefaultnet)去获取。
116

F
fuchao 已提交
117 118
**需要权限**:ohos.permission.GET_NETWORK_INFO

119 120 121 122
**系统能力**:SystemCapability.Communication.NetManager.Core

**返回值:**

Z
zengyawen 已提交
123 124 125
| 类型              | 说明                                            |
| ----------------- | ----------------------------------------------- |
| Promise\<boolean> | 以Promise形式返回,默认数据网络被激活返回true。 |
126 127 128

**示例:**

Z
zengyawen 已提交
129
```js
130
connection.hasDefaultNet().then(function (has) {
C
clevercong 已提交
131
    console.log('has: ' + has)
132 133 134
})
```

C
clevercong 已提交
135 136 137 138
## connection.getAllNets

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

139
获取所有处于连接状态的网络列表,使用callback方式作为异步方法。
C
clevercong 已提交
140

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

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

**参数:**
146

C
clevercong 已提交
147 148 149 150 151 152
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;Array&lt;[NetHandle](#nethandle)&gt;&gt; | 是 | 回调函数。 |

**示例:**

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


## connection.getAllNets

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

165
获取所有处于连接状态的网络列表,使用promise方式作为异步方法。
C
clevercong 已提交
166

C
clevercong 已提交
167 168 169
**需要权限**:ohos.permission.GET_NETWORK_INFO

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

**返回值:**
172

C
clevercong 已提交
173 174 175 176 177 178
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;Array&lt;[NetHandle](#nethandle)&gt;&gt; | 以Promise形式返回激活的数据网络列表。 |

**示例:**

Z
zengyawen 已提交
179
```js
C
clevercong 已提交
180 181 182 183 184
connection.getAllNets().then(function (nets) {
    console.log(JSON.stringify(nets))
});
```

185 186 187 188
## connection.getConnectionProperties

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

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

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

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

**参数:**

Z
zengyawen 已提交
197 198 199 200
| 参数名    | 类型                                                         | 必填 | 说明             |
| --------- | ------------------------------------------------------------ | ---- | ---------------- |
| netHandle | [NetHandle](#nethandle)                                      | 是   | 数据网络的句柄。 |
| callback  | AsyncCallback\<[ConnectionProperties](#connectionproperties)> | 是   | 回调函数。       |
201 202 203

**示例:**

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

## connection.getConnectionProperties

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

Z
zengyawen 已提交
217
获取netHandle对应的网络的连接信息,使用Promise方式作为异步方法。
218 219 220 221 222 223 224

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

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

**参数:**

Z
zengyawen 已提交
225 226 227
| 参数名    | 类型                    | 必填 | 说明             |
| --------- | ----------------------- | ---- | ---------------- |
| netHandle | [NetHandle](#nethandle) | 是   | 数据网络的句柄。 |
228 229 230

**返回值:**

Z
zengyawen 已提交
231 232 233
| 类型                                                    | 说明                              |
| ------------------------------------------------------- | --------------------------------- |
| Promise\<[ConnectionProperties](#connectionproperties)> | 以Promise形式返回网络的连接信息。 |
234 235 236

**示例:**

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

## connection.getNetCapabilities

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

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

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

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

**参数:**

Z
zengyawen 已提交
257 258 259 260
| 参数名    | 类型                                                | 必填 | 说明             |
| --------- | --------------------------------------------------- | ---- | ---------------- |
| netHandle | [NetHandle](#nethandle)                             | 是   | 数据网络的句柄。 |
| callback  | AsyncCallback\<[NetCapabilities](#netcapabilities)> | 是   | 回调函数。       |
261 262 263

**示例:**

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

## connection.getNetCapabilities

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

Z
zengyawen 已提交
277
获取netHandle对应的网络的能力信息,使用Promise方式作为异步方法。
278 279 280 281 282 283 284

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

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

**参数:**

Z
zengyawen 已提交
285 286 287
| 参数名    | 类型                    | 必填 | 说明             |
| --------- | ----------------------- | ---- | ---------------- |
| netHandle | [NetHandle](#nethandle) | 是   | 数据网络的句柄。 |
288 289 290

**返回值:**

Z
zengyawen 已提交
291 292 293
| 类型                                          | 说明                              |
| --------------------------------------------- | --------------------------------- |
| Promise\<[NetCapabilities](#netcapabilities)> | 以Promise形式返回网络的能力信息。 |
294 295 296

**示例:**

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

C
clevercong 已提交
305 306 307 308
## connection.reportNetConnected

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

309
向网络管理报告网络处于可用状态,调用此接口说明应用程序认为网络的可用性(ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED)与网络管理不一致。
310
使用callback方式作为异步方法。
C
clevercong 已提交
311

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

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

**参数:**
317

C
clevercong 已提交
318 319 320 321 322 323 324
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 |

**示例:**

Z
zengyawen 已提交
325
```js
C
clevercong 已提交
326 327 328 329 330 331 332 333 334 335 336 337
connection.getDefaultNet().then(function (netHandle) {
    connection.reportNetConnected(netHandle, function (error) {
        console.log(JSON.stringify(error))
    });
});
```


## connection.reportNetConnected

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

338
向网络管理报告网络处于可用状态,调用此接口说明应用程序认为网络的可用性(ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED)与网络管理不一致。
339
使用promise方式作为异步方法。
C
clevercong 已提交
340

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

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

**参数:**
346

C
clevercong 已提交
347 348 349 350 351 352 353 354 355 356 357
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 |

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

**示例:**

Z
zengyawen 已提交
358
```js
C
clevercong 已提交
359 360 361 362 363 364 365 366 367 368 369 370
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

371
向网络管理报告网络处于不可用状态,调用此接口说明应用程序认为网络的可用性(ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED)与网络管理不一致。
372
使用callback方式作为异步方法。
C
clevercong 已提交
373

C
clevercong 已提交
374 375 376
**需要权限**:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET

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

**参数:**
379

C
clevercong 已提交
380 381 382 383 384 385 386
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 |

**示例:**

Z
zengyawen 已提交
387
```js
C
clevercong 已提交
388 389 390 391 392 393 394 395 396 397 398 399
connection.getDefaultNet().then(function (netHandle) {
    connection.reportNetDisconnected(netHandle, function (error) {
        console.log(JSON.stringify(error))
    });
});
```


## connection.reportNetDisconnected

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

400
向网络管理报告网络处于不可用状态,调用此接口说明应用程序认为网络的可用性(ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED)与网络管理不一致。
401
使用promise方式作为异步方法。
C
clevercong 已提交
402

C
clevercong 已提交
403 404 405
**需要权限**:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET

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

**参数:**
408

C
clevercong 已提交
409 410 411 412 413 414 415 416 417 418 419
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 |

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

**示例:**

Z
zengyawen 已提交
420
```js
C
clevercong 已提交
421 422 423 424 425 426 427
connection.getDefaultNet().then(function (netHandle) {
    connection.reportNetDisconnected(netHandle).then(function () {
        console.log(`report success`)
    });
});
```

428 429 430 431
## connection.getAddressesByName

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

Z
zengyawen 已提交
432
使用默认网络解析主机名以获取所有IP地址,使用callback方式作为异步方法。
433 434 435 436 437 438 439

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

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

**参数:**

Z
zengyawen 已提交
440 441 442 443
| 参数名   | 类型                                              | 必填 | 说明               |
| -------- | ------------------------------------------------- | ---- | ------------------ |
| host     | string                                            | 是   | 需要解析的主机名。 |
| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | 是   | 回调函数。         |
444 445 446

**示例:**

Z
zengyawen 已提交
447
```js
C
clevercong 已提交
448 449 450 451
let host = "xxxx";
connection.getAddressesByName(host, function (error, addresses) {
    console.log(JSON.stringify(error))
    console.log(JSON.stringify(addresses))
452 453 454 455 456
})
```

## connection.getAddressesByName

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

Z
zengyawen 已提交
459
使用默认网络解析主机名以获取所有IP地址,使用Promise方式作为异步方法。
460 461 462 463 464 465 466

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

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

**参数:**

Z
zengyawen 已提交
467 468 469
| 参数名 | 类型   | 必填 | 说明               |
| ------ | ------ | ---- | ------------------ |
| host   | string | 是   | 需要解析的主机名。 |
470 471 472

**返回值:**

Z
zengyawen 已提交
473 474 475
| 类型                                        | 说明                          |
| ------------------------------------------- | ----------------------------- |
| Promise\<Array\<[NetAddress](#netaddress)>> | 以Promise形式返回所有IP地址。 |
476 477 478

**示例:**

Z
zengyawen 已提交
479
```js
C
clevercong 已提交
480 481 482
let host = "xxxx";
connection.getAddressesByName(host).then(function (addresses) {
    console.log(JSON.stringify(addresses))
483 484 485
})
```

C
clevercong 已提交
486 487 488 489 490 491 492

## connection.enableAirplaneMode

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

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

C
clevercong 已提交
493 494
该接口为系统接口。

C
clevercong 已提交
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
**系统能力**: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 已提交
517 518
该接口为系统接口。

C
clevercong 已提交
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
**系统能力**: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 已提交
542 543
该接口为系统接口。

C
clevercong 已提交
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
**系统能力**: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 已提交
566 567
该接口为系统接口。

C
clevercong 已提交
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
**系统能力**:SystemCapability.Communication.NetManager.Core

**返回值:**

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

**示例:**

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


585 586 587 588
## connection.createNetConnection

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

589
返回一个NetConnection对象,netSpecifier指定关注的网络的各项特征,timeout是超时时间(单位是毫秒),netSpecifier是timeout的必要条件,两者都没有则表示关注默认网络。
590 591 592 593 594

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

**参数:**

Z
zengyawen 已提交
595 596 597 598
| 参数名       | 类型                          | 必填 | 说明                                                         |
| ------------ | ----------------------------- | ---- | ------------------------------------------------------------ |
| netSpecifier | [NetSpecifier](#netspecifier) | 否   | 指定网络的各项特征,不指定则关注默认网络。                   |
| timeout      | number                        | 否   | 获取netSpecifier指定的网络时的超时时间,仅netSpecifier存在时生效。 |
599 600 601

**返回值:**

Z
zengyawen 已提交
602 603 604
| 类型                            | 说明                 |
| ------------------------------- | -------------------- |
| [NetConnection](#netconnection) | 所关注的网络的句柄。 |
605 606 607

**示例:**

Z
zengyawen 已提交
608
```js
609
// 关注默认网络
C
clevercong 已提交
610
let netConnection = connection.createNetConnection()
611 612

// 关注蜂窝网络
C
clevercong 已提交
613
let netConnectionCellular = connection.createNetConnection({
614
    netCapabilities: {
C
clevercong 已提交
615
        bearerTypes: [connection.NetBearType.BEARER_CELLULAR]
616 617 618 619
    }
})
```

M
maosiping 已提交
620
## NetConnection
621

Z
zengyawen 已提交
622
网络连接的句柄。
623

M
maosiping 已提交
624
### on('netAvailable')
625 626 627

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

C
clevercong 已提交
628
订阅网络可用事件。
629 630 631 632 633

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

**参数:**

Z
zengyawen 已提交
634 635
| 参数名   | 类型                               | 必填 | 说明                                                         |
| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ |
C
clevercong 已提交
636
| type     | string                             | 是   | 订阅事件,固定为'netAvailable'。<br>netAvailable:数据网络可用事件。 |
Z
zengyawen 已提交
637
| callback | Callback\<[NetHandle](#nethandle)> | 是   | 回调函数。                                                   |
638 639 640

**示例:**

Z
zengyawen 已提交
641
```js
C
clevercong 已提交
642
netConnection.on('netAvailable', function (data) {
643 644 645 646
    console.log(JSON.stringify(data))
})
```

M
maosiping 已提交
647
### on('netCapabilitiesChange')
648 649 650

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

C
clevercong 已提交
651
订阅网络能力变化事件。
652 653 654 655 656

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

**参数:**

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

**示例:**

Z
zengyawen 已提交
664
```js
C
clevercong 已提交
665
netConnection.on('netCapabilitiesChange', function (data) {
666 667 668 669
    console.log(JSON.stringify(data))
})
```

M
maosiping 已提交
670
### on('netConnectionPropertiesChange')
671 672 673

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

C
clevercong 已提交
674
订阅网络连接信息变化事件。
675 676 677 678 679

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

**参数:**

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

**示例:**

Z
zengyawen 已提交
687
```js
C
clevercong 已提交
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
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 已提交
710
```js
C
clevercong 已提交
711
netConnection.on('netBlockStatusChange', function (data) {
712 713 714 715
    console.log(JSON.stringify(data))
})
```

M
maosiping 已提交
716
### on('netLost')
717 718 719

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

C
clevercong 已提交
720
订阅网络丢失事件。
721 722 723 724 725

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

**参数:**

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

**示例:**

Z
zengyawen 已提交
733
```js
C
clevercong 已提交
734 735
let netConnection1 = connection.createNetConnection()
netConnection1.on('netLost', function (data) {
736 737 738 739
    console.log(JSON.stringify(data))
})
```

M
maosiping 已提交
740
### on('netUnavailable')
741 742 743

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

C
clevercong 已提交
744
订阅网络不可用事件。
745 746 747 748 749

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

**参数:**

Z
zengyawen 已提交
750 751
| 参数名   | 类型            | 必填 | 说明                                                         |
| -------- | --------------- | ---- | ------------------------------------------------------------ |
C
clevercong 已提交
752
| type     | string          | 是   | 订阅事件,固定为'netUnavailable'。<br/>netUnavailable:网络不可用事件。 |
Z
zengyawen 已提交
753
| callback | Callback\<void> | 是   | 回调函数。                                                   |
754 755 756

**示例:**

Z
zengyawen 已提交
757
```js
C
clevercong 已提交
758
netConnection.on('netUnavailable', function (data) {
759 760 761 762
    console.log(JSON.stringify(data))
})
```

M
maosiping 已提交
763
### register
764 765 766

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

C
clevercong 已提交
767
订阅指定网络状态变化的通知。
768 769 770 771 772 773 774

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

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

**参数:**

Z
zengyawen 已提交
775 776 777
| 参数名   | 类型                 | 必填 | 说明       |
| -------- | -------------------- | ---- | ---------- |
| callback | AsyncCallback\<void> | 是   | 回调函数。 |
778 779 780

**示例:**

Z
zengyawen 已提交
781
```js
C
clevercong 已提交
782
netConnection.register(function (error) {
783 784 785 786
    console.log(JSON.stringify(error))
})
```

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

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

C
clevercong 已提交
791
取消订阅默认网络状态变化的通知。
792 793 794 795 796

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

**参数:**

Z
zengyawen 已提交
797 798 799
| 参数名   | 类型                 | 必填 | 说明       |
| -------- | -------------------- | ---- | ---------- |
| callback | AsyncCallback\<void> | 是   | 回调函数。 |
800 801 802

**示例:**

Z
zengyawen 已提交
803
```js
C
clevercong 已提交
804
netConnection.unregister(function (error) {
805 806 807 808
    console.log(JSON.stringify(error))
})
```

M
maosiping 已提交
809
## NetHandle
810

Z
zengyawen 已提交
811
数据网络的句柄。
812

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

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

Z
zengyawen 已提交
817 818
### 属性

C
clevercong 已提交
819 820 821
| 参数名 | 类型   | 说明                      |
| ------ | ------ | ------------------------- |
| netId  | number | 网络ID,必须大于等于100。 |
822

Z
zhuwenchao 已提交
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924

### bindSocket

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

将TCPSocket或UDPSocket绑定到当前网络,使用callback方式作为异步方法。

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

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

**参数:**

| 参数名      | 类型                     | 必填 | 说明                            |
| ----------- | ------------------------ | ---- | -------------------------------|
| socketParam | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | 是 | 待绑定的TCPSocket或UDPSocket对象。|
| callback    | AsyncCallback\<void>      | 是   | 回调函数                        |

**示例:**

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

### bindSocket

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

将TCPSocket或UDPSockett绑定到当前网络,使用Promise方式作为异步方法。

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

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

**参数:**

| 参数名          | 类型                  | 必填  | 说明                           |
| --------------- | --------------------- | ---- | ------------------------------ |
| socketParam     | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | 是   | 待绑定的TCPSocket或UDPSocket对象。|

**返回值:**

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

**示例:**

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

M
maosiping 已提交
925
### getAddressesByName
926 927 928

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

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

C
clevercong 已提交
931 932
**需要权限**:ohos.permission.GET_NETWORK_INFO

933 934 935 936
**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
937 938 939 940
| 参数名   | 类型                                              | 必填 | 说明               |
| -------- | ------------------------------------------------- | ---- | ------------------ |
| host     | string                                            | 是   | 需要解析的主机名。 |
| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | 是   | 回调函数           |
941 942 943

**示例:**

Z
zengyawen 已提交
944
```js
945
connection.getDefaultNet().then(function (netHandle) {
C
clevercong 已提交
946 947
    let host = "xxxx";
    netHandle.getAddressesByName(host, function (error, addresses) {
948
        console.log(JSON.stringify(error))
C
clevercong 已提交
949
        console.log(JSON.stringify(addresses))
950 951 952 953
    })
})
```

M
maosiping 已提交
954
### getAddressesByName
955

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

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

C
clevercong 已提交
960 961
**需要权限**:ohos.permission.GET_NETWORK_INFO

962 963 964 965
**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
966 967 968
| 参数名 | 类型   | 必填 | 说明               |
| ------ | ------ | ---- | ------------------ |
| host   | string | 是   | 需要解析的主机名。 |
969 970 971

**返回值:**

Z
zengyawen 已提交
972 973 974
| 类型                                        | 说明                          |
| ------------------------------------------- | ----------------------------- |
| Promise\<Array\<[NetAddress](#netaddress)>> | 以Promise形式返回所有IP地址。 |
975 976 977

**示例:**

Z
zengyawen 已提交
978
```js
979
connection.getDefaultNet().then(function (netHandle) {
C
clevercong 已提交
980 981 982
    let host = "xxxx";
    netHandle.getAddressesByName(host).then(function (addresses) {
        console.log(JSON.stringify(addresses))
983 984 985 986
    })
})
```

M
maosiping 已提交
987
### getAddressByName
988 989 990

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

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

C
clevercong 已提交
993 994
**需要权限**:ohos.permission.GET_NETWORK_INFO

995 996 997 998
**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
999 1000 1001 1002
| 参数名   | 类型                                      | 必填 | 说明               |
| -------- | ----------------------------------------- | ---- | ------------------ |
| host     | string                                    | 是   | 需要解析的主机名。 |
| callback | AsyncCallback\<[NetAddress](#netaddress)> | 是   | 回调函数。         |
1003 1004 1005

**示例:**

Z
zengyawen 已提交
1006
```js
1007
connection.getDefaultNet().then(function (netHandle) {
C
clevercong 已提交
1008 1009
    let host = "xxxx";
    netHandle.getAddressByName(host, function (error, address) {
1010
        console.log(JSON.stringify(error))
C
clevercong 已提交
1011
        console.log(JSON.stringify(address))
1012 1013 1014 1015
    })
})
```

M
maosiping 已提交
1016
### getAddressByName
1017

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

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

C
clevercong 已提交
1022 1023
**需要权限**:ohos.permission.GET_NETWORK_INFO

1024 1025 1026 1027
**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
1028 1029 1030
| 参数名 | 类型   | 必填 | 说明               |
| ------ | ------ | ---- | ------------------ |
| host   | string | 是   | 需要解析的主机名。 |
1031 1032 1033

**返回值:**

Z
zengyawen 已提交
1034 1035 1036
| 类型                                | 说明                            |
| ----------------------------------- | ------------------------------- |
| Promise\<[NetAddress](#netaddress)> | 以Promise形式返回第一个IP地址。 |
1037 1038 1039

**示例:**

Z
zengyawen 已提交
1040
```js
1041
connection.getDefaultNet().then(function (netHandle) {
C
clevercong 已提交
1042 1043 1044
    let host = "xxxx";
    netHandle.getAddressByName(host).then(function (address) {
        console.log(JSON.stringify(address))
1045 1046 1047 1048
    })
})
```

M
maosiping 已提交
1049
## NetSpecifier
1050

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

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

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

M
maosiping 已提交
1060
## NetCapabilities
1061

M
maosiping 已提交
1062
网络的能力集。
1063

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

| 参数名                | 类型                               | 说明                     |
Z
zengyawen 已提交
1067 1068 1069 1070
| --------------------- | ---------------------------------- | ------------------------ |
| linkUpBandwidthKbps   | number                             | 上行(设备到网络)带宽。 |
| linkDownBandwidthKbps | number                             | 下行(网络到设备)带宽。 |
| networkCap            | Array<[NetCap](#netcap)>           | 网络具体能力。           |
Z
zengyawen 已提交
1071
| bearerTypes           | Array<[NetBearType](#netbeartype)> | 网络类型。               |
1072

M
maosiping 已提交
1073
## NetCap
1074

M
maosiping 已提交
1075
网络具体能力。
1076

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

| 参数名                  | 值   | 说明                   |
Z
zengyawen 已提交
1080
| ------------------------ | ---- | ---------------------- |
C
clevercong 已提交
1081 1082
| NET_CAPABILITY_MMS | 0 | 表示网络可以访问运营商的MMSC(Multimedia&nbsp;Message&nbsp;Service,多媒体短信服务)发送和接收彩信。 |
| NET_CAPABILITY_NOT_METERED | 11 | 表示网络流量未被计费。 |
1083
| NET_CAPABILITY_INTERNET  | 12   | 表示该网络应具有访问Internet的能力,该能力由网络提供者设置。 |
C
clevercong 已提交
1084
| NET_CAPABILITY_NOT_VPN | 15 | 表示网络不使用VPN(Virtual&nbsp;Private&nbsp;Network,虚拟专用网络)。 |
1085
| NET_CAPABILITY_VALIDATED | 16   | 表示该网络访问Internet的能力被网络管理成功验证,该能力由网络管理模块设置。 |
1086

M
maosiping 已提交
1087
## NetBearType
1088

M
maosiping 已提交
1089
网络类型。
1090

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

| 名称         | 值   | 说明        |
Z
zengyawen 已提交
1094 1095 1096
| --------------- | ---- | ----------- |
| BEARER_CELLULAR | 0    | 蜂窝网络。  |
| BEARER_WIFI     | 1    | Wi-Fi网络。 |
C
clevercong 已提交
1097
| BEARER_ETHERNET | 3 | 以太网网络。 |
1098

M
maosiping 已提交
1099
## ConnectionProperties
1100

M
maosiping 已提交
1101
网络连接信息。
1102

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

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

M
maosiping 已提交
1114
## LinkAddress
1115

M
maosiping 已提交
1116
网络链路信息。
1117

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

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

M
maosiping 已提交
1125
## RouteInfo
1126

M
maosiping 已提交
1127
网络路由信息。
1128

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

| 参数名         | 类型                        | 说明             |
Z
zengyawen 已提交
1132 1133 1134 1135 1136 1137
| -------------- | --------------------------- | ---------------- |
| interface      | string                      | 网卡名称。       |
| destination    | [LinkAddress](#linkaddress) | 目的地址。       |
| gateway        | [NetAddress](#netaddress)   | 网关地址。       |
| hasGateway     | boolean                     | 是否有网关。     |
| isDefaultRoute | boolean                     | 是否为默认路由。 |
1138

M
maosiping 已提交
1139
## NetAddress
1140

Z
zengyawen 已提交
1141
网络地址。
1142

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

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