net-connection-manager.md 15.5 KB
Newer Older
S
shawn_he 已提交
1 2 3
# Network Connection Management

## Introduction
S
shawn_he 已提交
4

S
shawn_he 已提交
5 6 7
The Network Connection Management module provides basic network management capabilities, including management of Wi-Fi/cellular/Ethernet connection priorities, network quality evaluation, subscription to network connection status changes, query of network connection information, and DNS resolution.

> **NOTE**
S
shawn_he 已提交
8
> 
S
shawn_he 已提交
9 10 11
> To maximize the application running efficiency, most API calls are called asynchronously in callback or promise mode. The following code examples use the callback mode. For details about the APIs, see [sms API Reference](../reference/apis/js-apis-net-connection.md).

## Basic Concepts
S
shawn_he 已提交
12 13 14 15 16

- Producer: a provider of data networks, such as Wi-Fi, cellular, and Ethernet.
- Consumer: a user of data networks, for example, an application or a system service.
- Network probe: a mechanism used to detect the network availability to prevent the switch from an available network to an unavailable network. The probe type can be binding network detection, DNS detection, HTTP detection, or HTTPS detection.
- Network selection: a mechanism used to select the optimal network when multiple networks coexist. It is triggered when the network status, network information, or network quality evaluation score changes.
S
shawn_he 已提交
17 18

## **Constraints**
S
shawn_he 已提交
19 20 21 22

- Programming language: C++ and JS
- System: Linux kernel
- 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.
S
shawn_he 已提交
23 24

## When to Use
S
shawn_he 已提交
25

S
shawn_he 已提交
26
Typical application scenarios of network connection management are as follows:
S
shawn_he 已提交
27 28 29 30 31

- Subscribing to status changes of the specified network
- Obtaining the list of all registered networks
- Querying network connection information based on the data network
- Resolving the domain name of a network to obtain all IP addresses
S
shawn_he 已提交
32 33

The following describes the development procedure specific to each application scenario.
S
shawn_he 已提交
34

S
shawn_he 已提交
35
## Available APIs
S
shawn_he 已提交
36

S
shawn_he 已提交
37 38 39 40 41 42
For the complete list of APIs and example code, see [Network Connection Management](../reference/apis/js-apis-net-connection.md).

