js-apis-net-connection.md 41.1 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))
})
```

L
liyufan 已提交
64
## connection.getDefaultNetSync<sup>9+</sup>
F
fuchao 已提交
65 66 67

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
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;

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

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

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

**返回值:**
171

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

**示例:**

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

184 185 186 187
## connection.getConnectionProperties

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

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

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

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

**参数:**

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

**示例:**

Z
zengyawen 已提交
203
```js
204 205 206 207 208 209 210 211 212 213 214 215
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 已提交
216
获取netHandle对应的网络的连接信息,使用Promise方式作为异步方法。
217 218 219 220 221 222 223

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

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

**参数:**

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

**返回值:**

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

**示例:**

Z
zengyawen 已提交
236
```js
237 238 239 240 241 242 243 244 245 246 247
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 已提交
248
获取netHandle对应的网络的能力信息,使用callback方式作为异步方法。
249 250 251 252 253 254 255

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

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

**参数:**

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

**示例:**

Z
zengyawen 已提交
263
```js
264 265 266 267 268 269 270 271 272 273 274 275
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 已提交
276
获取netHandle对应的网络的能力信息,使用Promise方式作为异步方法。
277 278 279 280 281 282 283

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

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

**参数:**

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

**返回值:**

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

**示例:**

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

L
liyufan 已提交
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
## connection.isDefaultNetMetered<sup>9+</sup>

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

检查当前网络上的数据流量使用是否被计量,使用callback方式作为异步方法。

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

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

**参数:**

| 参数名   | 类型                    | 必填 | 说明                                   |
| -------- | ----------------------- | ---- | -------------------------------------- |
| callback | AsyncCallback\<boolean> | 是   | 回调函数,当前网络上的数据流量使用被计量返回true。 |

**示例:**

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

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

isDefaultNetMetered(): Promise\<boolean>

检查当前网络上的数据流量使用是否被计量,使用Promise方式作为异步方法。

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

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

**返回值:**

| 类型              | 说明                                            |
| ----------------- | ----------------------------------------------- |
| Promise\<boolean> | 以Promise形式返回,当前网络上的数据流量使用被计量true。 |

**示例:**

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

C
clevercong 已提交
353 354 355 356
## connection.reportNetConnected

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

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

C
clevercong 已提交
360 361 362
**需要权限**:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET

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

**参数:**
365

C
clevercong 已提交
366 367 368 369 370 371 372
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 |

**示例:**

Z
zengyawen 已提交
373
```js
C
clevercong 已提交
374 375 376 377 378 379 380 381 382 383 384
connection.getDefaultNet().then(function (netHandle) {
    connection.reportNetConnected(netHandle, function (error) {
        console.log(JSON.stringify(error))
    });
});
```

## connection.reportNetConnected

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

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

C
clevercong 已提交
388 389 390
**需要权限**:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET

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

**参数:**
393

C
clevercong 已提交
394 395 396 397 398 399 400
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 |

**返回值:**
| 类型 | 说明 |
| -------- | -------- |
401
| Promise&lt;void&gt; | 无返回值的Promise对象。 |
C
clevercong 已提交
402 403 404

**示例:**

