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 14## connection.createNetConnection 15 16createNetConnection(netSpecifier?: NetSpecifier, timeout?: number): NetConnection 17 18Creates a **NetConnection** object. **netSpecifier** specifies the network, and **timeout** specifies the timeout duration in ms. **timeout** is configurable only when **netSpecifier** is specified. If neither of them is present, the default network is used. 19 20**System capability**: SystemCapability.Communication.NetManager.Core 21 22**Parameters** 23 24| Name | Type | Mandatory| Description | 25| ------------ | ----------------------------- | ---- | ------------------------------------------------------------ | 26| netSpecifier | [NetSpecifier](#netspecifier) | No | Network specifier, which specifies the characteristics of a network. If this parameter is not set or is set to **undefined**, the default network is used. | 27| timeout | number | No | Timeout duration for obtaining the network specified by **netSpecifier**. This parameter is valid only when **netSpecifier** is specified. The default value is **0** if **netSpecifier** is **undefined**.| 28 29**Return value** 30 31| Type | Description | 32| ------------------------------- | -------------------- | 33| [NetConnection](#netconnection) | Handle of the network specified by **netSpecifier**.| 34 35**Example** 36 37```js 38// For the default network, you do not need to pass in parameters. 39let netConnection = connection.createNetConnection() 40 41// For the cellular network, you need to pass in related network parameters. If the timeout parameter is not specified, the timeout value is 0 by default. 42let netConnectionCellular = connection.createNetConnection({ 43 netCapabilities: { 44 bearerTypes: [connection.NetBearType.BEARER_CELLULAR] 45 } 46}) 47``` 48 49## connection.getDefaultNet 50 51getDefaultNet(callback: AsyncCallback\<NetHandle>): void 52 53Obtains 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. 54 55**Required permission**: ohos.permission.GET_NETWORK_INFO 56 57**System capability**: SystemCapability.Communication.NetManager.Core 58 59**Parameters** 60 61| Name | Type | Mandatory| Description | 62| -------- | --------------------------------------- | ---- | ---------- | 63| 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.| 64 65**Error codes** 66 67| ID| Error Message | 68| ------- | ----------------------------- | 69| 201 | Permission denied. | 70| 2100002 | Operation failed. Cannot connect to service.| 71| 2100003 | System internal error. | 72 73**Example** 74 75```js 76connection.getDefaultNet(function (error, data) { 77 console.log(JSON.stringify(error)) 78 console.log(JSON.stringify(data)) 79}) 80``` 81 82## connection.getDefaultNet 83 84getDefaultNet(): Promise\<NetHandle> 85 86Obtains 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. 87 88**Required permission**: ohos.permission.GET_NETWORK_INFO 89 90**System capability**: SystemCapability.Communication.NetManager.Core 91 92**Return value** 93 94| Type | Description | 95| --------------------------------- | ------------------------------------- | 96| Promise\<[NetHandle](#nethandle)> | Promise used to return the result.| 97 98**Error codes** 99 100| ID| Error Message | 101| ------- | ----------------------------- | 102| 201 | Permission denied. | 103| 2100002 | Operation failed. Cannot connect to service.| 104| 2100003 | System internal error. | 105 106**Example** 107 108```js 109connection.getDefaultNet().then(function (data) { 110 console.log(JSON.stringify(data)) 111}) 112``` 113 114## connection.getDefaultNetSync<sup>9+</sup> 115 116getDefaultNetSync(): NetHandle 117 118Obtains the default active data network in synchronous mode. You can use [getNetCapabilities](#connectiongetnetcapabilities) to obtain information such as the network type and capabilities. 119 120**Required permission**: ohos.permission.GET_NETWORK_INFO 121 122**System capability**: SystemCapability.Communication.NetManager.Core 123 124**Return value** 125 126| Type | Description | 127| --------- | ---------------------------------- | 128| NetHandle | Handle of the default active data network.| 129 130**Error codes** 131 132| ID| Error Message | 133| ------- | ----------------------------- | 134| 201 | Permission denied. | 135| 2100002 | Operation failed. Cannot connect to service.| 136| 2100003 | System internal error. | 137 138**Example** 139 140```js 141let netHandle = connection.getDefaultNetSync(); 142``` 143 144## connection.getAppNet<sup>9+</sup> 145 146getAppNet(callback: AsyncCallback\<NetHandle>): void 147 148Obtains information about the network bound to an application. This API uses an asynchronous callback to return the result. 149 150**System capability**: SystemCapability.Communication.NetManager.Core 151 152**Parameters** 153 154| Name | Type | Mandatory| Description | 155| --------- | ------------------------------------------------------------ | ---- | ---------------- | 156| 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.| 157 158**Error codes** 159 160| ID| Error Message | 161| ------- | ----------------------------- | 162| 2100002 | Operation failed. Cannot connect to service.| 163| 2100003 | System internal error. | 164 165**Example** 166 167```js 168connection.getAppNet(function (error, data) { 169 console.log(JSON.stringify(error)) 170 console.log(JSON.stringify(data)) 171}) 172``` 173 174## connection.getAppNet<sup>9+</sup> 175 176getAppNet(): Promise\<NetHandle>; 177 178Obtains information about the network bound to an application. This API uses a promise to return the result. 179 180**System capability**: SystemCapability.Communication.NetManager.Core 181 182**Return value** 183 184| Type | Description | 185| --------------------------------- | ------------------------------------- | 186| Promise\<[NetHandle](#nethandle)> | Promise used to return the result.| 187 188**Error codes** 189 190| ID| Error Message | 191| ------- | ----------------------------- | 192| 2100002 | Operation failed. Cannot connect to service.| 193| 2100003 | System internal error. | 194 195**Example** 196 197```js 198connection.getAppNet().then((data) => { 199 console.info(JSON.stringify(data)); 200}).catch(error => { 201 console.info(JSON.stringify(error)); 202}) 203``` 204 205## connection.SetAppNet<sup>9+</sup> 206 207setAppNet(netHandle: NetHandle, callback: AsyncCallback\<void>): void 208 209Binds 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. 210 211**Required permissions**: ohos.permission.INTERNET 212 213**System capability**: SystemCapability.Communication.NetManager.Core 214 215**Parameters** 216 217| Name | Type | Mandatory| Description | 218| --------- | ------------------------------------------------------------ | ---- | ---------------- | 219| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network.| 220| 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.| 221 222**Error codes** 223 224| ID| Error Message | 225| ------- | ----------------------------- | 226| 201 | Permission denied. | 227| 401 | Parameter error. | 228| 2100001 | Invalid parameter value. | 229| 2100002 | Operation failed. Cannot connect to service.| 230| 2100003 | System internal error. | 231 232**Example** 233 234```js 235connection.getDefaultNet(function (error, netHandle) { 236 connection.setAppNet(netHandle, (error, data) => { 237 console.log(JSON.stringify(error)) 238 console.log(JSON.stringify(data)) 239 }); 240}) 241``` 242 243## connection.SetAppNet<sup>9+</sup> 244 245setAppNet(netHandle: NetHandle): Promise\<void>; 246 247Binds 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. 248 249**Required permissions**: ohos.permission.INTERNET 250 251**System capability**: SystemCapability.Communication.NetManager.Core 252 253**Parameters** 254 255| Name | Type | Mandatory| Description | 256| --------- | ------------------------------------------------------------ | ---- | ---------------- | 257| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network.| 258 259**Return value** 260 261| Type | Description | 262| ------------------------------------------- | ----------------------------- | 263| Promise\<void> | Promise that returns no value.| 264 265**Error codes** 266 267| ID| Error Message | 268| ------- | ----------------------------- | 269| 201 | Permission denied. | 270| 401 | Parameter error. | 271| 2100001 | Invalid parameter value. | 272| 2100002 | Operation failed. Cannot connect to service.| 273| 2100003 | System internal error. | 274 275**Example** 276 277```js 278connection.getDefaultNet().then(function (netHandle) { 279 connection.setAppNet(netHandle).then(() => { 280 console.log("success") 281 }).catch(error => { 282 console.log(JSON.stringify(error)) 283 }) 284}) 285``` 286 287## connection.getAllNets 288 289getAllNets(callback: AsyncCallback<Array<NetHandle>>): void 290 291Obtains the list of all connected networks. This API uses an asynchronous callback to return the result. 292 293**Required permission**: ohos.permission.GET_NETWORK_INFO 294 295**System capability**: SystemCapability.Communication.NetManager.Core 296 297**Parameters** 298 299| Name| Type| Mandatory| Description| 300| -------- | -------- | -------- | -------- | 301| 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.| 302 303**Error codes** 304 305| ID| Error Message | 306| ------- | ----------------------------- | 307| 201 | Permission denied. | 308| 2100002 | Operation failed. Cannot connect to service.| 309| 2100003 | System internal error. | 310 311**Example** 312 313```js 314connection.getAllNets(function (error, data) { 315 console.log(JSON.stringify(error)) 316 console.log(JSON.stringify(data)) 317}); 318``` 319 320## connection.getAllNets 321 322getAllNets(): Promise<Array<NetHandle>> 323 324Obtains the list of all connected networks. This API uses a promise to return the result. 325 326**Required permission**: ohos.permission.GET_NETWORK_INFO 327 328**System capability**: SystemCapability.Communication.NetManager.Core 329 330**Return value** 331 332| Type| Description| 333| -------- | -------- | 334| Promise<Array<[NetHandle](#nethandle)>> | Promise used to return the result.| 335 336**Error codes** 337 338| ID| Error Message | 339| ------- | ----------------------------- | 340| 201 | Permission denied. | 341| 2100002 | Operation failed. Cannot connect to service.| 342| 2100003 | System internal error. | 343 344**Example** 345 346```js 347connection.getAllNets().then(function (data) { 348 console.log(JSON.stringify(data)) 349}); 350``` 351 352## connection.getConnectionProperties 353 354getConnectionProperties(netHandle: NetHandle, callback: AsyncCallback\<ConnectionProperties>): void 355 356Obtains connection properties of the network corresponding to the **netHandle**. This API uses an asynchronous callback to return the result. 357 358**Required permission**: ohos.permission.GET_NETWORK_INFO 359 360**System capability**: SystemCapability.Communication.NetManager.Core 361 362**Parameters** 363 364| Name | Type | Mandatory| Description | 365| --------- | ------------------------------------------------------------ | ---- | ---------------- | 366| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network.| 367| 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.| 368 369**Error codes** 370 371| ID| Error Message | 372| ------- | ----------------------------- | 373| 201 | Permission denied. | 374| 401 | Parameter error. | 375| 2100001 | Invalid parameter value. | 376| 2100002 | Operation failed. Cannot connect to service.| 377| 2100003 | System internal error. | 378 379**Example** 380 381```js 382connection.getDefaultNet().then(function (netHandle) { 383 connection.getConnectionProperties(netHandle, function (error, data) { 384 console.log(JSON.stringify(error)) 385 console.log(JSON.stringify(data)) 386 }) 387}) 388``` 389 390## connection.getConnectionProperties 391 392getConnectionProperties(netHandle: NetHandle): Promise\<ConnectionProperties> 393 394Obtains connection properties of the network corresponding to the **netHandle**. This API uses a promise to return the result. 395 396**Required permission**: ohos.permission.GET_NETWORK_INFO 397 398**System capability**: SystemCapability.Communication.NetManager.Core 399 400**Parameters** 401 402| Name | Type | Mandatory| Description | 403| --------- | ----------------------- | ---- | ---------------- | 404| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network.| 405 406**Return value** 407 408| Type | Description | 409| ------------------------------------------------------- | --------------------------------- | 410| Promise\<[ConnectionProperties](#connectionproperties)> | Promise used to return the result.| 411 412**Error codes** 413 414| ID| Error Message | 415| ------- | ----------------------------- | 416| 201 | Permission denied. | 417| 401 | Parameter error. | 418| 2100001 | Invalid parameter value. | 419| 2100002 | Operation failed. Cannot connect to service.| 420| 2100003 | System internal error. | 421 422**Example** 423 424```js 425connection.getDefaultNet().then(function (netHandle) { 426 connection.getConnectionProperties(netHandle).then(function (data) { 427 console.log(JSON.stringify(data)) 428 }) 429}) 430``` 431 432## connection.getNetCapabilities 433 434getNetCapabilities(netHandle: NetHandle, callback: AsyncCallback\<NetCapabilities>): void 435 436Obtains capability information of the network corresponding to the **netHandle**. This API uses an asynchronous callback to return the result. 437 438**Required permission**: ohos.permission.GET_NETWORK_INFO 439 440**System capability**: SystemCapability.Communication.NetManager.Core 441 442**Parameters** 443 444| Name | Type | Mandatory| Description | 445| --------- | --------------------------------------------------- | ---- | ---------------- | 446| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network.| 447| 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. | 448 449**Error codes** 450 451| ID| Error Message | 452| ------- | ----------------------------- | 453| 201 | Permission denied. | 454| 401 | Parameter error. | 455| 2100001 | Invalid parameter value. | 456| 2100002 | Operation failed. Cannot connect to service.| 457| 2100003 | System internal error. | 458 459**Example** 460 461```js 462connection.getDefaultNet().then(function (netHandle) { 463 connection.getNetCapabilities(netHandle, function (error, data) { 464 console.log(JSON.stringify(error)) 465 console.log(JSON.stringify(data)) 466 }) 467}) 468``` 469 470## connection.getNetCapabilities 471 472getNetCapabilities(netHandle: NetHandle): Promise\<NetCapabilities> 473 474Obtains capability information of the network corresponding to the **netHandle**. This API uses a promise to return the result. 475 476**Required permission**: ohos.permission.GET_NETWORK_INFO 477 478**System capability**: SystemCapability.Communication.NetManager.Core 479 480**Parameters** 481 482| Name | Type | Mandatory| Description | 483| --------- | ----------------------- | ---- | ---------------- | 484| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network.| 485 486**Return value** 487 488| Type | Description | 489| --------------------------------------------- | --------------------------------- | 490| Promise\<[NetCapabilities](#netcapabilities)> | Promise used to return the result.| 491 492**Error codes** 493 494| ID| Error Message | 495| ------- | ----------------------------- | 496| 201 | Permission denied. | 497| 401 | Parameter error. | 498| 2100001 | Invalid parameter value. | 499| 2100002 | Operation failed. Cannot connect to service.| 500| 2100003 | System internal error. | 501 502**Example** 503 504```js 505connection.getDefaultNet().then(function (netHandle) { 506 connection.getNetCapabilities(netHandle).then(function (data) { 507 console.log(JSON.stringify(data)) 508 }) 509}) 510``` 511 512## connection.isDefaultNetMetered<sup>9+</sup> 513 514isDefaultNetMetered(callback: AsyncCallback\<boolean>): void 515 516Checks whether the data traffic usage on the current network is metered. This API uses an asynchronous callback to return the result. 517 518**Required permission**: ohos.permission.GET_NETWORK_INFO 519 520**System capability**: SystemCapability.Communication.NetManager.Core 521 522**Parameters** 523 524| Name | Type | Mandatory| Description | 525| -------- | ----------------------- | ---- | -------------------------------------- | 526| callback | AsyncCallback\<boolean> | Yes | Callback used to return the result. The value **true** indicates the data traffic usage is metered.| 527 528**Error codes** 529 530| ID| Error Message | 531| ------- | ----------------------------- | 532| 201 | Permission denied. | 533| 2100002 | Operation failed. Cannot connect to service.| 534| 2100003 | System internal error. | 535 536**Example** 537 538```js 539connection.isDefaultNetMetered(function (error, data) { 540 console.log(JSON.stringify(error)) 541 console.log('data: ' + data) 542}) 543``` 544 545## connection.isDefaultNetMetered<sup>9+</sup> 546 547isDefaultNetMetered(): Promise\<boolean> 548 549Checks whether the data traffic usage on the current network is metered. This API uses a promise to return the result. 550 551**Required permission**: ohos.permission.GET_NETWORK_INFO 552 553**System capability**: SystemCapability.Communication.NetManager.Core 554 555**Return value** 556 557| Type | Description | 558| ----------------- | ----------------------------------------------- | 559| Promise\<boolean> | Promise used to return the result. The value **true** indicates the data traffic usage is metered.| 560 561**Error codes** 562 563| ID| Error Message | 564| ------- | ----------------------------- | 565| 201 | Permission denied. | 566| 2100002 | Operation failed. Cannot connect to service.| 567| 2100003 | System internal error. | 568 569**Example** 570 571```js 572connection.isDefaultNetMetered().then(function (data) { 573 console.log('data: ' + data) 574}) 575``` 576 577## connection.hasDefaultNet 578 579hasDefaultNet(callback: AsyncCallback\<boolean>): void 580 581Checks 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. 582 583**Required permission**: ohos.permission.GET_NETWORK_INFO 584 585**System capability**: SystemCapability.Communication.NetManager.Core 586 587**Parameters** 588 589| Name | Type | Mandatory| Description | 590| -------- | ----------------------- | ---- | -------------------------------------- | 591| callback | AsyncCallback\<boolean> | Yes | Callback used to return the result. The value **true** indicates the default data network is activated.| 592 593**Error codes** 594 595| ID| Error Message | 596| ------- | ----------------------------- | 597| 201 | Permission denied. | 598| 2100002 | Operation failed. Cannot connect to service.| 599| 2100003 | System internal error. | 600 601**Example** 602 603```js 604connection.hasDefaultNet(function (error, data) { 605 console.log(JSON.stringify(error)) 606 console.log('data: ' + data) 607}) 608``` 609 610## connection.hasDefaultNet 611 612hasDefaultNet(): Promise\<boolean> 613 614Checks 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. 615 616**Required permission**: ohos.permission.GET_NETWORK_INFO 617 618**System capability**: SystemCapability.Communication.NetManager.Core 619 620**Return value** 621 622| Type | Description | 623| ----------------- | ----------------------------------------------- | 624| Promise\<boolean> | Promise used to return the result. The value **true** indicates that the default data network is activated.| 625 626**Error codes** 627 628| ID| Error Message | 629| ------- | ----------------------------- | 630| 201 | Permission denied. | 631| 2100002 | Operation failed. Cannot connect to service.| 632| 2100003 | System internal error. | 633 634**Example** 635 636```js 637connection.hasDefaultNet().then(function (data) { 638 console.log('data: ' + data) 639}) 640``` 641 642## connection.enableAirplaneMode 643 644enableAirplaneMode(callback: AsyncCallback\<void>): void 645 646Enables the airplane mode. This API uses an asynchronous callback to return the result. 647 648**System API**: This is a system API. 649 650**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 651 652**System capability**: SystemCapability.Communication.NetManager.Core 653 654**Parameters** 655 656| Name | Type | Mandatory| Description | 657| -------- | ------------------------------------------------- | ---- | ------------------ | 658| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 659 660**Error codes** 661 662| ID| Error Message | 663| ------- | ----------------------------- | 664| 2100002 | Operation failed. Cannot connect to service.| 665| 2100003 | System internal error. | 666 667**Example** 668 669```js 670connection.enableAirplaneMode(function (error) { 671 console.log(JSON.stringify(error)) 672}) 673``` 674 675## connection.enableAirplaneMode 676 677enableAirplaneMode(): Promise\<void> 678 679Enables the airplane mode. This API uses a promise to return the result. 680 681**System API**: This is a system API. 682 683**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 684 685**System capability**: SystemCapability.Communication.NetManager.Core 686 687**Return value** 688 689| Type | Description | 690| ------------------------------------------- | ----------------------------- | 691| Promise\<void> | Promise that returns no value.| 692 693**Error codes** 694 695| ID| Error Message | 696| ------- | ----------------------------- | 697| 2100002 | Operation failed. Cannot connect to service.| 698| 2100003 | System internal error. | 699 700**Example** 701 702```js 703connection.enableAirplaneMode().then(function (error) { 704 console.log(JSON.stringify(error)) 705}) 706``` 707 708## connection.disableAirplaneMode 709 710disableAirplaneMode(callback: AsyncCallback\<void>): void 711 712Disables the airplane mode. This API uses an asynchronous callback to return the result. 713 714**System API**: This is a system API. 715 716**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 717 718**System capability**: SystemCapability.Communication.NetManager.Core 719 720**Parameters** 721 722| Name | Type | Mandatory| Description | 723| -------- | ------------------------------------------------- | ---- | ------------------ | 724| 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.| 725 726**Error codes** 727 728| ID| Error Message | 729| ------- | ----------------------------- | 730| 2100002 | Operation failed. Cannot connect to service.| 731| 2100003 | System internal error. | 732 733**Example** 734 735```js 736connection.disableAirplaneMode(function (error) { 737 console.log(JSON.stringify(error)) 738}) 739``` 740 741## connection.disableAirplaneMode 742 743disableAirplaneMode(): Promise\<void> 744 745Disables the airplane mode. This API uses a promise to return the result. 746 747**System API**: This is a system API. 748 749**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 750 751**System capability**: SystemCapability.Communication.NetManager.Core 752 753**Return value** 754 755| Type | Description | 756| ------------------------------------------- | ----------------------------- | 757| Promise\<void> | Promise that returns no value.| 758 759**Error codes** 760 761| ID| Error Message | 762| ------- | ----------------------------- | 763| 2100002 | Operation failed. Cannot connect to service.| 764| 2100003 | System internal error. | 765 766**Example** 767 768```js 769connection.disableAirplaneMode().then(function (error) { 770 console.log(JSON.stringify(error)) 771}) 772``` 773 774## connection.reportNetConnected 775 776reportNetConnected(netHandle: NetHandle, callback: AsyncCallback<void>): void 777 778Reports connection of the data network to the network management module. This 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 connection of the data network to the network management module. This API uses a promise to return the result. 816 817**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET 818 819**System capability**: SystemCapability.Communication.NetManager.Core 820 821**Parameters** 822 823| Name| Type| Mandatory| Description| 824| -------- | -------- | -------- | -------- | 825| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).| 826 827**Return value** 828| Type| Description| 829| -------- | -------- | 830| Promise<void> | Promise that returns no value.| 831 832**Error codes** 833 834| ID| Error Message | 835| ------- | ----------------------------- | 836| 201 | Permission denied. | 837| 401 | Parameter error. | 838| 2100001 | Invalid parameter value. | 839| 2100002 | Operation failed. Cannot connect to service.| 840| 2100003 | System internal error. | 841 842**Example** 843 844```js 845connection.getDefaultNet().then(function (netHandle) { 846 connection.reportNetConnected(netHandle).then(function () { 847 console.log(`report success`) 848 }); 849}); 850``` 851 852## connection.reportNetDisconnected 853 854reportNetDisconnected(netHandle: NetHandle, callback: AsyncCallback<void>): void 855 856Reports disconnection of the data network to the network management module. This API uses an asynchronous callback to return the result. 857 858**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET 859 860**System capability**: SystemCapability.Communication.NetManager.Core 861 862**Parameters** 863 864| Name| Type| Mandatory| Description| 865| -------- | -------- | -------- | -------- | 866| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).| 867| 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.| 868 869**Error codes** 870 871| ID| Error Message | 872| ------- | ----------------------------- | 873| 201 | Permission denied. | 874| 401 | Parameter error. | 875| 2100001 | Invalid parameter value. | 876| 2100002 | Operation failed. Cannot connect to service.| 877| 2100003 | System internal error. | 878 879**Example** 880 881```js 882connection.getDefaultNet().then(function (netHandle) { 883 connection.reportNetDisconnected(netHandle, function (error) { 884 console.log(JSON.stringify(error)) 885 }); 886}); 887``` 888 889## connection.reportNetDisconnected 890 891reportNetDisconnected(netHandle: NetHandle): Promise<void> 892 893Reports disconnection of the data network to the network management module. This API uses a promise to return the result. 894 895**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET 896 897**System capability**: SystemCapability.Communication.NetManager.Core 898 899**Parameters** 900 901| Name| Type| Mandatory| Description| 902| -------- | -------- | -------- | -------- | 903| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).| 904 905**Return value** 906| Type| Description| 907| -------- | -------- | 908| Promise<void> | Promise that returns no value.| 909 910**Error codes** 911 912| ID| Error Message | 913| ------- | ----------------------------- | 914| 201 | Permission denied. | 915| 401 | Parameter error. | 916| 2100001 | Invalid parameter value. | 917| 2100002 | Operation failed. Cannot connect to service.| 918| 2100003 | System internal error. | 919 920**Example** 921 922```js 923connection.getDefaultNet().then(function (netHandle) { 924 connection.reportNetDisconnected(netHandle).then(function () { 925 console.log(`report success`) 926 }); 927}); 928``` 929 930## connection.getAddressesByName 931 932getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>>): void 933 934Resolves the host name by using the default network to obtain all IP addresses. This API uses an asynchronous callback to return the result. 935 936**Required permissions**: ohos.permission.INTERNET 937 938**System capability**: SystemCapability.Communication.NetManager.Core 939 940**Parameters** 941 942| Name | Type | Mandatory| Description | 943| -------- | ------------------------------------------------- | ---- | ------------------ | 944| host | string | Yes | Host name to resolve.| 945| 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.| 946 947**Error codes** 948 949| ID| Error Message | 950| ------- | ----------------------------- | 951| 201 | Permission denied. | 952| 401 | Parameter error. | 953| 2100001 | Invalid parameter value. | 954| 2100002 | Operation failed. Cannot connect to service.| 955| 2100003 | System internal error. | 956 957**Example** 958 959```js 960let host = "xxxx"; 961connection.getAddressesByName(host, function (error, data) { 962 console.log(JSON.stringify(error)) 963 console.log(JSON.stringify(data)) 964}) 965``` 966 967## connection.getAddressesByName 968 969getAddressesByName(host: string): Promise\<Array\<NetAddress>> 970 971Resolves the host name by using the default network to obtain all IP addresses. This API uses a promise to return the result. 972 973**Required permissions**: ohos.permission.INTERNET 974 975**System capability**: SystemCapability.Communication.NetManager.Core 976 977**Parameters** 978 979| Name| Type | Mandatory| Description | 980| ------ | ------ | ---- | ------------------ | 981| host | string | Yes | Host name to resolve.| 982 983**Return value** 984 985| Type | Description | 986| ------------------------------------------- | ----------------------------- | 987| Promise\<Array\<[NetAddress](#netaddress)>> | Promise used to return the result.| 988 989**Error codes** 990 991| ID| Error Message | 992| ------- | ----------------------------- | 993| 201 | Permission denied. | 994| 401 | Parameter error. | 995| 2100001 | Invalid parameter value. | 996| 2100002 | Operation failed. Cannot connect to service.| 997| 2100003 | System internal error. | 998 999**Example** 1000 1001```js 1002let host = "xxxx"; 1003connection.getAddressesByName(host).then(function (data) { 1004 console.log(JSON.stringify(data)) 1005}) 1006``` 1007 1008## NetConnection 1009 1010Represents the network connection handle. 1011 1012### register 1013 1014register(callback: AsyncCallback\<void>): void 1015 1016Registers a listener for network status changes. 1017 1018**Required permission**: ohos.permission.GET_NETWORK_INFO 1019 1020**System capability**: SystemCapability.Communication.NetManager.Core 1021 1022**Parameters** 1023 1024| Name | Type | Mandatory| Description | 1025| -------- | -------------------- | ---- | ---------- | 1026| 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.| 1027 1028**Error codes** 1029 1030| ID| Error Message | 1031| ------- | ----------------------------- | 1032| 201 | Permission denied. | 1033| 2100002 | Operation failed. Cannot connect to service.| 1034| 2100003 | System internal error. | 1035| 2101008 | The callback is not exists. | 1036| 2101022 | The number of requests exceeded the maximum. | 1037 1038**Example** 1039 1040```js 1041netConnection.register(function (error) { 1042 console.log(JSON.stringify(error)) 1043}) 1044``` 1045 1046### unregister 1047 1048unregister(callback: AsyncCallback\<void>): void 1049 1050Unregisters the listener for network status changes. 1051 1052**System capability**: SystemCapability.Communication.NetManager.Core 1053 1054**Parameters** 1055 1056| Name | Type | Mandatory| Description | 1057| -------- | -------------------- | ---- | ---------- | 1058| 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.| 1059 1060**Error codes** 1061 1062| ID| Error Message | 1063| ------- | ----------------------------- | 1064| 2100002 | Operation failed. Cannot connect to service.| 1065| 2100003 | System internal error. | 1066| 2101007 | The same callback exists. | 1067 1068**Example** 1069 1070```js 1071netConnection.unregister(function (error) { 1072 console.log(JSON.stringify(error)) 1073}) 1074``` 1075 1076### on('netAvailable') 1077 1078on(type: 'netAvailable', callback: Callback\<NetHandle>): void 1079 1080Registers a listener for **netAvailable** events. 1081 1082**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. 1083 1084**System capability**: SystemCapability.Communication.NetManager.Core 1085 1086**Parameters** 1087 1088| Name | Type | Mandatory| Description | 1089| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ | 1090| type | string | Yes | Event type. The value is fixed to **netAvailable**.<br>**netAvailable**: event indicating that the data network is available.| 1091| callback | Callback\<[NetHandle](#nethandle)> | Yes | Callback used to return the network handle.| 1092 1093**Example** 1094 1095```js 1096// Create a NetConnection object. 1097let netCon = connection.createNetConnection() 1098 1099// Call register to register a listener. 1100netCon.register(function (error) { 1101 console.log(JSON.stringify(error)) 1102}) 1103 1104// Subscribe to netAvailable events. Event notifications can be received only after register is called. 1105netCon.on('netAvailable', function (data) { 1106 console.log(JSON.stringify(data)) 1107}) 1108 1109// Call unregister to unregister the listener. 1110netCon.unregister(function (error) { 1111 console.log(JSON.stringify(error)) 1112}) 1113``` 1114 1115### on('netBlockStatusChange') 1116 1117on(type: 'netBlockStatusChange', callback: Callback<{ netHandle: NetHandle, blocked: boolean }>): void 1118 1119Registers a listener for **netBlockStatusChange** events. This API uses an asynchronous callback to return the result. 1120 1121**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. 1122 1123**System capability**: SystemCapability.Communication.NetManager.Core 1124 1125**Parameters** 1126 1127| Name | Type | Mandatory| Description | 1128| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1129| type | string | Yes | Event type. The value is fixed to **netBlockStatusChange**.<br>**netBlockStatusChange**: event indicating a change in the network blocking status.| 1130| callback | Callback<{ netHandle: [NetHandle](#nethandle), blocked: boolean }> | Yes | Callback used to return the network handle (**netHandle**) and network status (**blocked**).| 1131 1132**Example** 1133 1134```js 1135// Create a NetConnection object. 1136let netCon = connection.createNetConnection() 1137 1138// Call register to register a listener. 1139netCon.register(function (error) { 1140 console.log(JSON.stringify(error)) 1141}) 1142 1143// Subscribe to netBlockStatusChange events. Event notifications can be received only after register is called. 1144netCon.on('netBlockStatusChange', function (data) { 1145 console.log(JSON.stringify(data)) 1146}) 1147 1148// Call unregister to unregister the listener. 1149netCon.unregister(function (error) { 1150 console.log(JSON.stringify(error)) 1151}) 1152``` 1153 1154### on('netCapabilitiesChange') 1155 1156on(type: 'netCapabilitiesChange', callback: Callback<{ netHandle: NetHandle, netCap: NetCapabilities }>): void 1157 1158Registers a listener for **netCapabilitiesChange** events. 1159 1160**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. 1161 1162**System capability**: SystemCapability.Communication.NetManager.Core 1163 1164**Parameters** 1165 1166| Name | Type | Mandatory| Description | 1167| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1168| type | string | Yes | Event type. The value is fixed to **netCapabilitiesChange**.<br>**netCapabilitiesChange**: event indicating that the network capabilities have changed.| 1169| callback | Callback<{ netHandle: [NetHandle](#nethandle), netCap: [NetCapabilities](#netcapabilities) }> | Yes | Callback used to return the network handle (**netHandle**) and capability information (**netCap**).| 1170 1171**Example** 1172 1173```js 1174// Create a NetConnection object. 1175let netCon = connection.createNetConnection() 1176 1177// Call register to register a listener. 1178netCon.register(function (error) { 1179 console.log(JSON.stringify(error)) 1180}) 1181 1182// Subscribe to netCapabilitiesChange events. Event notifications can be received only after register is called. 1183netCon.on('netCapabilitiesChange', function (data) { 1184 console.log(JSON.stringify(data)) 1185}) 1186 1187// Call unregister to unregister the listener. 1188netCon.unregister(function (error) { 1189 console.log(JSON.stringify(error)) 1190}) 1191``` 1192 1193### on('netConnectionPropertiesChange') 1194 1195on(type: 'netConnectionPropertiesChange', callback: Callback<{ netHandle: NetHandle, connectionProperties: 1196ConnectionProperties }>): void 1197 1198Registers a listener for **netConnectionPropertiesChange** events. 1199 1200**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. 1201 1202**System capability**: SystemCapability.Communication.NetManager.Core 1203 1204**Parameters** 1205 1206| Name | Type | Mandatory| Description | 1207| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1208| type | string | Yes | Event type. The value is fixed to **netConnectionPropertiesChange**.<br>**netConnectionPropertiesChange**: event indicating that network connection properties have changed.| 1209| callback | Callback<{ netHandle: [NetHandle](#nethandle), connectionProperties: [ConnectionProperties](#connectionproperties) }> | Yes | Callback used to return the network handle (**netHandle**) and connection information (**connectionProperties**).| 1210 1211**Example** 1212 1213```js 1214// Create a NetConnection object. 1215let netCon = connection.createNetConnection() 1216 1217// Call register to register a listener. 1218netCon.register(function (error) { 1219 console.log(JSON.stringify(error)) 1220}) 1221 1222// Subscribe to netConnectionPropertiesChange events. Event notifications can be received only after register is called. 1223netCon.on('netConnectionPropertiesChange', function (data) { 1224 console.log(JSON.stringify(data)) 1225}) 1226 1227// Call unregister to unregister the listener. 1228netCon.unregister(function (error) { 1229 console.log(JSON.stringify(error)) 1230}) 1231``` 1232 1233### on('netLost') 1234 1235on(type: 'netLost', callback: Callback\<NetHandle>): void 1236 1237Registers a listener for **netLost** events. 1238 1239**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. 1240 1241**System capability**: SystemCapability.Communication.NetManager.Core 1242 1243**Parameters** 1244 1245| Name | Type | Mandatory| Description | 1246| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ | 1247| type | string | Yes | Event type. The value is fixed to **netLost**.<br>netLost: event indicating that the network is interrupted or normally disconnected.| 1248| callback | Callback\<[NetHandle](#nethandle)> | Yes | Callback used to return the network handle (**netHandle**).| 1249 1250**Example** 1251 1252```js 1253// Create a NetConnection object. 1254let netCon = connection.createNetConnection() 1255 1256// Call register to register a listener. 1257netCon.register(function (error) { 1258 console.log(JSON.stringify(error)) 1259}) 1260 1261// Subscribe to netLost events. Event notifications can be received only after register is called. 1262netCon.on('netLost', function (data) { 1263 console.log(JSON.stringify(data)) 1264}) 1265 1266// Call unregister to unregister the listener. 1267netCon.unregister(function (error) { 1268 console.log(JSON.stringify(error)) 1269}) 1270``` 1271 1272### on('netUnavailable') 1273 1274on(type: 'netUnavailable', callback: Callback\<void>): void 1275 1276Registers a listener for **netUnavailable** events. 1277 1278**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. 1279 1280**System capability**: SystemCapability.Communication.NetManager.Core 1281 1282**Parameters** 1283 1284| Name | Type | Mandatory| Description | 1285| -------- | --------------- | ---- | ------------------------------------------------------------ | 1286| type | string | Yes | Event type. The value is fixed to **netUnavailable**.<br>**netUnavailable**: event indicating that the network is unavailable.| 1287| callback | Callback\<void> | Yes | Callback used to return the result, which is empty.| 1288 1289**Example** 1290 1291```js 1292// Create a NetConnection object. 1293let netCon = connection.createNetConnection() 1294 1295// Call register to register a listener. 1296netCon.register(function (error) { 1297 console.log(JSON.stringify(error)) 1298}) 1299 1300// Subscribe to netUnavailable events. Event notifications can be received only after register is called. 1301netCon.on('netUnavailable', function (data) { 1302 console.log(JSON.stringify(data)) 1303}) 1304 1305// Call unregister to unregister the listener. 1306netCon.unregister(function (error) { 1307 console.log(JSON.stringify(error)) 1308}) 1309``` 1310 1311## NetHandle 1312 1313Defines the handle of the data network. 1314 1315Before invoking **NetHandle** APIs, call **getNetHandle** to obtain a **NetHandle** object. 1316 1317**System capability**: SystemCapability.Communication.NetManager.Core 1318 1319### Attributes 1320 1321| Name | Type | Mandatory| Description | 1322| ------ | ------ | --- |------------------------- | 1323| netId | number | Yes | Network ID. The value **0** indicates no default network. Any other value must be greater than or equal to 100.| 1324 1325### bindSocket<sup>9+</sup> 1326 1327bindSocket(socketParam: TCPSocket \| UDPSocket, callback: AsyncCallback\<void>): void 1328 1329Binds a **TCPSocket** or **UDPSocket** object to the data network. This API uses an asynchronous callback to return the result. 1330 1331**System capability**: SystemCapability.Communication.NetManager.Core 1332 1333**Parameters** 1334 1335| Name | Type | Mandatory| Description | 1336| ----------- | ------------------------ | ---- | -------------------------------| 1337| socketParam | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | Yes| **TCPSocket** or **UDPSocket** object.| 1338| 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.| 1339 1340**Error codes** 1341 1342| ID| Error Message | 1343| ------- | ----------------------------- | 1344| 401 | Parameter error. | 1345| 2100001 | Invalid parameter value. | 1346| 2100002 | Operation failed. Cannot connect to service.| 1347| 2100003 | System internal error. | 1348 1349**Example** 1350 1351```js 1352import socket from "@ohos.net.socket"; 1353 1354connection.getDefaultNet().then((netHandle) => { 1355 var tcp = socket.constructTCPSocketInstance(); 1356 var udp = socket.constructUDPSocketInstance(); 1357 let socketType = "TCPSocket"; 1358 if (socketType == "TCPSocket") { 1359 tcp.bind({ 1360 address: '192.168.xx.xxx', port: 8080, family: 1 1361 }, error => { 1362 if (error) { 1363 console.log('bind fail'); 1364 return; 1365 } 1366 netHandle.bindSocket(tcp, (error, data) => { 1367 if (error) { 1368 console.log(JSON.stringify(error)); 1369 } else { 1370 console.log(JSON.stringify(data)); 1371 } 1372 }) 1373 }) 1374 } else { 1375 let callback = value => { 1376 console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo); 1377 } 1378 udp.on('message', callback); 1379 udp.bind({ 1380 address: '192.168.xx.xxx', port: 8080, family: 1 1381 }, error => { 1382 if (error) { 1383 console.log('bind fail'); 1384 return; 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"; 1434 1435connection.getDefaultNet().then((netHandle) => { 1436 var tcp = socket.constructTCPSocketInstance(); 1437 var udp = socket.constructUDPSocketInstance(); 1438 let socketType = "TCPSocket"; 1439 if (socketType == "TCPSocket") { 1440 tcp.bind({ 1441 address: '192.168.xx.xxx', port: 8080, family: 1 1442 }, error => { 1443 if (error) { 1444 console.log('bind fail'); 1445 return; 1446 } 1447 netHandle.bindSocket(tcp).then((data) => { 1448 console.log(JSON.stringify(data)); 1449 }).catch(error => { 1450 console.log(JSON.stringify(error)); 1451 }) 1452 }) 1453 } else { 1454 let callback = value => { 1455 console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo); 1456 } 1457 udp.on('message', callback); 1458 udp.bind({ 1459 address: '192.168.xx.xxx', port: 8080, family: 1 1460 }, error => { 1461 if (error) { 1462 console.log('bind fail'); 1463 return; 1464 } 1465 udp.on('message', (data) => { 1466 console.log(JSON.stringify(data)); 1467 }) 1468 netHandle.bindSocket(udp).then((data) => { 1469 console.log(JSON.stringify(data)); 1470 }).catch(error => { 1471 console.log(JSON.stringify(error)); 1472 }) 1473 }) 1474 } 1475}) 1476``` 1477 1478### getAddressesByName 1479 1480getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>>): void 1481 1482Resolves the host name by using the corresponding network to obtain all IP addresses. This API uses an asynchronous callback to return the result. 1483 1484**Required permissions**: ohos.permission.INTERNET 1485 1486**System capability**: SystemCapability.Communication.NetManager.Core 1487 1488**Parameters** 1489 1490| Name | Type | Mandatory| Description | 1491| -------- | ------------------------------------------------- | ---- | ------------------ | 1492| host | string | Yes | Host name to resolve.| 1493| 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.| 1494 1495**Error codes** 1496 1497| ID| Error Message | 1498| ------- | ----------------------------- | 1499| 201 | Permission denied. | 1500| 401 | Parameter error. | 1501| 2100001 | Invalid parameter value. | 1502| 2100002 | Operation failed. Cannot connect to service.| 1503| 2100003 | System internal error. | 1504 1505**Example** 1506 1507```js 1508connection.getDefaultNet().then(function (netHandle) { 1509 let host = "xxxx"; 1510 netHandle.getAddressesByName(host, function (error, data) { 1511 console.log(JSON.stringify(error)) 1512 console.log(JSON.stringify(data)) 1513 }) 1514}) 1515``` 1516 1517### getAddressesByName 1518 1519getAddressesByName(host: string): Promise\<Array\<NetAddress>> 1520 1521Resolves the host name by using the corresponding network to obtain all IP addresses. This API uses a promise to return the result. 1522 1523**Required permissions**: ohos.permission.INTERNET 1524 1525**System capability**: SystemCapability.Communication.NetManager.Core 1526 1527**Parameters** 1528 1529| Name| Type | Mandatory| Description | 1530| ------ | ------ | ---- | ------------------ | 1531| host | string | Yes | Host name to resolve.| 1532 1533**Return value** 1534 1535| Type | Description | 1536| ------------------------------------------- | ----------------------------- | 1537| Promise\<Array\<[NetAddress](#netaddress)>> | Promise used to return the result.| 1538 1539**Error codes** 1540 1541| ID| Error Message | 1542| ------- | ----------------------------- | 1543| 201 | Permission denied. | 1544| 401 | Parameter error. | 1545| 2100001 | Invalid parameter value. | 1546| 2100002 | Operation failed. Cannot connect to service.| 1547| 2100003 | System internal error. | 1548 1549**Example** 1550 1551```js 1552connection.getDefaultNet().then(function (netHandle) { 1553 let host = "xxxx"; 1554 netHandle.getAddressesByName(host).then(function (data) { 1555 console.log(JSON.stringify(data)) 1556 }) 1557}) 1558``` 1559 1560### getAddressByName 1561 1562getAddressByName(host: string, callback: AsyncCallback\<NetAddress>): void 1563 1564Resolves the host name by using the corresponding network to obtain the first IP address. This API uses an asynchronous callback to return the result. 1565 1566**Required permissions**: ohos.permission.INTERNET 1567 1568**System capability**: SystemCapability.Communication.NetManager.Core 1569 1570**Parameters** 1571 1572| Name | Type | Mandatory| Description | 1573| -------- | ----------------------------------------- | ---- | ------------------ | 1574| host | string | Yes | Host name to resolve.| 1575| 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. | 1576 1577**Error codes** 1578 1579| ID| Error Message | 1580| ------- | ----------------------------- | 1581| 201 | Permission denied. | 1582| 401 | Parameter error. | 1583| 2100001 | Invalid parameter value. | 1584| 2100002 | Operation failed. Cannot connect to service.| 1585| 2100003 | System internal error. | 1586 1587**Example** 1588 1589```js 1590connection.getDefaultNet().then(function (netHandle) { 1591 let host = "xxxx"; 1592 netHandle.getAddressByName(host, function (error, data) { 1593 console.log(JSON.stringify(error)) 1594 console.log(JSON.stringify(data)) 1595 }) 1596}) 1597``` 1598 1599### getAddressByName 1600 1601getAddressByName(host: string): Promise\<NetAddress> 1602 1603Resolves the host name by using the corresponding network to obtain the first IP address. This API uses a promise to return the result. 1604 1605**Required permissions**: ohos.permission.INTERNET 1606 1607**System capability**: SystemCapability.Communication.NetManager.Core 1608 1609**Parameters** 1610 1611| Name| Type | Mandatory| Description | 1612| ------ | ------ | ---- | ------------------ | 1613| host | string | Yes | Host name to resolve.| 1614 1615**Return value** 1616 1617| Type | Description | 1618| ----------------------------------- | ------------------------------- | 1619| Promise\<[NetAddress](#netaddress)> | Promise used to return the result.| 1620 1621**Error codes** 1622 1623| ID| Error Message | 1624| ------- | ----------------------------- | 1625| 201 | Permission denied. | 1626| 401 | Parameter error. | 1627| 2100001 | Invalid parameter value. | 1628| 2100002 | Operation failed. Cannot connect to service.| 1629| 2100003 | System internal error. | 1630 1631**Example** 1632 1633```js 1634connection.getDefaultNet().then(function (netHandle) { 1635 let host = "xxxx"; 1636 netHandle.getAddressByName(host).then(function (data) { 1637 console.log(JSON.stringify(data)) 1638 }) 1639}) 1640``` 1641 1642## NetCap 1643 1644Defines the network capability. 1645 1646**System capability**: SystemCapability.Communication.NetManager.Core 1647 1648| Name | Value | Description | 1649| ------------------------ | ---- | ---------------------- | 1650| NET_CAPABILITY_MMS | 0 | The network can connect to the carrier's Multimedia Messaging Service Center (MMSC) to send and receive multimedia messages.| 1651| NET_CAPABILITY_NOT_METERED | 11 | The network traffic is not metered.| 1652| NET_CAPABILITY_INTERNET | 12 | The network has the Internet access capability, which is set by the network provider.| 1653| NET_CAPABILITY_NOT_VPN | 15 | The network does not use a virtual private network (VPN).| 1654| NET_CAPABILITY_VALIDATED | 16 | The Internet access capability of the network is successfully verified by the connection management module.| 1655 1656## NetBearType 1657 1658Enumerates network types. 1659 1660**System capability**: SystemCapability.Communication.NetManager.Core 1661 1662| Name | Value | Description | 1663| --------------- | ---- | ----------- | 1664| BEARER_CELLULAR | 0 | Cellular network. | 1665| BEARER_WIFI | 1 | Wi-Fi network.| 1666| BEARER_ETHERNET | 3 | Ethernet network.| 1667 1668## NetSpecifier 1669 1670Provides an instance that bears data network capabilities. 1671 1672**System capability**: SystemCapability.Communication.NetManager.Core 1673 1674| Name | Type | Mandatory | Description | 1675| ----------------------- | ----------------------------------- | ---- | ------------------------------------------------------------ | 1676| netCapabilities | [NetCapabilities](#netcapabilities) | Yes | Network transmission capabilities and bearer types of the data network. | 1677| 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).| 1678 1679## NetCapabilities 1680 1681Defines the network capability set. 1682 1683**System capability**: SystemCapability.Communication.NetManager.Core 1684 1685| Name | Type | Mandatory| Description | 1686| --------------------- | ---------------------------------- | --- | ------------------------ | 1687| linkUpBandwidthKbps | number | No| Uplink (from the device to the network) bandwidth. | 1688| linkDownBandwidthKbps | number | No| Downlink (from the network to the device) bandwidth. | 1689| networkCap | Array\<[NetCap](#netcap)> | No| Network capability. | 1690| bearerTypes | Array\<[NetBearType](#netbeartype)> | Yes| Network type. | 1691 1692## ConnectionProperties 1693 1694Defines the network connection properties. 1695 1696**System capability**: SystemCapability.Communication.NetManager.Core 1697 1698| Name | Type | Mandatory| Description | 1699| ------------- | ---------------------------------- | ----|---------------- | 1700| interfaceName | string | Yes|Network interface card (NIC) name. | 1701| domains | string | Yes|Domain. The default value is **""**.| 1702| linkAddresses | Array\<[LinkAddress](#linkaddress)> | Yes|Link information. | 1703| routes | Array\<[RouteInfo](#routeinfo)> | Yes|Route information. | 1704| dnses | Array\<[NetAddress](#netaddress)> | Yes|Network address. For details, see [NetAddress](#netaddress).| 1705| mtu | number | Yes|Maximum transmission unit (MTU). | 1706 1707## RouteInfo 1708 1709Defines network route information. 1710 1711**System capability**: SystemCapability.Communication.NetManager.Core 1712 1713| Name | Type | Mandatory|Description | 1714| -------------- | --------------------------- | --- |---------------- | 1715| interface | string | Yes|NIC name. | 1716| destination | [LinkAddress](#linkaddress) | Yes|Destination address. | 1717| gateway | [NetAddress](#netaddress) | Yes|Gateway address. | 1718| hasGateway | boolean | Yes|Whether a gateway is present. | 1719| isDefaultRoute | boolean | Yes|Whether the route is the default route.| 1720 1721## LinkAddress 1722 1723Defines network link information. 1724 1725**System capability**: SystemCapability.Communication.NetManager.Core 1726 1727| Name | Type | Mandatory|Description | 1728| ------------ | ----------------------- |---- |-------------------- | 1729| address | [NetAddress](#netaddress) | Yes| Link address. | 1730| prefixLength | number | Yes|Length of the link address prefix.| 1731 1732## NetAddress 1733 1734Defines a network address. 1735 1736**System capability**: SystemCapability.Communication.NetManager.Core 1737 1738| Name| Type| Mandatory| Description| 1739| ------- | ------ | -- |------------------------------ | 1740| address | string | Yes|Network address.| 1741| family | number | No|Address family identifier. The value is **1** for IPv4 and **2** for IPv6. The default value is **1**.| 1742| port | number | No|Port number. The value ranges from **0** to **65535**.| 1743