| Type| API| Description|
| ---- | ---- | ---- |
| ohos.net.connection | function getDefaultNet(callback: AsyncCallback\<NetHandle>): void; |Creates a **NetHandle** object that contains the **netId** of the default network. This API uses an asynchronous callback to return the result.|
| ohos.net.connection | function getGlobalHttpProxy<sup>10+</sup>(callback: AsyncCallback\<HttpProxy>): void;| Obtains the global HTTP proxy for the network. This API uses an asynchronous callback to return the result.|
S
shawn_he 已提交
43
| ohos.net.connection | function setGlobalHttpProxy<sup>10+</sup>(httpProxy: HttpProxy, callback: AsyncCallback\<void>): void;| Sets the global HTTP proxy for the network. This API uses an asynchronous callback to return the result.|
S
shawn_he 已提交
44 45 46 47 48 49 50
| ohos.net.connection | function getAppNet<sup>9+</sup>(callback: AsyncCallback\<NetHandle>): void;| Obtains a **NetHandle** object that contains the **netId** of the network bound to the application. This API uses an asynchronous callback to return the result.|
| ohos.net.connection | function setAppNet<sup>9+</sup>(netHandle: NetHandle, callback: AsyncCallback\<void>): void;| Binds an application to the specified network. The application can access the external network only through this network. This API uses an asynchronous callback to return the result.|
| ohos.net.connection | function getDefaultNetSync<sup>9+</sup>(): NetHandle; |Obtains the default active data network in synchronous mode. You can use **getNetCapabilities** to obtain information such as the network type and capabilities.|
| ohos.net.connection | function hasDefaultNet(callback: AsyncCallback\<boolean>): void; |Checks whether the default network is available. This API uses an asynchronous callback to return the result.|
| ohos.net.connection | function getAllNets(callback: AsyncCallback\<Array\<NetHandle>>): void;| Obtains the list of **NetHandle** objects of the connected network. This API uses an asynchronous callback to return the result.|
| ohos.net.connection | function getConnectionProperties(netHandle: NetHandle, callback: AsyncCallback\<ConnectionProperties>): void; |Obtains link information of the default network. This API uses an asynchronous callback to return the result.|
| ohos.net.connection | function getNetCapabilities(netHandle: NetHandle, callback: AsyncCallback\<NetCapabilities>): void; |Obtains the capability set of the default network. This API uses an asynchronous callback to return the result.|
S
shawn_he 已提交
51
| ohos.net.connection | function isDefaultNetMetered<sup>9+</sup>(callback: AsyncCallback\<boolean>): void; |Checks whether the data traffic usage on the current network is metered. This API uses an asynchronous callback to return the result.|
S
shawn_he 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| ohos.net.connection | function reportNetConnected(netHandle: NetHandle, callback: AsyncCallback\<void>): void;| Reports a **netAavailable** event to NetManager. If this API is called, the application considers that its network status (ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED) is inconsistent with that of NetManager. This API uses an asynchronous callback to return the result.|
| ohos.net.connection | function reportNetDisconnected(netHandle: NetHandle, callback: AsyncCallback\<void>): void;| Reports a **netAavailable** event to NetManager. If this API is called, the application considers that its network status (ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED) is inconsistent with that of NetManager. This API uses an asynchronous callback to return the result.|
| ohos.net.connection | function getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>>): void; |Obtains all IP addresses of the specified network by resolving the domain name. This API uses an asynchronous callback to return the result.|
| ohos.net.connection | function enableAirplaneMode(callback: AsyncCallback\<void>): void; | Enables the airplane mode. This API uses an asynchronous callback to return the result.|
| ohos.net.connection | function disableAirplaneMode(callback: AsyncCallback\<void>): void;| Disables the airplane mode. This API uses an asynchronous callback to return the result.|
| ohos.net.connection | function createNetConnection(netSpecifier?: NetSpecifier, timeout?: number): NetConnection; | Creates a **NetConnection** object. **netSpecifier** specifies the network, and **timeout** specifies the timeout interval in ms. **timeout** is configurable only when **netSpecifier** is specified. If neither of them is present, the default network is used.|
| ohos.net.connection.NetHandle | bindSocket(socketParam: TCPSocket \| UDPSocket, callback: AsyncCallback\<void>): void; | Binds a **TCPSocket** or **UDPSocket** to the current network. This API uses an asynchronous callback to return the result.|
| ohos.net.connection.NetHandle | getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>>): void; |Obtains all IP addresses of the default network by resolving the domain name. This API uses an asynchronous callback to return the result.|
| ohos.net.connection.NetHandle | getAddressByName(host: string, callback: AsyncCallback\<NetAddress>): void; |Obtains an IP address of the specified network by resolving the domain name. This API uses an asynchronous callback to return the result.|
| ohos.net.connection.NetConnection | on(type: 'netAvailable', callback: Callback\<NetHandle>): void; |Subscribes to **netAvailable** events.|
| ohos.net.connection.NetConnection | on(type: 'netCapabilitiesChange', callback: Callback\<{ netHandle: NetHandle, netCap: NetCapabilities }>): void; |Subscribes to **netCapabilitiesChange** events.|
| ohos.net.connection.NetConnection | on(type: 'netConnectionPropertiesChange', callback: Callback\<{ netHandle: NetHandle, connectionProperties: ConnectionProperties }>): void; |Subscribes to **netConnectionPropertiesChange** events.|
| ohos.net.connection.NetConnection | on(type: 'netBlockStatusChange', callback: Callback<{ netHandle: NetHandle, blocked: boolean }>): void; |Subscribes to **netBlockStatusChange** events.|
| ohos.net.connection.NetConnection | on(type: 'netLost', callback: Callback\<NetHandle>): void; |Subscribes to **netLost** events.|
| ohos.net.connection.NetConnection | on(type: 'netUnavailable', callback: Callback\<void>): void; |Subscribes to **netUnavailable** events.|
| ohos.net.connection.NetConnection | register(callback: AsyncCallback\<void>): void; |Registers an observer for the default network or the network specified in **createNetConnection**.|
| ohos.net.connection.NetConnection | unregister(callback: AsyncCallback\<void>): void; |Unregisters the observer for the default network or the network specified in **createNetConnection**.|