Z
zengyawen 已提交
405
```js
C
clevercong 已提交
406 407 408 409 410 411 412 413 414 415 416
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

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

C
clevercong 已提交
420 421 422
**需要权限**:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET

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

**参数:**
425

C
clevercong 已提交
426 427 428 429 430 431 432
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 |

**示例:**

Z
zengyawen 已提交
433
```js
C
clevercong 已提交
434 435 436 437 438 439 440 441 442 443 444
connection.getDefaultNet().then(function (netHandle) {
    connection.reportNetDisconnected(netHandle, function (error) {
        console.log(JSON.stringify(error))
    });
});
```

## connection.reportNetDisconnected

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

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

C
clevercong 已提交
448 449 450
**需要权限**:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET

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

**参数:**
453

C
clevercong 已提交
454 455 456 457 458 459 460
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 |

**返回值:**
| 类型 | 说明 |
| -------- | -------- |
461
| Promise&lt;void&gt; | 无返回值的Promise对象。 |
C
clevercong 已提交
462 463 464

**示例:**

Z
zengyawen 已提交
465
```js
C
clevercong 已提交
466 467 468 469 470 471 472
connection.getDefaultNet().then(function (netHandle) {
    connection.reportNetDisconnected(netHandle).then(function () {
        console.log(`report success`)
    });
});
```

473 474 475 476
## connection.getAddressesByName

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

Z
zengyawen 已提交
477
使用默认网络解析主机名以获取所有IP地址,使用callback方式作为异步方法。
478 479 480 481 482 483 484

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

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

**参数:**

Z
zengyawen 已提交
485 486 487 488
| 参数名   | 类型                                              | 必填 | 说明               |
| -------- | ------------------------------------------------- | ---- | ------------------ |
| host     | string                                            | 是   | 需要解析的主机名。 |
| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | 是   | 回调函数。         |
489 490 491

**示例:**

Z
zengyawen 已提交
492
```js
C
clevercong 已提交
493 494 495 496
let host = "xxxx";
connection.getAddressesByName(host, function (error, addresses) {
    console.log(JSON.stringify(error))
    console.log(JSON.stringify(addresses))
497 498 499 500 501
})
```

## connection.getAddressesByName

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

Z
zengyawen 已提交
504
使用默认网络解析主机名以获取所有IP地址,使用Promise方式作为异步方法。
505 506 507 508 509 510 511

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

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

**参数:**

Z
zengyawen 已提交
512 513 514
| 参数名 | 类型   | 必填 | 说明               |
| ------ | ------ | ---- | ------------------ |
| host   | string | 是   | 需要解析的主机名。 |
515 516 517

**返回值:**

Z
zengyawen 已提交
518 519 520
| 类型                                        | 说明                          |
| ------------------------------------------- | ----------------------------- |
| Promise\<Array\<[NetAddress](#netaddress)>> | 以Promise形式返回所有IP地址。 |
521 522 523

**示例:**

Z
zengyawen 已提交
524
```js
C
clevercong 已提交
525 526 527
let host = "xxxx";
connection.getAddressesByName(host).then(function (addresses) {
    console.log(JSON.stringify(addresses))
528 529 530
})
```

C
clevercong 已提交
531 532 533 534 535 536
## connection.enableAirplaneMode

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

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

L
liyufan 已提交
537
**系统接口**:此接口为系统接口。
C
clevercong 已提交
538

C
clevercong 已提交
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

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

**示例:**

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

## connection.enableAirplaneMode

enableAirplaneMode(): Promise\<void>

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

L
liyufan 已提交
561
**系统接口**:此接口为系统接口。
C
clevercong 已提交
562

C
clevercong 已提交
563 564 565 566 567 568
**系统能力**:SystemCapability.Communication.NetManager.Core

**返回值:**

| 类型                                        | 说明                          |
| ------------------------------------------- | ----------------------------- |
569
| Promise\<void> | 无返回值的Promise对象。 |
C
clevercong 已提交
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584

**示例:**

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

## connection.disableAirplaneMode

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

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

L
liyufan 已提交
585
**系统接口**:此接口为系统接口。
C
clevercong 已提交
586

C
clevercong 已提交
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

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

**示例:**

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

## connection.disableAirplaneMode

disableAirplaneMode(): Promise\<void>

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

L
liyufan 已提交
609
**系统接口**:此接口为系统接口。
C
clevercong 已提交
610

C
clevercong 已提交
611 612 613 614 615 616
**系统能力**:SystemCapability.Communication.NetManager.Core

**返回值:**

| 类型                                        | 说明                          |
| ------------------------------------------- | ----------------------------- |
617
| Promise\<void> | 无返回值的Promise对象。 |
C
clevercong 已提交
618 619 620 621 622 623 624 625 626

**示例:**

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

627 628 629 630
## connection.createNetConnection

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

631
返回一个NetConnection对象,netSpecifier指定关注的网络的各项特征,timeout是超时时间(单位是毫秒),netSpecifier是timeout的必要条件,两者都没有则表示关注默认网络。
632 633 634 635 636

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

**参数:**

Z
zengyawen 已提交
637 638 639 640
| 参数名       | 类型                          | 必填 | 说明                                                         |
| ------------ | ----------------------------- | ---- | ------------------------------------------------------------ |
| netSpecifier | [NetSpecifier](#netspecifier) | 否   | 指定网络的各项特征,不指定则关注默认网络。                   |
| timeout      | number                        | 否   | 获取netSpecifier指定的网络时的超时时间,仅netSpecifier存在时生效。 |
641 642 643

**返回值:**

Z
zengyawen 已提交
644 645 646
| 类型                            | 说明                 |
| ------------------------------- | -------------------- |
| [NetConnection](#netconnection) | 所关注的网络的句柄。 |
647 648 649

**示例:**

Z
zengyawen 已提交
650
```js
651
// 关注默认网络
C
clevercong 已提交
652
let netConnection = connection.createNetConnection()
653 654

