1# @ohos.net.connection (网络连接管理) 2 3网络连接管理提供管理网络一些基础能力,包括获取默认激活的数据网络、获取所有激活数据网络列表、开启关闭飞行模式、获取网络能力信息等功能。 4 5> **说明:** 6> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 7 8## 导入模块 9 10```js 11import connection from '@ohos.net.connection' 12``` 13## connection.createNetConnection 14 15createNetConnection(netSpecifier?: NetSpecifier, timeout?: number): NetConnection 16 17返回一个NetConnection对象,netSpecifier指定关注的网络的各项特征,timeout是超时时间(单位是毫秒),netSpecifier是timeout的必要条件,两者都没有则表示关注默认网络。 18 19**系统能力**:SystemCapability.Communication.NetManager.Core 20 21**参数:** 22 23| 参数名 | 类型 | 必填 | 说明 | 24| ------------ | ----------------------------- | ---- | ------------------------------------------------------------ | 25| netSpecifier | [NetSpecifier](#netspecifier) | 否 | 指定网络的各项特征,不指定则关注默认网络。 | 26| timeout | number | 否 | 获取netSpecifier指定的网络时的超时时间,仅netSpecifier存在时生效。 | 27 28**返回值:** 29 30| 类型 | 说明 | 31| ------------------------------- | -------------------- | 32| [NetConnection](#netconnection) | 所关注的网络的句柄。 | 33 34**示例:** 35 36```js 37// 关注默认网络 38let netConnection = connection.createNetConnection() 39 40// 关注蜂窝网络 41let netConnectionCellular = connection.createNetConnection({ 42 netCapabilities: { 43 bearerTypes: [connection.NetBearType.BEARER_CELLULAR] 44 } 45}) 46``` 47 48## connection.getDefaultNet 49 50getDefaultNet(callback: AsyncCallback\<NetHandle>): void 51 52获取默认激活的数据网络,使用callback方式作为异步方法。可以使用[getNetCapabilities](#connectiongetnetcapabilities)去获取网络的类型、拥有的能力等信息。 53 54**需要权限**:ohos.permission.GET_NETWORK_INFO 55 56**系统能力**:SystemCapability.Communication.NetManager.Core 57 58**参数:** 59 60| 参数名 | 类型 | 必填 | 说明 | 61| -------- | --------------------------------------- | ---- | ---------- | 62| callback | AsyncCallback\<[NetHandle](#nethandle)> | 是 | 回调函数。当成功获取默认激活的数据网络时,err为undefined,data为默认激活的数据网络;否则为错误对象 | 63 64**错误码:** 65 66| 错误码ID | 错误信息 | 67| ------- | ----------------------------- | 68| 201 | Permission denied. | 69| 2100002 | Operation failed. Cannot connect to service.| 70| 2100003 | System internal error. | 71 72**示例:** 73 74```js 75connection.getDefaultNet(function (error, data) { 76 console.log(JSON.stringify(error)) 77 console.log(JSON.stringify(data)) 78}) 79``` 80 81## connection.getDefaultNet 82 83getDefaultNet(): Promise\<NetHandle> 84 85获取默认激活的数据网络,使用Promise方式作为异步方法。可以使用[getNetCapabilities](#connectiongetnetcapabilities)去获取网络的类型、拥有的能力等信息。 86 87**需要权限**:ohos.permission.GET_NETWORK_INFO 88 89**系统能力**:SystemCapability.Communication.NetManager.Core 90 91**返回值:** 92 93| 类型 | 说明 | 94| --------------------------------- | ------------------------------------- | 95| Promise\<[NetHandle](#nethandle)> | 以Promise形式返回默认激活的数据网络。 | 96 97**错误码:** 98 99| 错误码ID | 错误信息 | 100| ------- | ----------------------------- | 101| 201 | Permission denied. | 102| 2100002 | Operation failed. Cannot connect to service.| 103| 2100003 | System internal error. | 104 105**示例:** 106 107```js 108connection.getDefaultNet().then(function (data) { 109 console.log(JSON.stringify(data)) 110}) 111``` 112 113## connection.getDefaultNetSync<sup>9+</sup> 114 115getDefaultNetSync(): NetHandle 116 117使用同步方法获取默认激活的数据网络。可以使用[getNetCapabilities](#connectiongetnetcapabilities)去获取网络的类型、拥有的能力等信息。 118 119**需要权限**:ohos.permission.GET_NETWORK_INFO 120 121**系统能力**:SystemCapability.Communication.NetManager.Core 122 123**返回值:** 124 125| 类型 | 说明 | 126| --------- | ---------------------------------- | 127| NetHandle | 以同步方式返回默认激活的数据网络。 | 128 129**错误码:** 130 131| 错误码ID | 错误信息 | 132| ------- | ----------------------------- | 133| 201 | Permission denied. | 134| 2100002 | Operation failed. Cannot connect to service.| 135| 2100003 | System internal error. | 136 137**示例:** 138 139```js 140let netHandle = connection.getDefaultNetSync(); 141``` 142 143## connection.getAppNet<sup>9+</sup> 144 145getAppNet(callback: AsyncCallback\<NetHandle>): void 146 147获取App绑定的网络信息,使用callback方式作为异步方法。 148 149**系统能力**:SystemCapability.Communication.NetManager.Core 150 151**参数:** 152 153| 参数名 | 类型 | 必填 | 说明 | 154| --------- | ------------------------------------------------------------ | ---- | ---------------- | 155| callback | AsyncCallback\<[NetHandle](#nethandle)> | 是 | 回调函数。当成功获取App绑定的网络信息时,err为undefined,data为获取到App绑定的网络信息;否则为错误对象| 156 157**错误码:** 158 159| 错误码ID | 错误信息 | 160| ------- | ----------------------------- | 161| 2100002 | Operation failed. Cannot connect to service.| 162| 2100003 | System internal error. | 163 164**示例:** 165 166```js 167connection.getAppNet(function(error, data) { 168 console.log(JSON.stringify(error)) 169 console.log(JSON.stringify(data)) 170}) 171``` 172 173## connection.getAppNet<sup>9+</sup> 174 175getAppNet(): Promise\<NetHandle>; 176 177获取App绑定的网络信息,使用Promise方式作为异步方法。 178 179**系统能力**:SystemCapability.Communication.NetManager.Core 180 181**返回值:** 182 183| 类型 | 说明 | 184| --------------------------------- | ------------------------------------- | 185| Promise\<[NetHandle](#nethandle)> | 以Promise形式返回App绑定的网络信息。 | 186 187**错误码:** 188 189| 错误码ID | 错误信息 | 190| ------- | ----------------------------- | 191| 2100002 | Operation failed. Cannot connect to service.| 192| 2100003 | System internal error. | 193 194**示例:** 195 196```js 197connection.getAppNet().then((data) => { 198 console.info(JSON.stringify(data)); 199}).catch(error => { 200 console.info(JSON.stringify(error)); 201}) 202``` 203 204## connection.SetAppNet<sup>9+</sup> 205 206setAppNet(netHandle: NetHandle, callback: AsyncCallback\<void>): void 207 208绑定App到指定网络,绑定后的App只能通过指定网络访问外网,使用callback方式作为异步方法。 209 210**需要权限**:ohos.permission.INTERNET 211 212**系统能力**:SystemCapability.Communication.NetManager.Core 213 214**参数:** 215 216| 参数名 | 类型 | 必填 | 说明 | 217| --------- | ------------------------------------------------------------ | ---- | ---------------- | 218| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄。 | 219| callback | AsyncCallback\<void> | 是 | 回调函数。当成功绑定App到指定网络时,err为undefined,否则为错误对象| 220 221**错误码:** 222 223| 错误码ID | 错误信息 | 224| ------- | ----------------------------- | 225| 201 | Permission denied. | 226| 401 | Parameter error. | 227| 2100001 | Invalid parameter value. | 228| 2100002 | Operation failed. Cannot connect to service.| 229| 2100003 | System internal error. | 230 231**示例:** 232 233```js 234connection.getDefaultNet(function (error, netHandle) { 235 connection.setAppNet(netHandle, (error, data) => { 236 console.log(JSON.stringify(error)) 237 console.log(JSON.stringify(data)) 238 }); 239}) 240``` 241 242## connection.SetAppNet<sup>9+</sup> 243 244setAppNet(netHandle: NetHandle): Promise\<void>; 245 246绑定App到指定网络,绑定后的App只能通过指定网络访问外网,使用Promise方式作为异步方法。 247 248**需要权限**:ohos.permission.INTERNET 249 250**系统能力**:SystemCapability.Communication.NetManager.Core 251 252**参数:** 253 254| 参数名 | 类型 | 必填 | 说明 | 255| --------- | ------------------------------------------------------------ | ---- | ---------------- | 256| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄。 | 257 258**返回值:** 259 260| 类型 | 说明 | 261| ------------------------------------------- | ----------------------------- | 262| Promise\<void> | 无返回值的Promise对象。 | 263 264**错误码:** 265 266| 错误码ID | 错误信息 | 267| ------- | ----------------------------- | 268| 201 | Permission denied. | 269| 401 | Parameter error. | 270| 2100001 | Invalid parameter value. | 271| 2100002 | Operation failed. Cannot connect to service.| 272| 2100003 | System internal error. | 273 274**示例:** 275 276```js 277connection.getDefaultNet().then(function (netHandle) { 278 connection.setAppNet(netHandle).then(() => { 279 console.log("success") 280 }).catch(error => { 281 console.log(JSON.stringify(error)) 282 }) 283}) 284``` 285 286## connection.getAllNets 287 288getAllNets(callback: AsyncCallback<Array<NetHandle>>): void 289 290获取所有处于连接状态的网络列表,使用callback方式作为异步方法。 291 292**需要权限**:ohos.permission.GET_NETWORK_INFO 293 294**系统能力**:SystemCapability.Communication.NetManager.Core 295 296**参数:** 297 298| 参数名 | 类型 | 必填 | 说明 | 299| -------- | -------- | -------- | -------- | 300| callback | AsyncCallback<Array<[NetHandle](#nethandle)>> | 是 | 回调函数。当成功获取所有处于连接状态的网络列表时,err为undefined,data为激活的数据网络列表;否则为错误对象 | 301 302**错误码:** 303 304| 错误码ID | 错误信息 | 305| ------- | ----------------------------- | 306| 201 | Permission denied. | 307| 2100002 | Operation failed. Cannot connect to service.| 308| 2100003 | System internal error. | 309 310**示例:** 311 312```js 313connection.getAllNets(function (error, data) { 314 console.log(JSON.stringify(error)) 315 console.log(JSON.stringify(data)) 316}); 317``` 318 319## connection.getAllNets 320 321getAllNets(): Promise<Array<NetHandle>> 322 323获取所有处于连接状态的网络列表,使用Promise方式作为异步方法。 324 325**需要权限**:ohos.permission.GET_NETWORK_INFO 326 327**系统能力**:SystemCapability.Communication.NetManager.Core 328 329**返回值:** 330 331| 类型 | 说明 | 332| -------- | -------- | 333| Promise<Array<[NetHandle](#nethandle)>> | 以Promise形式返回激活的数据网络列表。 | 334 335**错误码:** 336 337| 错误码ID | 错误信息 | 338| ------- | ----------------------------- | 339| 201 | Permission denied. | 340| 2100002 | Operation failed. Cannot connect to service.| 341| 2100003 | System internal error. | 342 343**示例:** 344 345```js 346connection.getAllNets().then(function (data) { 347 console.log(JSON.stringify(data)) 348}); 349``` 350 351## connection.getConnectionProperties 352 353getConnectionProperties(netHandle: NetHandle, callback: AsyncCallback\<ConnectionProperties>): void 354 355获取netHandle对应的网络的连接信息,使用callback方式作为异步方法。 356 357**需要权限**:ohos.permission.GET_NETWORK_INFO 358 359**系统能力**:SystemCapability.Communication.NetManager.Core 360 361**参数:** 362 363| 参数名 | 类型 | 必填 | 说明 | 364| --------- | ------------------------------------------------------------ | ---- | ---------------- | 365| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄。 | 366| callback | AsyncCallback\<[ConnectionProperties](#connectionproperties)> | 是 | 回调函数。当成功获取netHandle对应的网络的连接信息时,err为undefined,data为获取的网络连接信息;否则为错误对象| 367 368**错误码:** 369 370| 错误码ID | 错误信息 | 371| ------- | ----------------------------- | 372| 201 | Permission denied. | 373| 401 | Parameter error. | 374| 2100001 | Invalid parameter value. | 375| 2100002 | Operation failed. Cannot connect to service.| 376| 2100003 | System internal error. | 377 378**示例:** 379 380```js 381connection.getDefaultNet().then(function (netHandle) { 382 connection.getConnectionProperties(netHandle, function (error, data) { 383 console.log(JSON.stringify(error)) 384 console.log(JSON.stringify(data)) 385 }) 386}) 387``` 388 389## connection.getConnectionProperties 390 391getConnectionProperties(netHandle: NetHandle): Promise\<ConnectionProperties> 392 393获取netHandle对应的网络的连接信息,使用Promise方式作为异步方法。 394 395**需要权限**:ohos.permission.GET_NETWORK_INFO 396 397**系统能力**:SystemCapability.Communication.NetManager.Core 398 399**参数:** 400 401| 参数名 | 类型 | 必填 | 说明 | 402| --------- | ----------------------- | ---- | ---------------- | 403| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄。 | 404 405**返回值:** 406 407| 类型 | 说明 | 408| ------------------------------------------------------- | --------------------------------- | 409| Promise\<[ConnectionProperties](#connectionproperties)> | 以Promise形式返回网络的连接信息。 | 410 411**错误码:** 412 413| 错误码ID | 错误信息 | 414| ------- | ----------------------------- | 415| 201 | Permission denied. | 416| 401 | Parameter error. | 417| 2100001 | Invalid parameter value. | 418| 2100002 | Operation failed. Cannot connect to service.| 419| 2100003 | System internal error. | 420 421**示例:** 422 423```js 424connection.getDefaultNet().then(function (netHandle) { 425 connection.getConnectionProperties(netHandle).then(function (data) { 426 console.log(JSON.stringify(data)) 427 }) 428}) 429``` 430 431## connection.getNetCapabilities 432 433getNetCapabilities(netHandle: NetHandle, callback: AsyncCallback\<NetCapabilities>): void 434 435获取netHandle对应的网络的能力信息,使用callback方式作为异步方法。 436 437**需要权限**:ohos.permission.GET_NETWORK_INFO 438 439**系统能力**:SystemCapability.Communication.NetManager.Core 440 441**参数:** 442 443| 参数名 | 类型 | 必填 | 说明 | 444| --------- | --------------------------------------------------- | ---- | ---------------- | 445| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄。 | 446| callback | AsyncCallback\<[NetCapabilities](#netcapabilities)> | 是 | 回调函数。当成功获取netHandle对应的网络的能力信息时,err为undefined,data为获取到的网络能力信息;否则为错误对象 | 447 448**错误码:** 449 450| 错误码ID | 错误信息 | 451| ------- | ----------------------------- | 452| 201 | Permission denied. | 453| 401 | Parameter error. | 454| 2100001 | Invalid parameter value. | 455| 2100002 | Operation failed. Cannot connect to service.| 456| 2100003 | System internal error. | 457 458**示例:** 459 460```js 461connection.getDefaultNet().then(function (netHandle) { 462 connection.getNetCapabilities(netHandle, function (error, data) { 463 console.log(JSON.stringify(error)) 464 console.log(JSON.stringify(data)) 465 }) 466}) 467``` 468 469## connection.getNetCapabilities 470 471getNetCapabilities(netHandle: NetHandle): Promise\<NetCapabilities> 472 473获取netHandle对应的网络的能力信息,使用Promise方式作为异步方法。 474 475**需要权限**:ohos.permission.GET_NETWORK_INFO 476 477**系统能力**:SystemCapability.Communication.NetManager.Core 478 479**参数:** 480 481| 参数名 | 类型 | 必填 | 说明 | 482| --------- | ----------------------- | ---- | ---------------- | 483| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄。 | 484 485**返回值:** 486 487| 类型 | 说明 | 488| --------------------------------------------- | --------------------------------- | 489| Promise\<[NetCapabilities](#netcapabilities)> | 以Promise形式返回网络的能力信息。 | 490 491**错误码:** 492 493| 错误码ID | 错误信息 | 494| ------- | ----------------------------- | 495| 201 | Permission denied. | 496| 401 | Parameter error. | 497| 2100001 | Invalid parameter value. | 498| 2100002 | Operation failed. Cannot connect to service.| 499| 2100003 | System internal error. | 500 501**示例:** 502 503```js 504connection.getDefaultNet().then(function (netHandle) { 505 connection.getNetCapabilities(netHandle).then(function (data) { 506 console.log(JSON.stringify(data)) 507 }) 508}) 509``` 510 511## connection.isDefaultNetMetered<sup>9+</sup> 512 513isDefaultNetMetered(callback: AsyncCallback\<boolean>): void 514 515检查当前网络上的数据流量使用是否被计量,使用callback方式作为异步方法。 516 517**需要权限**:ohos.permission.GET_NETWORK_INFO 518 519**系统能力**:SystemCapability.Communication.NetManager.Core 520 521**参数:** 522 523| 参数名 | 类型 | 必填 | 说明 | 524| -------- | ----------------------- | ---- | -------------------------------------- | 525| callback | AsyncCallback\<boolean> | 是 | 回调函数。当前网络上的数据流量使用被计量返回true。 | 526 527**错误码:** 528 529| 错误码ID | 错误信息 | 530| ------- | ----------------------------- | 531| 201 | Permission denied. | 532| 2100002 | Operation failed. Cannot connect to service.| 533| 2100003 | System internal error. | 534 535**示例:** 536 537```js 538connection.isDefaultNetMetered(function (error, data) { 539 console.log(JSON.stringify(error)) 540 console.log('data: ' + data) 541}) 542``` 543 544## connection.isDefaultNetMetered<sup>9+</sup> 545 546isDefaultNetMetered(): Promise\<boolean> 547 548检查当前网络上的数据流量使用是否被计量,使用Promise方式作为异步方法。 549 550**需要权限**:ohos.permission.GET_NETWORK_INFO 551 552**系统能力**:SystemCapability.Communication.NetManager.Core 553 554**返回值:** 555 556| 类型 | 说明 | 557| ----------------- | ----------------------------------------------- | 558| Promise\<boolean> | 以Promise形式返回,当前网络上的数据流量使用被计量true。 | 559 560**错误码:** 561 562| 错误码ID | 错误信息 | 563| ------- | ----------------------------- | 564| 201 | Permission denied. | 565| 2100002 | Operation failed. Cannot connect to service.| 566| 2100003 | System internal error. | 567 568**示例:** 569 570```js 571connection.isDefaultNetMetered().then(function (data) { 572 console.log('data: ' + data) 573}) 574``` 575 576## connection.hasDefaultNet 577 578hasDefaultNet(callback: AsyncCallback\<boolean>): void 579 580检查默认数据网络是否被激活,使用callback方式作为异步方法。如果有默认数据网路,可以使用[getDefaultNet](#connectiongetdefaultnet)去获取。 581 582**需要权限**:ohos.permission.GET_NETWORK_INFO 583 584**系统能力**:SystemCapability.Communication.NetManager.Core 585 586**参数:** 587 588| 参数名 | 类型 | 必填 | 说明 | 589| -------- | ----------------------- | ---- | -------------------------------------- | 590| callback | AsyncCallback\<boolean> | 是 | 回调函数。默认数据网络被激活返回true。 | 591 592**错误码:** 593 594| 错误码ID | 错误信息 | 595| ------- | ----------------------------- | 596| 201 | Permission denied. | 597| 2100002 | Operation failed. Cannot connect to service.| 598| 2100003 | System internal error. | 599 600**示例:** 601 602```js 603connection.hasDefaultNet(function (error, data) { 604 console.log(JSON.stringify(error)) 605 console.log('data: ' + data) 606}) 607``` 608 609## connection.hasDefaultNet 610 611hasDefaultNet(): Promise\<boolean> 612 613检查默认数据网络是否被激活,使用Promise方式作为异步方法。如果有默认数据网路,可以使用[getDefaultNet](#connectiongetdefaultnet)去获取。 614 615**需要权限**:ohos.permission.GET_NETWORK_INFO 616 617**系统能力**:SystemCapability.Communication.NetManager.Core 618 619**返回值:** 620 621| 类型 | 说明 | 622| ----------------- | ----------------------------------------------- | 623| Promise\<boolean> | 以Promise形式返回,默认数据网络被激活返回true。 | 624 625**错误码:** 626 627| 错误码ID | 错误信息 | 628| ------- | ----------------------------- | 629| 201 | Permission denied. | 630| 2100002 | Operation failed. Cannot connect to service.| 631| 2100003 | System internal error. | 632 633**示例:** 634 635```js 636connection.hasDefaultNet().then(function (data) { 637 console.log('data: ' + data) 638}) 639``` 640 641## connection.enableAirplaneMode 642 643enableAirplaneMode(callback: AsyncCallback\<void>): void 644 645开启飞行模式,使用callback方式作为异步方法。 646 647**系统接口**:此接口为系统接口。 648 649**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 650 651**系统能力**:SystemCapability.Communication.NetManager.Core 652 653**参数:** 654 655| 参数名 | 类型 | 必填 | 说明 | 656| -------- | ------------------------------------------------- | ---- | ------------------ | 657| callback | AsyncCallback\<void> | 是 | 回调函数。 | 658 659**错误码:** 660 661| 错误码ID | 错误信息 | 662| ------- | ----------------------------- | 663| 2100002 | Operation failed. Cannot connect to service.| 664| 2100003 | System internal error. | 665 666**示例:** 667 668```js 669connection.enableAirplaneMode(function (error) { 670 console.log(JSON.stringify(error)) 671}) 672``` 673 674## connection.enableAirplaneMode 675 676enableAirplaneMode(): Promise\<void> 677 678开启飞行模式,使用Promise方式作为异步方法。 679 680**系统接口**:此接口为系统接口。 681 682**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 683 684**系统能力**:SystemCapability.Communication.NetManager.Core 685 686**返回值:** 687 688| 类型 | 说明 | 689| ------------------------------------------- | ----------------------------- | 690| Promise\<void> | 无返回值的Promise对象。 | 691 692**错误码:** 693 694| 错误码ID | 错误信息 | 695| ------- | ----------------------------- | 696| 2100002 | Operation failed. Cannot connect to service.| 697| 2100003 | System internal error. | 698 699**示例:** 700 701```js 702connection.enableAirplaneMode().then(function (error) { 703 console.log(JSON.stringify(error)) 704}) 705``` 706 707## connection.disableAirplaneMode 708 709disableAirplaneMode(callback: AsyncCallback\<void>): void 710 711关闭飞行模式,使用callback方式作为异步方法。 712 713**系统接口**:此接口为系统接口。 714 715**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 716 717**系统能力**:SystemCapability.Communication.NetManager.Core 718 719**参数:** 720 721| 参数名 | 类型 | 必填 | 说明 | 722| -------- | ------------------------------------------------- | ---- | ------------------ | 723| callback | AsyncCallback\<void> | 是 | 回调函数。当关闭飞行模式成功,err为undefined,否则为错误对象。| 724 725**错误码:** 726 727| 错误码ID | 错误信息 | 728| ------- | ----------------------------- | 729| 2100002 | Operation failed. Cannot connect to service.| 730| 2100003 | System internal error. | 731 732**示例:** 733 734```js 735connection.disableAirplaneMode(function (error) { 736 console.log(JSON.stringify(error)) 737}) 738``` 739 740## connection.disableAirplaneMode 741 742disableAirplaneMode(): Promise\<void> 743 744关闭飞行模式,使用Promise方式作为异步方法。 745 746**系统接口**:此接口为系统接口。 747 748**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 749 750**系统能力**:SystemCapability.Communication.NetManager.Core 751 752**返回值:** 753 754| 类型 | 说明 | 755| ------------------------------------------- | ----------------------------- | 756| Promise\<void> | 无返回值的Promise对象。 | 757 758**错误码:** 759 760| 错误码ID | 错误信息 | 761| ------- | ----------------------------- | 762| 2100002 | Operation failed. Cannot connect to service.| 763| 2100003 | System internal error. | 764 765**示例:** 766 767```js 768connection.disableAirplaneMode().then(function (error) { 769 console.log(JSON.stringify(error)) 770}) 771``` 772 773## connection.reportNetConnected 774 775reportNetConnected(netHandle: NetHandle, callback: AsyncCallback<void>): void 776 777向网络管理报告网络处于可用状态,调用此接口说明应用程序认为网络的可用性(ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED)与网络管理不一致。 778使用callback方式作为异步方法。 779 780**需要权限**:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET 781 782**系统能力**:SystemCapability.Communication.NetManager.Core 783 784**参数:** 785 786| 参数名 | 类型 | 必填 | 说明 | 787| -------- | -------- | -------- | -------- | 788| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 | 789| callback | AsyncCallback<void> | 是 | 回调函数。当向网络管理报告网络处于可用状态成功,err为undefined,否则为错误对象。 | 790 791**错误码:** 792 793| 错误码ID | 错误信息 | 794| ------- | ----------------------------- | 795| 201 | Permission denied. | 796| 401 | Parameter error. | 797| 2100001 | Invalid parameter value. | 798| 2100002 | Operation failed. Cannot connect to service.| 799| 2100003 | System internal error. | 800 801**示例:** 802 803```js 804connection.getDefaultNet().then(function (netHandle) { 805 connection.reportNetConnected(netHandle, function (error) { 806 console.log(JSON.stringify(error)) 807 }); 808}); 809``` 810 811## connection.reportNetConnected 812 813reportNetConnected(netHandle: NetHandle): Promise<void> 814 815向网络管理报告网络处于可用状态,调用此接口说明应用程序认为网络的可用性(ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED)与网络管理不一致。 816使用Promise方式作为异步方法。 817 818**需要权限**:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET 819 820**系统能力**:SystemCapability.Communication.NetManager.Core 821 822**参数:** 823 824| 参数名 | 类型 | 必填 | 说明 | 825| -------- | -------- | -------- | -------- | 826| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 | 827 828**返回值:** 829| 类型 | 说明 | 830| -------- | -------- | 831| Promise<void> | 无返回值的Promise对象。 | 832 833**错误码:** 834 835| 错误码ID | 错误信息 | 836| ------- | ----------------------------- | 837| 201 | Permission denied. | 838| 401 | Parameter error. | 839| 2100001 | Invalid parameter value. | 840| 2100002 | Operation failed. Cannot connect to service.| 841| 2100003 | System internal error. | 842 843**示例:** 844 845```js 846connection.getDefaultNet().then(function (netHandle) { 847 connection.reportNetConnected(netHandle).then(function () { 848 console.log(`report success`) 849 }); 850}); 851``` 852 853## connection.reportNetDisconnected 854 855reportNetDisconnected(netHandle: NetHandle, callback: AsyncCallback<void>): void 856 857向网络管理报告网络处于不可用状态,调用此接口说明应用程序认为网络的可用性(ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED)与网络管理不一致。 858使用callback方式作为异步方法。 859 860**需要权限**:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET 861 862**系统能力**:SystemCapability.Communication.NetManager.Core 863 864**参数:** 865 866| 参数名 | 类型 | 必填 | 说明 | 867| -------- | -------- | -------- | -------- | 868| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 | 869| callback | AsyncCallback<void> | 是 | 回调函数。当向网络管理报告网络处于不可用状态成功,err为undefined,否则为错误对象。 | 870 871**错误码:** 872 873| 错误码ID | 错误信息 | 874| ------- | ----------------------------- | 875| 201 | Permission denied. | 876| 401 | Parameter error. | 877| 2100001 | Invalid parameter value. | 878| 2100002 | Operation failed. Cannot connect to service.| 879| 2100003 | System internal error. | 880 881**示例:** 882 883```js 884connection.getDefaultNet().then(function (netHandle) { 885 connection.reportNetDisconnected(netHandle, function (error) { 886 console.log(JSON.stringify(error)) 887 }); 888}); 889``` 890 891## connection.reportNetDisconnected 892 893reportNetDisconnected(netHandle: NetHandle): Promise<void> 894 895向网络管理报告网络处于不可用状态,调用此接口说明应用程序认为网络的可用性(ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED)与网络管理不一致。 896使用Promise方式作为异步方法。 897 898**需要权限**:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET 899 900**系统能力**:SystemCapability.Communication.NetManager.Core 901 902**参数:** 903 904| 参数名 | 类型 | 必填 | 说明 | 905| -------- | -------- | -------- | -------- | 906| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 | 907 908**返回值:** 909| 类型 | 说明 | 910| -------- | -------- | 911| Promise<void> | 无返回值的Promise对象。 | 912 913**错误码:** 914 915| 错误码ID | 错误信息 | 916| ------- | ----------------------------- | 917| 201 | Permission denied. | 918| 401 | Parameter error. | 919| 2100001 | Invalid parameter value. | 920| 2100002 | Operation failed. Cannot connect to service.| 921| 2100003 | System internal error. | 922 923**示例:** 924 925```js 926connection.getDefaultNet().then(function (netHandle) { 927 connection.reportNetDisconnected(netHandle).then(function () { 928 console.log(`report success`) 929 }); 930}); 931``` 932 933## connection.getAddressesByName 934 935getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>>): void 936 937使用默认网络解析主机名以获取所有IP地址,使用callback方式作为异步方法。 938 939**需要权限**:ohos.permission.INTERNET 940 941**系统能力**:SystemCapability.Communication.NetManager.Core 942 943**参数:** 944 945| 参数名 | 类型 | 必填 | 说明 | 946| -------- | ------------------------------------------------- | ---- | ------------------ | 947| host | string | 是 | 需要解析的主机名。 | 948| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | 是 | 回调函数。当使用默认网络解析主机名成功获取所有IP地址,err为undefined,data为获取到的所有IP地址;否则为错误对象。| 949 950**错误码:** 951 952| 错误码ID | 错误信息 | 953| ------- | ----------------------------- | 954| 201 | Permission denied. | 955| 401 | Parameter error. | 956| 2100001 | Invalid parameter value. | 957| 2100002 | Operation failed. Cannot connect to service.| 958| 2100003 | System internal error. | 959 960**示例:** 961 962```js 963let host = "xxxx"; 964connection.getAddressesByName(host, function (error, data) { 965 console.log(JSON.stringify(error)) 966 console.log(JSON.stringify(data)) 967}) 968``` 969 970## connection.getAddressesByName 971 972getAddressesByName(host: string): Promise\<Array\<NetAddress>> 973 974使用默认网络解析主机名以获取所有IP地址,使用Promise方式作为异步方法。 975 976**需要权限**:ohos.permission.INTERNET 977 978**系统能力**:SystemCapability.Communication.NetManager.Core 979 980**参数:** 981 982| 参数名 | 类型 | 必填 | 说明 | 983| ------ | ------ | ---- | ------------------ | 984| host | string | 是 | 需要解析的主机名。 | 985 986**返回值:** 987 988| 类型 | 说明 | 989| ------------------------------------------- | ----------------------------- | 990| Promise\<Array\<[NetAddress](#netaddress)>> | 以Promise形式返回所有IP地址。 | 991 992**错误码:** 993 994| 错误码ID | 错误信息 | 995| ------- | ----------------------------- | 996| 201 | Permission denied. | 997| 401 | Parameter error. | 998| 2100001 | Invalid parameter value. | 999| 2100002 | Operation failed. Cannot connect to service.| 1000| 2100003 | System internal error. | 1001 1002**示例:** 1003 1004```js 1005let host = "xxxx"; 1006connection.getAddressesByName(host).then(function (data) { 1007 console.log(JSON.stringify(data)) 1008}) 1009``` 1010 1011## NetConnection 1012 1013网络连接的句柄。 1014 1015### register 1016 1017register(callback: AsyncCallback\<void>): void 1018 1019订阅指定网络状态变化的通知。 1020 1021**需要权限**:ohos.permission.GET_NETWORK_INFO 1022 1023**系统能力**:SystemCapability.Communication.NetManager.Core 1024 1025**参数:** 1026 1027| 参数名 | 类型 | 必填 | 说明 | 1028| -------- | -------------------- | ---- | ---------- | 1029| callback | AsyncCallback\<void> | 是 | 回调函数。当订阅指定网络状态变化的通知成功,err为undefined,否则为错误对象。 | 1030 1031**错误码:** 1032 1033| 错误码ID | 错误信息 | 1034| ------- | ----------------------------- | 1035| 201 | Permission denied. | 1036| 2100002 | Operation failed. Cannot connect to service.| 1037| 2100003 | System internal error. | 1038| 2101008 | The callback is not exists. | 1039| 2101022 | The number of requests exceeded the maximum. | 1040 1041 1042**示例:** 1043 1044```js 1045netConnection.register(function (error) { 1046 console.log(JSON.stringify(error)) 1047}) 1048``` 1049 1050### unregister 1051 1052unregister(callback: AsyncCallback\<void>): void 1053 1054取消订阅默认网络状态变化的通知。 1055 1056**系统能力**:SystemCapability.Communication.NetManager.Core 1057 1058**参数:** 1059 1060| 参数名 | 类型 | 必填 | 说明 | 1061| -------- | -------------------- | ---- | ---------- | 1062| callback | AsyncCallback\<void> | 是 | 回调函数。当取消订阅指定网络状态变化的通知成功,err为undefined,否则为错误对象。 | 1063 1064**错误码:** 1065 1066| 错误码ID | 错误信息 | 1067| ------- | ----------------------------- | 1068| 2100002 | Operation failed. Cannot connect to service.| 1069| 2100003 | System internal error. | 1070| 2101007 | The same callback exists. | 1071 1072**示例:** 1073 1074```js 1075netConnection.unregister(function (error) { 1076 console.log(JSON.stringify(error)) 1077}) 1078``` 1079 1080### on('netAvailable') 1081 1082on(type: 'netAvailable', callback: Callback\<NetHandle>): void 1083 1084订阅网络可用事件。 1085 1086**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。 1087 1088**系统能力**:SystemCapability.Communication.NetManager.Core 1089 1090**参数:** 1091 1092| 参数名 | 类型 | 必填 | 说明 | 1093| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ | 1094| type | string | 是 | 订阅事件,固定为'netAvailable'。<br>netAvailable:数据网络可用事件。 | 1095| callback | Callback\<[NetHandle](#nethandle)> | 是 | 回调函数,返回数据网络句柄。| 1096 1097**示例:** 1098 1099```js 1100// 创建NetConnection对象 1101let netCon = connection.createNetConnection() 1102 1103// 先使用register接口注册订阅事件 1104netCon.register(function (error) { 1105 console.log(JSON.stringify(error)) 1106}) 1107 1108// 订阅网络可用事件。调用register后,才能接收到此事件通知 1109netCon.on('netAvailable', function (data) { 1110 console.log(JSON.stringify(data)) 1111}) 1112 1113// 使用unregister接口取消订阅 1114netCon.unregister(function (error) { 1115 console.log(JSON.stringify(error)) 1116}) 1117``` 1118 1119### on('netBlockStatusChange') 1120 1121on(type: 'netBlockStatusChange', callback: Callback<{ netHandle: NetHandle, blocked: boolean }>): void 1122 1123订阅网络阻塞状态事件,使用callback方式作为异步方法。 1124 1125**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。 1126 1127**系统能力**:SystemCapability.Communication.NetManager.Core 1128 1129**参数:** 1130 1131| 参数名 | 类型 | 必填 | 说明 | 1132| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1133| type | string | 是 | 订阅事件,固定为'netBlockStatusChange'。<br/>netBlockStatusChange:网络阻塞状态事件。 | 1134| callback | Callback<{ netHandle: [NetHandle](#nethandle), blocked: boolean }> | 是 | 回调函数,返回数据网络句柄(netHandle),及网络堵塞状态(blocked)。| 1135 1136**示例:** 1137 1138```js 1139// 创建NetConnection对象 1140let netCon = connection.createNetConnection() 1141 1142// 先使用register接口注册订阅事件 1143netCon.register(function (error) { 1144 console.log(JSON.stringify(error)) 1145}) 1146 1147// 订阅网络阻塞状态事件。调用register后,才能接收到此事件通知 1148netCon.on('netBlockStatusChange', function (data) { 1149 console.log(JSON.stringify(data)) 1150}) 1151 1152// 使用unregister接口取消订阅 1153netCon.unregister(function (error) { 1154 console.log(JSON.stringify(error)) 1155}) 1156``` 1157 1158### on('netCapabilitiesChange') 1159 1160on(type: 'netCapabilitiesChange', callback: Callback<{ netHandle: NetHandle, netCap: NetCapabilities }>): void 1161 1162订阅网络能力变化事件。 1163 1164**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。 1165 1166**系统能力**:SystemCapability.Communication.NetManager.Core 1167 1168**参数:** 1169 1170| 参数名 | 类型 | 必填 | 说明 | 1171| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1172| type | string | 是 | 订阅事件,固定为'netCapabilitiesChange'。<br/>netCapabilitiesChange:网络能力变化事件。 | 1173| callback | Callback<{ netHandle: [NetHandle](#nethandle), netCap: [NetCapabilities](#netcapabilities) }> | 是 | 回调函数,返回数据网络句柄(netHandle)和网络的能力信息(netCap)。| 1174 1175**示例:** 1176 1177```js 1178// 创建NetConnection对象 1179let netCon = connection.createNetConnection() 1180 1181// 先使用register接口注册订阅事件 1182netCon.register(function (error) { 1183 console.log(JSON.stringify(error)) 1184}) 1185 1186// 订阅网络能力变化事件。调用register后,才能接收到此事件通知 1187netCon.on('netCapabilitiesChange', function (data) { 1188 console.log(JSON.stringify(data)) 1189}) 1190 1191// 使用unregister接口取消订阅 1192netCon.unregister(function (error) { 1193 console.log(JSON.stringify(error)) 1194}) 1195``` 1196 1197### on('netConnectionPropertiesChange') 1198 1199on(type: 'netConnectionPropertiesChange', callback: Callback<{ netHandle: NetHandle, connectionProperties: ConnectionProperties }>): void 1200 1201订阅网络连接信息变化事件。 1202 1203**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。 1204 1205**系统能力**:SystemCapability.Communication.NetManager.Core 1206 1207**参数:** 1208 1209| 参数名 | 类型 | 必填 | 说明 | 1210| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1211| type | string | 是 | 订阅事件,固定为'netConnectionPropertiesChange'。<br/>netConnectionPropertiesChange:网络连接信息变化事件。 | 1212| callback | Callback<{ netHandle: [NetHandle](#nethandle), connectionProperties: [ConnectionProperties](#connectionproperties) }> | 是 | 回调函数,返回数据网络句柄(netHandle)和网络的连接信息(connectionProperties)| 1213 1214**示例:** 1215 1216```js 1217// 创建NetConnection对象 1218let netCon = connection.createNetConnection() 1219 1220// 先使用register接口注册订阅事件 1221netCon.register(function (error) { 1222 console.log(JSON.stringify(error)) 1223}) 1224 1225// 订阅网络连接信息变化事件。调用register后,才能接收到此事件通知 1226netCon.on('netConnectionPropertiesChange', function (data) { 1227 console.log(JSON.stringify(data)) 1228}) 1229 1230// 使用unregister接口取消订阅 1231netCon.unregister(function (error) { 1232 console.log(JSON.stringify(error)) 1233}) 1234``` 1235 1236### on('netLost') 1237 1238on(type: 'netLost', callback: Callback\<NetHandle>): void 1239 1240订阅网络丢失事件。 1241 1242**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。 1243 1244**系统能力**:SystemCapability.Communication.NetManager.Core 1245 1246**参数:** 1247 1248| 参数名 | 类型 | 必填 | 说明 | 1249| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ | 1250| type | string | 是 | 订阅事件,固定为'netLost'。<br/>netLost:网络严重中断或正常断开事件。 | 1251| callback | Callback\<[NetHandle](#nethandle)> | 是 | 回调函数,数据网络句柄(netHandle)| 1252 1253**示例:** 1254 1255```js 1256// 创建NetConnection对象 1257let netCon = connection.createNetConnection() 1258 1259// 先使用register接口注册订阅事件 1260netCon.register(function (error) { 1261 console.log(JSON.stringify(error)) 1262}) 1263 1264// 订阅网络丢失事件。调用register后,才能接收到此事件通知 1265netCon.on('netLost', function (data) { 1266 console.log(JSON.stringify(data)) 1267}) 1268 1269// 使用unregister接口取消订阅 1270netCon.unregister(function (error) { 1271 console.log(JSON.stringify(error)) 1272}) 1273``` 1274 1275### on('netUnavailable') 1276 1277on(type: 'netUnavailable', callback: Callback\<void>): void 1278 1279订阅网络不可用事件。 1280 1281**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。 1282 1283**系统能力**:SystemCapability.Communication.NetManager.Core 1284 1285**参数:** 1286 1287| 参数名 | 类型 | 必填 | 说明 | 1288| -------- | --------------- | ---- | ------------------------------------------------------------ | 1289| type | string | 是 | 订阅事件,固定为'netUnavailable'。<br/>netUnavailable:网络不可用事件。 | 1290| callback | Callback\<void> | 是 | 回调函数,无返回结果。| 1291 1292**示例:** 1293 1294```js 1295// 创建NetConnection对象 1296let netCon = connection.createNetConnection() 1297 1298// 先使用register接口注册订阅事件 1299netCon.register(function (error) { 1300 console.log(JSON.stringify(error)) 1301}) 1302 1303// 订阅网络不可用事件。调用register后,才能接收到此事件通知 1304netCon.on('netUnavailable', function (data) { 1305 console.log(JSON.stringify(data)) 1306}) 1307 1308// 使用unregister接口取消订阅 1309netCon.unregister(function (error) { 1310 console.log(JSON.stringify(error)) 1311}) 1312``` 1313 1314## NetHandle 1315 1316数据网络的句柄。 1317 1318在调用NetHandle的方法之前,需要先获取NetHandle对象。 1319 1320**系统能力**:SystemCapability.Communication.NetManager.Core 1321 1322### 属性 1323 1324| 名称 | 类型 | 必填 | 说明 | 1325| ------ | ------ | --- |------------------------- | 1326| netId | number | 是 | 网络ID,取值为0代表没有默认网络,其余取值必须大于等于100。 | 1327 1328### bindSocket<sup>9+</sup> 1329 1330bindSocket(socketParam: TCPSocket \| UDPSocket, callback: AsyncCallback\<void>): void 1331 1332将TCPSocket或UDPSocket绑定到当前网络,使用callback方式作为异步方法。 1333 1334**系统能力**:SystemCapability.Communication.NetManager.Core 1335 1336**参数:** 1337 1338| 参数名 | 类型 | 必填 | 说明 | 1339| ----------- | ------------------------ | ---- | -------------------------------| 1340| socketParam | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | 是 | 待绑定的TCPSocket或UDPSocket对象。| 1341| callback | AsyncCallback\<void> | 是 | 回调函数。当TCPSocket或UDPSocket成功绑定到当前网络,err为undefined,否则为错误对象。| 1342 1343**错误码:** 1344 1345| 错误码ID | 错误信息 | 1346| ------- | ----------------------------- | 1347| 401 | Parameter error. | 1348| 2100001 | Invalid parameter value. | 1349| 2100002 | Operation failed. Cannot connect to service.| 1350| 2100003 | System internal error. | 1351 1352**示例:** 1353 1354```js 1355import socket from "@ohos.net.socket"; 1356connection.getDefaultNet().then((netHandle) => { 1357 var tcp = socket.constructTCPSocketInstance(); 1358 var udp = socket.constructUDPSocketInstance(); 1359 let socketType = "TCPSocket"; 1360 if (socketType == "TCPSocket") { 1361 tcp.bind({ 1362 address: '192.168.xx.xxx', port: 8080, family: 1 1363 }, error => { 1364 if (error) { 1365 console.log('bind fail'); 1366 } 1367 netHandle.bindSocket(tcp, (error, data) => { 1368 if (error) { 1369 console.log(JSON.stringify(error)); 1370 } else { 1371 console.log(JSON.stringify(data)); 1372 } 1373 }) 1374 }) 1375 } else { 1376 let callback = value => { 1377 console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo); 1378 } 1379 udp.on('message', callback); 1380 udp.bind({ 1381 address: '192.168.xx.xxx', port: 8080, family: 1 1382 }, error => { 1383 if (error) { 1384 console.log('bind fail'); 1385 } 1386 udp.on('message', (data) => { 1387 console.log(JSON.stringify(data)) 1388 }); 1389 netHandle.bindSocket(udp, (error, data) => { 1390 if (error) { 1391 console.log(JSON.stringify(error)); 1392 } else { 1393 console.log(JSON.stringify(data)); 1394 } 1395 }) 1396 }) 1397 } 1398}) 1399``` 1400 1401### bindSocket<sup>9+</sup> 1402 1403bindSocket(socketParam: TCPSocket \| UDPSocket): Promise\<void>; 1404 1405将TCPSocket或UDPSockett绑定到当前网络,使用Promise方式作为异步方法。 1406 1407**系统能力**:SystemCapability.Communication.NetManager.Core 1408 1409**参数:** 1410 1411| 参数名 | 类型 | 必填 | 说明 | 1412| --------------- | --------------------- | ---- | ------------------------------ | 1413| socketParam | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | 是 | 待绑定的TCPSocket或UDPSocket对象。| 1414 1415**返回值:** 1416 1417| 类型 | 说明 | 1418| -------------- | ---------------------- | 1419| Promise\<void> | 无返回值的Promise对象。 | 1420 1421**错误码:** 1422 1423| 错误码ID | 错误信息 | 1424| ------- | ----------------------------- | 1425| 401 | Parameter error. | 1426| 2100001 | Invalid parameter value. | 1427| 2100002 | Operation failed. Cannot connect to service.| 1428| 2100003 | System internal error. | 1429 1430**示例:** 1431 1432```js 1433import socket from "@ohos.net.socket"; 1434connection.getDefaultNet().then((netHandle) => { 1435 var tcp = socket.constructTCPSocketInstance(); 1436 var udp = socket.constructUDPSocketInstance(); 1437 let socketType = "TCPSocket"; 1438 if (socketType == "TCPSocket") { 1439 tcp.bind({ 1440 address: '192.168.xx.xxx', port: 8080, family: 1 1441 }, error => { 1442 if (error) { 1443 console.log('bind fail'); 1444 } 1445 netHandle.bindSocket(tcp).then((data) => { 1446 console.log(JSON.stringify(data)); 1447 }).catch(error => { 1448 console.log(JSON.stringify(error)); 1449 }) 1450 }) 1451 } else { 1452 let callback = value => { 1453 console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo); 1454 } 1455 udp.on('message', callback); 1456 udp.bind({ 1457 address: '192.168.xx.xxx', port: 8080, family: 1 1458 }, error => { 1459 if (error) { 1460 console.log('bind fail'); 1461 } 1462 udp.on('message', (data) => { 1463 console.log(JSON.stringify(data)); 1464 }) 1465 netHandle.bindSocket(udp).then((data) => { 1466 console.log(JSON.stringify(data)); 1467 }).catch(error => { 1468 console.log(JSON.stringify(error)); 1469 }) 1470 }) 1471 } 1472}) 1473``` 1474 1475### getAddressesByName 1476 1477getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>>): void 1478 1479使用对应网络解析主机名以获取所有IP地址,使用callback方式作为异步方法。 1480 1481**需要权限**:ohos.permission.INTERNET 1482 1483**系统能力**:SystemCapability.Communication.NetManager.Core 1484 1485**参数:** 1486 1487| 参数名 | 类型 | 必填 | 说明 | 1488| -------- | ------------------------------------------------- | ---- | ------------------ | 1489| host | string | 是 | 需要解析的主机名。 | 1490| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | 是 | 回调函数。当使用对应网络解析主机名成功获取所有IP地址,err为undefined,data为获取到的所有IP地址;否则为错误对象。| 1491 1492**错误码:** 1493 1494| 错误码ID | 错误信息 | 1495| ------- | ----------------------------- | 1496| 201 | Permission denied. | 1497| 401 | Parameter error. | 1498| 2100001 | Invalid parameter value. | 1499| 2100002 | Operation failed. Cannot connect to service.| 1500| 2100003 | System internal error. | 1501 1502**示例:** 1503 1504```js 1505connection.getDefaultNet().then(function (netHandle) { 1506 let host = "xxxx"; 1507 netHandle.getAddressesByName(host, function (error, data) { 1508 console.log(JSON.stringify(error)) 1509 console.log(JSON.stringify(data)) 1510 }) 1511}) 1512``` 1513 1514### getAddressesByName 1515 1516getAddressesByName(host: string): Promise\<Array\<NetAddress>> 1517 1518使用对应网络解析主机名以获取所有IP地址,使用Promise方式作为异步方法。 1519 1520**需要权限**:ohos.permission.INTERNET 1521 1522**系统能力**:SystemCapability.Communication.NetManager.Core 1523 1524**参数:** 1525 1526| 参数名 | 类型 | 必填 | 说明 | 1527| ------ | ------ | ---- | ------------------ | 1528| host | string | 是 | 需要解析的主机名。 | 1529 1530**返回值:** 1531 1532| 类型 | 说明 | 1533| ------------------------------------------- | ----------------------------- | 1534| Promise\<Array\<[NetAddress](#netaddress)>> | 以Promise形式返回所有IP地址。 | 1535 1536**错误码:** 1537 1538| 错误码ID | 错误信息 | 1539| ------- | ----------------------------- | 1540| 201 | Permission denied. | 1541| 401 | Parameter error. | 1542| 2100001 | Invalid parameter value. | 1543| 2100002 | Operation failed. Cannot connect to service.| 1544| 2100003 | System internal error. | 1545 1546**示例:** 1547 1548```js 1549connection.getDefaultNet().then(function (netHandle) { 1550 let host = "xxxx"; 1551 netHandle.getAddressesByName(host).then(function (data) { 1552 console.log(JSON.stringify(data)) 1553 }) 1554}) 1555``` 1556 1557### getAddressByName 1558 1559getAddressByName(host: string, callback: AsyncCallback\<NetAddress>): void 1560 1561使用对应网络解析主机名以获取第一个IP地址,使用callback方式作为异步方法。 1562 1563**需要权限**:ohos.permission.INTERNET 1564 1565**系统能力**:SystemCapability.Communication.NetManager.Core 1566 1567**参数:** 1568 1569| 参数名 | 类型 | 必填 | 说明 | 1570| -------- | ----------------------------------------- | ---- | ------------------ | 1571| host | string | 是 | 需要解析的主机名。 | 1572| callback | AsyncCallback\<[NetAddress](#netaddress)> | 是 | 回调函数。当使用对应网络解析主机名获取第一个IP地址成功,err为undefined,data为获取的第一个IP地址;否则为错误对象。 | 1573 1574**错误码:** 1575 1576| 错误码ID | 错误信息 | 1577| ------- | ----------------------------- | 1578| 201 | Permission denied. | 1579| 401 | Parameter error. | 1580| 2100001 | Invalid parameter value. | 1581| 2100002 | Operation failed. Cannot connect to service.| 1582| 2100003 | System internal error. | 1583 1584**示例:** 1585 1586```js 1587connection.getDefaultNet().then(function (netHandle) { 1588 let host = "xxxx"; 1589 netHandle.getAddressByName(host, function (error, data) { 1590 console.log(JSON.stringify(error)) 1591 console.log(JSON.stringify(data)) 1592 }) 1593}) 1594``` 1595 1596### getAddressByName 1597 1598getAddressByName(host: string): Promise\<NetAddress> 1599 1600使用对应网络解析主机名以获取第一个IP地址,使用Promise方式作为异步方法。 1601 1602**需要权限**:ohos.permission.INTERNET 1603 1604**系统能力**:SystemCapability.Communication.NetManager.Core 1605 1606**参数:** 1607 1608| 参数名 | 类型 | 必填 | 说明 | 1609| ------ | ------ | ---- | ------------------ | 1610| host | string | 是 | 需要解析的主机名。 | 1611 1612**返回值:** 1613 1614| 类型 | 说明 | 1615| ----------------------------------- | ------------------------------- | 1616| Promise\<[NetAddress](#netaddress)> | 以Promise形式返回第一个IP地址。 | 1617 1618**错误码:** 1619 1620| 错误码ID | 错误信息 | 1621| ------- | ----------------------------- | 1622| 201 | Permission denied. | 1623| 401 | Parameter error. | 1624| 2100001 | Invalid parameter value. | 1625| 2100002 | Operation failed. Cannot connect to service.| 1626| 2100003 | System internal error. | 1627 1628**示例:** 1629 1630```js 1631connection.getDefaultNet().then(function (netHandle) { 1632 let host = "xxxx"; 1633 netHandle.getAddressByName(host).then(function (data) { 1634 console.log(JSON.stringify(data)) 1635 }) 1636}) 1637``` 1638 1639## NetCap 1640 1641网络具体能力。 1642 1643**系统能力**:SystemCapability.Communication.NetManager.Core 1644 1645| 名称 | 值 | 说明 | 1646| ------------------------ | ---- | ---------------------- | 1647| NET_CAPABILITY_MMS | 0 | 表示网络可以访问运营商的MMSC(Multimedia Message Service,多媒体短信服务)发送和接收彩信。 | 1648| NET_CAPABILITY_NOT_METERED | 11 | 表示网络流量未被计费。 | 1649| NET_CAPABILITY_INTERNET | 12 | 表示该网络应具有访问Internet的能力,该能力由网络提供者设置。 | 1650| NET_CAPABILITY_NOT_VPN | 15 | 表示网络不使用VPN(Virtual Private Network,虚拟专用网络)。 | 1651| NET_CAPABILITY_VALIDATED | 16 | 表示该网络访问Internet的能力被网络管理成功验证,该能力由网络管理模块设置。 | 1652 1653## NetBearType 1654 1655网络类型。 1656 1657**系统能力**:SystemCapability.Communication.NetManager.Core 1658 1659| 名称 | 值 | 说明 | 1660| --------------- | ---- | ----------- | 1661| BEARER_CELLULAR | 0 | 蜂窝网络。 | 1662| BEARER_WIFI | 1 | Wi-Fi网络。 | 1663| BEARER_ETHERNET | 3 | 以太网网络。 | 1664 1665## NetSpecifier 1666 1667提供承载数据网络能力的实例。 1668 1669**系统能力**:SystemCapability.Communication.NetManager.Core 1670 1671| 名称 | 类型 | 必填 | 说明 | 1672| ----------------------- | ----------------------------------- | ---- | ------------------------------------------------------------ | 1673| netCapabilities | [NetCapabilities](#netcapabilities) | 是 | 存储数据网络的传输能力和承载类型。 | 1674| bearerPrivateIdentifier | string | 否 | 网络标识符,Wi-Fi网络的标识符是"wifi",蜂窝网络的标识符是"slot0"(对应SIM卡1)。 | 1675 1676## NetCapabilities 1677 1678网络的能力集。 1679 1680**系统能力**:SystemCapability.Communication.NetManager.Core 1681 1682| 名称 | 类型 | 必填 | 说明 | 1683| --------------------- | ---------------------------------- | --- | ------------------------ | 1684| linkUpBandwidthKbps | number | 否 | 上行(设备到网络)带宽。 | 1685| linkDownBandwidthKbps | number | 否 | 下行(网络到设备)带宽。 | 1686| networkCap | Array\<[NetCap](#netcap)> | 否 | 网络具体能力。 | 1687| bearerTypes | Array\<[NetBearType](#netbeartype)> | 是 | 网络类型。 | 1688 1689## ConnectionProperties 1690 1691网络连接信息。 1692 1693**系统能力**:SystemCapability.Communication.NetManager.Core 1694 1695| 名称 | 类型 | 必填 | 说明 | 1696| ------------- | ---------------------------------- | ----|---------------- | 1697| interfaceName | string | 是 |网卡名称。 | 1698| domains | string | 是 |所属域,默认""。 | 1699| linkAddresses | Array\<[LinkAddress](#linkaddress)> | 是 |链路信息。 | 1700| routes | Array\<[RouteInfo](#routeinfo)> | 是 |路由信息。 | 1701| dnses | Array\<[NetAddress](#netaddress)> | 是 |网络地址,参考[NetAddress](#netaddress)。 | 1702| mtu | number | 是 |最大传输单元。 | 1703 1704## RouteInfo 1705 1706网络路由信息。 1707 1708**系统能力**:SystemCapability.Communication.NetManager.Core 1709 1710| 名称 | 类型 | 必填 |说明 | 1711| -------------- | --------------------------- | --- |---------------- | 1712| interface | string | 是 |网卡名称。 | 1713| destination | [LinkAddress](#linkaddress) | 是 |目的地址。 | 1714| gateway | [NetAddress](#netaddress) | 是 |网关地址。 | 1715| hasGateway | boolean | 是 |是否有网关。 | 1716| isDefaultRoute | boolean | 是 |是否为默认路由。 | 1717 1718## LinkAddress 1719 1720网络链路信息。 1721 1722**系统能力**:SystemCapability.Communication.NetManager.Core 1723 1724| 名称 | 类型 | 必填 |说明 | 1725| ------------ | ----------------------- |---- |-------------------- | 1726| address | [NetAddress](#netaddress) | 是 | 链路地址。 | 1727| prefixLength | number | 是 |链路地址前缀的长度。 | 1728 1729## NetAddress 1730 1731网络地址。 1732 1733**系统能力**:SystemCapability.Communication.NetManager.Core 1734 1735| 名称 | 类型 | 必填 | 说明 | 1736| ------- | ------ | -- |------------------------------ | 1737| address | string | 是 |地址。 | 1738| family | number | 否 |IPv4 = 1,IPv6 = 2,默认IPv4。 | 1739| port | number | 否 |端口,取值范围\[0, 65535]。 | 1740