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