// 关注蜂窝网络
C
clevercong 已提交
655
let netConnectionCellular = connection.createNetConnection({
656
    netCapabilities: {
C
clevercong 已提交
657
        bearerTypes: [connection.NetBearType.BEARER_CELLULAR]
658 659 660 661
    }
})
```

M
maosiping 已提交
662
## NetConnection
663

Z
zengyawen 已提交
664
网络连接的句柄。
665

M
maosiping 已提交
666
### on('netAvailable')
667 668 669

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

C
clevercong 已提交
670
订阅网络可用事件。
671 672 673 674 675

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

**参数:**

Z
zengyawen 已提交
676 677
| 参数名   | 类型                               | 必填 | 说明                                                         |
| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ |
C
clevercong 已提交
678
| type     | string                             | 是   | 订阅事件,固定为'netAvailable'。<br>netAvailable:数据网络可用事件。 |
Z
zengyawen 已提交
679
| callback | Callback\<[NetHandle](#nethandle)> | 是   | 回调函数。                                                   |
680 681 682

**示例:**

Z
zengyawen 已提交
683
```js
C
clevercong 已提交
684
netConnection.on('netAvailable', function (data) {
685 686 687 688
    console.log(JSON.stringify(data))
})
```

M
maosiping 已提交
689
### on('netCapabilitiesChange')
690 691 692

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

C
clevercong 已提交
693
订阅网络能力变化事件。
694 695 696 697 698

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

**参数:**

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

**示例:**

Z
zengyawen 已提交
706
```js
C
clevercong 已提交
707
netConnection.on('netCapabilitiesChange', function (data) {
708 709 710 711
    console.log(JSON.stringify(data))
})
```

M
maosiping 已提交
712
### on('netConnectionPropertiesChange')
713 714 715

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

C
clevercong 已提交
716
订阅网络连接信息变化事件。
717 718 719 720 721

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

**参数:**

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

**示例:**

Z
zengyawen 已提交
729
```js
C
clevercong 已提交
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
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 已提交
752
```js
C
clevercong 已提交
753
netConnection.on('netBlockStatusChange', function (data) {
754 755 756 757
    console.log(JSON.stringify(data))
})
```

M
maosiping 已提交
758
### on('netLost')
759 760 761

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

C
clevercong 已提交
762
订阅网络丢失事件。
763 764 765 766 767

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

**参数:**

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

**示例:**

Z
zengyawen 已提交
775
```js
C
clevercong 已提交
776 777
let netConnection1 = connection.createNetConnection()
netConnection1.on('netLost', function (data) {
778 779 780 781
    console.log(JSON.stringify(data))
})
```

M
maosiping 已提交
782
### on('netUnavailable')
783 784 785

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

C
clevercong 已提交
786
订阅网络不可用事件。
787 788 789 790 791

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

**参数:**

