js-apis-net-connection.md 17.1 KB
Newer Older
M
maosiping 已提交
1
# 网络
2 3 4 5 6 7 8

> **说明:**
>
>本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

## 导入模块

M
maosiping 已提交
9
```javascript
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
import connection from '@ohos.net.connection'
```

## connection.getDefaultNet

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

获取默认网络,使用callback方式作为异步方法。

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| ----- | ----- | ---- | ----- |
M
maosiping 已提交
27
| callback | AsyncCallback\<[NetHandle](#nethandle)> | 是 | 回调函数 |
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

**示例:**

```javascript
connection.getDefaultNet(function (error, netHandle) {
    console.log(JSON.stringify(error))
    console.log(JSON.stringify(netHandle))
})
```

## connection.getDefaultNet

getDefaultNet(): Promise\<NetHandle>

获取默认网络,使用Promise方式作为异步方法。

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

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

**返回值:**

| 类型 | 说明 |
| ----- | ----- |
M
maosiping 已提交
52
| Promise\<[NetHandle](#nethandle)> | 以Promise形式返回 |
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

**示例:**

```javascript
connection.getDefaultNet().then(function (netHandle) {
    console.log(JSON.stringify(netHandle))
})
```

## connection.hasDefaultNet

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

判断是否有默认网络,使用callback方式作为异步方法。

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| ----- | ----- | ---- | ----- |
M
maosiping 已提交
74
| callback | AsyncCallback\<boolean> | 是 | 回调函数,有默认网络返回true。 |
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

**示例:**

```javascript
connection.hasDefaultNet(function (error, has) {
    console.log(JSON.stringify(error))
    console.log(has)
})
```

## connection.hasDefaultNet

hasDefaultNet(): Promise\<boolean>

判断是否有默认网络,使用Promise方式作为异步方法。

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

**返回值:**

| 类型 | 说明 |
| ----- | ----- |
M
maosiping 已提交
97
| Promise\<boolean> | 以Promise形式返回,有默认网络返回true。 |
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

**示例:**

```javascript
connection.hasDefaultNet().then(function (has) {
    console.log(has)
})
```

## connection.getConnectionProperties

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

查询netHandle对应的网络的连接信息,使用callback方式作为异步方法。

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| ----- | ----- | ---- | ----- |
M
maosiping 已提交
121 122
| netHandle |  [NetHandle](#nethandle) | 是 | 对应网络 |
| callback | AsyncCallback\<[ConnectionProperties](#connectionproperties)> | 是 | 回调函数 |
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

**示例:**

```javascript
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>

查询netHandle对应的网络的连接信息,使用Promise方式作为异步方法。

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| ----- | ----- | ---- | ----- |
M
maosiping 已提交
149
| netHandle |  [NetHandle](#nethandle) | 是 | 对应网络 |
150 151 152 153 154

**返回值:**

| 类型 | 说明 |
| ----- | ----- |
M
maosiping 已提交
155
| Promise\<[ConnectionProperties](#connectionproperties)> | 以Promise形式返回 |
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

**示例:**

```javascript
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

查询netHandle对应的网络的能力信息,使用callback方式作为异步方法。

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| ----- | ----- | ---- | ----- |
M
maosiping 已提交
181 182
| netHandle |  [NetHandle](#nethandle) | 是 | 对应网络 |
| callback | AsyncCallback\<[NetCapabilities](#netcapabilities)> | 是 | 回调函数 |
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

**示例:**

```javascript
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>

查询netHandle对应的网络的能力信息,使用Promise方式作为异步方法。

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| ----- | ----- | ---- | ----- |
M
maosiping 已提交
209
| netHandle |  [NetHandle](#nethandle) | 是 | 对应网络 |
210 211 212 213 214

**返回值:**

| 类型 | 说明 |
| ----- | ----- |
M
maosiping 已提交
215
| Promise\<[NetCapabilities](#netcapabilities)> | 以Promise形式返回 |
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241

**示例:**

```javascript
connection.getDefaultNet().then(function (netHandle) {
    connection.getNetCapabilities(netHandle).then(function (info) {
        console.log(JSON.stringify(info))
    })
})
```

## connection.getAddressesByName

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

使用默认网络将host解析成IP,返回所有IP,使用callback方式作为异步方法。

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| ----- | ----- | ---- | ----- |
| host | string | 是 | 需要解析的域名 |
M
maosiping 已提交
242
| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | 是 | 回调函数 |
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

**示例:**

```javascript
connection.getDefaultNet().then(function (netHandle) {
    connection.getAddressesByName(netHandle, function (error, info) {
        console.log(JSON.stringify(error))
        console.log(JSON.stringify(info))
    })
})
```

## connection.getAddressesByName

getAddressesByName(netHandle: NetHandle): Promise\<Array\<NetAddress>>

使用默认网络将host解析成IP,返回所有IP,使用Promise方式作为异步方法。

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| ----- | ----- | ---- | ----- |
| host | string | 是 | 需要解析的域名 |

**返回值:**

| 类型 | 说明 |
| ----- | ----- |
M
maosiping 已提交
275
| Promise\<Array\<[NetAddress](#netaddress)>> | 以Promise形式返回 |
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290

**示例:**

```javascript
connection.getDefaultNet().then(function (netHandle) {
    connection.getAddressesByName(netHandle).then(function (info) {
        console.log(JSON.stringify(info))
    })
})
```

## connection.createNetConnection

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

M
maosiping 已提交
291
获取一个netSpecifier指定的网络的句柄。
292 293 294 295 296 297 298

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| ----- | ----- | ---- | ----- |
M
maosiping 已提交
299 300
| netSpecifier | [NetSpecifier](#netspecifier) | 否 | 指定网络的各项特征,不指定则关注默认网络。 |
| timeout | number | 否 | 获取netSpecifier指定的网络时的超时时间,仅netSpecifier存在时生效。 |
301 302 303 304 305

**返回值:**

| 类型 | 说明 |
| ----- | ----- |
M
maosiping 已提交
306
|[NetConnection](#netconnection) | 所关注的网络的句柄 |
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321

**示例:**

```javascript
// 关注默认网络
let netConnection1 = connection.createNetConnection()

// 关注蜂窝网络
let netConnection2 = connection.createNetConnection({
    netCapabilities: {
        networkCap: [0]
    }
})
```

M
maosiping 已提交
322
## NetConnection
323 324 325

网络连接的句柄

M
maosiping 已提交
326
### on('netAvailable')
327 328 329

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

M
maosiping 已提交
330
监听网络可用事件。
331 332 333 334 335 336 337 338

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| ----- | ----- | ---- | ----- |
| type | string | 是 | 监听的事件,固定'netAvailable' |
M
maosiping 已提交
339
| callback | Callback\<[NetHandle](#nethandle)>> | 是 | 回调函数 |
340 341 342 343 344 345 346 347 348

**示例:**

```javascript
connection.createNetConnection().on('netAvailable', function (data) {
    console.log(JSON.stringify(data))
})
```

M
maosiping 已提交
349
### on('netCapabilitiesChange')
350 351 352

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

M
maosiping 已提交
353
监听网络能力变化事件。
354 355 356 357 358 359 360 361

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| ----- | ----- | ---- | ----- |
| type | string | 是 | 监听的事件,固定'netCapabilitiesChange' |
M
maosiping 已提交
362
| callback | Callback<{ netHandle: [NetHandle](#nethandle), netCap: [NetCapabilities](#netcapabilities) }> | 是 | 回调函数 |
363 364 365 366 367 368 369 370 371

**示例:**

```javascript
connection.createNetConnection().on('netCapabilitiesChange', function (data) {
    console.log(JSON.stringify(data))
})
```

M
maosiping 已提交
372
### on('netConnectionPropertiesChange')
373 374 375

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

M
maosiping 已提交
376
监听网络连接信息变化事件。
377 378 379 380 381 382 383 384

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| ----- | ----- | ---- | ----- |
| type | string | 是 | 监听的事件,固定'netConnectionPropertiesChange' |
M
maosiping 已提交
385
| callback | Callback<{ netHandle: [NetHandle](#nethandle), connectionProperties: [ConnectionProperties](#connectionproperties) }> | 是 | 回调函数 |
386 387 388 389 390 391 392 393 394

**示例:**

```javascript
connection.createNetConnection().on('netConnectionPropertiesChange', function (data) {
    console.log(JSON.stringify(data))
})
```

M
maosiping 已提交
395
### on('netLost')
396 397 398

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

M
maosiping 已提交
399
监听网络丢失事件。
400 401 402 403 404 405 406 407

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| ----- | ----- | ---- | ----- |
| type | string | 是 | 监听的事件,固定'netLost' |
M
maosiping 已提交
408
| callback | Callback\<[NetHandle](#nethandle)>> | 是 | 回调函数 |
409 410 411 412 413 414 415 416 417

**示例:**

```javascript
connection.createNetConnection().on('netLost', function (data) {
    console.log(JSON.stringify(data))
})
```

M
maosiping 已提交
418
### on('netUnavailable')
419 420 421

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

M
maosiping 已提交
422
监听网络不可用事件。
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| ----- | ----- | ---- | ----- |
| type | string | 是 | 监听的事件,固定'netUnavailable' |
| callback | Callback\<void>> | 是 | 回调函数 |

**示例:**

```javascript
connection.createNetConnection().on('netUnavailable', function (data) {
    console.log(JSON.stringify(data))
})
```

M
maosiping 已提交
441
### register
442 443 444

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

M
maosiping 已提交
445
注册网络的监听。
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464

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

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

**参数:**

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

**示例:**

```javascript
connection.createNetConnection().register(function (error) {
    console.log(JSON.stringify(error))
})
```

M
maosiping 已提交
465
### unregister
466 467 468

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

M
maosiping 已提交
469
注销网络的监听。
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486

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

**参数:**

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

**示例:**

```javascript
connection.createNetConnection().unregister(function (error) {
    console.log(JSON.stringify(error))
})
```

M
maosiping 已提交
487
## NetHandle
488 489 490 491 492 493 494

网络的句柄

| 变量 | 类型 | 说明 |
| ----- | ----- | ----- |
| netId | number | 对应网络的编号 |

M
maosiping 已提交
495
### getAddressesByName
496 497 498 499 500 501 502 503 504 505 506 507

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

使用对应网络将host解析成IP,返回所有IP,使用callback方式作为异步方法。

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| ----- | ----- | ---- | ----- |
| host | string | 是 | 需要解析的域名 |
M
maosiping 已提交
508
| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | 是 | 回调函数 |
509 510 511 512 513 514 515 516 517 518 519 520

**示例:**

```javascript
connection.getDefaultNet().then(function (netHandle) {
    connection.getAddressesByName(netHandle, function (error, info) {
        console.log(JSON.stringify(error))
        console.log(JSON.stringify(info))
    })
})
```

M
maosiping 已提交
521
### getAddressesByName
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538

getAddressesByName(netHandle: NetHandle): Promise\<Array\<NetAddress>>

使用对应网络将host解析成IP,返回所有IP,使用Promise方式作为异步方法。

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| ----- | ----- | ---- | ----- |
| host | string | 是 | 需要解析的域名 |

**返回值:**

| 类型 | 说明 |
| ----- | ----- |
M
maosiping 已提交
539
| Promise\<Array\<[NetAddress](#netaddress)>> | 以Promise形式返回 |
540 541 542 543 544 545 546 547 548 549 550

**示例:**

```javascript
connection.getDefaultNet().then(function (netHandle) {
    connection.getAddressesByName(netHandle).then(function (info) {
        console.log(JSON.stringify(info))
    })
})
```

M
maosiping 已提交
551
### getAddressByName
552 553 554 555 556 557 558 559 560 561 562 563

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

使用对应网络将host解析成IP,返回一个IP,使用callback方式作为异步方法。

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| ----- | ----- | ---- | ----- |
| host | string | 是 | 需要解析的域名 |
M
maosiping 已提交
564
| callback | AsyncCallback\<[NetAddress](#netaddress)> | 是 | 回调函数 |
565 566 567 568 569 570 571 572 573 574 575 576

**示例:**

```javascript
connection.getDefaultNet().then(function (netHandle) {
    connection.getAddressByName(netHandle, function (error, info) {
        console.log(JSON.stringify(error))
        console.log(JSON.stringify(info))
    })
})
```

M
maosiping 已提交
577
### getAddressByName
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594

getAddressByName(netHandle: NetHandle): Promise\<NetAddress>

使用对应网络将host解析成IP,返回一个IP,使用Promise方式作为异步方法。

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| ----- | ----- | ---- | ----- |
| host | string | 是 | 需要解析的域名 |

**返回值:**

| 类型 | 说明 |
| ----- | ----- |
M
maosiping 已提交
595
| Promise\<[NetAddress](#netaddress)> | 以Promise形式返回 |
596 597 598 599 600 601 602 603 604 605 606

**示例:**

```javascript
connection.getDefaultNet().then(function (netHandle) {
    connection.getAddressByName(netHandle).then(function (info) {
        console.log(JSON.stringify(info))
    })
})
```

M
maosiping 已提交
607
## NetSpecifier
608

M
maosiping 已提交
609
网络的特征。
610 611 612

| 变量 | 类型 | 说明 |
| ----- | ----- | ----- |
M
maosiping 已提交
613
| netCapabilities | [NetCapabilities](#netcapabilities) | 网络的能力集 |
614 615
| bearerPrivateIdentifier | string | 网络标识符,WIFI网络的标识符是"wifi",蜂窝网络的标识符是"slot0"(对应SIM卡1) |

M
maosiping 已提交
616
## NetCapabilities
617

M
maosiping 已提交
618
网络的能力集。
619 620 621 622 623

| 变量 | 类型 | 说明 |
| ----- | ----- | ----- |
| linkUpBandwidthKbps | number | 带宽上限 |
| linkDownBandwidthKbps | number | 带宽下限 |
M
maosiping 已提交
624 625
| networkCap | Array<[NetCap](#netcap)> | 网络具体能力 |
| bearerTypes | Array<[NetBearType](#netbearType)> | 网络类型 |
626

M
maosiping 已提交
627
## NetCap
628

M
maosiping 已提交
629
网络具体能力。
630 631 632 633 634 635

| 变量 | 值 | 说明 |
| ------ | ----- | ----- |
| NET_CAPABILITY_INTERNET | 12 | 联网能力 |
| NET_CAPABILITY_VALIDATED | 16 | 网络可用 |

M
maosiping 已提交
636
## NetBearType
637

M
maosiping 已提交
638
网络类型。
639 640 641 642 643 644

| 变量 | 值 | 说明 |
| ------ | ----- | ----- |
| BEARER_CELLULAR | 0 | 蜂窝网络 |
| BEARER_WIFI | 1 | WIFI网络 |

M
maosiping 已提交
645
## ConnectionProperties
646

M
maosiping 已提交
647
网络连接信息。
648 649 650 651 652

| 变量 | 类型 | 说明 |
| ----- | ----- | ----- |
| interfaceName | string | 网卡名称 |
| domains | string | 所属域,默认"" |
M
maosiping 已提交
653 654
| linkAddresses | Array<[LinkAddress](#linkaddress)> | 链路信息 |
| routes | Array<[RouteInfo](#routeinfo)> | 路由信息 |
655 656
| mtu | number | 最大传输单元 |

M
maosiping 已提交
657
## LinkAddress
658

M
maosiping 已提交
659
网络链路信息。
660 661 662

| 变量 | 类型 | 说明 |
| ----- | ----- | ----- |
M
maosiping 已提交
663
| address | [NetAddress](#netaddress) | 链路地址 |
664 665
| prefixLength | number | 地址前缀长度 |

M
maosiping 已提交
666
## RouteInfo
667

M
maosiping 已提交
668
网络路由信息。
669 670 671 672

| 变量 | 类型 | 说明 |
| ----- | ----- | ----- |
| interface | string | 网卡名称 |
M
maosiping 已提交
673 674
| destination | [LinkAddress](#linkaddress) | 目的地址 |
| gateway | [NetAddress](#netaddress) | 网关地址 |
675 676 677
| hasGateway | boolean | 是否有网关 |
| isDefaultRoute | boolean | 是否为默认路由 |

M
maosiping 已提交
678
## NetAddress
679

M
maosiping 已提交
680
地址。
681 682 683 684 685 686

| 变量 | 类型 | 说明 |
| ----- | ----- | ----- |
| address | string | 一个IPv4地址或者IPv6地址 |
| family | number | IPv4 = 1, IPv6 = 2, 默认IPv4 |
| port | number | 端口,取值范围\[0, 65535] |