1# @ohos.net.connection (Network Connection Management) 2 3The network connection management module provides basic network management capabilities. You can obtain the default active data network or the list of all active data networks, enable or disable the airplane mode, and obtain network capability information. 4 5> **NOTE** 6> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version. 7 8## Modules to Import 9 10```js 11import connection from '@ohos.net.connection' 12``` 13## connection.createNetConnection 14 15createNetConnection(netSpecifier?: NetSpecifier, timeout?: number): NetConnection 16 17Creates a **NetConnection** object. **netSpecifier** specifies the network, and **timeout** specifies the timeout interval in ms. **timeout** is configurable only when **netSpecifier** is specified. If neither of them is present, the default network is used. 18 19**System capability**: SystemCapability.Communication.NetManager.Core 20 21**Parameters** 22 23| Name | Type | Mandatory| Description | 24| ------------ | ----------------------------- | ---- | ------------------------------------------------------------ | 25| netSpecifier | [NetSpecifier](#netspecifier) | No | Network specifier. If this parameter is not set, the default network is used. | 26| timeout | number | No | Timeout interval for obtaining the network specified by **netSpecifier**. This parameter is valid only when **netSpecifier** is set.| 27 28**Return value** 29 30| Type | Description | 31| ------------------------------- | -------------------- | 32| [NetConnection](#netconnection) | Handle of the network specified by **netSpecifier**.| 33 34**Example** 35 36```js 37// Default network 38let netConnection = connection.createNetConnection() 39 40// Cellular network 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 52Obtains the default active data network. This API uses an asynchronous callback to return the result. You can use [getNetCapabilities](#connectiongetnetcapabilities) to obtain information such as the network type and capabilities. 53 54**Required permission**: ohos.permission.GET_NETWORK_INFO 55 56**System capability**: SystemCapability.Communication.NetManager.Core 57 58**Parameters** 59 60| Name | Type | Mandatory| Description | 61| -------- | --------------------------------------- | ---- | ---------- | 62| callback | AsyncCallback\<[NetHandle](#nethandle)> | Yes | Callback used to return the result. If the default activated data network is obtained successfully, err is undefined and data is the default activated data network. Otherwise, err is an error object.| 63 64**Error codes** 65 66| ID| Error Message | 67| ------- | ----------------------------- | 68| 201 | Permission denied. | 69| 2100002 | Operation failed. Cannot connect to service.| 70| 2100003 | System internal error. | 71 72**Example** 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 85Obtains the default active data network. This API uses a promise to return the result. You can use [getNetCapabilities](#connectiongetnetcapabilities) to obtain information such as the network type and capabilities. 86 87**Required permission**: ohos.permission.GET_NETWORK_INFO 88 89**System capability**: SystemCapability.Communication.NetManager.Core 90 91**Return value** 92 93| Type | Description | 94| --------------------------------- | ------------------------------------- | 95| Promise\<[NetHandle](#nethandle)> | Promise used to return the result.| 96 97**Error codes** 98 99| ID| Error Message | 100| ------- | ----------------------------- | 101| 201 | Permission denied. | 102| 2100002 | Operation failed. Cannot connect to service.| 103| 2100003 | System internal error. | 104 105**Example** 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 117Obtains the default active data network in synchronous mode. You can use [getNetCapabilities](#connectiongetnetcapabilities) to obtain information such as the network type and capabilities. 118 119**Required permission**: ohos.permission.GET_NETWORK_INFO 120 121**System capability**: SystemCapability.Communication.NetManager.Core 122 123**Return value** 124 125| Type | Description | 126| --------- | ---------------------------------- | 127| NetHandle | Handle of the default active data network.| 128 129**Error codes** 130 131| ID| Error Message | 132| ------- | ----------------------------- | 133| 201 | Permission denied. | 134| 2100002 | Operation failed. Cannot connect to service.| 135| 2100003 | System internal error. | 136 137**Example** 138 139```js 140let netHandle = connection.getDefaultNetSync(); 141``` 142 143## connection.getAppNet<sup>9+</sup> 144 145getAppNet(callback: AsyncCallback\<NetHandle>): void 146 147Obtains information about the network bound to an application. This API uses an asynchronous callback to return the result. 148 149**System capability**: SystemCapability.Communication.NetManager.Core 150 151**Parameters** 152 153| Name | Type | Mandatory| Description | 154| --------- | ------------------------------------------------------------ | ---- | ---------------- | 155| callback | AsyncCallback\<[NetHandle](#nethandle)> | Yes | Callback used to return the result. If information about the network bound to the application is successfully obtained, **err** is **undefined** and **data** is the obtained network information. Otherwise, **err** is an error object.| 156 157**Error codes** 158 159| ID| Error Message | 160| ------- | ----------------------------- | 161| 2100002 | Operation failed. Cannot connect to service.| 162| 2100003 | System internal error. | 163 164**Example** 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 177Obtains information about the network bound to an application. This API uses a promise to return the result. 178 179**System capability**: SystemCapability.Communication.NetManager.Core 180 181**Return value** 182 183| Type | Description | 184| --------------------------------- | ------------------------------------- | 185| Promise\<[NetHandle](#nethandle)> | Promise used to return the result.| 186 187**Error codes** 188 189| ID| Error Message | 190| ------- | ----------------------------- | 191| 2100002 | Operation failed. Cannot connect to service.| 192| 2100003 | System internal error. | 193 194**Example** 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 208Binds an application to the specified network, so that the application can access the external network only through this network. This API uses an asynchronous callback to return the result. 209 210**Required permissions**: ohos.permission.INTERNET 211 212**System capability**: SystemCapability.Communication.NetManager.Core 213 214**Parameters** 215 216| Name | Type | Mandatory| Description | 217| --------- | ------------------------------------------------------------ | ---- | ---------------- | 218| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network.| 219| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the application is successfully bound to the specified network, **err** is **undefined**. Otherwise, **err** is an error object.| 220 221**Error codes** 222 223| ID| Error Message | 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**Example** 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 246Binds an application to the specified network, so that the application can access the external network only through this network. This API uses a promise to return the result. 247 248**Required permissions**: ohos.permission.INTERNET 249 250**System capability**: SystemCapability.Communication.NetManager.Core 251 252**Parameters** 253 254| Name | Type | Mandatory| Description | 255| --------- | ------------------------------------------------------------ | ---- | ---------------- | 256| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network.| 257 258**Return value** 259 260| Type | Description | 261| ------------------------------------------- | ----------------------------- | 262| Promise\<void> | Promise that returns no value.| 263 264**Error codes** 265 266| ID| Error Message | 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**Example** 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 290Obtains the list of all connected networks. This API uses an asynchronous callback to return the result. 291 292**Required permission**: ohos.permission.GET_NETWORK_INFO 293 294**System capability**: SystemCapability.Communication.NetManager.Core 295 296**Parameters** 297 298| Name| Type| Mandatory| Description| 299| -------- | -------- | -------- | -------- | 300| callback | AsyncCallback<Array<[NetHandle](#nethandle)>> | Yes| Callback used to return the result. If the list of all connected networks is obtained successfully, **err** is **undefined** and **data** is the list of activated data networks. Otherwise, **err** is an error object.| 301 302**Error codes** 303 304| ID| Error Message | 305| ------- | ----------------------------- | 306| 201 | Permission denied. | 307| 2100002 | Operation failed. Cannot connect to service.| 308| 2100003 | System internal error. | 309 310**Example** 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 323Obtains the list of all connected networks. This API uses a promise to return the result. 324 325**Required permission**: ohos.permission.GET_NETWORK_INFO 326 327**System capability**: SystemCapability.Communication.NetManager.Core 328 329**Return value** 330 331| Type| Description| 332| -------- | -------- | 333| Promise<Array<[NetHandle](#nethandle)>> | Promise used to return the result.| 334 335**Error codes** 336 337| ID| Error Message | 338| ------- | ----------------------------- | 339| 201 | Permission denied. | 340| 2100002 | Operation failed. Cannot connect to service.| 341| 2100003 | System internal error. | 342 343**Example** 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 355Obtains connection properties of the network corresponding to the **netHandle**. This API uses an asynchronous callback to return the result. 356 357**Required permission**: ohos.permission.GET_NETWORK_INFO 358 359**System capability**: SystemCapability.Communication.NetManager.Core 360 361**Parameters** 362 363| Name | Type | Mandatory| Description | 364| --------- | ------------------------------------------------------------ | ---- | ---------------- | 365| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network.| 366| callback | AsyncCallback\<[ConnectionProperties](#connectionproperties)> | Yes | Callback used to return the result. If the connection properties of the network corresponding to the **netHandle** is obtained successfully, **err** is **undefined** and **data** is the obtained network connection information. Otherwise, **err** is an error object.| 367 368**Error codes** 369 370| ID| Error Message | 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**Example** 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 393Obtains connection properties of the network corresponding to the **netHandle**. This API uses a promise to return the result. 394 395**Required permission**: ohos.permission.GET_NETWORK_INFO 396 397**System capability**: SystemCapability.Communication.NetManager.Core 398 399**Parameters** 400 401| Name | Type | Mandatory| Description | 402| --------- | ----------------------- | ---- | ---------------- | 403| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network.| 404 405**Return value** 406 407| Type | Description | 408| ------------------------------------------------------- | --------------------------------- | 409| Promise\<[ConnectionProperties](#connectionproperties)> | Promise used to return the result.| 410 411**Error codes** 412 413| ID| Error Message | 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**Example** 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 435Obtains capability information of the network corresponding to the **netHandle**. This API uses an asynchronous callback to return the result. 436 437**Required permission**: ohos.permission.GET_NETWORK_INFO 438 439**System capability**: SystemCapability.Communication.NetManager.Core 440 441**Parameters** 442 443| Name | Type | Mandatory| Description | 444| --------- | --------------------------------------------------- | ---- | ---------------- | 445| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network.| 446| callback | AsyncCallback\<[NetCapabilities](#netcapabilities)> | Yes | Callback used to return the result. If the capability information of the network corresponding to the **netHandle** is obtained successfully, **err** is **undefined** and **data** is the obtained network capability information. Otherwise, **err** is an error object. | 447 448**Error codes** 449 450| ID| Error Message | 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**Example** 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 473Obtains capability information of the network corresponding to the **netHandle**. This API uses a promise to return the result. 474 475**Required permission**: ohos.permission.GET_NETWORK_INFO 476 477**System capability**: SystemCapability.Communication.NetManager.Core 478 479**Parameters** 480 481| Name | Type | Mandatory| Description | 482| --------- | ----------------------- | ---- | ---------------- | 483| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network.| 484 485**Return value** 486 487| Type | Description | 488| --------------------------------------------- | --------------------------------- | 489| Promise\<[NetCapabilities](#netcapabilities)> | Promise used to return the result.| 490 491**Error codes** 492 493| ID| Error Message | 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**Example** 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 515Checks whether the data traffic usage on the current network is metered. This API uses an asynchronous callback to return the result. 516 517**Required permission**: ohos.permission.GET_NETWORK_INFO 518 519**System capability**: SystemCapability.Communication.NetManager.Core 520 521**Parameters** 522 523| Name | Type | Mandatory| Description | 524| -------- | ----------------------- | ---- | -------------------------------------- | 525| callback | AsyncCallback\<boolean> | Yes | Callback used to return the result. The value **true** indicates the data traffic usage is metered.| 526 527**Error codes** 528 529| ID| Error Message | 530| ------- | ----------------------------- | 531| 201 | Permission denied. | 532| 2100002 | Operation failed. Cannot connect to service.| 533| 2100003 | System internal error. | 534 535**Example** 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 548Checks whether the data traffic usage on the current network is metered. This API uses a promise to return the result. 549 550**Required permission**: ohos.permission.GET_NETWORK_INFO 551 552**System capability**: SystemCapability.Communication.NetManager.Core 553 554**Return value** 555 556| Type | Description | 557| ----------------- | ----------------------------------------------- | 558| Promise\<boolean> | Promise used to return the result. The value **true** indicates the data traffic usage is metered.| 559 560**Error codes** 561 562| ID| Error Message | 563| ------- | ----------------------------- | 564| 201 | Permission denied. | 565| 2100002 | Operation failed. Cannot connect to service.| 566| 2100003 | System internal error. | 567 568**Example** 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 580Checks whether the default data network is activated. This API uses an asynchronous callback to return the result. You can use [getDefaultNet](#connectiongetdefaultnet) to obtain the default data network, if any. 581 582**Required permission**: ohos.permission.GET_NETWORK_INFO 583 584**System capability**: SystemCapability.Communication.NetManager.Core 585 586**Parameters** 587 588| Name | Type | Mandatory| Description | 589| -------- | ----------------------- | ---- | -------------------------------------- | 590| callback | AsyncCallback\<boolean> | Yes | Callback used to return the result. The value **true** indicates the default data network is activated.| 591 592**Error codes** 593 594| ID| Error Message | 595| ------- | ----------------------------- | 596| 201 | Permission denied. | 597| 2100002 | Operation failed. Cannot connect to service.| 598| 2100003 | System internal error. | 599 600**Example** 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 613Checks whether the default data network is activated. This API uses a promise to return the result. You can use [getDefaultNet](#connectiongetdefaultnet) to obtain the default data network, if any. 614 615**Required permission**: ohos.permission.GET_NETWORK_INFO 616 617**System capability**: SystemCapability.Communication.NetManager.Core 618 619**Return value** 620 621| Type | Description | 622| ----------------- | ----------------------------------------------- | 623| Promise\<boolean> | Promise used to return the result. The value **true** indicates that the default data network is activated.| 624 625**Error codes** 626 627| ID| Error Message | 628| ------- | ----------------------------- | 629| 201 | Permission denied. | 630| 2100002 | Operation failed. Cannot connect to service.| 631| 2100003 | System internal error. | 632 633**Example** 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 645Enables the airplane mode. This API uses an asynchronous callback to return the result. 646 647**System API**: This is a system API. 648 649**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 650 651**System capability**: SystemCapability.Communication.NetManager.Core 652 653**Parameters** 654 655| Name | Type | Mandatory| Description | 656| -------- | ------------------------------------------------- | ---- | ------------------ | 657| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 658 659**Error codes** 660 661| ID| Error Message | 662| ------- | ----------------------------- | 663| 2100002 | Operation failed. Cannot connect to service.| 664| 2100003 | System internal error. | 665 666**Example** 667 668```js 669connection.enableAirplaneMode(function (error) { 670 console.log(JSON.stringify(error)) 671}) 672``` 673 674## connection.enableAirplaneMode 675 676enableAirplaneMode(): Promise\<void> 677 678Enables the airplane mode. This API uses a promise to return the result. 679 680**System API**: This is a system API. 681 682**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 683 684**System capability**: SystemCapability.Communication.NetManager.Core 685 686**Return value** 687 688| Type | Description | 689| ------------------------------------------- | ----------------------------- | 690| Promise\<void> | Promise that returns no value.| 691 692**Error codes** 693 694| ID| Error Message | 695| ------- | ----------------------------- | 696| 2100002 | Operation failed. Cannot connect to service.| 697| 2100003 | System internal error. | 698 699**Example** 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 711Disables the airplane mode. This API uses an asynchronous callback to return the result. 712 713**System API**: This is a system API. 714 715**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 716 717**System capability**: SystemCapability.Communication.NetManager.Core 718 719**Parameters** 720 721| Name | Type | Mandatory| Description | 722| -------- | ------------------------------------------------- | ---- | ------------------ | 723| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the airplane mode is disabled successfully, **err** is **undefined**. Otherwise, **err** is an error object.| 724 725**Error codes** 726 727| ID| Error Message | 728| ------- | ----------------------------- | 729| 2100002 | Operation failed. Cannot connect to service.| 730| 2100003 | System internal error. | 731 732**Example** 733 734```js 735connection.disableAirplaneMode(function (error) { 736 console.log(JSON.stringify(error)) 737}) 738``` 739 740## connection.disableAirplaneMode 741 742disableAirplaneMode(): Promise\<void> 743 744Disables the airplane mode. This API uses a promise to return the result. 745 746**System API**: This is a system API. 747 748**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 749 750**System capability**: SystemCapability.Communication.NetManager.Core 751 752**Return value** 753 754| Type | Description | 755| ------------------------------------------- | ----------------------------- | 756| Promise\<void> | Promise that returns no value.| 757 758**Error codes** 759 760| ID| Error Message | 761| ------- | ----------------------------- | 762| 2100002 | Operation failed. Cannot connect to service.| 763| 2100003 | System internal error. | 764 765**Example** 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 777Reports a **netAavailable** event to NetManager. If this API is called, the application considers that its network status (ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED) is inconsistent with that of NetManager. 778This API uses an asynchronous callback to return the result. 779 780**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET 781 782**System capability**: SystemCapability.Communication.NetManager.Core 783 784**Parameters** 785 786| Name| Type| Mandatory| Description| 787| -------- | -------- | -------- | -------- | 788| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).| 789| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the network status is reported successfully, **err** is **undefined**. Otherwise, **err** is an error object.| 790 791**Error codes** 792 793| ID| Error Message | 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**Example** 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 815Reports a **netAavailable** event to NetManager. If this API is called, the application considers that its network status (ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED) is inconsistent with that of NetManager. 816This API uses a promise to return the result. 817 818**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET 819 820**System capability**: SystemCapability.Communication.NetManager.Core 821 822**Parameters** 823 824| Name| Type| Mandatory| Description| 825| -------- | -------- | -------- | -------- | 826| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).| 827 828**Return value** 829| Type| Description| 830| -------- | -------- | 831| Promise<void> | Promise that returns no value.| 832 833**Error codes** 834 835| ID| Error Message | 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**Example** 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 857Reports a **netAavailable** event to NetManager. If this API is called, the application considers that its network status (ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED) is inconsistent with that of NetManager. 858This API uses an asynchronous callback to return the result. 859 860**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET 861 862**System capability**: SystemCapability.Communication.NetManager.Core 863 864**Parameters** 865 866| Name| Type| Mandatory| Description| 867| -------- | -------- | -------- | -------- | 868| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).| 869| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the network status is reported successfully, **err** is **undefined**. Otherwise, **err** is an error object.| 870 871**Error codes** 872 873| ID| Error Message | 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**Example** 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 895Reports a **netAavailable** event to NetManager. If this API is called, the application considers that its network status (ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED) is inconsistent with that of NetManager. 896This API uses a promise to return the result. 897 898**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET 899 900**System capability**: SystemCapability.Communication.NetManager.Core 901 902**Parameters** 903 904| Name| Type| Mandatory| Description| 905| -------- | -------- | -------- | -------- | 906| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).| 907 908**Return value** 909| Type| Description| 910| -------- | -------- | 911| Promise<void> | Promise that returns no value.| 912 913**Error codes** 914 915| ID| Error Message | 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**Example** 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 937Resolves the host name by using the default network to obtain all IP addresses. This API uses an asynchronous callback to return the result. 938 939**Required permissions**: ohos.permission.INTERNET 940 941**System capability**: SystemCapability.Communication.NetManager.Core 942 943**Parameters** 944 945| Name | Type | Mandatory| Description | 946| -------- | ------------------------------------------------- | ---- | ------------------ | 947| host | string | Yes | Host name to resolve.| 948| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | Yes | Callback used to return the result. If all IP addresses are successfully obtained, **err** is **undefined**, and **data** is the list of all obtained IP addresses. Otherwise, **err** is an error object.| 949 950**Error codes** 951 952| ID| Error Message | 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**Example** 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 974Resolves the host name by using the default network to obtain all IP addresses. This API uses a promise to return the result. 975 976**Required permissions**: ohos.permission.INTERNET 977 978**System capability**: SystemCapability.Communication.NetManager.Core 979 980**Parameters** 981 982| Name| Type | Mandatory| Description | 983| ------ | ------ | ---- | ------------------ | 984| host | string | Yes | Host name to resolve.| 985 986**Return value** 987 988| Type | Description | 989| ------------------------------------------- | ----------------------------- | 990| Promise\<Array\<[NetAddress](#netaddress)>> | Promise used to return the result.| 991 992**Error codes** 993 994| ID| Error Message | 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**Example** 1003 1004```js 1005let host = "xxxx"; 1006connection.getAddressesByName(host).then(function (data) { 1007 console.log(JSON.stringify(data)) 1008}) 1009``` 1010 1011## NetConnection 1012 1013Represents the network connection handle. 1014 1015### register 1016 1017register(callback: AsyncCallback\<void>): void 1018 1019Registers a listener for network status changes. 1020 1021**Required permission**: ohos.permission.GET_NETWORK_INFO 1022 1023**System capability**: SystemCapability.Communication.NetManager.Core 1024 1025**Parameters** 1026 1027| Name | Type | Mandatory| Description | 1028| -------- | -------------------- | ---- | ---------- | 1029| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If a listener for network status changes is registered successfully, **err** is **undefined**. Otherwise, **err** is an error object.| 1030 1031**Error codes** 1032 1033| ID| Error Message | 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**Example** 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 1054Unregisters the listener for network status changes. 1055 1056**System capability**: SystemCapability.Communication.NetManager.Core 1057 1058**Parameters** 1059 1060| Name | Type | Mandatory| Description | 1061| -------- | -------------------- | ---- | ---------- | 1062| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If a listener for network status changes is unregistered successfully, **err** is **undefined**. Otherwise, **err** is an error object.| 1063 1064**Error codes** 1065 1066| ID| Error Message | 1067| ------- | ----------------------------- | 1068| 2100002 | Operation failed. Cannot connect to service.| 1069| 2100003 | System internal error. | 1070| 2101007 | The same callback exists. | 1071 1072**Example** 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 1084Registers a listener for **netAvailable** events. 1085 1086**Model restriction**: Before you call this API, make sure that you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network. 1087 1088**System capability**: SystemCapability.Communication.NetManager.Core 1089 1090**Parameters** 1091 1092| Name | Type | Mandatory| Description | 1093| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ | 1094| type | string | Yes | Event type. The value is fixed to **netAvailable**.<br>**netAvailable**: event indicating that the data network is available.| 1095| callback | Callback\<[NetHandle](#nethandle)> | Yes | Callback used to return the network handle.| 1096 1097**Example** 1098 1099```js 1100// Create a NetConnection object. 1101let netCon = connection.createNetConnection() 1102 1103// Call register to register a listener. 1104netCon.register(function (error) { 1105 console.log(JSON.stringify(error)) 1106}) 1107 1108// Subscribe to netAvailable events. Event notifications can be received only after register is called. 1109netCon.on('netAvailable', function (data) { 1110 console.log(JSON.stringify(data)) 1111}) 1112 1113// Call unregister to unregister the listener. 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 1123Registers a listener for **netBlockStatusChange** events. 1124 1125**Model restriction**: Before you call this API, make sure tat you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network. 1126 1127**System capability**: SystemCapability.Communication.NetManager.Core 1128 1129**Parameters** 1130 1131| Name | Type | Mandatory| Description | 1132| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1133| type | string | Yes | Event type. The value is fixed to **netBlockStatusChange**.<br>**netBlockStatusChange**: event indicating a change in the network blocking status.| 1134| callback | Callback<{ netHandle: [NetHandle](#nethandle), blocked: boolean }> | Yes | Callback used to return the network handle (**netHandle**) and network status (**blocked**).| 1135 1136**Example** 1137 1138```js 1139// Create a NetConnection object. 1140let netCon = connection.createNetConnection() 1141 1142// Call register to register a listener. 1143netCon.register(function (error) { 1144 console.log(JSON.stringify(error)) 1145}) 1146 1147// Subscribe to netBlockStatusChange events. Event notifications can be received only after register is called. 1148netCon.on('netBlockStatusChange', function (data) { 1149 console.log(JSON.stringify(data)) 1150}) 1151 1152// Call unregister to unregister the listener. 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 1162Registers a listener for **netCapabilitiesChange** events. 1163 1164**Model restriction**: Before you call this API, make sure tat you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network. 1165 1166**System capability**: SystemCapability.Communication.NetManager.Core 1167 1168**Parameters** 1169 1170| Name | Type | Mandatory| Description | 1171| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1172| type | string | Yes | Event type. The value is fixed to **netCapabilitiesChange**.<br>**netCapabilitiesChange**: event indicating that the network capabilities have changed.| 1173| callback | Callback<{ netHandle: [NetHandle](#nethandle), netCap: [NetCapabilities](#netcapabilities) }> | Yes | Callback used to return the network handle (**netHandle**) and capability information (**netCap**).| 1174 1175**Example** 1176 1177```js 1178// Create a NetConnection object. 1179let netCon = connection.createNetConnection() 1180 1181// Call register to register a listener. 1182netCon.register(function (error) { 1183 console.log(JSON.stringify(error)) 1184}) 1185 1186// Subscribe to netCapabilitiesChange events. Event notifications can be received only after register is called. 1187netCon.on('netCapabilitiesChange', function (data) { 1188 console.log(JSON.stringify(data)) 1189}) 1190 1191// Call unregister to unregister the listener. 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 1201Registers a listener for **netConnectionPropertiesChange** events. 1202 1203**Model restriction**: Before you call this API, make sure tat you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network. 1204 1205**System capability**: SystemCapability.Communication.NetManager.Core 1206 1207**Parameters** 1208 1209| Name | Type | Mandatory| Description | 1210| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1211| type | string | Yes | Event type. The value is fixed to **netConnectionPropertiesChange**.<br>**netConnectionPropertiesChange**: event indicating that network connection properties have changed.| 1212| callback | Callback<{ netHandle: [NetHandle](#nethandle), connectionProperties: [ConnectionProperties](#connectionproperties) }> | Yes | Callback used to return the network handle (**netHandle**) and capability information (**netCap**).| 1213 1214**Example** 1215 1216```js 1217// Create a NetConnection object. 1218let netCon = connection.createNetConnection() 1219 1220// Call register to register a listener. 1221netCon.register(function (error) { 1222 console.log(JSON.stringify(error)) 1223}) 1224 1225// Subscribe to netConnectionPropertiesChange events. Event notifications can be received only after register is called. 1226netCon.on('netConnectionPropertiesChange', function (data) { 1227 console.log(JSON.stringify(data)) 1228}) 1229 1230// Call unregister to unregister the listener. 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 1240Registers a listener for **netLost** events. 1241 1242**Model restriction**: Before you call this API, make sure tat you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network. 1243 1244**System capability**: SystemCapability.Communication.NetManager.Core 1245 1246**Parameters** 1247 1248| Name | Type | Mandatory| Description | 1249| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ | 1250| type | string | Yes | Event type. The value is fixed to **netLost**.<br>netLost: event indicating that the network is interrupted or normally disconnected.| 1251| callback | Callback\<[NetHandle](#nethandle)> | Yes | Callback used to return the network handle (**netHandle**).| 1252 1253**Example** 1254 1255```js 1256// Create a NetConnection object. 1257let netCon = connection.createNetConnection() 1258 1259// Call register to register a listener. 1260netCon.register(function (error) { 1261 console.log(JSON.stringify(error)) 1262}) 1263 1264// Subscribe to netLost events. Event notifications can be received only after register is called. 1265netCon.on('netLost', function (data) { 1266 console.log(JSON.stringify(data)) 1267}) 1268 1269// Call unregister to unregister the listener. 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 1279Registers a listener for **netUnavailable** events. 1280 1281**Model restriction**: Before you call this API, make sure tat you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network. 1282 1283**System capability**: SystemCapability.Communication.NetManager.Core 1284 1285**Parameters** 1286 1287| Name | Type | Mandatory| Description | 1288| -------- | --------------- | ---- | ------------------------------------------------------------ | 1289| type | string | Yes | Event type. The value is fixed to **netUnavailable**.<br>**netUnavailable**: event indicating that the network is unavailable.| 1290| callback | Callback\<void> | Yes | Callback used to return the result, which is empty.| 1291 1292**Example** 1293 1294```js 1295// Create a NetConnection object. 1296let netCon = connection.createNetConnection() 1297 1298// Call register to register a listener. 1299netCon.register(function (error) { 1300 console.log(JSON.stringify(error)) 1301}) 1302 1303// Subscribe to netUnavailable events. Event notifications can be received only after register is called. 1304netCon.on('netUnavailable', function (data) { 1305 console.log(JSON.stringify(data)) 1306}) 1307 1308// Call unregister to unregister the listener. 1309netCon.unregister(function (error) { 1310 console.log(JSON.stringify(error)) 1311}) 1312``` 1313 1314## NetHandle 1315 1316Defines the handle of the data network. 1317 1318Before invoking **NetHandle** APIs, call **getNetHandle** to obtain a **NetHandle** object. 1319 1320**System capability**: SystemCapability.Communication.NetManager.Core 1321 1322### Attributes 1323 1324| Name | Type | Mandatory| Description | 1325| ------ | ------ | --- |------------------------- | 1326| netId | number | Yes | Network ID. The value **0** indicates no default network. Any other value must be greater than or equal to 100.| 1327 1328### bindSocket<sup>9+</sup> 1329 1330bindSocket(socketParam: TCPSocket \| UDPSocket, callback: AsyncCallback\<void>): void 1331 1332Binds a **TCPSocket** or **UDPSocket** object to the data network. This API uses an asynchronous callback to return the result. 1333 1334**System capability**: SystemCapability.Communication.NetManager.Core 1335 1336**Parameters** 1337 1338| Name | Type | Mandatory| Description | 1339| ----------- | ------------------------ | ---- | -------------------------------| 1340| socketParam | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | Yes| **TCPSocket** or **UDPSocket** object.| 1341| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the **TCPSocket** or **UDPSocket** object is successfully bound to the current network, **err** is **undefined**. Otherwise, **err** is an error object.| 1342 1343**Error codes** 1344 1345| ID| Error Message | 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**Example** 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 1405Binds a **TCPSocket** or **UDPSocket** object to the data network. This API uses a promise to return the result. 1406 1407**System capability**: SystemCapability.Communication.NetManager.Core 1408 1409**Parameters** 1410 1411| Name | Type | Mandatory | Description | 1412| --------------- | --------------------- | ---- | ------------------------------ | 1413| socketParam | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | Yes | **TCPSocket** or **UDPSocket** object.| 1414 1415**Return value** 1416 1417| Type | Description | 1418| -------------- | ---------------------- | 1419| Promise\<void> | Promise that returns no value.| 1420 1421**Error codes** 1422 1423| ID| Error Message | 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**Example** 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 1479Resolves the host name by using the corresponding network to obtain all IP addresses. This API uses an asynchronous callback to return the result. 1480 1481**Required permissions**: ohos.permission.INTERNET 1482 1483**System capability**: SystemCapability.Communication.NetManager.Core 1484 1485**Parameters** 1486 1487| Name | Type | Mandatory| Description | 1488| -------- | ------------------------------------------------- | ---- | ------------------ | 1489| host | string | Yes | Host name to resolve.| 1490| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | Yes | Callback used to return the result. If all IP addresses are successfully obtained, **err** is **undefined**, and **data** is the list of all obtained IP addresses. Otherwise, **err** is an error object.| 1491 1492**Error codes** 1493 1494| ID| Error Message | 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**Example** 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 1518Resolves the host name by using the corresponding network to obtain all IP addresses. This API uses a promise to return the result. 1519 1520**Required permissions**: ohos.permission.INTERNET 1521 1522**System capability**: SystemCapability.Communication.NetManager.Core 1523 1524**Parameters** 1525 1526| Name| Type | Mandatory| Description | 1527| ------ | ------ | ---- | ------------------ | 1528| host | string | Yes | Host name to resolve.| 1529 1530**Return value** 1531 1532| Type | Description | 1533| ------------------------------------------- | ----------------------------- | 1534| Promise\<Array\<[NetAddress](#netaddress)>> | Promise used to return the result.| 1535 1536**Error codes** 1537 1538| ID| Error Message | 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**Example** 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 1561Resolves the host name by using the corresponding network to obtain the first IP address. This API uses an asynchronous callback to return the result. 1562 1563**Required permissions**: ohos.permission.INTERNET 1564 1565**System capability**: SystemCapability.Communication.NetManager.Core 1566 1567**Parameters** 1568 1569| Name | Type | Mandatory| Description | 1570| -------- | ----------------------------------------- | ---- | ------------------ | 1571| host | string | Yes | Host name to resolve.| 1572| callback | AsyncCallback\<[NetAddress](#netaddress)> | Yes | Callback used to return the result. If the first IP address is obtained successfully, **err** is **undefined**, and **data** is the first obtained IP address. Otherwise, **err** is an error object. | 1573 1574**Error codes** 1575 1576| ID| Error Message | 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**Example** 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 1600Resolves the host name by using the corresponding network to obtain the first IP address. This API uses a promise to return the result. 1601 1602**Required permissions**: ohos.permission.INTERNET 1603 1604**System capability**: SystemCapability.Communication.NetManager.Core 1605 1606**Parameters** 1607 1608| Name| Type | Mandatory| Description | 1609| ------ | ------ | ---- | ------------------ | 1610| host | string | Yes | Host name to resolve.| 1611 1612**Return value** 1613 1614| Type | Description | 1615| ----------------------------------- | ------------------------------- | 1616| Promise\<[NetAddress](#netaddress)> | Promise used to return the result.| 1617 1618**Error codes** 1619 1620| ID| Error Message | 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**Example** 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 1641Defines the network capability. 1642 1643**System capability**: SystemCapability.Communication.NetManager.Core 1644 1645| Name | Value | Description | 1646| ------------------------ | ---- | ---------------------- | 1647| NET_CAPABILITY_MMS | 0 | The network can connect to the carrier's Multimedia Messaging Service Center (MMSC) to send and receive multimedia messages.| 1648| NET_CAPABILITY_NOT_METERED | 11 | The network traffic is not metered.| 1649| NET_CAPABILITY_INTERNET | 12 | The network has the Internet access capability, which is set by the network provider.| 1650| NET_CAPABILITY_NOT_VPN | 15 | The network does not use a virtual private network (VPN).| 1651| NET_CAPABILITY_VALIDATED | 16 | The Internet access capability of the network is successfully verified by the connection management module.| 1652 1653## NetBearType 1654 1655Enumerates network types. 1656 1657**System capability**: SystemCapability.Communication.NetManager.Core 1658 1659| Name | Value | Description | 1660| --------------- | ---- | ----------- | 1661| BEARER_CELLULAR | 0 | Cellular network. | 1662| BEARER_WIFI | 1 | Wi-Fi network.| 1663| BEARER_ETHERNET | 3 | Ethernet network.| 1664 1665## NetSpecifier 1666 1667Provides an instance that bears data network capabilities. 1668 1669**System capability**: SystemCapability.Communication.NetManager.Core 1670 1671| Name | Type | Mandatory | Description | 1672| ----------------------- | ----------------------------------- | ---- | ------------------------------------------------------------ | 1673| netCapabilities | [NetCapabilities](#netcapabilities) | Yes | Network transmission capabilities and bearer types of the data network. | 1674| bearerPrivateIdentifier | string | No | Network identifier. The identifier of a Wi-Fi network is **wifi**, and that of a cellular network is **slot0** (corresponding to SIM card 1).| 1675 1676## NetCapabilities 1677 1678Defines the network capability set. 1679 1680**System capability**: SystemCapability.Communication.NetManager.Core 1681 1682| Name | Type | Mandatory| Description | 1683| --------------------- | ---------------------------------- | --- | ------------------------ | 1684| linkUpBandwidthKbps | number | No| Uplink (from the device to the network) bandwidth. | 1685| linkDownBandwidthKbps | number | No| Downlink (from the network to the device) bandwidth. | 1686| networkCap | Array\<[NetCap](#netcap)> | No| Network capability. | 1687| bearerTypes | Array\<[NetBearType](#netbeartype)> | Yes| Network type. | 1688 1689## ConnectionProperties 1690 1691Defines the network connection properties. 1692 1693**System capability**: SystemCapability.Communication.NetManager.Core 1694 1695| Name | Type | Mandatory| Description | 1696| ------------- | ---------------------------------- | ----|---------------- | 1697| interfaceName | string | Yes|Network interface card (NIC) name. | 1698| domains | string | Yes|Domain. The default value is **""**.| 1699| linkAddresses | Array\<[LinkAddress](#linkaddress)> | Yes|Link information. | 1700| routes | Array\<[RouteInfo](#routeinfo)> | Yes|Route information. | 1701| dnses | Array\<[NetAddress](#netaddress)> | Yes|Network address. For details, see [NetAddress](#netaddress).| 1702| mtu | number | Yes|Maximum transmission unit (MTU). | 1703 1704## RouteInfo 1705 1706Defines network route information. 1707 1708**System capability**: SystemCapability.Communication.NetManager.Core 1709 1710| Name | Type | Mandatory|Description | 1711| -------------- | --------------------------- | --- |---------------- | 1712| interface | string | Yes|NIC name. | 1713| destination | [LinkAddress](#linkaddress) | Yes|Destination address. | 1714| gateway | [NetAddress](#netaddress) | Yes|Gateway address. | 1715| hasGateway | boolean | Yes|Whether a gateway is present. | 1716| isDefaultRoute | boolean | Yes|Whether the route is the default route.| 1717 1718## LinkAddress 1719 1720Defines network link information. 1721 1722**System capability**: SystemCapability.Communication.NetManager.Core 1723 1724| Name | Type | Mandatory|Description | 1725| ------------ | ----------------------- |---- |-------------------- | 1726| address | [NetAddress](#netaddress) | Yes| Link address. | 1727| prefixLength | number | Yes|Length of the link address prefix.| 1728 1729## NetAddress 1730 1731Defines a network address. 1732 1733**System capability**: SystemCapability.Communication.NetManager.Core 1734 1735| Name | Type | Mandatory| Description | 1736| ------- | ------ | -- |------------------------------ | 1737| address | string | Yes|Network address. | 1738| family | number | No|Address family identifier. The value is **1** for IPv4 and **2** for IPv6. The default value is **1**.| 1739| port | number | No|Port number. The value ranges from **0** to **65535**. | 1740