Z
zengyawen 已提交
792 793
| 参数名   | 类型            | 必填 | 说明                                                         |
| -------- | --------------- | ---- | ------------------------------------------------------------ |
C
clevercong 已提交
794
| type     | string          | 是   | 订阅事件,固定为'netUnavailable'。<br/>netUnavailable:网络不可用事件。 |
Z
zengyawen 已提交
795
| callback | Callback\<void> | 是   | 回调函数。                                                   |
796 797 798

**示例:**

Z
zengyawen 已提交
799
```js
C
clevercong 已提交
800
netConnection.on('netUnavailable', function (data) {
801 802 803 804
    console.log(JSON.stringify(data))
})
```

M
maosiping 已提交
805
### register
806 807 808

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

C
clevercong 已提交
809
订阅指定网络状态变化的通知。
810 811 812 813 814 815 816

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

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

**参数:**

Z
zengyawen 已提交
817 818 819
| 参数名   | 类型                 | 必填 | 说明       |
| -------- | -------------------- | ---- | ---------- |
| callback | AsyncCallback\<void> | 是   | 回调函数。 |
820 821 822

**示例:**

Z
zengyawen 已提交
823
```js
C
clevercong 已提交
824
netConnection.register(function (error) {
825 826 827 828
    console.log(JSON.stringify(error))
})
```

M
maosiping 已提交
829
### unregister
830 831 832

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

C
clevercong 已提交
833
取消订阅默认网络状态变化的通知。
834 835 836 837 838

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

**参数:**

Z
zengyawen 已提交
839 840 841
| 参数名   | 类型                 | 必填 | 说明       |
| -------- | -------------------- | ---- | ---------- |
| callback | AsyncCallback\<void> | 是   | 回调函数。 |
842 843 844

**示例:**

Z
zengyawen 已提交
845
```js
C
clevercong 已提交
846
netConnection.unregister(function (error) {
847 848 849 850
    console.log(JSON.stringify(error))
})
```

M
maosiping 已提交
851
## NetHandle
852

Z
zengyawen 已提交
853
数据网络的句柄。
854

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

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

Z
zengyawen 已提交
859 860
### 属性

L
liyufan 已提交
861 862 863
| 名称    | 类型   | 必填 | 说明                      |
| ------ | ------ | --- |------------------------- |
| netId  | number | 是  |  网络ID,取值为0代表没有默认网络,其余取值必须大于等于100。 |
864

L
liyufan 已提交
865
### bindSocket<sup>9+</sup>
Z
zhuwenchao 已提交
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882

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

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

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

**参数:**

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

**示例:**

```js
Y
YOUR_NAME 已提交
883 884
import socket from "@ohos.net.socket";
connection.getDefaultNet().then((netHandle)=>{
Z
zhuwenchao 已提交
885 886
    var tcp = socket.constructTCPSocketInstance();
    var udp = socket.constructUDPSocketInstance();
Y
YOUR_NAME 已提交
887
    let socketType = "TCPSocket";
Z
zhuwenchao 已提交
888 889
    if (socketType == "TCPSocket") {
        tcp.bind({
Y
YOUR_NAME 已提交
890
            address: '192.168.xx.xxx', port: xxxx, family: 1
Z
zhuwenchao 已提交
891
        }, err => {
Y
YOUR_NAME 已提交
892 893 894 895 896 897 898 899 900 901
            if (err) {
                console.log('bind fail');
            }
            netHandle.bindSocket(tcp, (error, data)=>{
                if (error) {
                    console.log(JSON.stringify(error));
                } else {
                    console.log(JSON.stringify(data));
                }
            })
Z
zhuwenchao 已提交
902 903
        })
    } else {
Y
YOUR_NAME 已提交
904 905 906
        let callback = value => {
            console.log(TAG + "on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo);
        }
Z
zhuwenchao 已提交
907 908
        udp.on('message', callback);
        udp.bind({
Y
YOUR_NAME 已提交
909
            address: '192.168.xx.xxx', port: xxxx, family: 1
Z
zhuwenchao 已提交
910
        }, err => {
Y
YOUR_NAME 已提交
911 912 913
            if (err) {
                console.log('bind fail');
            }
Z
zhuwenchao 已提交
914
            udp.on('message', (data) => {
Y
YOUR_NAME 已提交
915
                console.log(JSON.stringify(data))
Z
zhuwenchao 已提交
916
            });
Y
YOUR_NAME 已提交
917 918 919 920 921 922 923
            netHandle.bindSocket(udp,(error, data)=>{
                if (error) {
                    console.log(JSON.stringify(error));
                } else {
                    console.log(JSON.stringify(data));
                }
            })
Z
zhuwenchao 已提交
924
        })
Y
YOUR_NAME 已提交
925 926
    }
})
Z
zhuwenchao 已提交
927 928
```

