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```ts 11import connection from '@ohos.net.connection'; 12``` 13 14## connection.createNetConnection 15 16createNetConnection(netSpecifier?: NetSpecifier, timeout?: number): NetConnection 17 18Creates a **NetConnection** object, where [netSpecifier](#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```ts 38import connection from '@ohos.net.connection'; 39 40// For the default network, you do not need to pass in parameters. 41let netConnection = connection.createNetConnection(); 42 43// 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. 44let netConnectionCellular = connection.createNetConnection({ 45 netCapabilities: { 46 bearerTypes: [connection.NetBearType.BEARER_CELLULAR] 47 } 48}); 49``` 50 51## connection.getDefaultNet 52 53getDefaultNet(callback: AsyncCallback\<NetHandle>): void 54 55Obtains 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. 56 57**Required permission**: ohos.permission.GET_NETWORK_INFO 58 59**System capability**: SystemCapability.Communication.NetManager.Core 60 61**Parameters** 62 63| Name | Type | Mandatory| Description | 64| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 65| callback | AsyncCallback\<[NetHandle](#nethandle)> | Yes | Callback used to return the result. If the default activated data network is obtained successfully, **error** is **undefined** and **data** is the default activated data network. Otherwise, **error** is an error object.| 66 67**Error codes** 68 69| ID| Error Message | 70| ------- | ----------------------------- | 71| 201 | Permission denied. | 72| 401 | Parameter error. | 73| 2100002 | Operation failed. Cannot connect to service.| 74| 2100003 | System internal error. | 75 76**Example** 77 78```ts 79import connection from '@ohos.net.connection'; 80import { BusinessError } from '@ohos.base'; 81 82connection.getDefaultNet((error: BusinessError, data: connection.NetHandle) => { 83 console.log(JSON.stringify(error)); 84 console.log(JSON.stringify(data)); 85}); 86``` 87 88## connection.getDefaultNet 89 90getDefaultNet(): Promise\<NetHandle> 91 92Obtains 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. 93 94**Required permission**: ohos.permission.GET_NETWORK_INFO 95 96**System capability**: SystemCapability.Communication.NetManager.Core 97 98**Return value** 99 100| Type | Description | 101| --------------------------------- | ------------------------------------- | 102| Promise\<[NetHandle](#nethandle)> | Promise used to return the result.| 103 104**Error codes** 105 106| ID| Error Message | 107| ------- | ----------------------------- | 108| 201 | Permission denied. | 109| 401 | Parameter error. | 110| 2100002 | Operation failed. Cannot connect to service.| 111| 2100003 | System internal error. | 112 113**Example** 114 115```ts 116import connection from '@ohos.net.connection'; 117connection.getDefaultNet().then((data: connection.NetHandle) => { 118 console.log(JSON.stringify(data)); 119}); 120``` 121 122## connection.getDefaultNetSync<sup>9+</sup> 123 124getDefaultNetSync(): NetHandle 125 126Obtains the default active data network in synchronous mode. You can use [getNetCapabilities](#connectiongetnetcapabilities) to obtain information such as the network type and capabilities. 127 128**Required permission**: ohos.permission.GET_NETWORK_INFO 129 130**System capability**: SystemCapability.Communication.NetManager.Core 131 132**Return value** 133 134| Type | Description | 135| --------- | ---------------------------------- | 136| [NetHandle](#nethandle) | Handle of the default active data network.| 137 138**Error codes** 139 140| ID| Error Message | 141| ------- | ----------------------------- | 142| 201 | Permission denied. | 143| 401 | Parameter error. | 144| 2100002 | Operation failed. Cannot connect to service.| 145| 2100003 | System internal error. | 146 147**Example** 148 149```ts 150import connection from '@ohos.net.connection'; 151 152let netHandle = connection.getDefaultNetSync(); 153``` 154 155## connection.getGlobalHttpProxy<sup>10+</sup> 156 157getGlobalHttpProxy(callback: AsyncCallback\<HttpProxy>): void 158 159Obtains the global HTTP proxy configuration of the network. This API uses an asynchronous callback to return the result. 160 161**System API**: This is a system API. 162 163**System capability**: SystemCapability.Communication.NetManager.Core 164 165**Parameters** 166 167| Name | Type | Mandatory| Description | 168| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 169| callback | AsyncCallback\<[HttpProxy](#httpproxy10)> | Yes | Callback used to return the result. If the global HTTP proxy configuration of the network is obtained successfully, **error** is **undefined** and **data** is the global HTTP proxy configuration. Otherwise, **error** is an error object.| 170 171**Error codes** 172 173| ID| Error Message | 174| ------- | ----------------------------- | 175| 401 | Parameter error. | 176| 202 | Non-system applications use system APIs. | 177| 2100002 | Operation failed. Cannot connect to service.| 178| 2100003 | System internal error. | 179 180**Example** 181 182```ts 183import connection from '@ohos.net.connection'; 184import { BusinessError } from '@ohos.base'; 185 186connection.getGlobalHttpProxy((error: BusinessError, data: connection.HttpProxy) => { 187 console.info(JSON.stringify(error)); 188 console.info(JSON.stringify(data)); 189}); 190``` 191 192## connection.getGlobalHttpProxy<sup>10+</sup> 193 194getGlobalHttpProxy(): Promise\<HttpProxy>; 195 196Obtains the global HTTP proxy configuration of the network. This API uses a promise to return the result. 197 198**System API**: This is a system API. 199 200**System capability**: SystemCapability.Communication.NetManager.Core 201 202**Return value** 203 204| Type | Description | 205| --------------------------------- | ------------------------------------- | 206| Promise\<[HttpProxy](#httpproxy10)> | Promise used to return the result.| 207 208**Error codes** 209 210| ID| Error Message | 211| ------- | ----------------------------- | 212| 401 | Parameter error. | 213| 202 | Non-system applications use system APIs. | 214| 2100002 | Operation failed. Cannot connect to service.| 215| 2100003 | System internal error. | 216 217**Example** 218 219```ts 220import connection from '@ohos.net.connection'; 221import { BusinessError } from '@ohos.base'; 222 223connection.getGlobalHttpProxy().then((data: connection.HttpProxy) => { 224 console.info(JSON.stringify(data)); 225}).catch((error: BusinessError) => { 226 console.info(JSON.stringify(error)); 227}); 228``` 229 230## connection.setGlobalHttpProxy<sup>10+</sup> 231 232setGlobalHttpProxy(httpProxy: HttpProxy, callback: AsyncCallback\<void>): void 233 234Sets the global HTTP proxy configuration of the network. This API uses an asynchronous callback to return the result. 235 236**System API**: This is a system API. 237 238**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 239 240**System capability**: SystemCapability.Communication.NetManager.Core 241 242**Parameters** 243 244| Name | Type | Mandatory| Description | 245| --------- | ----------------------- | ---- | ------------------------------------------------------------ | 246| httpProxy | [HttpProxy](#httpproxy10) | Yes | Global HTTP proxy configuration of the network. | 247| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the global HTTP proxy configuration of the network is set successfully, **error** is **undefined**. Otherwise, **error** is an error object.| 248 249**Error codes** 250 251| ID| Error Message | 252| ------- | ----------------------------- | 253| 201 | Permission denied. | 254| 401 | Parameter error. | 255| 202 | Non-system applications use system APIs. | 256| 2100001 | Invalid parameter value. | 257| 2100002 | Operation failed. Cannot connect to service.| 258| 2100003 | System internal error. | 259 260**Example** 261 262```ts 263import connection from '@ohos.net.connection'; 264import { BusinessError } from '@ohos.base'; 265 266let exclusionStr = "192.168,baidu.com"; 267let exclusionArray = exclusionStr.split(','); 268connection.setGlobalHttpProxy({ 269 host: "192.168.xx.xxx", 270 port: 8080, 271 exclusionList: exclusionArray 272} as connection.HttpProxy).then(() => { 273 console.info("success"); 274}).catch((error: BusinessError) => { 275 console.info(JSON.stringify(error)); 276}); 277``` 278 279## connection.setGlobalHttpProxy<sup>10+</sup> 280 281setGlobalHttpProxy(httpProxy: HttpProxy): Promise\<void>; 282 283Sets the global HTTP proxy configuration of the network. This API uses a promise to return the result. 284 285**System API**: This is a system API. 286 287**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 288 289**System capability**: SystemCapability.Communication.NetManager.Core 290 291**Parameters** 292 293| Name | Type | Mandatory| Description | 294| --------- | ------------------------------------------------------------ | ---- | ---------------- | 295| httpProxy | [HttpProxy](#httpproxy10) | Yes | Global HTTP proxy configuration of the network.| 296 297**Return value** 298 299| Type | Description | 300| ------------------------------------------- | ----------------------------- | 301| Promise\<void> | Promise that returns no value.| 302 303**Error codes** 304 305| ID| Error Message | 306| ------- | ----------------------------- | 307| 201 | Permission denied. | 308| 401 | Parameter error. | 309| 202 | Non-system applications use system APIs. | 310| 2100001 | Invalid parameter value. | 311| 2100002 | Operation failed. Cannot connect to service.| 312| 2100003 | System internal error. | 313 314**Example** 315 316```ts 317import connection from '@ohos.net.connection'; 318import { BusinessError } from '@ohos.base'; 319 320let exclusionStr = "192.168,baidu.com"; 321let exclusionArray = exclusionStr.split(','); 322connection.setGlobalHttpProxy({ 323 host: "192.168.xx.xxx", 324 port: 8080, 325 exclusionList: exclusionArray 326} as connection.HttpProxy).then(() => { 327 console.info("success"); 328}).catch((error: BusinessError) => { 329 console.info(JSON.stringify(error)); 330}); 331``` 332 333## connection.setAppHttpProxy<sup>11+</sup> 334 335setAppHttpProxy(httpProxy: HttpProxy): void; 336 337Sets the application-level HTTP proxy configuration of the network. 338 339**System capability**: SystemCapability.Communication.NetManager.Core 340 341**Parameters** 342 343| Name | Type | Mandatory| Description | 344| --------- | ------------------------------------------------------------ | ---- | ---------------- | 345| httpProxy | [HttpProxy](#httpproxy10) | Yes | Application-level HTTP proxy configuration.| 346 347**Error codes** 348 349| ID| Error Message | 350| ------- | ----------------------------- | 351| 401 | Parameter error. | 352| 2100001 | Invalid http proxy. | 353 354**Example** 355 356```ts 357import connection from '@ohos.net.connection'; 358import { BusinessError } from '@ohos.base'; 359 360let exclusionStr = "192.168,baidu.com"; 361let exclusionArray = exclusionStr.split(','); 362connection.setAppHttpProxy({ 363 host: "192.168.xx.xxx", 364 port: 8080, 365 exclusionList: exclusionArray 366} as connection.HttpProxy); 367``` 368 369**Preset certificate PIN:** 370 371A certificate PIN is the hash value calculated using the SHA256 algorithm for a certificate file. 372For the **server.pem** certificate, you can use the following openssl command to calculate its PIN: 373 374```shell 375cat server.pem \ 376| sed -n '/-----BEGIN/,/-----END/p' \ 377| openssl x509 -noout -pubkey \ 378| openssl pkey -pubin -outform der \ 379| openssl dgst -sha256 -binary \ 380| openssl enc -base64 381``` 382 383**Preset application-level certificate:** 384 385The original certificate file is preset in the application. Currently, certificate files in the **.crt** and **.pem** formats are supported. 386 387**Preset JSON configuration file:** 388 389The mapping between preset certificates and network servers is configured in a JSON configuration file. 390The configuration file is stored in the **src/main/resources/base/profile/network_config.json** directory of the application. 391 392**JSON configuration file:** 393 394The following is an example configuration of the certificate pin: 395```json 396{ 397 "network-security-config": { 398 "domain-config": { 399 "domains": [ 400 { 401 "include-subdomains": true, 402 "name": "server.com" 403 } 404 ], 405 "pin-set": { 406 "expiration": "2024-11-08", 407 "pin": [ 408 { 409 "digest-algorithm": "sha256", 410 "digest": "FEDCBA987654321" 411 } 412 ] 413 } 414 } 415 } 416} 417``` 418 419The following is an example configuration of the application-level certificate: 420```json 421{ 422 "network-security-config": { 423 "base-config": { 424 "trust-anchors": [ 425 {"certificates": "/etc/security/certificates"} 426 ] 427 }, 428 "domain-config": { 429 "domains": [ 430 { 431 "include-subdomains": true, 432 "name": "example.com" 433 } 434 ], 435 "trust-anchors": [ 436 {"certificates": "/data/storage/el1/bundle/entry/resources/resfile"} 437 ] 438 } 439 } 440} 441 442``` 443 444**Description of fields** 445 446**network-security-config (object: network security configuration)** 447 448This field can contain zero or one **base-config**. 449 450This field must contain one **domain-config**. 451 452**base-config (object: application-wide security configuration)** 453 454This field must contain one **trust-anchors**. 455 456**domain-config (array: security configuration of each domain)** 457 458This field can contain any number of items. 459 460An item must contain one **domain**. 461 462An item can contain zero or one **trust-anchors**. 463 464An item can contain zero or one **pin-set**. 465 466**trust-anchors (array: trusted CA)** 467 468This field can contain any number of items. 469 470An item must contain one **certificates** (string: CA certificate path). 471 472**domain (array: domain)** 473 474This field can contain any number of items. 475 476An item must contain one **name** (string: domain name). 477 478An item can contain zero or one **include-subdomains** (boolean: whether a rule is applicable to subdomains). 479 480**pin-set (object: certificate PIN setting)** 481 482This field must contain one **pin**. 483 484This field can contain zero or one **expiration** (string: expiration time of the certificate PIN). 485 486**pin (array: certificate PIN)** 487 488This field can contain any number of items. 489 490An item must contain one **digest-algorithm** (string: digest algorithm used to generate the PIN). 491 492An item must contain one **digest** (string: public key PIN). 493 494## connection.getDefaultHttpProxy<sup>10+</sup> 495 496getDefaultHttpProxy(callback: AsyncCallback\<HttpProxy>): void 497 498Obtains the default HTTP proxy configuration of the network. 499If the global proxy is set, the global HTTP proxy configuration is returned. If [setAppNet](#connectionsetappnet9) is used to bind the application to the network specified by [NetHandle](#nethandle), the HTTP proxy configuration of this network is returned. In other cases, the HTTP proxy configuration of the default network is returned. 500This API uses an asynchronous callback to return the result. 501 502**System capability**: SystemCapability.Communication.NetManager.Core 503 504**Parameters** 505 506| Name | Type | Mandatory| Description | 507| -------- | -------------------------------------- | ---- | ------------------------------------------------------------ | 508| callback | AsyncCallback<[HttpProxy](#httpproxy10)> | Yes | Callback used to return the result. If the global HTTP proxy configuration of the network is obtained successfully, **error** is **undefined** and **data** is the global HTTP proxy configuration. Otherwise, **error** is an error object.| 509 510**Error codes** 511 512| ID| Error Message | 513| -------- | -------------------------------------------- | 514| 2100002 | Operation failed. Cannot connect to service. | 515| 2100003 | System internal error. | 516 517**Example** 518 519```ts 520import connection from '@ohos.net.connection'; 521import { BusinessError } from '@ohos.base'; 522 523connection.getDefaultHttpProxy((error: BusinessError, data: connection.HttpProxy) => { 524 console.info(JSON.stringify(error)); 525 console.info(JSON.stringify(data)); 526}); 527``` 528 529## connection.getDefaultHttpProxy<sup>10+</sup> 530 531getDefaultHttpProxy(): Promise\<HttpProxy>; 532 533Obtains the default HTTP proxy configuration of the network. 534If the global proxy is set, the global HTTP proxy configuration is returned. If [setAppNet](#connectionsetappnet9) is used to bind the application to the network specified by [NetHandle](#nethandle), the HTTP proxy configuration of this network is returned. In other cases, the HTTP proxy configuration of the default network is returned. 535This API uses a promise to return the result. 536 537**System capability**: SystemCapability.Communication.NetManager.Core 538 539**Return value** 540 541| Type | Description | 542| -------------------------------- | ----------------------------------------- | 543| Promise<[HttpProxy](#httpproxy10)> | Promise used to return the result.| 544 545**Error codes** 546 547| ID| Error Message | 548| -------- | -------------------------------------------- | 549| 2100002 | Operation failed. Cannot connect to service. | 550| 2100003 | System internal error. | 551 552**Example** 553 554```ts 555import connection from '@ohos.net.connection'; 556import { BusinessError } from '@ohos.base'; 557 558connection.getDefaultHttpProxy().then((data: connection.HttpProxy) => { 559 console.info(JSON.stringify(data)); 560}).catch((error: BusinessError) => { 561 console.info(JSON.stringify(error)); 562}); 563``` 564 565## connection.getAppNet<sup>9+</sup> 566 567getAppNet(callback: AsyncCallback\<NetHandle>): void 568 569Obtains information about the network bound to an application. This API uses an asynchronous callback to return the result. 570 571**System capability**: SystemCapability.Communication.NetManager.Core 572 573**Parameters** 574 575| Name | Type | Mandatory| Description | 576| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 577| callback | AsyncCallback\<[NetHandle](#nethandle)> | Yes | Callback used to return the result. If information about the network bound to the application is successfully obtained, **error** is **undefined** and **data** is the obtained network information. Otherwise, **error** is an error object.| 578 579**Error codes** 580 581| ID| Error Message | 582| ------- | ----------------------------- | 583| 401 | Parameter error.| 584| 2100002 | Operation failed. Cannot connect to service.| 585| 2100003 | System internal error. | 586 587**Example** 588 589```ts 590import connection from '@ohos.net.connection'; 591import { BusinessError } from '@ohos.base'; 592 593connection.getAppNet((error: BusinessError, data: connection.NetHandle) => { 594 console.log(JSON.stringify(error)); 595 console.log(JSON.stringify(data)); 596}) 597``` 598 599## connection.getAppNet<sup>9+</sup> 600 601getAppNet(): Promise\<NetHandle>; 602 603Obtains information about the network bound to an application. This API uses a promise to return the result. 604 605**System capability**: SystemCapability.Communication.NetManager.Core 606 607**Return value** 608 609| Type | Description | 610| --------------------------------- | ------------------------------------- | 611| Promise\<[NetHandle](#nethandle)> | Promise used to return the result.| 612 613**Error codes** 614 615| ID| Error Message | 616| ------- | ----------------------------- | 617| 401 | Parameter error.| 618| 2100002 | Operation failed. Cannot connect to service.| 619| 2100003 | System internal error. | 620 621**Example** 622 623```ts 624import connection from '@ohos.net.connection'; 625import { BusinessError } from '@ohos.base'; 626 627connection.getAppNet().then((data: connection.NetHandle) => { 628 console.info(JSON.stringify(data)); 629}).catch((error: BusinessError) => { 630 console.info(JSON.stringify(error)); 631}); 632``` 633 634## connection.getAppNetSync<sup>10+</sup> 635 636getAppNetSync(): NetHandle 637 638Obtains information about the network bound to an application. This API returns the result synchronously. 639 640**System capability**: SystemCapability.Communication.NetManager.Core 641 642**Return value** 643 644| Type | Description | 645| --------- | ---------------------------------- | 646| [NetHandle](#nethandle) | Handle of the data network bound to the application.| 647 648**Error codes** 649 650| ID| Error Message | 651| ------- | ----------------------------- | 652| 401 | Parameter error. | 653| 2100002 | Operation failed. Cannot connect to service.| 654| 2100003 | System internal error. | 655 656**Example** 657 658```ts 659import connection from '@ohos.net.connection'; 660 661let netHandle = connection.getAppNetSync(); 662``` 663 664## connection.setAppNet<sup>9+</sup> 665 666setAppNet(netHandle: NetHandle, callback: AsyncCallback\<void>): void 667 668Binds 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. 669 670**Required permissions**: ohos.permission.INTERNET 671 672**System capability**: SystemCapability.Communication.NetManager.Core 673 674**Parameters** 675 676| Name | Type | Mandatory| Description | 677| --------- | ----------------------- | ---- | ------------------------------------------------------------ | 678| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network. | 679| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the application is successfully bound to the specified network, **error** is **undefined**. Otherwise, **error** is an error object.| 680 681**Error codes** 682 683| ID| Error Message | 684| ------- | ----------------------------- | 685| 201 | Permission denied. | 686| 401 | Parameter error. | 687| 2100001 | Invalid parameter value. | 688| 2100002 | Operation failed. Cannot connect to service.| 689| 2100003 | System internal error. | 690 691**Example** 692 693```ts 694import connection from '@ohos.net.connection'; 695import { BusinessError } from '@ohos.base'; 696 697connection.getDefaultNet((error: BusinessError, netHandle: connection.NetHandle) => { 698 connection.setAppNet(netHandle, (error: BusinessError, data: void) => { 699 console.log(JSON.stringify(error)); 700 console.log(JSON.stringify(data)); 701 }); 702}); 703``` 704 705## connection.setAppNet<sup>9+</sup> 706 707setAppNet(netHandle: NetHandle): Promise\<void>; 708 709Binds 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. 710 711**Required permissions**: ohos.permission.INTERNET 712 713**System capability**: SystemCapability.Communication.NetManager.Core 714 715**Parameters** 716 717| Name | Type | Mandatory| Description | 718| --------- | ------------------------------------------------------------ | ---- | ---------------- | 719| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network.| 720 721**Return value** 722 723| Type | Description | 724| ------------------------------------------- | ----------------------------- | 725| Promise\<void> | Promise that returns no value.| 726 727**Error codes** 728 729| ID| Error Message | 730| ------- | ----------------------------- | 731| 201 | Permission denied. | 732| 401 | Parameter error. | 733| 2100001 | Invalid parameter value. | 734| 2100002 | Operation failed. Cannot connect to service.| 735| 2100003 | System internal error. | 736 737**Example** 738 739```ts 740import connection from '@ohos.net.connection'; 741import { BusinessError } from '@ohos.base'; 742 743connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 744 connection.setAppNet(netHandle).then(() => { 745 console.log("success"); 746 }).catch((error: BusinessError) => { 747 console.log(JSON.stringify(error)); 748 }) 749}); 750``` 751 752## connection.getAllNets 753 754getAllNets(callback: AsyncCallback<Array<NetHandle>>): void 755 756Obtains the list of all connected networks. This API uses an asynchronous callback to return the result. 757 758**Required permission**: ohos.permission.GET_NETWORK_INFO 759 760**System capability**: SystemCapability.Communication.NetManager.Core 761 762**Parameters** 763 764| Name| Type| Mandatory| Description| 765| -------- | -------- | -------- | -------- | 766| callback | AsyncCallback<Array<[NetHandle](#nethandle)>> | Yes| Callback used to return the result. If the list of all connected networks is obtained successfully, **error** is **undefined** and **data** is the list of activated data networks. Otherwise, **error** is an error object.| 767 768**Error codes** 769 770| ID| Error Message | 771| ------- | ----------------------------- | 772| 201 | Permission denied. | 773| 401 | Parameter error. | 774| 2100002 | Operation failed. Cannot connect to service.| 775| 2100003 | System internal error. | 776 777**Example** 778 779```ts 780import connection from '@ohos.net.connection'; 781import { BusinessError } from '@ohos.base'; 782 783connection.getAllNets((error: BusinessError, data: connection.NetHandle[]) => { 784 console.log(JSON.stringify(error)); 785 console.log(JSON.stringify(data)); 786}); 787``` 788 789## connection.getAllNets 790 791getAllNets(): Promise<Array<NetHandle>> 792 793Obtains the list of all connected networks. This API uses a promise to return the result. 794 795**Required permission**: ohos.permission.GET_NETWORK_INFO 796 797**System capability**: SystemCapability.Communication.NetManager.Core 798 799**Return value** 800 801| Type| Description| 802| -------- | -------- | 803| Promise<Array<[NetHandle](#nethandle)>> | Promise used to return the result.| 804 805**Error codes** 806 807| ID| Error Message | 808| ------- | ----------------------------- | 809| 201 | Permission denied. | 810| 401 | Parameter error. | 811| 2100002 | Operation failed. Cannot connect to service.| 812| 2100003 | System internal error. | 813 814**Example** 815 816```ts 817import connection from '@ohos.net.connection'; 818 819connection.getAllNets().then((data: connection.NetHandle[]) => { 820 console.log(JSON.stringify(data)); 821}); 822``` 823 824## connection.getAllNetsSync<sup>10+</sup> 825 826getAllNetsSync(): Array<NetHandle> 827 828Obtains the list of all connected networks. This API returns the result synchronously. 829 830**Required permission**: ohos.permission.GET_NETWORK_INFO 831 832**System capability**: SystemCapability.Communication.NetManager.Core 833 834**Return value** 835 836| Type | Description | 837| --------- | ---------------------------------- | 838| Array<[NetHandle](#nethandle)> | List of all activated data networks.| 839 840**Error codes** 841 842| ID| Error Message | 843| ------- | ----------------------------- | 844| 201 | Permission denied. | 845| 401 | Parameter error. | 846| 2100002 | Operation failed. Cannot connect to service.| 847| 2100003 | System internal error. | 848 849**Example** 850 851```ts 852import connection from '@ohos.net.connection'; 853 854let netHandle = connection.getAllNetsSync(); 855``` 856 857## connection.getConnectionProperties 858 859getConnectionProperties(netHandle: NetHandle, callback: AsyncCallback\<ConnectionProperties>): void 860 861Obtains connection properties of the network corresponding to the **netHandle**. This API uses an asynchronous callback to return the result. 862 863**Required permission**: ohos.permission.GET_NETWORK_INFO 864 865**System capability**: SystemCapability.Communication.NetManager.Core 866 867**Parameters** 868 869| Name | Type | Mandatory| Description | 870| --------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 871| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network. | 872| 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, **error** is **undefined** and **data** is the obtained network connection information. Otherwise, **error** is an error object.| 873 874**Error codes** 875 876| ID| Error Message | 877| ------- | ----------------------------- | 878| 201 | Permission denied. | 879| 401 | Parameter error. | 880| 2100001 | Invalid parameter value. | 881| 2100002 | Operation failed. Cannot connect to service.| 882| 2100003 | System internal error. | 883 884**Example** 885 886```ts 887import connection from '@ohos.net.connection'; 888import { BusinessError } from '@ohos.base'; 889 890connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 891 connection.getConnectionProperties(netHandle, (error: BusinessError, data: connection.ConnectionProperties) => { 892 console.log(JSON.stringify(error)); 893 console.log(JSON.stringify(data)); 894 }) 895}); 896``` 897 898## connection.getConnectionProperties 899 900getConnectionProperties(netHandle: NetHandle): Promise\<ConnectionProperties> 901 902Obtains connection properties of the network corresponding to the **netHandle**. This API uses a promise to return the result. 903 904**Required permission**: ohos.permission.GET_NETWORK_INFO 905 906**System capability**: SystemCapability.Communication.NetManager.Core 907 908**Parameters** 909 910| Name | Type | Mandatory| Description | 911| --------- | ----------------------- | ---- | ---------------- | 912| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network.| 913 914**Return value** 915 916| Type | Description | 917| ------------------------------------------------------- | --------------------------------- | 918| Promise\<[ConnectionProperties](#connectionproperties)> | Promise used to return the result.| 919 920**Error codes** 921 922| ID| Error Message | 923| ------- | ----------------------------- | 924| 201 | Permission denied. | 925| 401 | Parameter error. | 926| 2100001 | Invalid parameter value. | 927| 2100002 | Operation failed. Cannot connect to service.| 928| 2100003 | System internal error. | 929 930**Example** 931 932```ts 933import connection from '@ohos.net.connection'; 934 935connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 936 connection.getConnectionProperties(netHandle).then((data: connection.ConnectionProperties) => { 937 console.log(JSON.stringify(data)); 938 }) 939}); 940``` 941 942## connection.getConnectionPropertiesSync<sup>10+</sup> 943 944getConnectionPropertiesSync(netHandle: NetHandle): ConnectionProperties 945 946Obtains network connection information based on the specified **netHandle**. 947 948**Required permission**: ohos.permission.GET_NETWORK_INFO 949 950**System capability**: SystemCapability.Communication.NetManager.Core 951 952**Parameters** 953 954| Name | Type | Mandatory| Description | 955| --------- | ----------------------- | ---- | ---------------- | 956| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network.| 957 958**Return value** 959 960| Type | Description | 961| ------------------------------------------------------- | --------------------------------- | 962| [ConnectionProperties](#connectionproperties) | Network connection information.| 963 964**Error codes** 965 966| ID| Error Message | 967| ------- | ----------------------------- | 968| 201 | Permission denied. | 969| 401 | Parameter error. | 970| 2100001 | Invalid parameter value. | 971| 2100002 | Operation failed. Cannot connect to service.| 972| 2100003 | System internal error. | 973 974**Example** 975 976```ts 977import connection from '@ohos.net.connection'; 978 979let netHandle = connection.getDefaultNetSync(); 980let connectionproperties = connection.getConnectionPropertiesSync(netHandle); 981``` 982 983## connection.getNetCapabilities 984 985getNetCapabilities(netHandle: NetHandle, callback: AsyncCallback\<NetCapabilities>): void 986 987Obtains capability information of the network corresponding to the **netHandle**. This API uses an asynchronous callback to return the result. 988 989**Required permission**: ohos.permission.GET_NETWORK_INFO 990 991**System capability**: SystemCapability.Communication.NetManager.Core 992 993**Parameters** 994 995| Name | Type | Mandatory| Description | 996| --------- | --------------------------------------------------- | ---- | ------------------------------------------------------------ | 997| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network. | 998| 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, **error** is **undefined** and **data** is the obtained network capability information. Otherwise, **error** is an error object.| 999 1000**Error codes** 1001 1002| ID| Error Message | 1003| ------- | ----------------------------- | 1004| 201 | Permission denied. | 1005| 401 | Parameter error. | 1006| 2100001 | Invalid parameter value. | 1007| 2100002 | Operation failed. Cannot connect to service.| 1008| 2100003 | System internal error. | 1009 1010**Example** 1011 1012```ts 1013import connection from '@ohos.net.connection'; 1014import { BusinessError } from '@ohos.base'; 1015 1016connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 1017 connection.getNetCapabilities(netHandle, (error: BusinessError, data: connection.NetCapabilities) => { 1018 console.log(JSON.stringify(error)); 1019 console.log(JSON.stringify(data)); 1020 }) 1021}); 1022``` 1023 1024## connection.getNetCapabilities 1025 1026getNetCapabilities(netHandle: NetHandle): Promise\<NetCapabilities> 1027 1028Obtains capability information of the network corresponding to the **netHandle**. This API uses a promise to return the result. 1029 1030**Required permission**: ohos.permission.GET_NETWORK_INFO 1031 1032**System capability**: SystemCapability.Communication.NetManager.Core 1033 1034**Parameters** 1035 1036| Name | Type | Mandatory| Description | 1037| --------- | ----------------------- | ---- | ---------------- | 1038| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network.| 1039 1040**Return value** 1041 1042| Type | Description | 1043| --------------------------------------------- | --------------------------------- | 1044| Promise\<[NetCapabilities](#netcapabilities)> | Promise used to return the result.| 1045 1046**Error codes** 1047 1048| ID| Error Message | 1049| ------- | ----------------------------- | 1050| 201 | Permission denied. | 1051| 401 | Parameter error. | 1052| 2100001 | Invalid parameter value. | 1053| 2100002 | Operation failed. Cannot connect to service.| 1054| 2100003 | System internal error. | 1055 1056**Example** 1057 1058```ts 1059import connection from '@ohos.net.connection'; 1060 1061connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 1062 connection.getNetCapabilities(netHandle).then((data: connection.NetCapabilities) => { 1063 console.log(JSON.stringify(data)); 1064 }) 1065}); 1066``` 1067 1068## connection.getNetCapabilitiesSync<sup>10+</sup> 1069 1070getNetCapabilitiesSync(netHandle: NetHandle): NetCapabilities 1071 1072Obtains capability information of the network corresponding to the **netHandle**. This API returns the result synchronously. 1073 1074**Required permission**: ohos.permission.GET_NETWORK_INFO 1075 1076**System capability**: SystemCapability.Communication.NetManager.Core 1077 1078**Parameters** 1079 1080| Name | Type | Mandatory| Description | 1081| --------- | ----------------------- | ---- | ---------------- | 1082| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network.| 1083 1084**Return value** 1085 1086| Type | Description | 1087| --------------------------------------------- | --------------------------------- | 1088| [NetCapabilities](#netcapabilities) | Network capability information.| 1089 1090**Error codes** 1091 1092| ID| Error Message | 1093| ------- | ----------------------------- | 1094| 201 | Permission denied. | 1095| 401 | Parameter error. | 1096| 2100001 | Invalid parameter value. | 1097| 2100002 | Operation failed. Cannot connect to service.| 1098| 2100003 | System internal error. | 1099 1100**Example** 1101 1102```ts 1103import connection from '@ohos.net.connection'; 1104 1105let netHandle = connection.getDefaultNetSync(); 1106let getNetCapabilitiesSync = connection.getNetCapabilitiesSync(netHandle); 1107``` 1108 1109## connection.isDefaultNetMetered<sup>9+</sup> 1110 1111isDefaultNetMetered(callback: AsyncCallback\<boolean>): void 1112 1113Checks whether the data traffic usage on the current network is metered. This API uses an asynchronous callback to return the result. 1114 1115**Required permission**: ohos.permission.GET_NETWORK_INFO 1116 1117**System capability**: SystemCapability.Communication.NetManager.Core 1118 1119**Parameters** 1120 1121| Name | Type | Mandatory| Description | 1122| -------- | ----------------------- | ---- | -------------------------------------- | 1123| callback | AsyncCallback\<boolean> | Yes | Callback used to return the result. The value **true** indicates the data traffic usage is metered.| 1124 1125**Error codes** 1126 1127| ID| Error Message | 1128| ------- | ----------------------------- | 1129| 201 | Permission denied. | 1130| 401 | Parameter error. | 1131| 2100002 | Operation failed. Cannot connect to service.| 1132| 2100003 | System internal error. | 1133 1134**Example** 1135 1136```ts 1137import connection from '@ohos.net.connection'; 1138import { BusinessError } from '@ohos.base'; 1139 1140connection.isDefaultNetMetered((error: BusinessError, data: boolean) => { 1141 console.log(JSON.stringify(error)); 1142 console.log('data: ' + data); 1143}); 1144``` 1145 1146## connection.isDefaultNetMetered<sup>9+</sup> 1147 1148isDefaultNetMetered(): Promise\<boolean> 1149 1150Checks whether the data traffic usage on the current network is metered. This API uses a promise to return the result. 1151 1152**Required permission**: ohos.permission.GET_NETWORK_INFO 1153 1154**System capability**: SystemCapability.Communication.NetManager.Core 1155 1156**Return value** 1157 1158| Type | Description | 1159| ----------------- | ----------------------------------------------- | 1160| Promise\<boolean> | Promise used to return the result. The value **true** indicates the data traffic usage is metered.| 1161 1162**Error codes** 1163 1164| ID| Error Message | 1165| ------- | ----------------------------- | 1166| 201 | Permission denied. | 1167| 401 | Parameter error. | 1168| 2100002 | Operation failed. Cannot connect to service.| 1169| 2100003 | System internal error. | 1170 1171**Example** 1172 1173```ts 1174import connection from '@ohos.net.connection'; 1175 1176connection.isDefaultNetMetered().then((data: boolean) => { 1177 console.log('data: ' + data); 1178}); 1179``` 1180 1181## connection.isDefaultNetMeteredSync<sup>10+</sup> 1182 1183isDefaultNetMeteredSync(): boolean 1184 1185Checks whether the data traffic usage on the current network is metered. This API returns the result synchronously. 1186 1187**Required permission**: ohos.permission.GET_NETWORK_INFO 1188 1189**System capability**: SystemCapability.Communication.NetManager.Core 1190 1191**Return value** 1192 1193| Type | Description | 1194| ----------------- | ----------------------------------------------- | 1195| boolean | The value **true** indicates the data traffic usage is metered.| 1196 1197**Error codes** 1198 1199| ID| Error Message | 1200| ------- | ----------------------------- | 1201| 201 | Permission denied. | 1202| 401 | Parameter error. | 1203| 2100002 | Operation failed. Cannot connect to service.| 1204| 2100003 | System internal error. | 1205 1206**Example** 1207 1208```ts 1209import connection from '@ohos.net.connection'; 1210 1211let isMetered = connection.isDefaultNetMeteredSync(); 1212``` 1213 1214## connection.hasDefaultNet 1215 1216hasDefaultNet(callback: AsyncCallback\<boolean>): void 1217 1218Checks 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. 1219 1220**Required permission**: ohos.permission.GET_NETWORK_INFO 1221 1222**System capability**: SystemCapability.Communication.NetManager.Core 1223 1224**Parameters** 1225 1226| Name | Type | Mandatory| Description | 1227| -------- | ----------------------- | ---- | -------------------------------------- | 1228| callback | AsyncCallback\<boolean> | Yes | Callback used to return the result. The value **true** indicates the default data network is activated.| 1229 1230**Error codes** 1231 1232| ID| Error Message | 1233| ------- | ----------------------------- | 1234| 201 | Permission denied. | 1235| 401 | Parameter error. | 1236| 2100002 | Operation failed. Cannot connect to service.| 1237| 2100003 | System internal error. | 1238 1239**Example** 1240 1241```ts 1242import connection from '@ohos.net.connection'; 1243import { BusinessError } from '@ohos.base'; 1244 1245connection.hasDefaultNet((error: BusinessError, data: boolean) => { 1246 console.log(JSON.stringify(error)); 1247 console.log('data: ' + data); 1248}); 1249``` 1250 1251## connection.hasDefaultNet 1252 1253hasDefaultNet(): Promise\<boolean> 1254 1255Checks 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. 1256 1257**Required permission**: ohos.permission.GET_NETWORK_INFO 1258 1259**System capability**: SystemCapability.Communication.NetManager.Core 1260 1261**Return value** 1262 1263| Type | Description | 1264| ----------------- | ----------------------------------------------- | 1265| Promise\<boolean> | Promise used to return the result. The value **true** indicates that the default data network is activated.| 1266 1267**Error codes** 1268 1269| ID| Error Message | 1270| ------- | ----------------------------- | 1271| 201 | Permission denied. | 1272| 401 | Parameter error. | 1273| 2100002 | Operation failed. Cannot connect to service.| 1274| 2100003 | System internal error. | 1275 1276**Example** 1277 1278```ts 1279import connection from '@ohos.net.connection'; 1280connection.hasDefaultNet().then((data: boolean) => { 1281 console.log('data: ' + data); 1282}); 1283``` 1284 1285## connection.hasDefaultNetSync<sup>10+</sup> 1286 1287hasDefaultNetSync(): boolean 1288 1289Checks whether the default data network is activated. This API returns the result synchronously. 1290 1291**Required permission**: ohos.permission.GET_NETWORK_INFO 1292 1293**System capability**: SystemCapability.Communication.NetManager.Core 1294 1295**Return value** 1296 1297| Type | Description | 1298| ----------------- | ----------------------------------------------- | 1299| boolean | The value **true** indicates the default data network is activated.| 1300 1301**Error codes** 1302 1303| ID| Error Message | 1304| ------- | ----------------------------- | 1305| 201 | Permission denied. | 1306| 401 | Parameter error. | 1307| 2100002 | Operation failed. Cannot connect to service.| 1308| 2100003 | System internal error. | 1309 1310**Example** 1311 1312```ts 1313import connection from '@ohos.net.connection'; 1314 1315let isDefaultNet = connection.hasDefaultNetSync(); 1316``` 1317 1318## connection.enableAirplaneMode 1319 1320enableAirplaneMode(callback: AsyncCallback\<void>): void 1321 1322Enables the airplane mode. This API uses an asynchronous callback to return the result. 1323 1324**System API**: This is a system API. 1325 1326**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 1327 1328**System capability**: SystemCapability.Communication.NetManager.Core 1329 1330**Parameters** 1331 1332| Name | Type | Mandatory| Description | 1333| -------- | ------------------------------------------------- | ---- | ------------------ | 1334| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 1335 1336**Error codes** 1337 1338| ID| Error Message | 1339| ------- | ----------------------------- | 1340| 201 | Permission denied. | 1341| 202 | Non-system applications use system APIs. | 1342| 401 | Parameter error. | 1343| 2100002 | Operation failed. Cannot connect to service.| 1344| 2100003 | System internal error. | 1345 1346**Example** 1347 1348```ts 1349import connection from '@ohos.net.connection'; 1350import { BusinessError } from '@ohos.base'; 1351 1352connection.enableAirplaneMode((error: BusinessError) => { 1353 console.log(JSON.stringify(error)); 1354}); 1355``` 1356 1357## connection.enableAirplaneMode 1358 1359enableAirplaneMode(): Promise\<void> 1360 1361Enables the airplane mode. This API uses a promise to return the result. 1362 1363**System API**: This is a system API. 1364 1365**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 1366 1367**System capability**: SystemCapability.Communication.NetManager.Core 1368 1369**Return value** 1370 1371| Type | Description | 1372| ------------------------------------------- | ----------------------------- | 1373| Promise\<void> | Promise that returns no value.| 1374 1375**Error codes** 1376 1377| ID| Error Message | 1378| ------- | ----------------------------- | 1379| 201 | Permission denied. | 1380| 202 | Non-system applications use system APIs. | 1381| 401 | Parameter error. | 1382| 2100002 | Operation failed. Cannot connect to service.| 1383| 2100003 | System internal error. | 1384 1385**Example** 1386 1387```ts 1388import connection from '@ohos.net.connection'; 1389 1390connection.enableAirplaneMode().then((error: void) => { 1391 console.log(JSON.stringify(error)); 1392}); 1393``` 1394 1395## connection.disableAirplaneMode 1396 1397disableAirplaneMode(callback: AsyncCallback\<void>): void 1398 1399Disables the airplane mode. This API uses an asynchronous callback to return the result. 1400 1401**System API**: This is a system API. 1402 1403**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 1404 1405**System capability**: SystemCapability.Communication.NetManager.Core 1406 1407**Parameters** 1408 1409| Name | Type | Mandatory| Description | 1410| -------- | ------------------------------------------------- | ---- | ------------------ | 1411| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the airplane mode is disabled successfully, **error** is **undefined**. Otherwise, **error** is an error object.| 1412 1413**Error codes** 1414 1415| ID| Error Message | 1416| ------- | ----------------------------- | 1417| 201 | Permission denied. | 1418| 202 | Non-system applications use system APIs. | 1419| 401 | Parameter error. | 1420| 2100002 | Operation failed. Cannot connect to service.| 1421| 2100003 | System internal error. | 1422 1423**Example** 1424 1425```ts 1426import connection from '@ohos.net.connection'; 1427import { BusinessError } from '@ohos.base'; 1428 1429connection.disableAirplaneMode((error: BusinessError) => { 1430 console.log(JSON.stringify(error)); 1431}); 1432``` 1433 1434## connection.disableAirplaneMode 1435 1436disableAirplaneMode(): Promise\<void> 1437 1438Disables the airplane mode. This API uses a promise to return the result. 1439 1440**System API**: This is a system API. 1441 1442**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 1443 1444**System capability**: SystemCapability.Communication.NetManager.Core 1445 1446**Return value** 1447 1448| Type | Description | 1449| ------------------------------------------- | ----------------------------- | 1450| Promise\<void> | Promise that returns no value.| 1451 1452**Error codes** 1453 1454| ID| Error Message | 1455| ------- | ----------------------------- | 1456| 201 | Permission denied. | 1457| 202 | Non-system applications use system APIs. | 1458| 401 | Parameter error. | 1459| 2100002 | Operation failed. Cannot connect to service.| 1460| 2100003 | System internal error. | 1461 1462**Example** 1463 1464```ts 1465import connection from '@ohos.net.connection'; 1466 1467connection.disableAirplaneMode().then((error: void) => { 1468 console.log(JSON.stringify(error)); 1469}); 1470``` 1471 1472## connection.reportNetConnected 1473 1474reportNetConnected(netHandle: NetHandle, callback: AsyncCallback<void>): void 1475 1476Reports connection of the data network to the network management module. This API uses an asynchronous callback to return the result. 1477 1478**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET 1479 1480**System capability**: SystemCapability.Communication.NetManager.Core 1481 1482**Parameters** 1483 1484| Name| Type| Mandatory| Description| 1485| -------- | -------- | -------- | -------- | 1486| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).| 1487| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the network status is reported successfully, **error** is **undefined**. Otherwise, **error** is an error object.| 1488 1489**Error codes** 1490 1491| ID| Error Message | 1492| ------- | ----------------------------- | 1493| 201 | Permission denied. | 1494| 401 | Parameter error. | 1495| 2100001 | Invalid parameter value. | 1496| 2100002 | Operation failed. Cannot connect to service.| 1497| 2100003 | System internal error. | 1498 1499**Example** 1500 1501```ts 1502import connection from '@ohos.net.connection'; 1503import { BusinessError } from '@ohos.base'; 1504 1505connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 1506 connection.reportNetConnected(netHandle, (error: BusinessError) => { 1507 console.log(JSON.stringify(error)); 1508 }); 1509}); 1510``` 1511 1512## connection.reportNetConnected 1513 1514reportNetConnected(netHandle: NetHandle): Promise<void> 1515 1516Reports connection of the data network to the network management module. This API uses a promise to return the result. 1517 1518**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET 1519 1520**System capability**: SystemCapability.Communication.NetManager.Core 1521 1522**Parameters** 1523 1524| Name| Type| Mandatory| Description| 1525| -------- | -------- | -------- | -------- | 1526| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).| 1527 1528**Return value** 1529| Type| Description| 1530| -------- | -------- | 1531| Promise<void> | Promise that returns no value.| 1532 1533**Error codes** 1534 1535| ID| Error Message | 1536| ------- | ----------------------------- | 1537| 201 | Permission denied. | 1538| 401 | Parameter error. | 1539| 2100001 | Invalid parameter value. | 1540| 2100002 | Operation failed. Cannot connect to service.| 1541| 2100003 | System internal error. | 1542 1543**Example** 1544 1545```ts 1546import connection from '@ohos.net.connection'; 1547connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 1548 connection.reportNetConnected(netHandle).then(() => { 1549 console.log(`report success`); 1550 }); 1551}); 1552``` 1553 1554## connection.reportNetDisconnected 1555 1556reportNetDisconnected(netHandle: NetHandle, callback: AsyncCallback<void>): void 1557 1558Reports disconnection of the data network to the network management module. This API uses an asynchronous callback to return the result. 1559 1560**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET 1561 1562**System capability**: SystemCapability.Communication.NetManager.Core 1563 1564**Parameters** 1565 1566| Name| Type| Mandatory| Description| 1567| -------- | -------- | -------- | -------- | 1568| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).| 1569| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the network status is reported successfully, **error** is **undefined**. Otherwise, **error** is an error object.| 1570 1571**Error codes** 1572 1573| ID| Error Message | 1574| ------- | ----------------------------- | 1575| 201 | Permission denied. | 1576| 401 | Parameter error. | 1577| 2100001 | Invalid parameter value. | 1578| 2100002 | Operation failed. Cannot connect to service.| 1579| 2100003 | System internal error. | 1580 1581**Example** 1582 1583```ts 1584import connection from '@ohos.net.connection'; 1585connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 1586 connection.reportNetDisconnected(netHandle).then( () => { 1587 console.log(`report success`); 1588 }); 1589}); 1590``` 1591 1592## connection.reportNetDisconnected 1593 1594reportNetDisconnected(netHandle: NetHandle): Promise<void> 1595 1596Reports disconnection of the data network to the network management module. This API uses a promise to return the result. 1597 1598**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET 1599 1600**System capability**: SystemCapability.Communication.NetManager.Core 1601 1602**Parameters** 1603 1604| Name| Type| Mandatory| Description| 1605| -------- | -------- | -------- | -------- | 1606| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).| 1607 1608**Return value** 1609| Type| Description| 1610| -------- | -------- | 1611| Promise<void> | Promise that returns no value.| 1612 1613**Error codes** 1614 1615| ID| Error Message | 1616| ------- | ----------------------------- | 1617| 201 | Permission denied. | 1618| 401 | Parameter error. | 1619| 2100001 | Invalid parameter value. | 1620| 2100002 | Operation failed. Cannot connect to service.| 1621| 2100003 | System internal error. | 1622 1623**Example** 1624 1625```ts 1626import connection from '@ohos.net.connection'; 1627connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 1628 connection.reportNetDisconnected(netHandle).then( () => { 1629 console.log(`report success`); 1630 }); 1631}); 1632``` 1633 1634## connection.getAddressesByName 1635 1636getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>>): void 1637 1638Resolves the host name by using the corresponding network to obtain all IP addresses. This API uses an asynchronous callback to return the result. 1639 1640**Required permissions**: ohos.permission.INTERNET 1641 1642**System capability**: SystemCapability.Communication.NetManager.Core 1643 1644**Parameters** 1645 1646| Name | Type | Mandatory| Description | 1647| -------- | ------------------------------------------------- | ---- | ------------------------------------------------------------ | 1648| host | string | Yes | Host name to resolve. | 1649| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | Yes | Callback used to return the result. If all IP addresses are successfully obtained, **error** is **undefined**, and **data** is the list of all obtained IP addresses. Otherwise, **error** is an error object.| 1650 1651**Error codes** 1652 1653| ID| Error Message | 1654| ------- | ----------------------------- | 1655| 201 | Permission denied. | 1656| 401 | Parameter error. | 1657| 2100001 | Invalid parameter value. | 1658| 2100002 | Operation failed. Cannot connect to service.| 1659| 2100003 | System internal error. | 1660 1661**Example** 1662 1663```ts 1664import connection from '@ohos.net.connection'; 1665import { BusinessError } from "@ohos.base"; 1666connection.getAddressesByName("xxxx", (error: BusinessError, data: connection.NetAddress[]) => { 1667 console.log(JSON.stringify(error)); 1668 console.log(JSON.stringify(data)); 1669}); 1670``` 1671 1672## connection.getAddressesByName 1673 1674getAddressesByName(host: string): Promise\<Array\<NetAddress>> 1675 1676Resolves the host name by using the corresponding network to obtain all IP addresses. This API uses a promise to return the result. 1677 1678**Required permissions**: ohos.permission.INTERNET 1679 1680**System capability**: SystemCapability.Communication.NetManager.Core 1681 1682**Parameters** 1683 1684| Name| Type | Mandatory| Description | 1685| ------ | ------ | ---- | ------------------ | 1686| host | string | Yes | Host name to resolve.| 1687 1688**Return value** 1689 1690| Type | Description | 1691| ------------------------------------------- | ----------------------------- | 1692| Promise\<Array\<[NetAddress](#netaddress)>> | Promise used to return the result.| 1693 1694**Error codes** 1695 1696| ID| Error Message | 1697| ------- | ----------------------------- | 1698| 201 | Permission denied. | 1699| 401 | Parameter error. | 1700| 2100001 | Invalid parameter value. | 1701| 2100002 | Operation failed. Cannot connect to service.| 1702| 2100003 | System internal error. | 1703 1704**Example** 1705 1706```ts 1707import connection from '@ohos.net.connection'; 1708connection.getAddressesByName("xxxx").then((data: connection.NetAddress[]) => { 1709 console.log(JSON.stringify(data)); 1710}); 1711``` 1712 1713## connection.addCustomDnsRule<sup>11+</sup> 1714 1715addCustomDnsRule(host: string, ip: Array\<string\>, callback: AsyncCallback\<void\>): void 1716 1717Adds the mapping between a custom host and the corresponding IP address for the current application. This API uses an asynchronous callback to return the result. 1718 1719**Required permissions**: ohos.permission.INTERNET 1720 1721**System capability**: SystemCapability.Communication.NetManager.Core 1722 1723**Parameters** 1724 1725| Name | Type | Mandatory| Description | 1726| -------- | -------------------- | ---- | ------------------------------------------------------------ | 1727| host | string | Yes | Name of the custom host. | 1728| ip | Array\<string> | Yes | List of IP addresses mapped to the host name. | 1729| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the mapping is added successfully, **error** is **undefined**. Otherwise, **error** is an error object.| 1730 1731**Error codes** 1732 1733| ID| Error Message | 1734| ------- | ----------------------------- | 1735| 201 | Permission denied. | 1736| 401 | Parameter error. | 1737| 2100001 | Invalid parameter value. | 1738| 2100002 | Operation failed. Cannot connect to service.| 1739| 2100003 | System internal error. | 1740 1741**Example** 1742 1743```ts 1744import connection from '@ohos.net.connection'; 1745import { BusinessError } from '@ohos.base'; 1746connection.addCustomDnsRule("xxxx", ["xx.xx.xx.xx","xx.xx.xx.xx"], (error: BusinessError, data: void) => { 1747 console.log(JSON.stringify(error)); 1748 console.log(JSON.stringify(data)); 1749}) 1750``` 1751 1752## connection.addCustomDnsRule<sup>11+</sup> 1753 1754addCustomDnsRule(host: string, ip: Array\<string\>): Promise\<void\> 1755 1756Adds the mapping between a custom host and the corresponding IP address for the current application. This API uses a promise to return the result. 1757 1758**Required permissions**: ohos.permission.INTERNET 1759 1760**System capability**: SystemCapability.Communication.NetManager.Core 1761 1762**Parameters** 1763 1764| Name| Type | Mandatory| Description | 1765| ------ | -------------- | ---- | -------------------------- | 1766| host | string | Yes | Name of the custom host. | 1767| ip | Array\<string> | Yes | List of IP addresses mapped to the host name.| 1768 1769**Return value** 1770 1771| Type | Description | 1772| ---------------------- | ----------------------- | 1773| Promise\<Array\<void>> | Promise that returns no value.| 1774 1775**Error codes** 1776 1777| ID| Error Message | 1778| ------- | ----------------------------- | 1779| 201 | Permission denied. | 1780| 401 | Parameter error. | 1781| 2100001 | Invalid parameter value. | 1782| 2100002 | Operation failed. Cannot connect to service.| 1783| 2100003 | System internal error. | 1784 1785**Example** 1786 1787```ts 1788import connection from '@ohos.net.connection'; 1789import { BusinessError } from '@ohos.base'; 1790connection.addCustomDNSRule("xxxx", ["xx.xx.xx.xx","xx.xx.xx.xx"]).then(() => { 1791 console.log("success"); 1792}).catch((error: BusinessError) => { 1793 console.log(JSON.stringify(error)); 1794}) 1795``` 1796 1797## connection.removeCustomDnsRule<sup>11+</sup> 1798 1799removeCustomDnsRule(host: string, callback: AsyncCallback\<void\>): void 1800 1801Removes the custom DNS rules of the specified host from the current application. This API uses an asynchronous callback to return the result. 1802 1803**Required permissions**: ohos.permission.INTERNET 1804 1805**System capability**: SystemCapability.Communication.NetManager.Core 1806 1807**Parameters** 1808 1809| Name | Type | Mandatory| Description | 1810| -------- | -------------------- | ---- | ------------------------------------------------------------ | 1811| host | string | Yes | Name of the host for which DNS rules are to be deleted. | 1812| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the DNS rules are removed successfully, **error** is **undefined**. Otherwise, **error** is an error object.| 1813 1814**Error codes** 1815 1816| ID| Error Message | 1817| ------- | ----------------------------- | 1818| 201 | Permission denied. | 1819| 401 | Parameter error. | 1820| 2100001 | Invalid parameter value. | 1821| 2100002 | Operation failed. Cannot connect to service.| 1822| 2100003 | System internal error. | 1823 1824**Example** 1825 1826```ts 1827import connection from '@ohos.net.connection'; 1828import { BusinessError } from '@ohos.base'; 1829connection.removeCustomDnsRule("xxxx", (error: BusinessError, data: void) => { 1830 console.log(JSON.stringify(error)); 1831 console.log(JSON.stringify(data)); 1832}) 1833``` 1834 1835## connection.removeCustomDnsRule<sup>11+</sup> 1836 1837removeCustomDnsRule(host: string): Promise\<void\> 1838 1839Removes the custom DNS rules of the specified host from the current application. This API uses a promise to return the result. 1840 1841**Required permissions**: ohos.permission.INTERNET 1842 1843**System capability**: SystemCapability.Communication.NetManager.Core 1844 1845**Parameters** 1846 1847| Name| Type | Mandatory| Description | 1848| ------ | ------ | ---- | ------------------------------- | 1849| host | string | Yes | Name of the host for which DNS rules are to be deleted.| 1850 1851**Return value** 1852 1853| Type | Description | 1854| ---------------------- | ----------------------- | 1855| Promise\<Array\<void>> | Promise that returns no value.| 1856 1857**Error codes** 1858 1859| ID| Error Message | 1860| ------- | ----------------------------- | 1861| 201 | Permission denied. | 1862| 401 | Parameter error. | 1863| 2100001 | Invalid parameter value. | 1864| 2100002 | Operation failed. Cannot connect to service.| 1865| 2100003 | System internal error. | 1866 1867**Example** 1868 1869```ts 1870import connection from '@ohos.net.connection'; 1871import { BusinessError } from '@ohos.base'; 1872connection.removeCustomDnsRule("xxxx").then(() => { 1873 console.log("success"); 1874}).catch((error: BusinessError) => { 1875 console.log(JSON.stringify(error)); 1876}) 1877``` 1878 1879## connection.clearCustomDnsRules<sup>11+</sup> 1880 1881clearCustomDnsRules(callback: AsyncCallback\<void\>): void 1882 1883Removes all custom DNS rules from the current application. This API uses an asynchronous callback to return the result. 1884 1885**Required permissions**: ohos.permission.INTERNET 1886 1887**System capability**: SystemCapability.Communication.NetManager.Core 1888 1889**Parameters** 1890 1891| Name | Type | Mandatory| Description | 1892| -------- | -------------------- | ---- | ------------------------------------------------------------ | 1893| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If all the DNS rules are removed successfully, **error** is **undefined**. Otherwise, **error** is an error object.| 1894 1895**Error codes** 1896 1897| ID| Error Message | 1898| ------- | ----------------------------- | 1899| 201 | Permission denied. | 1900| 401 | Parameter error. | 1901| 2100001 | Invalid parameter value. | 1902| 2100002 | Operation failed. Cannot connect to service.| 1903| 2100003 | System internal error. | 1904 1905**Example** 1906 1907```ts 1908import connection from '@ohos.net.connection'; 1909import { BusinessError } from '@ohos.base'; 1910connection.clearCustomDnsRules((error: BusinessError, data: void) => { 1911 console.log(JSON.stringify(error)); 1912 console.log(JSON.stringify(data)); 1913}) 1914``` 1915 1916## connection.clearCustomDnsRules<sup>11+</sup> 1917 1918clearCustomDnsRules(): Promise\<void\> 1919 1920Removes all custom DNS rules from the current application. This API uses a promise to return the result. 1921 1922**Required permissions**: ohos.permission.INTERNET 1923 1924**System capability**: SystemCapability.Communication.NetManager.Core 1925 1926**Return value** 1927 1928| Type | Description | 1929| ---------------------- | ----------------------- | 1930| Promise\<void\> | Promise that returns no value. | 1931 1932**Error codes** 1933 1934| ID| Error Message | 1935| ------- | ----------------------------- | 1936| 201 | Permission denied. | 1937| 401 | Parameter error. | 1938| 2100001 | Invalid parameter value. | 1939| 2100002 | Operation failed. Cannot connect to service.| 1940| 2100003 | System internal error. | 1941 1942**Example** 1943 1944```ts 1945import connection from '@ohos.net.connection'; 1946import { BusinessError } from '@ohos.base'; 1947connection.clearCustomDnsRules().then(() => { 1948 console.log("success"); 1949}).catch((error: BusinessError) => { 1950 console.log(JSON.stringify(error)); 1951}) 1952``` 1953 1954 1955## connection.factoryReset<sup>11+</sup> 1956 1957factoryReset(): Promise\<void\> 1958 1959Resets the network settings to factory defaults. This API uses a promise to return the result. 1960 1961**System API**: This is a system API. 1962 1963**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 1964 1965**System capability**: SystemCapability.Communication.NetManager.Core 1966 1967**Return value** 1968 1969| Type | Description | 1970| ---------------------- | ----------------------- | 1971| Promise\<void\> | Promise that returns no value. | 1972 1973**Error codes** 1974 1975| ID| Error Message | 1976| ------- | ------------------------------------------ | 1977| 201 | Permission denied. | 1978| 202 | Non-system applications use system APIs. | 1979| 401 | Parameter error. | 1980| 2100002 | Operation failed. Cannot connect to service.| 1981| 2100003 | System internal error. | 1982 1983**Example** 1984 1985```ts 1986import connection from '@ohos.net.connection'; 1987import { BusinessError } from '@ohos.base'; 1988connection.factoryReset().then(() => { 1989 console.log("success"); 1990}).catch((error: BusinessError) => { 1991 console.log(JSON.stringify(error)); 1992}) 1993``` 1994 1995 1996## NetConnection 1997 1998Represents the network connection handle. 1999 2000> **NOTE** 2001> When a device changes to the network connected state, the **netAvailable**, **netCapabilitiesChange**, and **netConnectionPropertiesChange** events will be triggered. 2002> When a device changes to the network disconnected state, the **netLost** event will be triggered. 2003> When a device switches from a Wi-Fi network to a cellular network, the **netLost** event will be first triggered to indicate that the Wi-Fi network is lost and then the **netAvaliable** event will be triggered to indicate that the cellular network is available. 2004 2005### register 2006 2007register(callback: AsyncCallback\<void>): void 2008 2009Registers a listener for network status changes. 2010 2011**Required permission**: ohos.permission.GET_NETWORK_INFO 2012 2013**System capability**: SystemCapability.Communication.NetManager.Core 2014 2015**Parameters** 2016 2017| Name | Type | Mandatory| Description | 2018| -------- | -------------------- | ---- | ------------------------------------------------------------ | 2019| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If a listener for network status changes is registered successfully, **error** is **undefined**. Otherwise, **error** is an error object.| 2020 2021**Error codes** 2022 2023| ID| Error Message | 2024| ------- | ----------------------------- | 2025| 201 | Permission denied. | 2026| 401 | Parameter error. | 2027| 2100002 | Operation failed. Cannot connect to service.| 2028| 2100003 | System internal error. | 2029| 2101008 | The same callback exists. | 2030| 2101022 | The number of requests exceeded the maximum. | 2031 2032**Example** 2033 2034```ts 2035import connection from '@ohos.net.connection'; 2036import { BusinessError } from "@ohos.base"; 2037let netCon: connection.NetConnection = connection.createNetConnection(); 2038netCon.register((error: BusinessError) => { 2039 console.log(JSON.stringify(error)); 2040}); 2041``` 2042 2043### unregister 2044 2045unregister(callback: AsyncCallback\<void>): void 2046 2047Unregisters the listener for network status changes. 2048 2049**System capability**: SystemCapability.Communication.NetManager.Core 2050 2051**Parameters** 2052 2053| Name | Type | Mandatory| Description | 2054| -------- | -------------------- | ---- | ------------------------------------------------------------ | 2055| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If a listener for network status changes is unregistered successfully, **error** is **undefined**. Otherwise, **error** is an error object.| 2056 2057**Error codes** 2058 2059| ID| Error Message | 2060| ------- | ----------------------------- | 2061| 201 | Permission denied.| 2062| 401 | Parameter error. | 2063| 2100002 | Operation failed. Cannot connect to service.| 2064| 2100003 | System internal error. | 2065| 2101007 | The callback is not exists. | 2066 2067**Example** 2068 2069```ts 2070import connection from '@ohos.net.connection'; 2071import { BusinessError } from "@ohos.base"; 2072let netCon: connection.NetConnection = connection.createNetConnection(); 2073netCon.unregister((error: BusinessError) => { 2074 console.log(JSON.stringify(error)); 2075}); 2076``` 2077 2078### on('netAvailable') 2079 2080on(type: 'netAvailable', callback: Callback\<NetHandle>): void 2081 2082Registers a listener for **netAvailable** events. 2083 2084**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. 2085 2086**System capability**: SystemCapability.Communication.NetManager.Core 2087 2088**Parameters** 2089 2090| Name | Type | Mandatory| Description | 2091| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ | 2092| type | string | Yes | Event type. This field has a fixed value of **netAvailable**.<br>**netAvailable**: event indicating that the data network is available.| 2093| callback | Callback\<[NetHandle](#nethandle)> | Yes | Callback used to return the network handle.| 2094 2095**Example** 2096 2097```ts 2098import connection from '@ohos.net.connection'; 2099import { BusinessError } from "@ohos.base"; 2100 2101// Create a NetConnection object. 2102let netCon: connection.NetConnection = connection.createNetConnection(); 2103 2104// Call register to register a listener. 2105netCon.register((error: BusinessError) => { 2106 console.log(JSON.stringify(error)); 2107}); 2108 2109// Subscribe to netAvailable events. Event notifications can be received only after register is called. 2110netCon.on('netAvailable', (data: connection.NetHandle) => { 2111 console.log(JSON.stringify(data)); 2112}); 2113 2114// Call unregister to unregister the listener. 2115netCon.unregister((error: BusinessError) => { 2116 console.log(JSON.stringify(error)); 2117}); 2118``` 2119 2120### on('netBlockStatusChange') 2121 2122on(type: 'netBlockStatusChange', callback: Callback\<NetBlockStatusInfo>): void 2123 2124Registers a listener for **netBlockStatusChange** events. This API uses an asynchronous callback to return the result. 2125 2126**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. 2127 2128**System capability**: SystemCapability.Communication.NetManager.Core 2129 2130**Parameters** 2131 2132| Name | Type | Mandatory| Description | 2133| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2134| type | string | Yes | Event type. This field has a fixed value of **netBlockStatusChange**.<br>**netBlockStatusChange**: event indicating a change in the network blocking status.| 2135| callback | Callback<[NetBlockStatusInfo](#netblockstatusinfo11)> | Yes | Callback used to return the result. | 2136 2137**Example** 2138 2139```ts 2140import connection from '@ohos.net.connection'; 2141import { BusinessError } from "@ohos.base"; 2142 2143// Create a NetConnection object. 2144let netCon: connection.NetConnection = connection.createNetConnection(); 2145 2146// Call register to register a listener. 2147netCon.register((error: BusinessError) => { 2148 console.log(JSON.stringify(error)); 2149}); 2150 2151// Subscribe to netAvailable events. Event notifications can be received only after register is called. 2152class Value { 2153 netHandle: NetHandle = connection.NetHandle 2154 blocked: boolean = false 2155} 2156netCon.on('netBlockStatusChange', (data: Value) => { 2157 console.log(JSON.stringify(data)); 2158}); 2159 2160// Call unregister to unregister the listener. 2161netCon.unregister((error: BusinessError) => { 2162 console.log(JSON.stringify(error)); 2163}); 2164``` 2165 2166### on('netCapabilitiesChange') 2167 2168on(type: 'netCapabilitiesChange', callback: Callback\<NetCapabilityInfo\>): void 2169 2170Registers a listener for **netCapabilitiesChange** events. 2171 2172**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. 2173 2174**System capability**: SystemCapability.Communication.NetManager.Core 2175 2176**Parameters** 2177 2178| Name | Type | Mandatory| Description | 2179| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2180| type | string | Yes | Event type. This field has a fixed value of **netCapabilitiesChange**.<br>**netCapabilitiesChange**: event indicating that the network capabilities have changed.| 2181| callback | Callback<[NetCapabilityInfo](#netcapabilityinfo10)> | Yes | Callback used to return the network handle (**netHandle**) and capability information (**netCap**).| 2182 2183**Example** 2184 2185```ts 2186import connection from '@ohos.net.connection'; 2187import { BusinessError } from "@ohos.base"; 2188 2189// Create a NetConnection object. 2190let netCon: connection.NetConnection = connection.createNetConnection(); 2191 2192// Call register to register a listener. 2193netCon.register((error: BusinessError) => { 2194 console.log(JSON.stringify(error)); 2195}); 2196 2197// Subscribe to netCapabilitiesChange events. Event notifications can be received only after register is called. 2198netCon.on('netCapabilitiesChange', (data: connection.NetCapabilityInfo) => { 2199 console.log(JSON.stringify(data)); 2200}); 2201 2202// Call unregister to unregister the listener. 2203netCon.unregister((error: BusinessError) => { 2204 console.log(JSON.stringify(error)); 2205}); 2206``` 2207 2208### on('netConnectionPropertiesChange') 2209 2210on(type: 'netConnectionPropertiesChange', callback: Callback\<NetConnectionPropertyInfo\>): void 2211 2212Registers a listener for **netConnectionPropertiesChange** events. 2213 2214**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. 2215 2216**System capability**: SystemCapability.Communication.NetManager.Core 2217 2218**Parameters** 2219 2220| Name | Type | Mandatory| Description | 2221| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2222| type | string | Yes | Event type. This field has a fixed value of **netConnectionPropertiesChange**.<br>**netConnectionPropertiesChange**: event indicating that network connection properties have changed.| 2223| callback | Callback<[NetConnectionPropertyInfo](#netconnectionpropertyinfo11)> | Yes | Callback used to return the result. | 2224 2225**Example** 2226 2227```ts 2228import connection from '@ohos.net.connection'; 2229import { BusinessError } from "@ohos.base"; 2230 2231// Create a NetConnection object. 2232let netCon: connection.NetConnection = connection.createNetConnection(); 2233 2234// Call register to register a listener. 2235netCon.register((error: BusinessError) => { 2236 console.log(JSON.stringify(error)); 2237}); 2238 2239class Value { 2240 netHandle: NetHandle = connection.NetHandle 2241 connectionProperties: ConnectionProperties = connection.ConnectionProperties 2242} 2243 2244// Subscribe to netAvailable events. Event notifications can be received only after register is called. 2245netCon.on('netConnectionPropertiesChange', (data: Value) => { 2246 console.log(JSON.stringify(data)); 2247}); 2248 2249// Call unregister to unregister the listener. 2250netCon.unregister((error: BusinessError) => { 2251 console.log(JSON.stringify(error)); 2252}); 2253``` 2254 2255### on('netLost') 2256 2257on(type: 'netLost', callback: Callback\<NetHandle>): void 2258 2259Registers a listener for **netLost** events. 2260 2261**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. 2262 2263**System capability**: SystemCapability.Communication.NetManager.Core 2264 2265**Parameters** 2266 2267| Name | Type | Mandatory| Description | 2268| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ | 2269| type | string | Yes | Event type. This field has a fixed value of **netLost**.<br>netLost: event indicating that the network is interrupted or normally disconnected.| 2270| callback | Callback\<[NetHandle](#nethandle)> | Yes | Callback used to return **netHandle**.| 2271 2272**Example** 2273 2274```ts 2275import connection from '@ohos.net.connection'; 2276import { BusinessError } from "@ohos.base"; 2277 2278// Create a NetConnection object. 2279let netCon: connection.NetConnection = connection.createNetConnection(); 2280 2281// Call register to register a listener. 2282netCon.register((error: BusinessError) => { 2283 console.log(JSON.stringify(error)); 2284}); 2285 2286// Subscribe to netAvailable events. Event notifications can be received only after register is called. 2287netCon.on('netLost', (data: connection.NetHandle) => { 2288 console.log(JSON.stringify(data)); 2289}); 2290 2291// Call unregister to unregister the listener. 2292netCon.unregister((error: BusinessError) => { 2293 console.log(JSON.stringify(error)); 2294}); 2295``` 2296 2297### on('netUnavailable') 2298 2299on(type: 'netUnavailable', callback: Callback\<void>): void 2300 2301Registers a listener for **netUnavailable** events. 2302 2303**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. 2304 2305**System capability**: SystemCapability.Communication.NetManager.Core 2306 2307**Parameters** 2308 2309| Name | Type | Mandatory| Description | 2310| -------- | --------------- | ---- | ------------------------------------------------------------ | 2311| type | string | Yes | Event type. This field has a fixed value of **netUnavailable**.<br>**netUnavailable**: event indicating that the network is unavailable.| 2312| callback | Callback\<void> | Yes | Callback used to return the result, which is empty.| 2313 2314**Example** 2315 2316```ts 2317import connection from '@ohos.net.connection'; 2318import { BusinessError } from "@ohos.base"; 2319 2320// Create a NetConnection object. 2321let netCon: connection.NetConnection = connection.createNetConnection(); 2322 2323// Call register to register a listener. 2324netCon.register((error: BusinessError) => { 2325 console.log(JSON.stringify(error)); 2326}); 2327 2328// Subscribe to netUnavailable events. Event notifications can be received only after register is called. 2329netCon.on('netUnavailable', () => { 2330 console.log(JSON.stringify(data)); 2331}); 2332 2333// Call unregister to unregister the listener. 2334netCon.unregister((error: BusinessError) => { 2335 console.log(JSON.stringify(error)); 2336}); 2337``` 2338 2339## NetHandle 2340 2341Defines the handle of the data network. 2342 2343Before invoking **NetHandle** APIs, call **getNetHandle** to obtain a **NetHandle** object. 2344 2345**System capability**: SystemCapability.Communication.NetManager.Core 2346 2347### Attributes 2348 2349| Name | Type | Mandatory| Description | 2350| ------ | ------ | --- |------------------------- | 2351| netId | number | Yes | Network ID. The value **0** indicates no default network. Any other value must be greater than or equal to 100.| 2352 2353### bindSocket<sup>9+</sup> 2354 2355bindSocket(socketParam: TCPSocket \| UDPSocket, callback: AsyncCallback\<void>): void 2356 2357Binds a **TCPSocket** or **UDPSocket** object to the data network. This API uses an asynchronous callback to return the result. 2358 2359**System capability**: SystemCapability.Communication.NetManager.Core 2360 2361**Parameters** 2362 2363| Name | Type | Mandatory| Description | 2364| ----------- | ------------------------ | ---- | -------------------------------| 2365| socketParam | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | Yes| **TCPSocket** or **UDPSocket** object.| 2366| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the **TCPSocket** or **UDPSocket** object is successfully bound to the current network, **error** is **undefined**. Otherwise, **error** is an error object.| 2367 2368**Error codes** 2369 2370| ID| Error Message | 2371| ------- | ----------------------------- | 2372| 401 | Parameter error. | 2373| 2100001 | Invalid parameter value. | 2374| 2100002 | Operation failed. Cannot connect to service.| 2375| 2100003 | System internal error. | 2376 2377**Example** 2378 2379```ts 2380import socket from "@ohos.net.socket"; 2381import connection from '@ohos.net.connection'; 2382import { BusinessError } from '@ohos.base'; 2383 2384interface Data { 2385 message: ArrayBuffer, 2386 remoteInfo: socket.SocketRemoteInfo 2387} 2388 2389connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 2390 let tcp = socket.constructTCPSocketInstance(); 2391 let udp = socket.constructUDPSocketInstance(); 2392 let socketType = "TCPSocket"; 2393 if (socketType == "TCPSocket") { 2394 tcp.bind({address:"192.168.xxx.xxx", 2395 port:8080, 2396 family:1} as socket.NetAddress, (error: Error) => { 2397 if (error) { 2398 console.log('bind fail'); 2399 return; 2400 } 2401 netHandle.bindSocket(tcp, (error: BusinessError, data: void) => { 2402 if (error) { 2403 console.log(JSON.stringify(error)); 2404 } else { 2405 console.log(JSON.stringify(data)); 2406 } 2407 }); 2408 }); 2409 } else { 2410 let callback: (value: Data) => void = (value: Data) => { 2411 console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo); 2412 }; 2413 udp.bind({address:"192.168.xxx.xxx", 2414 port:8080, 2415 family:1} as socket.NetAddress, (error: BusinessError) => { 2416 if (error) { 2417 console.log('bind fail'); 2418 return; 2419 } 2420 udp.on('message', (data: Data) => { 2421 console.log(JSON.stringify(data)); 2422 }); 2423 netHandle.bindSocket(udp, (error: BusinessError, data: void) => { 2424 if (error) { 2425 console.log(JSON.stringify(error)); 2426 } else { 2427 console.log(JSON.stringify(data)); 2428 } 2429 }); 2430 }); 2431 } 2432}); 2433``` 2434 2435### bindSocket<sup>9+</sup> 2436 2437bindSocket(socketParam: TCPSocket \| UDPSocket): Promise\<void>; 2438 2439Binds a **TCPSocket** or **UDPSocket** object to the data network. This API uses a promise to return the result. 2440 2441**System capability**: SystemCapability.Communication.NetManager.Core 2442 2443**Parameters** 2444 2445| Name | Type | Mandatory | Description | 2446| --------------- | --------------------- | ---- | ------------------------------ | 2447| socketParam | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | Yes | **TCPSocket** or **UDPSocket** object.| 2448 2449**Return value** 2450 2451| Type | Description | 2452| -------------- | ---------------------- | 2453| Promise\<void> | Promise that returns no value.| 2454 2455**Error codes** 2456 2457| ID| Error Message | 2458| ------- | ----------------------------- | 2459| 401 | Parameter error. | 2460| 2100001 | Invalid parameter value. | 2461| 2100002 | Operation failed. Cannot connect to service.| 2462| 2100003 | System internal error. | 2463 2464**Example** 2465 2466```ts 2467import socket from "@ohos.net.socket"; 2468import connection from '@ohos.net.connection'; 2469import { BusinessError } from '@ohos.base'; 2470interface Data { 2471 message: ArrayBuffer, 2472 remoteInfo: socket.SocketRemoteInfo 2473} 2474 2475connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 2476 let tcp = socket.constructTCPSocketInstance(); 2477 let udp = socket.constructUDPSocketInstance(); 2478 let socketType = "TCPSocket"; 2479 if (socketType == "TCPSocket") { 2480 tcp.bind({address:"192.168.xxx.xxx", 2481 port:8080, 2482 family:1} as socket.NetAddress, (error: Error) => { 2483 if (error) { 2484 console.log('bind fail'); 2485 return; 2486 } 2487 netHandle.bindSocket(tcp, (error: BusinessError, data: void) => { 2488 if (error) { 2489 console.log(JSON.stringify(error)); 2490 } else { 2491 console.log(JSON.stringify(data)); 2492 } 2493 }); 2494 }); 2495 } else { 2496 let callback: (value: Data) => void = (value: Data) => { 2497 console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo); 2498 } 2499 udp.bind({address:"192.168.xxx.xxx", 2500 port:8080, 2501 family:1} as socket.NetAddress, (error: BusinessError) => { 2502 if (error) { 2503 console.log('bind fail'); 2504 return; 2505 } 2506 udp.on('message', (data: Data) => { 2507 console.log(JSON.stringify(data)); 2508 }); 2509 netHandle.bindSocket(udp, (error: BusinessError, data: void) => { 2510 if (error) { 2511 console.log(JSON.stringify(error)); 2512 } else { 2513 console.log(JSON.stringify(data)); 2514 } 2515 }); 2516 }); 2517} 2518}); 2519``` 2520 2521### getAddressesByName 2522 2523getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>>): void 2524 2525Resolves the host name by using the corresponding network to obtain all IP addresses. This API uses an asynchronous callback to return the result. 2526 2527**Required permissions**: ohos.permission.INTERNET 2528 2529**System capability**: SystemCapability.Communication.NetManager.Core 2530 2531**Parameters** 2532 2533| Name | Type | Mandatory| Description | 2534| -------- | ------------------------------------------------- | ---- | ------------------------------------------------------------ | 2535| host | string | Yes | Host name to resolve. | 2536| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | Yes | Callback used to return the result. If all IP addresses are successfully obtained, **error** is **undefined**, and **data** is the list of all obtained IP addresses. Otherwise, **error** is an error object.| 2537 2538**Error codes** 2539 2540| ID| Error Message | 2541| ------- | ----------------------------- | 2542| 201 | Permission denied. | 2543| 401 | Parameter error. | 2544| 2100001 | Invalid parameter value. | 2545| 2100002 | Operation failed. Cannot connect to service.| 2546| 2100003 | System internal error. | 2547 2548**Example** 2549 2550```ts 2551import connection from '@ohos.net.connection'; 2552import { BusinessError } from "@ohos.base"; 2553 2554connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 2555 let host = "xxxx"; 2556 netHandle.getAddressesByName(host, (error: BusinessError, data: connection.NetAddress[]) => { 2557 console.log(JSON.stringify(error)); 2558 console.log(JSON.stringify(data)); 2559 }); 2560}); 2561``` 2562 2563### getAddressesByName 2564 2565getAddressesByName(host: string): Promise\<Array\<NetAddress>> 2566 2567Resolves the host name by using the corresponding network to obtain all IP addresses. This API uses a promise to return the result. 2568 2569**Required permissions**: ohos.permission.INTERNET 2570 2571**System capability**: SystemCapability.Communication.NetManager.Core 2572 2573**Parameters** 2574 2575| Name| Type | Mandatory| Description | 2576| ------ | ------ | ---- | ------------------ | 2577| host | string | Yes | Host name to resolve.| 2578 2579**Return value** 2580 2581| Type | Description | 2582| ------------------------------------------- | ----------------------------- | 2583| Promise\<Array\<[NetAddress](#netaddress)>> | Promise used to return the result.| 2584 2585**Error codes** 2586 2587| ID| Error Message | 2588| ------- | ----------------------------- | 2589| 201 | Permission denied. | 2590| 401 | Parameter error. | 2591| 2100001 | Invalid parameter value. | 2592| 2100002 | Operation failed. Cannot connect to service.| 2593| 2100003 | System internal error. | 2594 2595**Example** 2596 2597```ts 2598import connection from '@ohos.net.connection'; 2599 2600connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 2601 let host = "xxxx"; 2602 netHandle.getAddressesByName(host).then((data: connection.NetAddress[]) => { 2603 console.log(JSON.stringify(data)); 2604 }); 2605}); 2606``` 2607 2608### getAddressByName 2609 2610getAddressByName(host: string, callback: AsyncCallback\<NetAddress>): void 2611 2612Resolves the host name by using the corresponding network to obtain the first IP address. This API uses an asynchronous callback to return the result. 2613 2614**Required permissions**: ohos.permission.INTERNET 2615 2616**System capability**: SystemCapability.Communication.NetManager.Core 2617 2618**Parameters** 2619 2620| Name | Type | Mandatory| Description | 2621| -------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | 2622| host | string | Yes | Host name to resolve. | 2623| callback | AsyncCallback\<[NetAddress](#netaddress)> | Yes | Callback used to return the result. If the first IP address is obtained successfully, **error** is **undefined**, and **data** is the first obtained IP address. Otherwise, **error** is an error object.| 2624 2625**Error codes** 2626 2627| ID| Error Message | 2628| ------- | ----------------------------- | 2629| 201 | Permission denied. | 2630| 401 | Parameter error. | 2631| 2100001 | Invalid parameter value. | 2632| 2100002 | Operation failed. Cannot connect to service.| 2633| 2100003 | System internal error. | 2634 2635**Example** 2636 2637```ts 2638import connection from '@ohos.net.connection'; 2639import { BusinessError } from "@ohos.base"; 2640 2641connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 2642 let host = "xxxx"; 2643 netHandle.getAddressByName(host, (error: BusinessError, data: connection.NetAddress) => { 2644 console.log(JSON.stringify(error)); 2645 console.log(JSON.stringify(data)); 2646 }); 2647}); 2648``` 2649 2650### getAddressByName 2651 2652getAddressByName(host: string): Promise\<NetAddress> 2653 2654Resolves the host name by using the corresponding network to obtain the first IP address. This API uses a promise to return the result. 2655 2656**Required permissions**: ohos.permission.INTERNET 2657 2658**System capability**: SystemCapability.Communication.NetManager.Core 2659 2660**Parameters** 2661 2662| Name| Type | Mandatory| Description | 2663| ------ | ------ | ---- | ------------------ | 2664| host | string | Yes | Host name to resolve.| 2665 2666**Return value** 2667 2668| Type | Description | 2669| ----------------------------------- | ------------------------------- | 2670| Promise\<[NetAddress](#netaddress)> | Promise used to return the result.| 2671 2672**Error codes** 2673 2674| ID| Error Message | 2675| ------- | ----------------------------- | 2676| 201 | Permission denied. | 2677| 401 | Parameter error. | 2678| 2100001 | Invalid parameter value. | 2679| 2100002 | Operation failed. Cannot connect to service.| 2680| 2100003 | System internal error. | 2681 2682**Example** 2683 2684```ts 2685import connection from '@ohos.net.connection'; 2686 2687connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 2688 let host = "xxxx"; 2689 netHandle.getAddressByName(host).then((data: connection.NetAddress) => { 2690 console.log(JSON.stringify(data)); 2691 }); 2692}); 2693``` 2694 2695## NetCap 2696 2697Defines the network capability. 2698 2699**System capability**: SystemCapability.Communication.NetManager.Core 2700 2701| Name | Value | Description | 2702| ------------------------ | ---- | ---------------------- | 2703| NET_CAPABILITY_MMS | 0 | The network can connect to the carrier's Multimedia Messaging Service Center (MMSC) to send and receive multimedia messages.| 2704| NET_CAPABILITY_NOT_METERED | 11 | The network traffic is not metered.| 2705| NET_CAPABILITY_INTERNET | 12 | The network has the Internet access capability, which is set by the network provider.| 2706| NET_CAPABILITY_NOT_VPN | 15 | The network does not use a virtual private network (VPN).| 2707| NET_CAPABILITY_VALIDATED | 16 | The Internet access capability of the network is successfully verified by the connection management module.| 2708 2709## NetBearType 2710 2711Enumerates network types. 2712 2713**System capability**: SystemCapability.Communication.NetManager.Core 2714 2715| Name | Value | Description | 2716| --------------- | ---- | ----------- | 2717| BEARER_CELLULAR | 0 | Cellular network. | 2718| BEARER_WIFI | 1 | Wi-Fi network.| 2719| BEARER_ETHERNET | 3 | Ethernet network.| 2720 2721## HttpProxy<sup>10+</sup> 2722 2723Represents the HTTP proxy configuration. 2724 2725**System capability**: SystemCapability.Communication.NetManager.Core 2726 2727| Name | Type | Mandatory| Description | 2728| ------ | ------ | --- |------------------------- | 2729| host | string | No | Host name of the proxy server.| 2730| port | number | No | Host port.| 2731| exclusionList | Array<string> | No | List of the names of hosts that do not use a proxy. Host names can be domain names, IP addresses, or wildcards. The detailed matching rules are as follows:<br>- Domain name matching:<br> - Exact match: The host name of the proxy server exactly matches any host name in the list.<br> - Partial match: The host name of the proxy server contains any host name in the list.<br>For example, if **ample.com** is set in the host name list, **ample.com**, **www.ample.com**, and **ample.com:80** are matched, and **www.example.com** and **ample.com.org** are not matched.<br>- IP address matching: The host name of the proxy server exactly matches any IP address in the list.<br>- Both the domain name and IP address are added to the list for matching.<br>- A single asterisk (*) is the only valid wildcard. If the list contains only wildcards, the wildcards match all host names; that is, the HTTP proxy is disabled. A wildcard can only be added independently. It cannot be added to the list together with other domain names or IP addresses. Otherwise, the wildcard does not take effect.<br>- Host names are case insensitive.<br>- Protocol prefixes such as **http** and **https** are ignored during matching.| 2732 2733## NetSpecifier 2734 2735Provides an instance that bears data network capabilities. 2736 2737**System capability**: SystemCapability.Communication.NetManager.Core 2738 2739| Name | Type | Mandatory | Description | 2740| ----------------------- | ----------------------------------- | ---- | ------------------------------------------------------------ | 2741| netCapabilities | [NetCapabilities](#netcapabilities) | Yes | Network transmission capabilities and bearer types of the data network. | 2742| 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).| 2743 2744## NetCapabilityInfo<sup>10+</sup> 2745 2746Provides an instance that bears data network capabilities. 2747 2748**System capability**: SystemCapability.Communication.NetManager.Core 2749 2750| Name | Type | Mandatory | Description | 2751| ----------------------- | ----------------------------------- | ---- | ------------------------------------------------------------ | 2752| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network. | 2753| netCap | [NetCapabilities](#netcapabilities) | No | Network transmission capabilities and bearer types of the data network.| 2754 2755## NetCapabilities 2756 2757Defines the network capability set. 2758 2759**System capability**: SystemCapability.Communication.NetManager.Core 2760 2761| Name | Type | Mandatory| Description | 2762| --------------------- | ---------------------------------- | --- | ------------------------ | 2763| linkUpBandwidthKbps | number | No| Uplink (device-to-network) bandwidth. The value **0** indicates that the current network bandwidth cannot be evaluated. | 2764| linkDownBandwidthKbps | number | No| Downlink (network-to-device) bandwidth. The value **0** indicates that the current network bandwidth cannot be evaluated. | 2765| networkCap | Array\<[NetCap](#netcap)> | No| Network capability. | 2766| bearerTypes | Array\<[NetBearType](#netbeartype)> | Yes| Network type. | 2767 2768## NetConnectionPropertyInfo<sup>11+</sup> 2769 2770Defines the network connection properties. 2771 2772**System capability**: SystemCapability.Communication.NetManager.Core 2773 2774### Attributes 2775 2776| Name | Type | Mandatory| Description | 2777| -------------------- | ------------------------------------- | ---- |---------------- | 2778| netHandle | [NetHandle](#nethandle) | Yes |Data network handle. | 2779| connectionProperties | [ConnectionProperties](#connectionproperties) | Yes |Network connection properties.| 2780 2781## NetBlockStatusInfo<sup>11+</sup> 2782 2783Obtains the network block status information. 2784 2785**System capability**: SystemCapability.Communication.NetManager.Core 2786 2787### Attributes 2788 2789| Name | Type | Mandatory| Description | 2790| -------------------- | ------------------------------------- | ---- |---------------- | 2791| netHandle | [NetHandle](#nethandle) | Yes |Data network handle. | 2792| blocked | boolean | Yes |Whether the current network is blocked.| 2793 2794## ConnectionProperties 2795 2796Defines the network connection properties. 2797 2798**System capability**: SystemCapability.Communication.NetManager.Core 2799 2800| Name | Type | Mandatory| Description | 2801| ------------- | ---------------------------------- | ----|---------------- | 2802| interfaceName | string | Yes|Network interface card (NIC) name. | 2803| domains | string | Yes|Domain. The default value is **""**.| 2804| linkAddresses | Array\<[LinkAddress](#linkaddress)> | Yes|Link information. | 2805| routes | Array\<[RouteInfo](#routeinfo)> | Yes|Route information. | 2806| dnses | Array\<[NetAddress](#netaddress)> | Yes|Network address. For details, see [NetAddress](#netaddress).| 2807| mtu | number | Yes|Maximum transmission unit (MTU). | 2808 2809## RouteInfo 2810 2811Defines network route information. 2812 2813**System capability**: SystemCapability.Communication.NetManager.Core 2814 2815| Name | Type | Mandatory|Description | 2816| -------------- | --------------------------- | --- |---------------- | 2817| interface | string | Yes|NIC name. | 2818| destination | [LinkAddress](#linkaddress) | Yes|Destination address. | 2819| gateway | [NetAddress](#netaddress) | Yes|Gateway address. | 2820| hasGateway | boolean | Yes|Whether a gateway is present. | 2821| isDefaultRoute | boolean | Yes|Whether the route is the default route.| 2822 2823## LinkAddress 2824 2825Defines network link information. 2826 2827**System capability**: SystemCapability.Communication.NetManager.Core 2828 2829| Name | Type | Mandatory|Description | 2830| ------------ | ----------------------- |---- |-------------------- | 2831| address | [NetAddress](#netaddress) | Yes| Link address. | 2832| prefixLength | number | Yes|Length of the link address prefix.| 2833 2834## NetAddress 2835 2836Defines a network address. 2837 2838**System capability**: SystemCapability.Communication.NetManager.Core 2839 2840| Name| Type| Mandatory| Description| 2841| ------- | ------ | -- |------------------------------ | 2842| address | string | Yes|Network address.| 2843| family | number | No|Address family identifier. The value is **1** for IPv4 and **2** for IPv6. The default value is **1**.| 2844| port | number | No|Port number. The value ranges from **0** to **65535**.| 2845