## Subscribing to Status Changes of the Specified Network

1. Import the connection namespace from **@ohos.net.connection.d.ts**.

2. Call **createNetConnection()** to create a **NetConnection** object. You can specify the network type, capability, and timeout interval. If you do not specify parameters, the default values will be used. 

3. Call **conn.on()** to subscribe to the target event. You must pass in **type** and **callback**.

4. Call **conn.register()** to subscribe to network status changes of the specified network.

5. When the network is available, the callback will be invoked to return the **netAvailable** event. When the network is unavailable, the callback will be invoked to return the **netUnavailable** event.

6. Call **conn.unregister()** to unsubscribe from the network status changes if required.

```js
S
shawn_he 已提交
85
// Import the connection namespace.
S
shawn_he 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
import connection from '@ohos.net.connection'

let netCap = {
  // Assume that the default network is Wi-Fi. If you need to create a cellular network connection, set the network type to CELLULAR.
  bearerTypes: [connection.NetBearType.BEARER_CELLULAR],
  // Set the network capability to INTERNET.
  networkCap: [connection.NetCap.NET_CAPABILITY_INTERNET],
};
let netSpec = {
  netCapabilities: netCap,
};

// Set the timeout value to 10s. The default value is 0.
let timeout = 10 * 1000;

// Create a NetConnection object.
let conn = connection.createNetConnection(netSpec, timeout);

// Listen to network status change events. If the network is available, an on_netAvailable event is returned.
conn.on('netAvailable', (data => {
  console.log("net is available, netId is " + data.netId);
}));

// Listen to network status change events. If the network is unavailable, an on_netUnavailable event is returned.
conn.on('netUnavailable', (data => {
S
shawn_he 已提交
111
  console.log("net is unavailable, data is " + JSON.stringify(data));
S
shawn_he 已提交
112 113 114 115 116 117 118 119 120
}));

// Register an observer for network status changes.
conn.register((err, data) => {
});

// Unregister the observer for network status changes.
conn.unregister((err, data) => {
});
S
shawn_he 已提交
121 122
```

S
shawn_he 已提交
123
## Obtaining the List of All Registered Networks
S
shawn_he 已提交
124

S
shawn_he 已提交
125
### How to Develop
S
shawn_he 已提交
126 127 128 129 130 131

1. Import the connection namespace from **@ohos.net.connection.d.ts**.

2. Call **getAllNets** to obtain the list of all connected networks.

```js
S
shawn_he 已提交
132
// Import the connection namespace.
S
shawn_he 已提交
133 134 135 136 137 138 139 140 141 142
import connection from '@ohos.net.connection'

// Obtain the list of all connected networks.
connection.getAllNets((err, data) => {
  console.log(JSON.stringify(err));
  console.log(JSON.stringify(data));
  if (data) {
    this.netList = data;
  }
})
S
shawn_he 已提交
143 144
```

S
shawn_he 已提交
145
## Querying Network Capability Information and Connection Information of Specified Data Network
S
shawn_he 已提交
146

S
shawn_he 已提交
147
### How to Develop
S
shawn_he 已提交
148 149 150 151 152 153 154 155 156 157

1. Import the connection namespace from **@ohos.net.connection.d.ts**.

2. Call **getDefaultNet** to obtain the default data network via **NetHandle** or call **getAllNets** to obtain the list of all connected networks via **Array\<NetHandle>**.

3. Call **getNetCapabilities** to obtain the network capability information of the data network specified by **NetHandle**. The capability information includes information such as the network type (cellular, Wi-Fi, or Ethernet network) and the specific network capabilities.