L
liyufan 已提交
929
### bindSocket<sup>9+</sup>
Z
zhuwenchao 已提交
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946

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

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

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

**参数:**

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

**返回值:**

| 类型           | 说明                   |
| -------------- | ---------------------- |
947
| Promise\<void> | 无返回值的Promise对象。 |
Z
zhuwenchao 已提交
948 949 950 951

**示例:**

```js
Y
YOUR_NAME 已提交
952 953
import socket from "@ohos.net.socket";
connection.getDefaultNet().then((netHandle)=>{
Z
zhuwenchao 已提交
954 955
    var tcp = socket.constructTCPSocketInstance();
    var udp = socket.constructUDPSocketInstance();
Y
YOUR_NAME 已提交
956 957
    let socketType = "TCPSocket";
    if (socketType == "TCPSocket") {
Z
zhuwenchao 已提交
958
        tcp.bind({
Y
YOUR_NAME 已提交
959
            address: '192.168.xx.xxx', port: xxxx, family: 1
Z
zhuwenchao 已提交
960
        }, err => {
Y
YOUR_NAME 已提交
961 962 963 964 965 966 967 968 969 970
            if (err) {
                console.log('bind fail');
            }
            netHandle.bindSocket(tcp).then((err, data) => {
                if (err) {
                    console.log(JSON.stringify(err));
                } else {
                    console.log(JSON.stringify(data));
                }
            })
Z
zhuwenchao 已提交
971 972
        })
    } else {
Y
YOUR_NAME 已提交
973 974 975
        let callback = value => {
            console.log(TAG + "on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo);
        }
Z
zhuwenchao 已提交
976 977
        udp.on('message', callback);
        udp.bind({
Y
YOUR_NAME 已提交
978
            address: '192.168.xx.xxx', port: xxxx, family: 1
Z
zhuwenchao 已提交
979
        }, err => {
Y
YOUR_NAME 已提交
980 981 982
            if (err) {
                console.log('bind fail');
            }
Z
zhuwenchao 已提交
983
            udp.on('message', (data) => {
Y
YOUR_NAME 已提交
984 985 986 987 988 989 990 991 992
                console.log(JSON.stringify(data));
            })
            netHandle.bindSocket(udp).then((err, data) => {
                if (err) {
                    console.log(JSON.stringify(err));
                } else {
                    console.log(JSON.stringify(data));
                }
            })
Z
zhuwenchao 已提交
993
        })
Y
YOUR_NAME 已提交
994 995
    }
})
Z
zhuwenchao 已提交
996 997
```

M
maosiping 已提交
998
### getAddressesByName
999 1000 1001

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

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

C
clevercong 已提交
1004 1005
**需要权限**:ohos.permission.GET_NETWORK_INFO

1006 1007 1008 1009
**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
1010 1011 1012 1013
| 参数名   | 类型                                              | 必填 | 说明               |
| -------- | ------------------------------------------------- | ---- | ------------------ |
| host     | string                                            | 是   | 需要解析的主机名。 |
| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | 是   | 回调函数           |
1014 1015 1016

**示例:**

Z
zengyawen 已提交
1017
```js
1018
connection.getDefaultNet().then(function (netHandle) {
C
clevercong 已提交
1019 1020
    let host = "xxxx";
    netHandle.getAddressesByName(host, function (error, addresses) {
1021
        console.log(JSON.stringify(error))
C
clevercong 已提交
1022
        console.log(JSON.stringify(addresses))
1023 1024 1025 1026
    })
})
```

M
maosiping 已提交
1027
### getAddressesByName
1028

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

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

C
clevercong 已提交
1033 1034
**需要权限**:ohos.permission.GET_NETWORK_INFO

1035 1036 1037 1038
**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
1039 1040 1041
| 参数名 | 类型   | 必填 | 说明               |
| ------ | ------ | ---- | ------------------ |
| host   | string | 是   | 需要解析的主机名。 |
1042 1043 1044

**返回值:**

Z
zengyawen 已提交
1045 1046 1047
| 类型                                        | 说明                          |
| ------------------------------------------- | ----------------------------- |
| Promise\<Array\<[NetAddress](#netaddress)>> | 以Promise形式返回所有IP地址。 |
1048 1049 1050

**示例:**

Z
zengyawen 已提交
1051
```js
1052
connection.getDefaultNet().then(function (netHandle) {
C
clevercong 已提交
1053 1054 1055
    let host = "xxxx";
    netHandle.getAddressesByName(host).then(function (addresses) {
        console.log(JSON.stringify(addresses))
1056 1057 1058 1059
    })
})
```

M
maosiping 已提交
1060
### getAddressByName
1061 1062 1063

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

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

C
clevercong 已提交
1066 1067
**需要权限**:ohos.permission.GET_NETWORK_INFO

1068 1069 1070 1071
**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
1072 1073 1074 1075
| 参数名   | 类型                                      | 必填 | 说明               |
| -------- | ----------------------------------------- | ---- | ------------------ |
| host     | string                                    | 是   | 需要解析的主机名。 |
| callback | AsyncCallback\<[NetAddress](#netaddress)> | 是   | 回调函数。         |
1076 1077 1078

**示例:**

Z
zengyawen 已提交
1079
```js
1080
connection.getDefaultNet().then(function (netHandle) {
C
clevercong 已提交
1081 1082
    let host = "xxxx";
    netHandle.getAddressByName(host, function (error, address) {
1083
        console.log(JSON.stringify(error))
C
clevercong 已提交
1084
        console.log(JSON.stringify(address))
1085 1086 1087 1088
    })
})
```

M
maosiping 已提交
1089
### getAddressByName
1090

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

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

C
clevercong 已提交
1095 1096
**需要权限**:ohos.permission.GET_NETWORK_INFO

1097 1098 1099 1100
**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
1101 1102 1103
| 参数名 | 类型   | 必填 | 说明               |
| ------ | ------ | ---- | ------------------ |
| host   | string | 是   | 需要解析的主机名。 |
1104 1105 1106

**返回值:**

Z
zengyawen 已提交
1107 1108 1109
| 类型                                | 说明                            |
| ----------------------------------- | ------------------------------- |
| Promise\<[NetAddress](#netaddress)> | 以Promise形式返回第一个IP地址。 |
1110 1111 1112

**示例:**

Z
zengyawen 已提交
1113
```js
1114
connection.getDefaultNet().then(function (netHandle) {
C
clevercong 已提交
1115 1116 1117
    let host = "xxxx";
    netHandle.getAddressByName(host).then(function (address) {
        console.log(JSON.stringify(address))
1118 1119 1120 1121
    })
})
```

M
maosiping 已提交
1122
## NetSpecifier
1123

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

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

L
liyufan 已提交
1128 1129 1130 1131
| 名称                     | 类型                                | 必填  | 说明                                                         |
| ----------------------- | ----------------------------------- | ---- | ------------------------------------------------------------ |
| netCapabilities         | [NetCapabilities](#netcapabilities) |  是  | 存储数据网络的传输能力和承载类型。                                |
| bearerPrivateIdentifier | string                              |  否  |  网络标识符,Wi-Fi网络的标识符是"wifi",蜂窝网络的标识符是"slot0"(对应SIM卡1)。 |
1132

M
maosiping 已提交
1133
## NetCapabilities
1134

M
maosiping 已提交
1135
网络的能力集。
1136

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

L
liyufan 已提交
1139 1140 1141 1142 1143 1144
| 名称                  | 类型                                | 必填 | 说明                     |
| --------------------- | ---------------------------------- | --- | ------------------------ |
| linkUpBandwidthKbps   | number                             |  否 |  上行(设备到网络)带宽。  |
| linkDownBandwidthKbps | number                             |  否 |  下行(网络到设备)带宽。   |
| networkCap            | Array\<[NetCap](#netcap)>           |  否 |  网络具体能力。           |
| bearerTypes           | Array\<[NetBearType](#netbeartype)> |  是 |  网络类型。               |
1145

M
maosiping 已提交
1146
## NetCap
1147

M
maosiping 已提交
1148
网络具体能力。
1149

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

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

M
maosiping 已提交
1160
## NetBearType
1161

M
maosiping 已提交
1162
网络类型。
1163

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

| 名称         | 值   | 说明        |
Z
zengyawen 已提交
1167 1168 1169
| --------------- | ---- | ----------- |
| BEARER_CELLULAR | 0    | 蜂窝网络。  |
| BEARER_WIFI     | 1    | Wi-Fi网络。 |
C
clevercong 已提交
1170
| BEARER_ETHERNET | 3 | 以太网网络。 |
1171

M
maosiping 已提交
1172
## ConnectionProperties
1173

M
maosiping 已提交
1174
网络连接信息。
1175

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

L
liyufan 已提交
1178 1179 1180 1181 1182 1183 1184 1185
| 名称           | 类型                               | 必填 |  说明             |
| ------------- | ---------------------------------- | ----|---------------- |
| interfaceName | string                             | 是 |网卡名称。       |
| domains       | string                             | 是 |所属域,默认""。 |
| linkAddresses | Array\<[LinkAddress](#linkaddress)> | 是 |链路信息。       |
| routes        | Array\<[RouteInfo](#routeinfo)>     | 是 |路由信息。       |
| dnses     | Array\<[NetAddress](#netaddress)>; | 是 |网络地址,参考[NetAddress](#netaddress)。 |
| mtu           | number                             | 是 |最大传输单元。   |
1186

M
maosiping 已提交
1187
## LinkAddress
1188

M
maosiping 已提交
1189
网络链路信息。
1190

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

L
liyufan 已提交
1193 1194 1195 1196
| 名称        | 类型                      | 必填 |说明                 |
| ------------ | ----------------------- |---- |-------------------- |
| address      | [NetAddress](#netaddress) | 是 | 链路地址。           |
| prefixLength | number                    | 是 |链路地址前缀的长度。 |
1197

M
maosiping 已提交
1198
## RouteInfo
1199

M
maosiping 已提交
1200
网络路由信息。
1201

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

L
liyufan 已提交
1204 1205 1206 1207 1208 1209 1210
| 名称           | 类型                        | 必填 |说明             |
| -------------- | --------------------------- | --- |---------------- |
| interface      | string                      | 是 |网卡名称。       |
| destination    | [LinkAddress](#linkaddress) | 是 |目的地址。       |
| gateway        | [NetAddress](#netaddress)   | 是 |网关地址。       |
| hasGateway     | boolean                     | 是 |是否有网关。     |
| isDefaultRoute | boolean                     | 是 |是否为默认路由。 |
1211

M
maosiping 已提交
1212
## NetAddress
1213

Z
zengyawen 已提交
1214
网络地址。
1215

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

L
liyufan 已提交
1218 1219 1220 1221 1222
| 名称    | 类型   | 必填 | 说明                           |
| ------- | ------ | -- |------------------------------ |
| address | string | 是 |地址。                         |
| family  | number | 否 |IPv4 = 1,IPv6 = 2,默认IPv4。 |
| port    | number | 否 |端口,取值范围\[0, 65535]。    |