4. Call **getConnectionProperties** to obtain the connection information of the data network specified by **NetHandle**.

```js
S
shawn_he 已提交
158
// Import the connection namespace.
S
shawn_he 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
import connection from '@ohos.net.connection'

// Call getDefaultNet to obtain the default data network specified by **NetHandle**.
connection.getDefaultNet((err, data) => {
  console.log(JSON.stringify(err));
  console.log(JSON.stringify(data));
  if (data) {
    this.netHandle = data;
  }
})

// Obtain the network capability information of the data network specified by **NetHandle**. The capability information includes information such as the network type and specific network capabilities.
connection.getNetCapabilities(this.netHandle, (err, data) => {
  console.log(JSON.stringify(err));

  // Obtain the network type via bearerTypes.
  for (let item of data.bearerTypes) {
    if (item == 0) {
      // Cellular network
      console.log(JSON.stringify("BEARER_CELLULAR"));
    } else if (item == 1) {
      // Wi-Fi network
      console.log(JSON.stringify("BEARER_WIFI"));
    } else if (item == 3) {
      // Ethernet network
      console.log(JSON.stringify("BEARER_ETHERNET"));
    }
  }

  // Obtain the specific network capabilities via networkCap.
  for (let item of data.networkCap) {
    if (item == 0) {
      // The network can connect to the carrier's Multimedia Messaging Service Center (MMSC) to send and receive multimedia messages.
      console.log(JSON.stringify("NET_CAPABILITY_MMS"));
    } else if (item == 11) {
      // The network traffic is not metered.
      console.log(JSON.stringify("NET_CAPABILITY_NOT_METERED"));
    } else if (item == 12) {
      // The network has the Internet access capability, which is set by the network provider.
      console.log(JSON.stringify("NET_CAPABILITY_INTERNET"));
    } else if (item == 15) {
      // The network does not use a Virtual Private Network (VPN).
      console.log(JSON.stringify("NET_CAPABILITY_NOT_VPN"));
    } else if (item == 16) {
      // The Internet access capability of the network is successfully verified by the connection management module.
      console.log(JSON.stringify("NET_CAPABILITY_VALIDATED"));
    }
  }
})

// Obtain the connection information of the data network specified by NetHandle. Connection information includes link and route information.
connection.getConnectionProperties(this.netHandle, (err, data) => {
  console.log(JSON.stringify(err));
  console.log(JSON.stringify(data));
})

// Call getAllNets to obtain the list of all connected networks via Array<NetHandle>.
connection.getAllNets((err, data) => {
  console.log(JSON.stringify(err));
  console.log(JSON.stringify(data));
  if (data) {
    this.netList = data;
  }
})

for (let item of this.netList) {
  // Obtain the network capability information of the network specified by each netHandle on the network list cyclically.
  connection.getNetCapabilities(item, (err, data) => {
    console.log(JSON.stringify(err));
    console.log(JSON.stringify(data));
  })

  // Obtain the connection information of the network specified by each netHandle on the network list cyclically.
  connection.getConnectionProperties(item, (err, data) => {
    console.log(JSON.stringify(err));
    console.log(JSON.stringify(data));
  })
}
S
shawn_he 已提交
237 238
```

S
shawn_he 已提交
239
## Resolving the domain name of a network to obtain all IP addresses
S
shawn_he 已提交
240

S
shawn_he 已提交
241
### How to Develop
S
shawn_he 已提交
242 243 244 245 246 247 248

1. Import the connection namespace from **@ohos.net.connection.d.ts**.

2. Call **getAddressesByName** to use the default network to resolve the host name to obtain the list of all IP addresses.

```js
    // Import the connection namespace.
S
shawn_he 已提交
249
import connection from '@ohos.net.connection'
S
shawn_he 已提交
250

S
shawn_he 已提交
251 252 253 254 255
// Use the default network to resolve the host name to obtain the list of all IP addresses.
connection.getAddressesByName(this.host, (err, data) => {
  console.log(JSON.stringify(err));
  console.log(JSON.stringify(data));
})
S
shawn_he 已提交
256
```