diff --git a/en/application-dev/reference/apis/js-apis-wifi.md b/en/application-dev/reference/apis/js-apis-wifi.md index 9eb6f28a9880ada20a1a5c6bce2bac006a6efd5f..76f27aa775302ff5d313d17f664401b6364e25b9 100644 --- a/en/application-dev/reference/apis/js-apis-wifi.md +++ b/en/application-dev/reference/apis/js-apis-wifi.md @@ -31,6 +31,17 @@ Enables WLAN. | -------- | -------- | | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| +**Example** + +```js +import wifi from '@ohos.wifi'; + +try { + wifi.enableWifi(); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## wifi.disableWifi @@ -50,6 +61,18 @@ Disables WLAN. | -------- | -------- | | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| +**Example** + +```js +import wifi from '@ohos.wifi'; + +try { + wifi.disableWifi(); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} + +``` ## wifi.isWifiActive @@ -67,6 +90,18 @@ Checks whether WLAN is enabled. | -------- | -------- | | boolean | Returns **true** if WLAN is enabled; returns **false** otherwise.| +**Example** + +```js +import wifi from '@ohos.wifi'; + +try { + let isActivate = wifi.isActivate(); + console.info("isActivate:" + isActivate); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## wifi.scan @@ -84,6 +119,17 @@ Starts a scan for WLAN. | -------- | -------- | | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| +**Example** + +```js +import wifi from '@ohos.wifi'; + +try { + wifi.scan(); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## wifi.getScanInfos @@ -119,46 +165,47 @@ Obtains the scan result. This API uses an asynchronous callback to return the re | callback | AsyncCallback< Array<[WifiScanInfo](#wifiscaninfo)>> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is the detected hotspots. Otherwise, **err** is a non-zero value and **data** is empty.| **Example** - ```js - import wifi from '@ohos.wifi'; - - wifi.getScanInfos((err, result) => { - if (err) { - console.error("get scan info error"); - return; - } - - var len = Object.keys(result).length; - console.log("wifi received scan info: " + len); - for (var i = 0; i < len; ++i) { - console.info("ssid: " + result[i].ssid); - console.info("bssid: " + result[i].bssid); - console.info("capabilities: " + result[i].capabilities); - console.info("securityType: " + result[i].securityType); - console.info("rssi: " + result[i].rssi); - console.info("band: " + result[i].band); - console.info("frequency: " + result[i].frequency); - console.info("channelWidth: " + result[i].channelWidth); - console.info("timestamp: " + result[i].timestamp); - } - }); - - wifi.getScanInfos().then(result => { - var len = Object.keys(result).length; - console.log("wifi received scan info: " + len); - for (var i = 0; i < len; ++i) { - console.info("ssid: " + result[i].ssid); - console.info("bssid: " + result[i].bssid); - console.info("capabilities: " + result[i].capabilities); - console.info("securityType: " + result[i].securityType); - console.info("rssi: " + result[i].rssi); - console.info("band: " + result[i].band); - console.info("frequency: " + result[i].frequency); - console.info("channelWidth: " + result[i].channelWidth); - console.info("timestamp: " + result[i].timestamp); - } - }); - ``` + +```js +import wifi from '@ohos.wifi'; + +wifi.getScanInfos((err, result) => { + if (err) { + console.error("get scan info error"); + return; + } + + var len = Object.keys(result).length; + console.log("wifi received scan info: " + len); + for (var i = 0; i < len; ++i) { + console.info("ssid: " + result[i].ssid); + console.info("bssid: " + result[i].bssid); + console.info("capabilities: " + result[i].capabilities); + console.info("securityType: " + result[i].securityType); + console.info("rssi: " + result[i].rssi); + console.info("band: " + result[i].band); + console.info("frequency: " + result[i].frequency); + console.info("channelWidth: " + result[i].channelWidth); + console.info("timestamp: " + result[i].timestamp); + } +}); + +wifi.getScanInfos().then(result => { + var len = Object.keys(result).length; + console.log("wifi received scan info: " + len); + for (var i = 0; i < len; ++i) { + console.info("ssid: " + result[i].ssid); + console.info("bssid: " + result[i].bssid); + console.info("capabilities: " + result[i].capabilities); + console.info("securityType: " + result[i].securityType); + console.info("rssi: " + result[i].rssi); + console.info("band: " + result[i].band); + console.info("frequency: " + result[i].frequency); + console.info("channelWidth: " + result[i].channelWidth); + console.info("timestamp: " + result[i].timestamp); + } +}); +``` ## WifiScanInfo @@ -238,6 +285,25 @@ Adds network configuration. This API uses a promise to return the result. | -------- | -------- | | Promise<number> | Promise used to return the WLAN configuration ID. If **-1** is returned, the network configuration fails to be added.| + **Example** + +```js +import wifi from '@ohos.wifi'; + +try { + let config = { + ssid : "****", + preSharedKey : "****", + securityType : 0 + } + wifi.addDeviceConfig(config).then(result => { + console.info("result:" + JSON.stringify(result)); + }); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` + ## WifiDeviceConfig Represents the WLAN configuration. @@ -252,13 +318,13 @@ Represents the WLAN configuration. | preSharedKey | string | Yes| No| PSK of the hotspot.| | isHiddenSsid | boolean | Yes| No| Whether the network is hidden.| | securityType | [WifiSecurityType](#wifisecuritytype) | Yes| No| Security type.| -| creatorUid | number | Yes| No| ID of the creator.
**System API**: This is a system API.| -| disableReason | number | Yes| No| Reason for disabling WLAN.
**System API**: This is a system API.| -| netId | number | Yes| No| Network ID.
**System API**: This is a system API.| -| randomMacType | number | Yes| No| Random MAC type.
**System API**: This is a system API.| -| randomMacAddr | string | Yes| No| Random MAC address.
**System API**: This is a system API.| -| ipType | [IpType](#iptype7) | Yes| No| IP address type.
**System API**: This is a system API.| -| staticIp | [IpConfig](#ipconfig7) | Yes| No| Static IP address configuration.
**System API**: This is a system API.| +| creatorUid | number | Yes| No| ID of the creator.
**System API**: This is a system API.| +| disableReason | number | Yes| No| Reason for disabling WLAN.
**System API**: This is a system API.| +| netId | number | Yes| No| Network ID.
**System API**: This is a system API.| +| randomMacType | number | Yes| No| Random MAC type.
**System API**: This is a system API.| +| randomMacAddr | string | Yes| No| Random MAC address.
**System API**: This is a system API.| +| ipType | [IpType](#iptype7) | Yes| No| IP address type.
**System API**: This is a system API.| +| staticIp | [IpConfig](#ipconfig7) | Yes| No| Static IP address configuration.
**System API**: This is a system API.| ## IpType7+ @@ -312,7 +378,24 @@ Adds network configuration. This API uses an asynchronous callback to return the | config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration to add.| | callback | AsyncCallback<number> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is the network configuration ID. If **data** is **-1**, the operation has failed. If **err** is not **0**, an error has occurred.| +**Example** + +```js +import wifi from '@ohos.wifi'; +try { + let config = { + ssid : "****", + preSharedKey : "****", + securityType : 0 + } + wifi.addDeviceConfig(config,(error,result) => { + console.info("result:" + JSON.stringify(result)); + }); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## wifi.addUntrustedConfig7+ addUntrustedConfig(config: WifiDeviceConfig): Promise<boolean> @@ -335,6 +418,23 @@ Adds the configuration of an untrusted network. This API uses a promise to retur | -------- | -------- | | Promise<boolean> | Promise used to return the result. If the operation is successful, **true** is returned; otherwise, **false** is returned.| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + let config = { + ssid : "****", + preSharedKey : "****", + securityType : 0 + } + wifi.addUntrustedConfig(config).then(result => { + console.info("result:" + JSON.stringify(result)); + }); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## wifi.addUntrustedConfig7+ @@ -353,6 +453,23 @@ Adds the configuration of an untrusted network. This API uses an asynchronous ca | config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration to add.| | callback | AsyncCallback<boolean> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is **true**. If the operation fails, **data** is **false**. If **err** is not **0**, an error has occurred.| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + let config = { + ssid : "****", + preSharedKey : "****", + securityType : 0 + } + wifi.addUntrustedConfig(config,(error,result) => { + console.info("result:" + JSON.stringify(result)); + }); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## wifi.removeUntrustedConfig7+ @@ -376,6 +493,21 @@ Removes the configuration of an untrusted network. This API uses a promise to re | -------- | -------- | | Promise<boolean> | Promise used to return the result. If the operation is successful, **true** is returned; otherwise, **false** is returned.| +**Example** + +```js +import wifi from '@ohos.wifi'; + +try { + let networkId = 0; + wifi.removeUntrustedConfig(networkId).then(result => { + console.info("result:" + JSON.stringify(result)); + }); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` + ## wifi.removeUntrustedConfig7+ @@ -394,6 +526,19 @@ Removes the configuration of an untrusted network. This API uses an asynchronous | config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration to remove.| | callback | AsyncCallback<boolean> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is **true**. If the operation fails, **data** is **false**. If **err** is not **0**, an error has occurred.| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + let networkId = 0; + wifi.removeUntrustedConfig(networkId,(error,result) => { + console.info("result:" + JSON.stringify(result)); + }); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## wifi.connectToNetwork @@ -419,6 +564,18 @@ Connects to the specified network. | -------- | -------- | | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| +**Example** + +```js +import wifi from '@ohos.wifi'; + +try { + let networkId = 0; + wifi.connectToNetwork(networkId); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## wifi.connectToDevice @@ -445,6 +602,22 @@ Connects to the specified network. | -------- | -------- | | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + let config = { + ssid : "****", + preSharedKey : "****", + securityType : 3 + } + wifi.connectToDevice(config); + +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## wifi.disconnect @@ -465,6 +638,16 @@ Disconnects the network. | -------- | -------- | | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + wifi.disconnect(); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## wifi.getSignalLevel @@ -489,6 +672,20 @@ Obtains the WLAN signal level. | -------- | -------- | | number | Signal level obtained. The value range is [0, 4].| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + let rssi = 0; + let band = 0; + let level = wifi.getSignalLevel(rssi,band); + console.info("lelvel:" + JSON.stringify(lelvel)); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} + +``` ## wifi.getLinkedInfo @@ -524,23 +721,23 @@ Obtains WLAN connection information. This API uses an asynchronous callback to r | callback | AsyncCallback<[WifiLinkedInfo](#wifilinkedinfo)> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is the WLAN connection information obtained. If **err** is not **0**, an error has occurred.| **Example** - ```js - import wifi from '@ohos.wifi'; - - wifi.getLinkedInfo((err, data) => { - if (err) { - console.error("get linked info error"); - return; - } - console.info("get wifi linked info: " + JSON.stringify(data)); - }); - - wifi.getLinkedInfo().then(data => { - console.info("get wifi linked info: " + JSON.stringify(data)); - }).catch(error => { - console.info("get linked info error"); - }); - ``` +```js +import wifi from '@ohos.wifi'; + +wifi.getLinkedInfo((err, data) => { + if (err) { + console.error("get linked info error"); + return; + } + console.info("get wifi linked info: " + JSON.stringify(data)); +}); + +wifi.getLinkedInfo().then(data => { + console.info("get wifi linked info: " + JSON.stringify(data)); +}).catch(error => { + console.info("get linked info error"); +}); +``` ## WifiLinkedInfo @@ -553,18 +750,18 @@ Represents the WLAN connection information. | -------- | -------- | -------- | -------- | -------- | | ssid | string | Yes| No| SSID of the hotspot, in UTF-8 format.| | bssid | string | Yes| No| BSSID of the hotspot.| -| networkId | number | Yes| No| Network configuration ID.
**System API**: This is a system API.| +| networkId | number | Yes| No| Network configuration ID.
**System API**: This is a system API.| | rssi | number | Yes| No| RSSI of the hotspot, in dBm.| | band | number | Yes| No| Frequency band of the WLAN AP.| | linkSpeed | number | Yes| No| Speed of the WLAN AP.| | frequency | number | Yes| No| Frequency of the WLAN AP.| | isHidden | boolean | Yes| No| Whether to hide the WLAN AP.| | isRestricted | boolean | Yes| No| Whether to restrict data volume at the WLAN AP.| -| chload | number | Yes| No| Channel load. A larger value indicates a higher load.
**System API**: This is a system API.| -| snr | number | Yes| No| Signal-to-noise ratio (SNR).
**System API**: This is a system API.| +| chload | number | Yes| No| Channel load. A larger value indicates a higher load.
**System API**: This is a system API.| +| snr | number | Yes| No| Signal-to-noise ratio (SNR).
**System API**: This is a system API.| | macAddress | string | Yes| No| MAC address of the device.| | ipAddress | number | Yes| No| IP address of the device that sets up the WLAN connection.| -| suppState | [SuppState](#suppstate) | Yes| No| Supplicant state.
**System API**: This is a system API.| +| suppState | [SuppState](#suppstate) | Yes| No| Supplicant state.
**System API**: This is a system API.| | connState | [ConnState](#connstate) | Yes| No| WLAN connection state.| @@ -684,6 +881,19 @@ Checks whether the device supports the specified WLAN feature. | -------- | -------- | | boolean | Returns **true** if the feature is supported; returns **false** otherwise.| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + let featureId = 0; + let ret = wifi.isFeatureSupported(featureId); + console.info("isFeatureSupported:" + ret); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} + +``` ## wifi.getDeviceMacAddress7+ @@ -703,6 +913,18 @@ Obtains the device MAC address. | -------- | -------- | | string[] | MAC address obtained.| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + let ret = wifi.getDeviceMacAddress(); + console.info("deviceMacAddress:" + JSON.stringify(ret)); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} + +``` ## wifi.getIpInfo7+ @@ -720,6 +942,17 @@ Obtains IP information. | -------- | -------- | | [IpInfo](#ipinfo7) | IP information obtained.| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + let info = wifi.getIpInfo(); + console.info("info:" + JSON.stringify(info)); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## IpInfo7+ @@ -754,6 +987,17 @@ Obtains the country code. | -------- | -------- | | string | Country code obtained.| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + let code = wifi.getCountryCode(); + console.info("code:" + code); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## wifi.reassociate7+ @@ -773,6 +1017,16 @@ Re-associates with the network. | -------- | -------- | | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + wifi.reassociate(); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## wifi.reconnect7+ @@ -792,6 +1046,16 @@ Reconnects to the network. | -------- | -------- | | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + wifi.reconnect(); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## wifi.getDeviceConfigs7+ @@ -811,6 +1075,17 @@ Obtains network configuration. | -------- | -------- | |  Array<[WifiDeviceConfig](#wifideviceconfig)> | Array of network configuration obtained.| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + let configs = wifi.getDeviceConfigs(); + console.info("configs:" + JSON.stringify(configs)); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## wifi.updateNetwork7+ @@ -836,6 +1111,22 @@ Updates network configuration. | -------- | -------- | | number | ID of the updated network configuration. The value **-1** indicates that the operation has failed.| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + let config = { + ssid : "****", + preSharedKey : "****", + securityType : 3 + } + let ret = wifi.updateNetwork(config); + console.error("ret:" + ret); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## wifi.disableNetwork7+ @@ -861,6 +1152,17 @@ Disables network configuration. | -------- | -------- | | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + let netId = 0; + wifi.disableNetwork(netId); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## wifi.removeAllNetwork7+ @@ -880,6 +1182,16 @@ Removes the configuration of all networks. | -------- | -------- | | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + wifi.removeAllNetwork(); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## wifi.removeDevice7+ @@ -905,6 +1217,17 @@ Removes the specified network configuration. | -------- | -------- | | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + let id = 0; + wifi.removeDevice(id); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## wifi.enableHotspot7+ @@ -924,6 +1247,16 @@ Enables this hotspot. | -------- | -------- | | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + wifi.enableHotspot(); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## wifi.disableHotspot7+ @@ -943,6 +1276,16 @@ Disables this hotspot. | -------- | -------- | | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| +**Example** +```js +import wifi from '@ohos.wifiManager'; + +try { + wifiManager.disableHotspot(); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## wifi.isHotspotDualBandSupported7+ @@ -962,6 +1305,17 @@ Checks whether the hotspot supports dual band. | -------- | -------- | | boolean | Returns **true** if the feature is supported; returns **false** otherwise.| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + let ret = wifi.isHotspotDualBandSupported(); + console.info("result:" + ret); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## wifi.isHotspotActive7+ @@ -981,6 +1335,17 @@ Checks whether this hotspot is active. | -------- | -------- | | boolean | Returns **true** if the hotspot is active; returns **false** otherwise.| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + let ret = wifi.isHotspotActive(); + console.info("result:" + ret); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## wifi.setHotspotConfig7+ @@ -1006,6 +1371,25 @@ Sets hotspot configuration. | -------- | -------- | | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + let config = { + ssid: "****", + securityType: 3, + band: 0, + channel: 0, + preSharedKey: "****", + maxConn: 0 + } + let ret = wifi.setHotspotConfig(); + console.info("result:" + ret); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## HotspotConfig7+ @@ -1042,6 +1426,17 @@ obtains hotspot configuration. | -------- | -------- | | [HotspotConfig](#hotspotconfig7) | Hotspot configuration obtained.| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + let config = wifi.getHotspotConfig(); + console.info("result:" + JSON.stringify(config)); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## wifi.getStations7+ @@ -1061,6 +1456,17 @@ Obtains information about the connected stations. | -------- | -------- | |  Array<[StationInfo](#stationinfo7)> | Connected stations obtained.| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + let stations = wifi.getStations(); + console.info("result:" + JSON.stringify(stations)); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## StationInfo7+ @@ -1136,6 +1542,22 @@ Obtains P2P link information. This API uses an asynchronous callback to return t | -------- | -------- | -------- | -------- | | callback | AsyncCallback<[WifiP2pLinkedInfo](#wifip2plinkedinfo8)> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is the P2P link information. If **err** is not **0**, an error has occurred.| +**Example** +```js +import wifi from '@ohos.wifi'; + +wifi.getP2pLinkedInfo((err, data) => { + if (err) { + console.error("get p2p linked info error"); + return; + } + console.info("get wifi p2p linked info: " + JSON.stringify(data)); +}); + +wifi.getP2pLinkedInfo().then(data => { + console.info("get wifi p2p linked info: " + JSON.stringify(data)); +}); +``` ## wifi.getCurrentGroup8+ @@ -1170,6 +1592,22 @@ Obtains the current P2P group information. This API uses an asynchronous callbac | -------- | -------- | -------- | -------- | | callback | AsyncCallback<[WifiP2pGroupInfo](#wifip2pgroupinfo8)> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is the group information obtained. If **err** is not **0**, an error has occurred.| +**Example** +```js +import wifi from '@ohos.wifi'; + +wifi.getCurrentGroup((err, data) => { + if (err) { + console.error("get current P2P group error"); + return; + } + console.info("get current P2P group: " + JSON.stringify(data)); +}); + +wifi.getCurrentGroup().then(data => { + console.info("get current P2P group: " + JSON.stringify(data)); +}); +``` ## wifi.getP2pPeerDevices8+ @@ -1204,6 +1642,22 @@ Obtains the peer device list in the P2P connection. This API uses an asynchronou | -------- | -------- | -------- | -------- | | callback | AsyncCallback<[WifiP2pDevice[]](#wifip2pdevice8)> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is the peer device list obtained. If **err** is not **0**, an error has occurred.| +**Example** +```js +import wifi from '@ohos.wifiManager'; + +wifi.getP2pPeerDevices((err, data) => { + if (err) { + console.error("get P2P peer devices error"); + return; + } + console.info("get P2P peer devices: " + JSON.stringify(data)); +}); + +wifi.getP2pPeerDevices().then(data => { + console.info("get P2P peer devices: " + JSON.stringify(data)); +}); +``` ## WifiP2pDevice8+ @@ -1257,6 +1711,24 @@ Creates a P2P group. | -------- | -------- | | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + let config = { + deviceAddress: "****", + netId: 0, + passphrase: "*****", + groupName: "****", + goBand: 0 + } + wifi.createGroup(config); + +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## WifiP2PConfig8+ @@ -1302,6 +1774,16 @@ Removes this P2P group. | -------- | -------- | | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + wifi.removeGroup(); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## wifi.p2pConnect8+ @@ -1328,71 +1810,71 @@ Sets up a P2P connection. **Example** - ```js - import wifi from '@ohos.wifi'; - - var recvP2pConnectionChangeFunc = result => { - console.info("p2p connection change receive event: " + JSON.stringify(result)); - wifi.getP2pLinkedInfo((err, data) => { - if (err) { - console.error('failed to get getP2pLinkedInfo: ' + JSON.stringify(err)); - return; - } - console.info("get getP2pLinkedInfo: " + JSON.stringify(data)); - }); - } - wifi.on("p2pConnectionChange", recvP2pConnectionChangeFunc); - - var recvP2pDeviceChangeFunc = result => { - console.info("p2p device change receive event: " + JSON.stringify(result)); - } - wifi.on("p2pDeviceChange", recvP2pDeviceChangeFunc); - - var recvP2pPeerDeviceChangeFunc = result => { - console.info("p2p peer device change receive event: " + JSON.stringify(result)); - wifi.getP2pPeerDevices((err, data) => { - if (err) { - console.error('failed to get peer devices: ' + JSON.stringify(err)); - return; - } - console.info("get peer devices: " + JSON.stringify(data)); - var len = Object.keys(data).length; - for (var i = 0; i < len; ++i) { - if (data[i].deviceName === "my_test_device") { - console.info("p2p connect to test device: " + data[i].deviceAddress); - var config = { - "deviceAddress":data[i].deviceAddress, - "netId":-2, - "passphrase":"", - "groupName":"", - "goBand":0, - } - wifi.p2pConnect(config); - } - } - }); - } - wifi.on("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc); - - var recvP2pPersistentGroupChangeFunc = () => { - console.info("p2p persistent group change receive event"); - - wifi.getCurrentGroup((err, data) => { - if (err) { - console.error('failed to get current group: ' + JSON.stringify(err)); - return; - } - console.info("get current group: " + JSON.stringify(data)); - }); - } - wifi.on("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc); - - setTimeout(function() {wifi.off("p2pConnectionChange", recvP2pConnectionChangeFunc);}, 125 * 1000); - setTimeout(function() {wifi.off("p2pDeviceChange", recvP2pDeviceChangeFunc);}, 125 * 1000); - setTimeout(function() {wifi.off("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc);}, 125 * 1000); - setTimeout(function() {wifi.off("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc);}, 125 * 1000); - console.info("start discover devices -> " + wifi.startDiscoverDevices()); - ``` +```js +import wifi from '@ohos.wifi'; + +var recvP2pConnectionChangeFunc = result => { + console.info("p2p connection change receive event: " + JSON.stringify(result)); + wifi.getP2pLinkedInfo((err, data) => { + if (err) { + console.error('failed to get getP2pLinkedInfo: ' + JSON.stringify(err)); + return; + } + console.info("get getP2pLinkedInfo: " + JSON.stringify(data)); + }); +} +wifi.on("p2pConnectionChange", recvP2pConnectionChangeFunc); + +var recvP2pDeviceChangeFunc = result => { + console.info("p2p device change receive event: " + JSON.stringify(result)); +} +wifi.on("p2pDeviceChange", recvP2pDeviceChangeFunc); + +var recvP2pPeerDeviceChangeFunc = result => { + console.info("p2p peer device change receive event: " + JSON.stringify(result)); + wifi.getP2pPeerDevices((err, data) => { + if (err) { + console.error('failed to get peer devices: ' + JSON.stringify(err)); + return; + } + console.info("get peer devices: " + JSON.stringify(data)); + var len = Object.keys(data).length; + for (var i = 0; i < len; ++i) { + if (data[i].deviceName === "my_test_device") { + console.info("p2p connect to test device: " + data[i].deviceAddress); + var config = { + "deviceAddress":data[i].deviceAddress, + "netId":-2, + "passphrase":"", + "groupName":"", + "goBand":0, + } + wifi.p2pConnect(config); + } + } + }); +} +wifi.on("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc); + +var recvP2pPersistentGroupChangeFunc = () => { + console.info("p2p persistent group change receive event"); + + wifi.getCurrentGroup((err, data) => { + if (err) { + console.error('failed to get current group: ' + JSON.stringify(err)); + return; + } + console.info("get current group: " + JSON.stringify(data)); + }); +} +wifi.on("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc); + +setTimeout(function() {wifi.off("p2pConnectionChange", recvP2pConnectionChangeFunc);}, 125 * 1000); +setTimeout(function() {wifi.off("p2pDeviceChange", recvP2pDeviceChangeFunc);}, 125 * 1000); +setTimeout(function() {wifi.off("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc);}, 125 * 1000); +setTimeout(function() {wifi.off("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc);}, 125 * 1000); +console.info("start discover devices -> " + wifi.startDiscoverDevices()); +``` ## wifi.p2pCancelConnect8+ @@ -1410,6 +1892,16 @@ Cancels this P2P connection. | -------- | -------- | | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + wifi.p2pCancelConnect(); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## wifi.startDiscoverDevices8+ @@ -1427,6 +1919,16 @@ Starts to discover devices. | -------- | -------- | | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + wifi.startDiscoverDevices(); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## wifi.stopDiscoverDevices8+ @@ -1444,6 +1946,16 @@ Stops discovering devices. | -------- | -------- | | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + wifi.stopDiscoverDevices(); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## wifi.deletePersistentGroup8+ @@ -1470,6 +1982,17 @@ Deletes a persistent group. | -------- | -------- | | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + let netId = 0; + wifi.deletePersistentGroup(netId); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## WifiP2pGroupInfo8+ @@ -1514,6 +2037,17 @@ Sets the device name. | -------- | -------- | | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| +**Example** +```js +import wifi from '@ohos.wifi'; + +try { + let name = "****"; + wifi.setDeviceName(netId); +}catch(error){ + console.error("failed:" + JSON.stringify(error)); +} +``` ## wifi.on('wifiStateChange')7+ @@ -1560,19 +2094,19 @@ Unregisters the WLAN state change events. | callback | Callback<number> | No| Callback for the WLAN state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| **Example** - ```js - import wifi from '@ohos.wifi'; - - var recvPowerNotifyFunc = result => { - console.info("Receive power state change event: " + result); - } - - // Register an event. - wifi.on("wifiStateChange", recvPowerNotifyFunc); - - // Unregister an event. - wifi.off("wifiStateChange", recvPowerNotifyFunc); - ``` +```js +import wifi from '@ohos.wifi'; + +var recvPowerNotifyFunc = result => { + console.info("Receive power state change event: " + result); +} + +// Register an event. +wifi.on("wifiStateChange", recvPowerNotifyFunc); + +// Unregister an event. +wifi.off("wifiStateChange", recvPowerNotifyFunc); +``` ## wifi.on('wifiConnectionChange')7+ @@ -1617,6 +2151,20 @@ Unregisters the WLAN connection state change events. | type | string | Yes| Event type. The value is **wifiConnectionChange**.| | callback | Callback<number> | No| Callback for the WLAN connection state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| +**Example** +```js +import wifi from '@ohos.wifi'; + +var recvWifiConnectionChangeFunc = result => { + console.info("Receive wifi connection change event: " + result); +} + +// Register an event. +wifi.on("wifiConnectionChange", recvWifiConnectionChangeFunc); + +// Unregister an event. +wifi.off("wifiConnectionChange", recvWifiConnectionChangeFunc); +``` ## wifi.on('wifiScanStateChange')7+ @@ -1660,6 +2208,20 @@ Unregisters the WLAN scan state change events. | type | string | Yes| Event type. The value is **wifiScanStateChange**.| | callback | Callback<number> | No| Callback for the WLAN scan state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| +**Example** +```js +import wifi from '@ohos.wifi'; + +var recvWifiScanStateChangeFunc = result => { + console.info("Receive Wifi scan state change event: " + result); +} + +// Register an event. +wifi.on("wifiScanStateChange", recvWifiScanStateChangeFunc); + +// Unregister an event. +wifi.off("wifiScanStateChange", recvWifiScanStateChangeFunc); +``` ## wifi.on('wifiRssiChange')7+ @@ -1696,7 +2258,20 @@ Unregisters the RSSI change events. | type | string | Yes| Event type. The value is **wifiRssiChange**.| | callback | Callback<number> | No| Callback for the RSSI. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| +**Example** +```js +import wifi from '@ohos.wifi'; + +var recvWifiRssiChangeFunc = result => { + console.info("Receive wifi rssi change event: " + result); +} + +// Register an event. +wifi.on("wifiRssiChange", recvWifiRssiChangeFunc); +// Unregister an event. +wifi.off("wifiRssiChange", recvWifiRssiChangeFunc); +``` ## wifi.on('hotspotStateChange')7+ on(type: "hotspotStateChange", callback: Callback<number>): void @@ -1723,6 +2298,20 @@ Registers the hotspot state change events. | 2 | Activating| | 3 | Deactivating| +**Example** +```js +import wifi from '@ohos.wifi'; + +var recvHotspotStateChangeFunc = result => { + console.info("Receive hotspot state change event: " + result); +} + +// Register an event. +wifi.on("hotspotStateChange", recvHotspotStateChangeFunc); + +// Unregister an event. +wifi.off("hotspotStateChange", recvHotspotStateChangeFunc); +``` ## wifi.off('hotspotStateChange')7+ @@ -1786,6 +2375,20 @@ Unregisters the P2P state change events. | type | string | Yes| Event type. The value is **p2pStateChange**.| | callback | Callback<number> | No| Callback for the P2P state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| +**Example** +```js +import wifi from '@ohos.wifi'; + +var recvP2pStateChangeFunc = result => { + console.info("Receive p2p state change event: " + result); +} + +// Register an event. +wifi.on("p2pStateChange", recvP2pStateChangeFunc); + +// Unregister an event. +wifi.off("p2pStateChange", recvP2pStateChangeFunc); +``` ## wifi.on('p2pConnectionChange')8+ @@ -1822,6 +2425,20 @@ Unregisters the P2P connection state change events. | type | string | Yes| Event type. The value is **p2pConnectionChange**.| | callback | Callback<[WifiP2pLinkedInfo](#wifip2plinkedinfo8)> | No| Callback for the P2P connection state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| +**Example** +```js +import wifi from '@ohos.wifi'; + +var recvP2pConnectionChangeFunc = result => { + console.info("Receive p2p connection change event: " + result); +} + +// Register an event. +wifi.on("p2pConnectionChange", recvP2pConnectionChangeFunc); + +// Unregister an event. +wifi.off("p2pConnectionChange", recvP2pConnectionChangeFunc); +``` ## wifi.on('p2pDeviceChange')8+ @@ -1858,6 +2475,20 @@ Unregisters the P2P device state change events. | type | string | Yes| Event type. The value is **p2pDeviceChange**.| | callback | Callback<[WifiP2pDevice](#wifip2pdevice8)> | No| Callback for the P2P device state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| +**Example** +```js +import wifi from '@ohos.wifi'; + +var recvP2pDeviceChangeFunc = result => { + console.info("Receive recv p2p device change event: " + result); +} + +// Register an event. +wifi.on("p2pDeviceChange", recvP2pDeviceChangeFunc); + +// Unregister an event. +wifi.off("p2pDeviceChange", recvP2pDeviceChangeFunc); +``` ## wifi.on('p2pPeerDeviceChange')8+ @@ -1894,6 +2525,20 @@ Unregisters the P2P peer device state change events. | type | string | Yes| Event type. The value is **p2pPeerDeviceChange**.| | callback | Callback<[WifiP2pDevice[]](#wifip2pdevice8)> | No| Callback for the peer device state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| +**Example** +```js +import wifi from '@ohos.wifi'; + +var recvP2pPeerDeviceChangeFunc = result => { + console.info("Receive recv p2p peer device change event: " + result); +} + +// Register an event. +wifi.on("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc); + +// Unregister an event. +wifi.off("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc); +``` ## wifi.on('p2pPersistentGroupChange')8+ @@ -1930,6 +2575,21 @@ Unregisters the P2P persistent group state change events. | type | string | Yes| Event type. The value is **p2pPersistentGroupChange**.| | callback | Callback<void> | No| Callback for the P2P persistent group state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| +**Example** +```js +import wifi from '@ohos.wifi'; + +var recvP2pPersistentGroupChangeFunc = result => { + console.info("Receive recv p2p persistent group change event: " + result); +} + +// Register an event. +wifi.on("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc); + +// Unregister an event. +wifi.off("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc); + +``` ## wifi.on('p2pDiscoveryChange')8+ @@ -1972,3 +2632,18 @@ Unregisters the P2P device discovery state change events. | -------- | -------- | -------- | -------- | | type | string | Yes| Event type. The value is **p2pDiscoveryChange**.| | callback | Callback<number> | No| Callback for the P2P device discovery state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| + +**Example** +```js +import wifi from '@ohos.wifi'; + +var recvP2pDiscoveryChangeFunc = result => { + console.info("Receive recv p2p discovery change event: " + result); +} + +// Register an event. +wifi.on("p2pDiscoveryChange", recvP2pDiscoveryChangeFunc); + +// Unregister an event. +wifi.off("p2pDiscoveryChange", recvP2pDiscoveryChangeFunc); +``` diff --git a/en/application-dev/reference/apis/js-apis-wifiManager.md b/en/application-dev/reference/apis/js-apis-wifiManager.md index dc9beb94d7fcc13d6c82e805bf8855d537e907bd..6d560d345805826369d8b83cfdcbee53a47b00f5 100644 --- a/en/application-dev/reference/apis/js-apis-wifiManager.md +++ b/en/application-dev/reference/apis/js-apis-wifiManager.md @@ -26,18 +26,29 @@ Enables WLAN. **Return value** - | **Type**| **Description**| - | -------- | -------- | - | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| +| **Type**| **Description**| +| -------- | -------- | +| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| +**Example** + +``` + import wifi from '@ohos.wifiManager'; + + try { + wifiManager.enableWifi(); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` ## wifi.disableWifi9+ @@ -53,18 +64,30 @@ Disables WLAN. **Return value** - | **Type**| **Description**| - | -------- | -------- | - | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| +| **Type**| **Description**| +| -------- | -------- | +| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| +**Example** + +``` + import wifi from '@ohos.wifiManager'; + + try { + wifiManager.disableWifi(); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + ## wifi.isWifiActive9+ isWifiActive(): boolean @@ -77,132 +100,119 @@ Checks whether WLAN is enabled. **Return value** - | **Type**| **Description**| - | -------- | -------- | - | boolean | Returns **true** if WLAN is enabled; returns **false** otherwise.| +| **Type**| **Description**| +| -------- | -------- | +| boolean | Returns **true** if WLAN is enabled; returns **false** otherwise.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| +**Example** + +``` + import wifi from '@ohos.wifiManager'; + + try { + let isActivate = wifiManager.isActivate(); + console.info("isActivate:" + isActivate); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + ## wifi.scan9+ scan(): void Starts a scan for WLAN. -**Required permissions**: **ohos.permission.SET_WIFI_INFO** and **ohos.permission.LOCATION** +**Required permissions**: ohos.permission.SET_WIFI_INFO, ohos.permission.LOCATION, and ohos.permission.APPROXIMATELY_LOCATION **System capability**: SystemCapability.Communication.WiFi.STA **Return value** - | **Type**| **Description**| - | -------- | -------- | - | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| +| **Type**| **Description**| +| -------- | -------- | +| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| -## wifi.getScanResults9+ - -getScanResults(): Promise<Array<WifiScanInfo>> - -Obtains the scan result. This API uses a promise to return the result. - -**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.GET_WIFI_PEERS_MAC (or ohos.permission.LOCATION) - -**System capability**: SystemCapability.Communication.WiFi.STA - -**Return value** - - | **Type**| **Description**| - | -------- | -------- | - | Promise< Array<[WifiScanInfo](#wifiscaninfo)> > | Promise used to return the hotspots detected.| - -**Error codes** +**Example** -For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). +``` + import wifi from '@ohos.wifiManager'; -| **Type**| **Description**| - | -------- | -------- | -| 2501000 | Operation failed.| + try { + wifiManager.scan(); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` -## wifi.getScanResults9+ +## wifi.getScanInfoList9+ -getScanResults(callback: AsyncCallback<Array<WifiScanInfo>>): void +getScanInfoList(): Array<WifiScanInfo>; -Obtains the scan result. This API uses an asynchronous callback to return the result. +Obtains the scan result. -**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.GET_WIFI_PEERS_MAC (or ohos.permission.LOCATION) +**Required permissions**: ohos.permission.GET_WIFI_INFO and (ohos.permission.GET_WIFI_PEERS_MAC or (ohos.permission.LOCATION and ohos.permission.APPROXIMATELY_LOCATION)) **System capability**: SystemCapability.Communication.WiFi.STA -**Parameters** +**Return value** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | callback | AsyncCallback< Array<[WifiScanInfo](#wifiscaninfo)>> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is the detected hotspots. Otherwise, **err** is a non-zero value and **data** is empty.| +| **Type**| **Description**| +| -------- | -------- | +| Array<[WifiScanInfo](#wifiscaninfo)> | Returns the hotspots detected.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| **Example** - ```js - import wifi from '@ohos.wifi'; - - wifi.getScanInfos((err, result) => { - if (err) { - console.error("get scan info error"); - return; - } - - var len = Object.keys(result).length; - console.log("wifi received scan info: " + len); - for (var i = 0; i < len; ++i) { - console.info("ssid: " + result[i].ssid); - console.info("bssid: " + result[i].bssid); - console.info("capabilities: " + result[i].capabilities); - console.info("securityType: " + result[i].securityType); - console.info("rssi: " + result[i].rssi); - console.info("band: " + result[i].band); - console.info("frequency: " + result[i].frequency); - console.info("channelWidth: " + result[i].channelWidth); - console.info("timestamp: " + result[i].timestamp); - } - }); - - wifi.getScanInfos().then(result => { - var len = Object.keys(result).length; - console.log("wifi received scan info: " + len); - for (var i = 0; i < len; ++i) { - console.info("ssid: " + result[i].ssid); - console.info("bssid: " + result[i].bssid); - console.info("capabilities: " + result[i].capabilities); - console.info("securityType: " + result[i].securityType); - console.info("rssi: " + result[i].rssi); - console.info("band: " + result[i].band); - console.info("frequency: " + result[i].frequency); - console.info("channelWidth: " + result[i].channelWidth); - console.info("timestamp: " + result[i].timestamp); - } - }); - ``` +``` + import wifi from '@ohos.wifiManager'; + + try { + let scanInfoList = wifiManager.getScanInfoList(); + console.info("scanInfoList:" + JSON.stringify(scanInfoList)); + let len = Object.keys(result).length; + console.log("wifi received scan info: " + len); + if(len > 0){ + for (var i = 0; i < len; ++i) { + console.info("ssid: " + scanInfoList[i].ssid); + console.info("bssid: " + scanInfoList[i].bssid); + console.info("capabilities: " + scanInfoList[i].capabilities); + console.info("securityType: " + scanInfoList[i].securityType); + console.info("rssi: " + scanInfoList[i].rssi); + console.info("band: " + scanInfoList[i].band); + console.info("frequency: " + scanInfoList[i].frequency); + console.info("channelWidth: " + scanInfoList[i].channelWidth); + console.info("timestamp: " + scanInfoList[i].timestamp); + } + } + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } + +``` ## WifiScanInfo9+ @@ -248,59 +258,65 @@ Enumerates the WLAN security types. | WIFI_SEC_TYPE_WAPI_PSK9+ | 9 | WAPI-PSK.| -## WifiInfoElem9+ +## WifiBandType10+ -Represents a WLAN information element. +Enumerates the Wi-Fi band types. **System capability**: SystemCapability.Communication.WiFi.STA +| **Name**| **Value**| **Description**| +| -------- | -------- | -------- | +| WIFI_BAND_NONE | 0 | Invalid band type| +| WIFI_BAND_2G | 1 | 2.4 GHz| +| WIFI_BAND_5G | 2 | 5 GHz| +| WIFI_BAND_6G | 3 | 6 GHz| +| WIFI_BAND_60G | 4 | 60 GHz| -| **Name**| **Type**| **Readable**| **Writable**| **Description**| -| -------- | -------- | -------- | -------- | -------- | -| eid | number | Yes| No| ID of the information element.| -| content | Uint8Array | Yes| No| Content of the information element.| - - -## WifiChannelWidth9+ +## WifiStandard10+ -Enumerates the WLAN channel widths. +Enumerates the Wi-Fi standards. **System capability**: SystemCapability.Communication.WiFi.STA - | **Name**| **Value**| **Description**| | -------- | -------- | -------- | -| WIDTH_20MHZ | 0 | 20 MHz.| -| WIDTH_40MHZ | 1 | 40 MHz.| -| WIDTH_80MHZ | 2 | 80 MHz.| -| WIDTH_160MHZ | 3 | 160 MHz.| -| WIDTH_80MHZ_PLUS | 4 | 80 MHz+.| -| WIDTH_INVALID | 5 | Invalid value.| +| WIFI_STANDARD_UNDEFINED | 0 | Invalid Wi-Fi standard| +| WIFI_STANDARD_11A | 1 | 802.11a| +| WIFI_STANDARD_11B | 2 | 802.11b| +| WIFI_STANDARD_11G | 3 | 802.11g| +| WIFI_STANDARD_11N | 4 | 802.11n| +| WIFI_STANDARD_11AC | 5 | 802.11ac| +| WIFI_STANDARD_11AX | 6 | 802.11ax| +| WIFI_STANDARD_11AD | 7 | 802.11ad| +## WifiInfoElem9+ -## wifi.getScanResultsSync9+ +Represents a WLAN information element. -getScanResultsSync():  Array<[WifiScanInfo](#wifiscaninfo)> +**System capability**: SystemCapability.Communication.WiFi.STA -Obtains the scan result. This API returns the result synchronously. -**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.GET_WIFI_PEERS_MAC (or ohos.permission.LOCATION) +| **Name**| **Type**| **Readable**| **Writable**| **Description**| +| -------- | -------- | -------- | -------- | -------- | +| eid | number | Yes| No| ID of the information element.| +| content | Uint8Array | Yes| No| Content of the information element.| -**System capability**: SystemCapability.Communication.WiFi.STA -**Return value** +## WifiChannelWidth9+ - | **Type**| **Description**| - | -------- | -------- | - |  Array<[WifiScanInfo](#wifiscaninfo)> | Scan result obtained.| +Enumerates the WLAN channel widths. -**Error codes** +**System capability**: SystemCapability.Communication.WiFi.STA -For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | -| 2501000 | Operation failed.| +| **Name**| **Value**| **Description**| +| -------- | -------- | -------- | +| WIDTH_20MHZ | 0 | 20 MHz| +| WIDTH_40MHZ | 1 | 40 MHz| +| WIDTH_80MHZ | 2 | 80 MHz| +| WIDTH_160MHZ | 3 | 160 MHz| +| WIDTH_80MHZ_PLUS | 4 | 80 MHz+| +| WIDTH_INVALID | 5 | Invalid value| ## wifi.addDeviceConfig9+ @@ -316,24 +332,43 @@ Adds network configuration. This API uses a promise to return the result. **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration to add.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration to add.| **Return value** - | **Type**| **Description**| - | -------- | -------- | - | Promise<number> | Promise used to return the ID of the added network configuration. If **-1** is returned, the network configuration fails to be added.| +| **Type**| **Description**| +| -------- | -------- | +| Promise<number> | Promise used to return the ID of the added network configuration. If **-1** is returned, the network configuration fails to be added.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| +**Example** + +``` + import wifi from '@ohos.wifiManager'; + + try { + let config = { + ssid : "****", + preSharedKey : "****", + securityType : 0 + } + wifiManager.addDeviceConfig(config).then(result => { + console.info("result:" + JSON.stringify(result)); + }); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + ## WifiDeviceConfig9+ Represents the WLAN configuration. @@ -348,14 +383,14 @@ Represents the WLAN configuration. | preSharedKey | string | Yes| No| PSK of the hotspot.| | isHiddenSsid | boolean | Yes| No| Whether the network is hidden.| | securityType | [WifiSecurityType](#wifisecuritytype) | Yes| No| Security type.| -| creatorUid | number | Yes| No| ID of the creator.
**System API**: This is a system API.| -| disableReason | number | Yes| No| Reason for disabling WLAN.
**System API**: This is a system API.| -| netId | number | Yes| No| Network ID.
**System API**: This is a system API.| -| randomMacType | number | Yes| No| Random MAC type.
**System API**: This is a system API.| -| randomMacAddr | string | Yes| No| Random MAC address.
**System API**: This is a system API.| -| ipType | [IpType](#iptype9) | Yes| No| IP address type.
**System API**: This is a system API.| -| staticIp | [IpConfig](#ipconfig9) | Yes| No| Static IP address configuration.
**System API**: This is a system API.| -| eapConfig9+ | [WifiEapConfig](#wifieapconfig9) | Yes| No| EAP configuration.
**System API**: This is a system API.| +| creatorUid | number | Yes| No| ID of the creator.
**System API**: This is a system API.| +| disableReason | number | Yes| No| Reason for disabling WLAN.
**System API**: This is a system API.| +| netId | number | Yes| No| Network ID.
**System API**: This is a system API.| +| randomMacType | number | Yes| No| Random MAC type.
**System API**: This is a system API.| +| randomMacAddr | string | Yes| No| Random MAC address.
**System API**: This is a system API.| +| ipType | [IpType](#iptype9) | Yes| No| IP address type.
**System API**: This is a system API.| +| staticIp | [IpConfig](#ipconfig9) | Yes| No| Static IP address configuration.
**System API**: This is a system API.| +| eapConfig9+ | [WifiEapConfig](#wifieapconfig9) | Yes| No| EAP configuration.
**System API**: This is a system API.| ## IpType9+ @@ -473,19 +508,38 @@ Adds network configuration. This API uses an asynchronous callback to return the **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration to add.| - | callback | AsyncCallback<number> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is the network configuration ID. If **data** is **-1**, the operation has failed. If **err** is not **0**, an error has occurred.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration to add.| +| callback | AsyncCallback<number> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is the network configuration ID. If **data** is **-1**, the operation has failed. If **err** is not **0**, an error has occurred.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| +**Example** + +``` + import wifi from '@ohos.wifiManager'; + + try { + let config = { + ssid : "****", + preSharedKey : "****", + securityType : 0 + } + wifiManager.addDeviceConfig(config,(error,result) => { + console.info("result:" + JSON.stringify(result)); + }); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + ## wifi.addCandidateConfig9+ addCandidateConfig(config: WifiDeviceConfig): Promise<number> @@ -498,24 +552,42 @@ Adds the configuration of a candidate network. This API uses a promise to return **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration to add.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration to add.| **Return value** - | **Type**| **Description**| - | -------- | -------- | - | Promise<number> | Promise used to return the network configuration ID.| +| **Type**| **Description**| +| -------- | -------- | +| Promise<number> | Promise used to return the network configuration ID.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| +**Example** +````` + import wifi from '@ohos.wifiManager'; + + try { + let config = { + ssid : "****", + preSharedKey : "****", + securityType : 0 + } + wifiManager.addCandidateConfig(config).then(result => { + console.info("result:" + JSON.stringify(result)); + }); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +````` + ## wifi.addCandidateConfig9+ addCandidateConfig(config: WifiDeviceConfig, callback: AsyncCallback<number>): void @@ -528,19 +600,37 @@ Adds the configuration of a candidate network. This API uses an asynchronous cal **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration to add.| - | callback | AsyncCallback<number> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is the network configuration ID. If **data** is **-1**, the operation has failed. If **err** is not **0**, an error has occurred.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration to add.| +| callback | AsyncCallback<number> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is the network configuration ID. If **data** is **-1**, the operation has failed. If **err** is not **0**, an error has occurred.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| +**Example** +````` + import wifi from '@ohos.wifiManager'; + + try { + let config = { + ssid : "****", + preSharedKey : "****", + securityType : 0 + } + wifiManager.addCandidateConfig(config,(error,result) => { + console.info("result:" + JSON.stringify(result)); + }); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +````` + ## wifi.removeCandidateConfig9+ removeCandidateConfig(networkId: number): Promise<void> @@ -553,24 +643,39 @@ Removes the configuration of a candidate network. This API uses a promise to ret **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | networkId | number | Yes| ID of the network configuration to remove.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| networkId | number | Yes| ID of the network configuration to remove.| **Return value** - | **Type**| **Description**| - | -------- | -------- | - | Promise<void> | Promise used to return the result.| +| **Type**| **Description**| +| -------- | -------- | +| Promise<void> | Promise used to return the result.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| +**Example** + +``` + import wifi from '@ohos.wifiManager'; + + try { + let networkId = 0; + wifiManager.removeCandidateConfig(networkId).then(result => { + console.info("result:" + JSON.stringify(result)); + }); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + ## wifi.removeCandidateConfig9+ removeCandidateConfig(networkId: number, callback: AsyncCallback<void>): void @@ -583,48 +688,84 @@ Removes the configuration of a candidate network. This API uses an asynchronous **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | networkId | number | Yes| ID of the network configuration to remove.| - | callback | AsyncCallback<void> | Yes| Callback invoked to return the result. If the operation is successful, the value of **err** is **0**. If **err** is not **0**, an error has occurred.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| networkId | number | Yes| ID of the network configuration to remove.| +| callback | AsyncCallback<void> | Yes| Callback invoked to return the result. If the operation is successful, the value of **err** is **0**. If **err** is not **0**, an error has occurred.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + let networkId = 0; + wifiManager.removeCandidateConfig(networkId,(error,result) => { + console.info("result:" + JSON.stringify(result)); + }); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + ## wifi.getCandidateConfigs9+ getCandidateConfigs():  Array<[WifiDeviceConfig](#wifideviceconfig)> Obtains candidate network configuration. -**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION +**Required permissions**: ohos.permission.GET_WIFI_INFO, ohos.permission.LOCATION, and ohos.permission.APPROXIMATELY_LOCATION **System capability**: SystemCapability.Communication.WiFi.STA **Return value** - | **Type**| **Description**| - | -------- | -------- | - |  Array<[WifiDeviceConfig](#wifideviceconfig)> | Candidate network configuration obtained.| +| **Type**| **Description**| +| -------- | -------- | +|  Array<[WifiDeviceConfig](#wifideviceconfig)> | Candidate network configuration obtained.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| +**Example** + +````` + import wifi from '@ohos.wifiManager'; + + try { + let configs = wifiManager.getCandidateConfigs(); + console.info("configs:" + JSON.stringify(configs)); + let len = Object.keys(configs).length; + console.log("result len: " + len); + if(len > 0){ + for (var i = 0; i < len; ++i) { + console.info("ssid: " + configs[i].ssid); + console.info("bssid: " + configs[i].bssid); + } + } + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } + +````` + ## wifi.connectToCandidateConfig9+ connectToCandidateConfig(networkId: number): void -Connects to a candidate network. +Connects to a candidate network added by the application. If the device is already connected to a hotspot, disconnect it from the hotspot first. **Required permissions**: ohos.permission.SET_WIFI_INFO @@ -632,24 +773,38 @@ Connects to a candidate network. **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | networkId | number | Yes| ID of the candidate network configuration.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| networkId | number | Yes| ID of the candidate network configuration.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| | 2501001 | Wifi is closed.| +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + let networkId = 0; + let ret = wifiManager.connectToCandidateConfig(networkId); + console.info("result:" + ret); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } + +``` + ## wifi.connectToNetwork9+ connectToNetwork(networkId: number): void -Connects to the specified network. +Connects to the specified network. If the device is already connected to a hotspot, use **disconnect()** to disconnect it from the hotspot first. **System API**: This is a system API. @@ -659,24 +814,37 @@ Connects to the specified network. **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | networkId | number | Yes| Network configuration ID.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| networkId | number | Yes| Network configuration ID.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| | 2501001 | Wifi is closed.| +**Example** + +``` + import wifi from '@ohos.wifiManager'; + + try { + let networkId = 0; + wifiManager.connectToNetwork(networkId); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + ## wifi.connectToDevice9+ connectToDevice(config: WifiDeviceConfig): void -Connects to the specified network. +Connects to the specified network. If the device is already connected to a hotspot, use **disconnect()** to disconnect it from the hotspot first. **System API**: This is a system API. @@ -687,19 +855,36 @@ Connects to the specified network. **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| config | [WifiDeviceConfig](#wifideviceconfig) | Yes| Configuration of the WLAN to connect. | **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| | 2501001 | Wifi is closed.| +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + let config = { + ssid : "****", + preSharedKey : "****", + securityType : 3 + } + wifiManager.connectToDevice(config); + + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + ## wifi.disconnect9+ disconnect(): void @@ -717,10 +902,21 @@ Disconnects the network. For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + wifiManager.disconnect(); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + ## wifi.getSignalLevel9+ getSignalLevel(rssi: number, band: number): number @@ -733,25 +929,40 @@ Obtains the WLAN signal level. **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | rssi | number | Yes| RSSI of the hotspot, in dBm.| - | band | number | Yes| Frequency band of the WLAN AP.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| rssi | number | Yes| RSSI of the hotspot, in dBm.| +| band | number | Yes| Frequency band of the WLAN AP.| **Return value** - | **Type**| **Description**| - | -------- | -------- | - | number | Signal level obtained. The value range is [0, 4].| +| **Type**| **Description**| +| -------- | -------- | +| number | Signal level obtained. The value range is [0, 4].| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + let rssi = 0; + let band = 0; + let level = wifiManager.getSignalLevel(rssi,band); + console.info("lelvel:" + JSON.stringify(lelvel)); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } + +``` + ## wifi.getLinkedInfo9+ getLinkedInfo(): Promise<WifiLinkedInfo> @@ -764,16 +975,16 @@ Obtains WLAN connection information. This API uses a promise to return the resul **Return value** - | Type| Description| - | -------- | -------- | - | Promise<[WifiLinkedInfo](#wifilinkedinfo)> | Promise used to return the WLAN connection information obtained.| +| Type| Description| +| -------- | -------- | +| Promise<[WifiLinkedInfo](#wifilinkedinfo)> | Promise used to return the WLAN connection information obtained.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| | 2501001 | Wifi is closed.| @@ -789,22 +1000,22 @@ Obtains WLAN connection information. This API uses an asynchronous callback to r **Parameters** - | Name| Type| Mandatory| Description| - | -------- | -------- | -------- | -------- | - | callback | AsyncCallback<[WifiLinkedInfo](#wifilinkedinfo)> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is the WLAN connection information obtained. If **err** is not **0**, an error has occurred.| +| Name| Type| Mandatory| Description| +| -------- | -------- | -------- | -------- | +| callback | AsyncCallback<[WifiLinkedInfo](#wifilinkedinfo)> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is the WLAN connection information obtained. If **err** is not **0**, an error has occurred.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| | 2501001 | Wifi is closed.| **Example** ```js - import wifi from '@ohos.wifi'; + import wifi from '@ohos.wifiManager'; wifi.getLinkedInfo((err, data) => { if (err) { @@ -832,21 +1043,25 @@ Represents the WLAN connection information. | -------- | -------- | -------- | -------- | -------- | | ssid | string | Yes| No| SSID of the hotspot, in UTF-8 format.| | bssid | string | Yes| No| BSSID of the hotspot.| -| networkId | number | Yes| No| Network configuration ID.
**System API**: This is a system API.| +| networkId | number | Yes| No| Network configuration ID.
**System API**: This is a system API.| | rssi | number | Yes| No| RSSI of the hotspot, in dBm.| -| band | number | Yes| No| Frequency band of the WLAN AP.| -| linkSpeed | number | Yes| No| Speed of the WLAN AP.| +| band | number | Yes| No| Band of the WLAN AP.| +| linkSpeed | number | Yes| No| Uplink speed of the WLAN AP.| +| rxLinkSpeed10+ | number | Yes| No| Downlink speed of the WLAN AP.| +| maxSupportedTxLinkSpeed10+ | number | Yes| No| Maximum uplink speed supported.| +| maxSupportedRxLinkSpeed10+ | number | Yes| No| Maximum uplink speed supported.| | frequency | number | Yes| No| Frequency of the WLAN AP.| | isHidden | boolean | Yes| No| Whether to hide the WLAN AP.| | isRestricted | boolean | Yes| No| Whether to restrict data volume at the WLAN AP.| -| chload | number | Yes| No| Channel load. A larger value indicates a higher load.
**System API**: This is a system API.| -| snr | number | Yes| No| Signal-to-noise ratio (SNR).
**System API**: This is a system API.| -| macType9+ | number | Yes| No| MAC address type.| +| chload | number | Yes| No| Channel load. A larger value indicates a higher load.
**System API**: This is a system API.| +| snr | number | Yes| No| Signal-to-noise ratio (SNR).
**System API**: This is a system API.| +| macType | number | Yes| No| MAC address type.| | macAddress | string | Yes| No| MAC address of the device.| | ipAddress | number | Yes| No| IP address of the device that sets up the WLAN connection.| -| suppState | [SuppState](#suppstate) | Yes| No| Supplicant state.
**System API**: This is a system API.| +| suppState | [SuppState](#suppstate) | Yes| No| Supplicant state.
**System API**: This is a system API.| | connState | [ConnState](#connstate) | Yes| No| WLAN connection state.| - +| channelWidth10+ | [WifiChannelWidth](#wifichannelwidth) | Yes| No| Channel bandwidth of the connected hotspot.| +| wifiStandard10+ | [WifiStandard](#wifistandard) | Yes| No| Wi-Fi standard used by the connected hotspot.| ## ConnState9+ @@ -889,6 +1104,23 @@ Enumerates the supplicant states. | UNINITIALIZED | 10 | The supplicant failed to set up the connection.| | INVALID | 11 | Invalid value.| +## SuppState10+ + +Enumerates the Wi-Fi standards. + +**System capability**: SystemCapability.Communication.WiFi.STA + +| Name| Value| Description| +| -------- | -------- | -------- | +| WIFI_STANDARD_UNDEFINED | 0 | Undefined| +| WIFI_STANDARD_11A | 1 | 802.11a| +| WIFI_STANDARD_11B | 2 | 802.11b| +| WIFI_STANDARD_11G | 3 | 802.11g| +| WIFI_STANDARD_11N | 4 | 802.11n| +| WIFI_STANDARD_11AC | 5 | 802.11ac| +| WIFI_STANDARD_11AX | 6 | 802.11ax| +| WIFI_STANDARD_11AD | 7 | 802.11ad| + ## wifi.isConnected9+ @@ -902,18 +1134,31 @@ Checks whether the WLAN is connected. **Return value** - | **Type**| **Description**| - | -------- | -------- | - | boolean | Returns **true** if the WLAN is connected; returns **false** otherwise.| +| **Type**| **Description**| +| -------- | -------- | +| boolean | Returns **true** if the WLAN is connected; returns **false** otherwise.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + let ret = wifiManager.isConnected(); + console.info("isConnected:" + ret); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } + +``` + ## wifi.getSupportedFeatures9+ getSupportedFeatures(): number @@ -928,9 +1173,9 @@ Obtains the features supported by this device. **Return value** - | **Type**| **Description**| - | -------- | -------- | - | number | Feature value. | +| **Type**| **Description**| +| -------- | -------- | +| number | Feature value. | **Feature IDs** @@ -951,10 +1196,23 @@ Obtains the features supported by this device. For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2401000 | Operation failed.| +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + let ret = wifiManager.getSupportedFeatures(); + console.info("supportedFeatures:" + ret); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } + +``` + ## wifi.isFeatureSupported9+ isFeatureSupported(featureId: number): boolean @@ -968,24 +1226,38 @@ Checks whether the device supports the specified WLAN feature. **Parameters** - | **Name**| **Type**| Mandatory| **Description**| - | -------- | -------- | -------- | -------- | - | featureId | number | Yes| Feature ID.| +| **Name**| **Type**| Mandatory| **Description**| +| -------- | -------- | -------- | -------- | +| featureId | number | Yes| Feature ID.| **Return value** - | **Type**| **Description**| - | -------- | -------- | - | boolean | Returns **true** if the feature is supported; returns **false** otherwise.| +| **Type**| **Description**| +| -------- | -------- | +| boolean | Returns **true** if the feature is supported; returns **false** otherwise.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2401000 | Operation failed.| +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + let featureId = 0; + let ret = wifiManager.isFeatureSupported(featureId); + console.info("isFeatureSupported:" + ret); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } + +``` + ## wifi.getDeviceMacAddress9+ getDeviceMacAddress(): string[] @@ -1000,18 +1272,31 @@ Obtains the device MAC address. **Return value** - | **Type**| **Description**| - | -------- | -------- | - | string[] | MAC address obtained.| +| **Type**| **Description**| +| -------- | -------- | +| string[] | MAC address obtained.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + let ret = wifiManager.getDeviceMacAddress(); + console.info("deviceMacAddress:" + JSON.stringify(ret)); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } + +``` + ## wifi.getIpInfo9+ getIpInfo(): IpInfo @@ -1024,18 +1309,30 @@ Obtains IP information. **Return value** - | **Type**| **Description**| - | -------- | -------- | - | [IpInfo](#ipinfo9) | IP information obtained.| +| **Type**| **Description**| +| -------- | -------- | +| [IpInfo](#ipinfo9) | IP information obtained.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + let info = wifiManager.getIpInfo(); + console.info("info:" + JSON.stringify(info)); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + ## IpInfo9+ Represents IP information. @@ -1065,18 +1362,30 @@ Obtains the country code. **Return value** - | **Type**| **Description**| - | -------- | -------- | - | string | Country code obtained.| +| **Type**| **Description**| +| -------- | -------- | +| string | Country code obtained.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2401000 | Operation failed.| +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + let code = wifiManager.getCountryCode(); + console.info("code:" + code); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + ## wifi.reassociate9+ reassociate(): void @@ -1093,11 +1402,22 @@ Re-associates with the network. For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| | 2501001 | Wifi is closed.| +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + wifiManager.reassociate(); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + ## wifi.reconnect9+ reconnect(): void @@ -1114,11 +1434,22 @@ Reconnects to the network. For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| | 2501001 | Wifi is closed.| +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + wifiManager.reconnect(); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + ## wifi.getDeviceConfigs9+ getDeviceConfigs():  Array<[WifiDeviceConfig](#wifideviceconfig)> @@ -1127,27 +1458,39 @@ Obtains network configuration. **System API**: This is a system API. -**Required permissions**: ohos.permission.GET_WIFI_INFO, ohos.permission.LOCATION, and ohos.permission.GET_WIFI_CONFIG +**Required permissions**: ohos.permission.GET_WIFI_INFO, ohos.permission.LOCATION, ohos.permission.APPROXIMATELY_LOCATION, and ohos.permission.GET_WIFI_CONFIG **System capability**: SystemCapability.Communication.WiFi.STA **Return value** - | **Type**| **Description**| - | -------- | -------- | - |  Array<[WifiDeviceConfig](#wifideviceconfig)> | Array of network configuration obtained.| +| **Type**| **Description**| +| -------- | -------- | +|  Array<[WifiDeviceConfig](#wifideviceconfig)> | Array of network configuration obtained.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| -## wifi.updateNetwork9+ +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + let configs = wifiManager.getDeviceConfigs(); + console.info("configs:" + JSON.stringify(configs)); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` -updateNetwork(config: WifiDeviceConfig): number +## wifi.updateDeviceConfig9+ + +updateDeviceConfig(config: WifiDeviceConfig): number Updates network configuration. @@ -1159,27 +1502,44 @@ Updates network configuration. **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | config | [WifiDeviceConfig](#wifideviceconfig) | Yes| New WLAN configuration.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| config | [WifiDeviceConfig](#wifideviceconfig) | Yes| New WLAN configuration.| **Return value** - | **Type**| **Description**| - | -------- | -------- | - | number | ID of the updated network configuration. The value **-1** indicates that the operation has failed.| +| **Type**| **Description**| +| -------- | -------- | +| number | ID of the updated network configuration. The value **-1** indicates that the operation has failed.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| -## wifi.disableNetwork9+ +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + let config = { + ssid : "****", + preSharedKey : "****", + securityType : 3 + } + let ret = wifiManager.updateDeviceConfig(config); + console.error("ret:" + ret); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + +## wifi.disableDeviceConfig9+ -disableNetwork(netId: number): void +disableDeviceConfig(networkId: number): void Disables network configuration. @@ -1191,21 +1551,33 @@ Disables network configuration. **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | netId | number | Yes| ID of the network configuration to disable.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| netId | number | Yes| ID of the network configuration to disable.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| -## wifi.removeAllNetwork9+ +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + let netId = 0; + wifiManager.disableDeviceConfig(netId); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + +## wifi.removeAllDeviceConfigs9+ -removeAllNetwork(): void +removeAllDeviceConfigs(): void Removes the configuration of all networks. @@ -1219,13 +1591,24 @@ Removes the configuration of all networks. For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| -## wifi.removeDevice9+ +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + wifiManager.removeAllDeviceConfigs(); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + +## wifi.removeDeviceConfig9+ -removeDevice(id: number): void +removeDeviceConfig(networkId: number): void Removes the specified network configuration. @@ -1237,18 +1620,114 @@ Removes the specified network configuration. **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | id | number | Yes| ID of the network configuration to remove.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| networkId | number | Yes| ID of the network configuration to remove.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | +| 2501000 | Operation failed.| + +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + let id = 0; + wifiManager.removeDeviceConfig(id); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + +## wifi.isBandTypeSupported10+ + +isBandTypeSupported(bandType: WifiBandType): boolean + +Checks whether the current frequency band is supported. + +**Required permissions**: ohos.permission.GET_WIFI_INFO + +**System capability**: SystemCapability.Communication.WiFi.STA + +**Parameters** + +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| bandType | WifiBandType | Yes| Wi-Fi band type.| + +**Error codes** + +For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). + +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + let type = 0; + boolean isBandTypeSupported = wifiManager.isBandTypeSupported(type); + console.info("isBandTypeSupported:" + isBandTypeSupported); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + +## WifiBandType 10+ + +Enumerates the Wi-Fi band types. + +**System capability**: SystemCapability.Communication.WiFi.STA + +| Name| Value| Description| +| -------- | -------- | -------- | +| WIFI_BAND_NONE | 0 | Undefined| +| WIFI_BAND_2G | 1 | 2 GHz| +| WIFI_BAND_5G | 2 | 5 GHz| +| WIFI_BAND_6G | 3 | 6 GHz| +| WIFI_BAND_60G | 4 | 60 GHz| + + +## wifi.get5GChannelList10+ + +get5GChannelList(): Array<number> + +Obtains the list of 5 GHz channels supported by this device. + +**System API**: This is a system API. + +**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.GET_WIFI_CONFIG + +**System capability**: SystemCapability.Communication.WiFi.STA + +**Error codes** + +For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). + +| **ID**| **Error Message**| +| -------- | -------- | +| 2501000 | Operation failed.| + +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + let channelList = wifiManager.get5GChannelList(); + console.info("channelList:" + JSON.stringify(channelList)); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + ## wifi.enableHotspot9+ enableHotspot(): void @@ -1265,10 +1744,21 @@ Enables this hotspot. For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2601000 | Operation failed.| +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + wifiManager.enableHotspot(); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + ## wifi.disableHotspot9+ disableHotspot(): void @@ -1285,10 +1775,21 @@ Disables this hotspot. For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2601000 | Operation failed.| +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + wifiManager.disableHotspot(); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + ## wifi.isHotspotDualBandSupported9+ isHotspotDualBandSupported(): boolean @@ -1303,18 +1804,30 @@ Checks whether the hotspot supports dual band. **Return value** - | **Type**| **Description**| - | -------- | -------- | - | boolean | Returns **true** if the hotspot supports dual band; returns **false** otherwise.| +| **Type**| **Description**| +| -------- | -------- | +| boolean | Returns **true** if the hotspot supports dual band; returns **false** otherwise.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2601000 | Operation failed.| +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + let ret = wifiManager.isHotspotDualBandSupported(); + console.info("result:" + ret); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + ## wifi.isHotspotActive9+ isHotspotActive(): boolean @@ -1329,18 +1842,30 @@ Checks whether this hotspot is active. **Return value** - | **Type**| **Description**| - | -------- | -------- | - | boolean | Returns **true** if the hotspot is active; returns **false** otherwise.| +| **Type**| **Description**| +| -------- | -------- | +| boolean | Returns **true** if the hotspot is active; returns **false** otherwise.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2601000 | Operation failed.| +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + let ret = wifiManager.isHotspotActive(); + console.info("result:" + ret); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + ## wifi.setHotspotConfig9+ setHotspotConfig(config: HotspotConfig): void @@ -1355,18 +1880,38 @@ Sets hotspot configuration. **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | config | [HotspotConfig](#hotspotconfig9) | Yes| Hotspot configuration to set.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| config | [HotspotConfig](#hotspotconfig9) | Yes| Hotspot configuration to set.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2601000 | Operation failed.| +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + let config = { + ssid: "****", + securityType: 3, + band: 0, + channel: 0, + preSharedKey: "****", + maxConn: 0 + } + let ret = wifiManager.setHotspotConfig(); + console.info("result:" + ret); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + ## HotspotConfig9+ Represents the hotspot configuration. @@ -1377,12 +1922,12 @@ Represents the hotspot configuration. | **Name**| **Type**| **Readable**| **Writable**| **Description**| | -------- | -------- | -------- | -------- | -------- | -| ssid | string | Yes| No| SSID of the hotspot, in UTF-8 format.| -| securityType | [WifiSecurityType](#wifisecuritytype) | Yes| No| Security type.| -| band | number | Yes| No| Hotspot band. The value **1** stands for 2.4 GHz, the value **2** for 5 GHz, and the value **3** for dual band.| -| preSharedKey | string | Yes| No| PSK of the hotspot.| -| maxConn | number | Yes| No| Maximum number of connections allowed.| - +| ssid | string | Yes| Yes| SSID of the hotspot, in UTF-8 format.| +| securityType | [WifiSecurityType](#wifisecuritytype) | Yes| Yes| Security type.| +| band | number | Yes| Yes| Hotspot band. The value **1** stands for 2.4 GHz, the value **2** for 5 GHz, and the value **3** for dual band.| +| channel10+ | number | Yes| Yes| Hotspot channel (2.4 GHz: 1 to 14; 5 GHz: 7 to 196; Dual-band: not supported currently) | +| preSharedKey | string | Yes| Yes| PSK of the hotspot.| +| maxConn | number | Yes| Yes| Maximum number of connections allowed.| ## wifi.getHotspotConfig9+ @@ -1398,44 +1943,68 @@ Obtains hotspot configuration. **Return value** - | **Type**| **Description**| - | -------- | -------- | - | [HotspotConfig](#hotspotconfig9) | Hotspot configuration obtained.| +| **Type**| **Description**| +| -------- | -------- | +| [HotspotConfig](#hotspotconfig9) | Hotspot configuration obtained.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2601000 | Operation failed.| -## wifi.getStations9+ +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + let config = wifiManager.getHotspotConfig(); + console.info("result:" + JSON.stringify(config)); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + +## wifi.getHotspotStations9+ -getStations():  Array<[StationInfo](#stationinfo9)> +getHotspotStations():  Array<[StationInfo](#stationinfo9)> Obtains information about the connected stations. **System API**: This is a system API. -**Required permissions**: ohos.permission.GET_WIFI_INFO, ohos.permission.LOCATION, and ohos.permission.MANAGE_WIFI_HOTSPOT (available only to system applications) +**Required permissions**: ohos.permission.GET_WIFI_INFO, ohos.permission.LOCATION, ohos.permission.APPROXIMATELY_LOCATION, and ohos.permission.MANAGE_WIFI_HOTSPOT (available only to system applications) **System capability**: SystemCapability.Communication.WiFi.AP.Core **Return value** - | **Type**| **Description**| - | -------- | -------- | - |  Array<[StationInfo](#stationinfo9)> | Connected stations obtained.| +| **Type**| **Description**| +| -------- | -------- | +|  Array<[StationInfo](#stationinfo9)> | Connected stations obtained.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2601000 | Operation failed.| +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + let stations = wifiManager.getHotspotStations(); + console.info("result:" + JSON.stringify(stations)); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + ## StationInfo9+ Represents the station information. @@ -1463,18 +2032,36 @@ Obtains P2P link information. This API uses a promise to return the result. **Return value** - | Type| Description| - | -------- | -------- | - | Promise<[WifiP2pLinkedInfo](#wifip2plinkedinfo9)> | Promise used to return the P2P link information obtained.| +| Type| Description| +| -------- | -------- | +| Promise<[WifiP2pLinkedInfo](#wifip2plinkedinfo9)> | Promise used to return the P2P link information obtained.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2801000 | Operation failed.| +**Example** +``` + import wifi from '@ohos.wifiManager'; + + wifi.getP2pLinkedInfo((err, data) => { + if (err) { + console.error("get p2p linked info error"); + return; + } + console.info("get wifi p2p linked info: " + JSON.stringify(data)); + }); + + wifi.getP2pLinkedInfo().then(data => { + console.info("get wifi p2p linked info: " + JSON.stringify(data)); + }); +``` + + ## WifiP2pLinkedInfo9+ Represents the P2P link information. @@ -1512,81 +2099,98 @@ Obtains P2P link information. This API uses an asynchronous callback to return t **Parameters** - | Name| Type| Mandatory| Description| - | -------- | -------- | -------- | -------- | - | callback | AsyncCallback<[WifiP2pLinkedInfo](#wifip2plinkedinfo9)> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is the P2P link information. If **err** is not **0**, an error has occurred.| +| Name| Type| Mandatory| Description| +| -------- | -------- | -------- | -------- | +| callback | AsyncCallback<[WifiP2pLinkedInfo](#wifip2plinkedinfo9)> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is the P2P link information. If **err** is not **0**, an error has occurred.| -## wifi.getCurrentGroup9+ +## wifi.getCurrentP2pGroup9+ -getCurrentGroup(): Promise<WifiP2pGroupInfo> +getCurrentP2pGroup(): Promise<WifiP2pGroupInfo> Obtains the current P2P group information. This API uses a promise to return the result. -**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION +**Required permissions**: ohos.permission.GET_WIFI_INFO, ohos.permission.LOCATION, and ohos.permission.APPROXIMATELY_LOCATION **System capability**: SystemCapability.Communication.WiFi.P2P **Return value** - | Type| Description| - | -------- | -------- | - | Promise<[WifiP2pGroupInfo](#wifip2pgroupinfo9)> | Promise used to return the P2P group information obtained.| +| Type| Description| +| -------- | -------- | +| Promise<[WifiP2pGroupInfo](#wifip2pgroupinfo9)> | Promise used to return the P2P group information obtained.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2801000 | Operation failed.| -## wifi.getCurrentGroup9+ +## wifi.getCurrentP2pGroup9+ -getCurrentGroup(callback: AsyncCallback<WifiP2pGroupInfo>): void +getCurrentP2pGroup(callback: AsyncCallback<WifiP2pGroupInfo>): void Obtains the current P2P group information. This API uses an asynchronous callback to return the result. -**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION +**Required permissions**: ohos.permission.GET_WIFI_INFO, ohos.permission.LOCATION, and ohos.permission.APPROXIMATELY_LOCATION **System capability**: SystemCapability.Communication.WiFi.P2P **Parameters** - | Name| Type| Mandatory| Description| - | -------- | -------- | -------- | -------- | - | callback | AsyncCallback<[WifiP2pGroupInfo](#wifip2pgroupinfo9)> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is the group information obtained. If **err** is not **0**, an error has occurred.| +| Name| Type| Mandatory| Description| +| -------- | -------- | -------- | -------- | +| callback | AsyncCallback<[WifiP2pGroupInfo](#wifip2pgroupinfo9)> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is the group information obtained. If **err** is not **0**, an error has occurred.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2801000 | Operation failed.| +**Example** +``` + import wifi from '@ohos.wifiManager'; + + wifi.getCurrentP2pGroup((err, data) => { + if (err) { + console.error("get current P2P group error"); + return; + } + console.info("get current P2P group: " + JSON.stringify(data)); + }); + + wifi.getCurrentP2pGroup().then(data => { + console.info("get current P2P group: " + JSON.stringify(data)); + }); +``` + ## wifi.getP2pPeerDevices9+ getP2pPeerDevices(): Promise<WifiP2pDevice[]> Obtains the peer device list in the P2P connection. This API uses a promise to return the result. -**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION +**Required permissions**: ohos.permission.GET_WIFI_INFO, ohos.permission.LOCATION, and ohos.permission.APPROXIMATELY_LOCATION **System capability**: SystemCapability.Communication.WiFi.P2P **Return value** - | Type| Description| - | -------- | -------- | - | Promise<[WifiP2pDevice[]](#wifip2pdevice9)> | Promise used to return the peer device list.| +| Type| Description| +| -------- | -------- | +| Promise<[WifiP2pDevice[]](#wifip2pdevice9)> | Promise used to return the peer device list.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2801000 | Operation failed.| ## wifi.getP2pPeerDevices9+ @@ -1595,24 +2199,41 @@ getP2pPeerDevices(callback: AsyncCallback<WifiP2pDevice[]>): void Obtains the peer device list in the P2P connection. This API uses an asynchronous callback to return the result. -**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION +**Required permissions**: ohos.permission.GET_WIFI_INFO, ohos.permission.LOCATION, and ohos.permission.APPROXIMATELY_LOCATION **System capability**: SystemCapability.Communication.WiFi.P2P **Parameters** - | Name| Type| Mandatory| Description| - | -------- | -------- | -------- | -------- | - | callback | AsyncCallback<[WifiP2pDevice[]](#wifip2pdevice9)> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is the peer device list obtained. If **err** is not **0**, an error has occurred.| +| Name| Type| Mandatory| Description| +| -------- | -------- | -------- | -------- | +| callback | AsyncCallback<[WifiP2pDevice[]](#wifip2pdevice9)> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is the peer device list obtained. If **err** is not **0**, an error has occurred.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2801000 | Operation failed.| +**Example** +``` + import wifi from '@ohos.wifiManager'; + + wifi.getP2pPeerDevices((err, data) => { + if (err) { + console.error("get P2P peer devices error"); + return; + } + console.info("get P2P peer devices: " + JSON.stringify(data)); + }); + + wifi.getP2pPeerDevices().then(data => { + console.info("get P2P peer devices: " + JSON.stringify(data)); + }); +``` + ## WifiP2pDevice9+ Represents the P2P device information. @@ -1655,16 +2276,16 @@ Obtains the local device information in the P2P connection. This API uses a prom **Return value** - | Type| Description| - | -------- | -------- | - | Promise<[WifiP2pDevice](#wifip2pdevice9)> | Promise used to return the local device information obtained.| +| Type| Description| +| -------- | -------- | +| Promise<[WifiP2pDevice](#wifip2pdevice9)> | Promise used to return the local device information obtained.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2801000 | Operation failed.| ## wifi.getP2pLocalDevice9+ @@ -1679,14 +2300,34 @@ Obtains the local device information in the P2P connection. This API uses an asy **Parameters** - | Name| Type| Mandatory| Description| - | -------- | -------- | -------- | -------- | - | callback | AsyncCallback<[WifiP2pDevice](#wifip2pdevice9)> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is the local device information obtained. If **err** is not **0**, an error has occurred.| +| Name| Type| Mandatory| Description| +| -------- | -------- | -------- | -------- | +| callback | AsyncCallback<[WifiP2pDevice](#wifip2pdevice9)> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is the local device information obtained. If **err** is not **0**, an error has occurred.| + +| **ID**| **Error Message**| +| -------- | -------- | +| 2801000 | Operation failed.| +**Example** +``` + import wifi from '@ohos.wifiManager'; + + wifiManager.getP2pLocalDevice((err, data) => { + if (err) { + console.error("get P2P local device error"); + return; + } + console.info("get P2P local device: " + JSON.stringify(data)); + }); + + wifi.getP2pLocalDevice().then(data => { + console.info("get P2P local device: " + JSON.stringify(data)); + }); +``` -## wifi.createGroup9+ +## wifi.createP2pGroup9+ -createGroup(config: WifiP2PConfig): void +createP2pGroup(config: WifiP2PConfig): void Creates a P2P group. @@ -1696,18 +2337,37 @@ Creates a P2P group. **Parameters** - | **Name**| **Type**| Mandatory| **Description**| - | -------- | -------- | -------- | -------- | - | config | [WifiP2PConfig](#wifip2pconfig9) | Yes| Group configuration.| +| **Name**| **Type**| Mandatory| **Description**| +| -------- | -------- | -------- | -------- | +| config | [WifiP2PConfig](#wifip2pconfig9) | Yes| Group configuration.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2801000 | Operation failed.| +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + let config = { + deviceAddress: "****", + netId: 0, + passphrase: "*****", + groupName: "****", + goBand: 0 + } + wifiManager.createP2pGroup(config); + + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + ## WifiP2PConfig9+ Represents P2P group configuration. @@ -1736,9 +2396,9 @@ Enumerates the P2P group frequency bands. | GO_BAND_5GHZ | 2 | 5 GHz.| -## wifi.removeGroup9+ +## wifi.removeP2pGroup9+ -removeGroup(): void +removeP2pGroup(): void Removes this P2P group. @@ -1750,33 +2410,43 @@ Removes this P2P group. For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2801000 | Operation failed.| +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + wifiManager.removeP2pGroup(); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + ## wifi.p2pConnect9+ p2pConnect(config: WifiP2PConfig): void Sets up a P2P connection. -**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION +**Required permissions**: ohos.permission.GET_WIFI_INFO, ohos.permission.LOCATION, and ohos.permission.APPROXIMATELY_LOCATION **System capability**: SystemCapability.Communication.WiFi.P2P **Parameters** - - | **Name**| **Type**| Mandatory| **Description**| - | -------- | -------- | -------- | -------- | - | config | [WifiP2PConfig](#wifip2pconfig9) | Yes| P2P group configuration.| +| **Name**| **Type**| Mandatory| **Description**| +| -------- | -------- | -------- | -------- | +| config | [WifiP2PConfig](#wifip2pconfig9) | Yes| P2P group configuration.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2801000 | Operation failed.| **Example** @@ -1843,7 +2513,7 @@ For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorco setTimeout(function() {wifi.off("p2pDeviceChange", recvP2pDeviceChangeFunc);}, 125 * 1000); setTimeout(function() {wifi.off("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc);}, 125 * 1000); setTimeout(function() {wifi.off("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc);}, 125 * 1000); - console.info("start discover devices -> " + wifi.startDiscoverDevices()); + console.info("start discover devices -> " + wifi.startP2pDiscoverDevices()); ``` ## wifi.p2pCancelConnect9+ @@ -1860,17 +2530,28 @@ Cancels this P2P connection. For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2801000 | Operation failed.| -## wifi.startDiscoverDevices9+ +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + wifiManager.p2pCancelConnect(); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + +## wifi.startDiscoverP2pDevices9+ -startDiscoverDevices(): void +startDiscoverP2pDevices(): void Starts to discover devices. -**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION +**Required permissions**: ohos.permission.GET_WIFI_INFO, ohos.permission.LOCATION, and ohos.permission.APPROXIMATELY_LOCATION **System capability**: SystemCapability.Communication.WiFi.P2P @@ -1878,13 +2559,24 @@ Starts to discover devices. For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2801000 | Operation failed.| -## wifi.stopDiscoverDevices9+ +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + wifiManager.startDiscoverP2pDevices(); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + +## wifi.stopDiscoverP2pDevices9+ -stopDiscoverDevices(): void +stopDiscoverP2pDevices(): void Stops discovering devices. @@ -1896,13 +2588,24 @@ Stops discovering devices. For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2801000 | Operation failed.| -## wifi.deletePersistentGroup9+ +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + wifiManager.stopDiscoverP2pDevices(); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + +## wifi.deletePersistentP2pGroup9+ -deletePersistentGroup(netId: number): void +deletePersistentP2pGroup(netId: number): void Deletes a persistent group. @@ -1915,18 +2618,30 @@ Deletes a persistent group. **Parameters** - | **Name**| **Type**| Mandatory| **Description**| - | -------- | -------- | -------- | -------- | - | netId | number | Yes| ID of the group to delete.| +| **Name**| **Type**| Mandatory| **Description**| +| -------- | -------- | -------- | -------- | +| netId | number | Yes| ID of the group to delete.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2801000 | Operation failed.| +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + let netId = 0; + wifiManager.deletePersistentP2pGroup(netId); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + ## wifi.getP2pGroups9+ getP2pGroups(): Promise<Array<WifiP2pGroupInfo>> @@ -1935,24 +2650,42 @@ Obtains information about all P2P groups. This API uses a promise to return the **System API**: This is a system API. -**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION +**Required permissions**: ohos.permission.GET_WIFI_INFO, ohos.permission.LOCATION, and ohos.permission.APPROXIMATELY_LOCATION **System capability**: SystemCapability.Communication.WiFi.P2P **Return value** - | Type| Description| - | -------- | -------- | - | Promise< Array<[WifiP2pGroupInfo](#wifip2pgroupinfo9)> > | Promise used to return the group information obtained.| +| Type| Description| +| -------- | -------- | +| Promise< Array<[WifiP2pGroupInfo](#wifip2pgroupinfo9)> > | Promise used to return the group information obtained.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2801000 | Operation failed.| +**Example** +``` + import wifi from '@ohos.wifiManager'; + + wifiManager.getP2pGroups((err, data) => { + if (err) { + console.error("get P2P groups error"); + return; + } + console.info("get P2P groups: " + JSON.stringify(data)); + }); + + wifi.getP2pGroups().then(data => { + console.info("get P2P groups: " + JSON.stringify(data)); + }); + +``` + ## WifiP2pGroupInfo9+ Represents the P2P group information. @@ -1980,27 +2713,27 @@ Obtains information about all P2P groups. This API uses an asynchronous callback **System API**: This is a system API. -**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION +**Required permissions**: ohos.permission.GET_WIFI_INFO, ohos.permission.LOCATION, and ohos.permission.APPROXIMATELY_LOCATION **System capability**: SystemCapability.Communication.WiFi.P2P **Parameters** - | Name| Type| Mandatory| Description| - | -------- | -------- | -------- | -------- | - | callback | AsyncCallback< Array<[WifiP2pGroupInfo](#wifip2pgroupinfo9)>> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is the group information obtained. If **err** is not **0**, an error has occurred.| +| Name| Type| Mandatory| Description| +| -------- | -------- | -------- | -------- | +| callback | AsyncCallback< Array<[WifiP2pGroupInfo](#wifip2pgroupinfo9)>> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is the group information obtained. If **err** is not **0**, an error has occurred.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2801000 | Operation failed.| -## wifi.setDeviceName9+ +## wifi.setP2pDeviceName9+ -setDeviceName(devName: string): void +setP2pDeviceName(devName: string): void Sets the device name. @@ -2012,18 +2745,30 @@ Sets the device name. **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | devName | string | Yes| Device name to set.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| devName | string | Yes| Device name to set.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2801000 | Operation failed.| +**Example** +``` + import wifi from '@ohos.wifiManager'; + + try { + let name = "****"; + wifiManager.setP2pDeviceName(netId); + }catch(error){ + console.error("failed:" + JSON.stringify(error)); + } +``` + ## wifi.on('wifiStateChange')9+ on(type: "wifiStateChange", callback: Callback<number>): void @@ -2036,17 +2781,17 @@ Registers the WLAN state change events. **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | type | string | Yes| Event type. The value is **wifiStateChange**.| - | callback | Callback<number> | Yes| Callback invoked to return the WLAN state.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| type | string | Yes| Event type. The value is **wifiStateChange**.| +| callback | Callback<number> | Yes| Callback invoked to return the WLAN state.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| **WLAN states** @@ -2071,17 +2816,17 @@ Unregisters the WLAN state change events. **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | type | string | Yes| Event type. The value is **wifiStateChange**.| - | callback | Callback<number> | No| Callback for the WLAN state change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| type | string | Yes| Event type. The value is **wifiStateChange**.| +| callback | Callback<number> | No| Callback for the WLAN state change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| **Example** @@ -2112,10 +2857,10 @@ Registers the WLAN connection state change events. **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | type | string | Yes| Event type. The value is **wifiConnectionChange**.| - | callback | Callback<number> | Yes| Callback invoked to return the WLAN connection state.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| type | string | Yes| Event type. The value is **wifiConnectionChange**.| +| callback | Callback<number> | Yes| Callback invoked to return the WLAN connection state.| **WLAN connection states** @@ -2128,8 +2873,8 @@ Registers the WLAN connection state change events. For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| ## wifi.off('wifiConnectionChange')9+ @@ -2144,19 +2889,34 @@ Unregisters the WLAN connection state change events. **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | type | string | Yes| Event type. The value is **wifiConnectionChange**.| - | callback | Callback<number> | No| Callback for the WLAN connection state change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| type | string | Yes| Event type. The value is **wifiConnectionChange**.| +| callback | Callback<number> | No| Callback for the WLAN connection state change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| +**Example** + ```js + import wifi from '@ohos.wifi'; + + var recvWifiConnectionChangeFunc = result => { + console.info("Receive wifi connection change event: " + result); + } + + // Register an event. + wifi.on("wifiConnectionChange", recvWifiConnectionChangeFunc); + + // Unregister an event. + wifi.off("wifiConnectionChange", recvWifiConnectionChangeFunc); + ``` + ## wifi.on('wifiScanStateChange')9+ on(type: "wifiScanStateChange", callback: Callback<number>): void @@ -2169,10 +2929,10 @@ Registers the WLAN scan state change events. **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | type | string | Yes| Event type. The value is **wifiScanStateChange**.| - | callback | Callback<number> | Yes| Callback invoked to return the WLAN scan state.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| type | string | Yes| Event type. The value is **wifiScanStateChange**.| +| callback | Callback<number> | Yes| Callback invoked to return the WLAN scan state.| **WLAN scan states** @@ -2185,8 +2945,8 @@ Registers the WLAN scan state change events. For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| ## wifi.off('wifiScanStateChange')9+ @@ -2210,10 +2970,25 @@ Unregisters the WLAN scan state change events. For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| +**Example** + ```js + import wifi from '@ohos.wifi'; + + var recvWifiScanStateChangeFunc = result => { + console.info("Receive Wifi scan state change event: " + result); + } + + // Register an event. + wifi.on("wifiScanStateChange", recvWifiScanStateChangeFunc); + + // Unregister an event. + wifi.off("wifiScanStateChange", recvWifiScanStateChangeFunc); + ``` + ## wifi.on('wifiRssiChange')9+ on(type: "wifiRssiChange", callback: Callback<number>): void @@ -2226,17 +3001,17 @@ Registers the RSSI change events. **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | type | string | Yes| Event type. The value is **wifiRssiChange**.| - | callback | Callback<number> | Yes| Callback invoked to return the RSSI, in dBm.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| type | string | Yes| Event type. The value is **wifiRssiChange**.| +| callback | Callback<number> | Yes| Callback invoked to return the RSSI, in dBm.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| ## wifi.off('wifiRssiChange')9+ @@ -2251,19 +3026,34 @@ Unregisters the RSSI change events. **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | type | string | Yes| Event type. The value is **wifiRssiChange**.| - | callback | Callback<number> | No| Callback for the RSSI change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| type | string | Yes| Event type. The value is **wifiRssiChange**.| +| callback | Callback<number> | No| Callback for the RSSI change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2501000 | Operation failed.| +**Example** + ```js + import wifi from '@ohos.wifiManager'; + + var recvWifiRssiChangeFunc = result => { + console.info("Receive wifi rssi change event: " + result); + } + + // Register an event. + wifiManager.on("wifiRssiChange", recvWifiRssiChangeFunc); + + // Unregister an event. + wifiManager.off("wifiRssiChange", recvWifiRssiChangeFunc); + ``` + ## wifi.on('hotspotStateChange')9+ on(type: "hotspotStateChange", callback: Callback<number>): void @@ -2276,10 +3066,10 @@ Registers the hotspot state change events. **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | type | string | Yes| Event type. The value is **hotspotStateChange**.| - | callback | Callback<number> | Yes| Callback invoked to return the hotspot state.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| type | string | Yes| Event type. The value is **hotspotStateChange**.| +| callback | Callback<number> | Yes| Callback invoked to return the hotspot state.| **Hotspot states** @@ -2294,8 +3084,8 @@ Registers the hotspot state change events. For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2601000 | Operation failed.| ## wifi.off('hotspotStateChange')9+ @@ -2310,19 +3100,34 @@ Unregisters the hotspot state change events. **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | type | string | Yes| Event type. The value is **hotspotStateChange**.| - | callback | Callback<number> | No| Callback for the hotspot state change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| type | string | Yes| Event type. The value is **hotspotStateChange**.| +| callback | Callback<number> | No| Callback for the hotspot state change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2601000 | Operation failed.| +**Example** + ```js + import wifi from '@ohos.wifiManager'; + + var recvHotspotStateChangeFunc = result => { + console.info("Receive hotspot state change event: " + result); + } + + // Register an event. + wifiManager.on("hotspotStateChange", recvHotspotStateChangeFunc); + + // Unregister an event. + wifiManager.off("hotspotStateChange", recvHotspotStateChangeFunc); + ``` + ## wifi.on('p2pStateChange')9+ on(type: "p2pStateChange", callback: Callback<number>): void @@ -2335,10 +3140,10 @@ Registers the P2P state change events. **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | type | string | Yes| Event type. The value is **p2pStateChange**.| - | callback | Callback<number> | Yes| Callback invoked to return the P2P state.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| type | string | Yes| Event type. The value is **p2pStateChange**.| +| callback | Callback<number> | Yes| Callback invoked to return the P2P state.| **P2P states** @@ -2354,8 +3159,8 @@ Registers the P2P state change events. For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2801000 | Operation failed.| ## wifi.off('p2pStateChange')9+ @@ -2370,19 +3175,34 @@ Unregisters the P2P state change events. **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | type | string | Yes| Event type. The value is **p2pStateChange**.| - | callback | Callback<number> | No| Callback for the P2P state change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| type | string | Yes| Event type. The value is **p2pStateChange**.| +| callback | Callback<number> | No| Callback for the P2P state change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2801000 | Operation failed.| +**Example** + ```js + import wifi from '@ohos.wifiManager'; + + var recvP2pStateChangeFunc = result => { + console.info("Receive p2p state change event: " + result); + } + + // Register an event. + wifiManager.on("p2pStateChange", recvP2pStateChangeFunc); + + // Unregister an event. + wifiManager.off("p2pStateChange", recvP2pStateChangeFunc); + ``` + ## wifi.on('p2pConnectionChange')9+ on(type: "p2pConnectionChange", callback: Callback<WifiP2pLinkedInfo>): void @@ -2395,17 +3215,17 @@ Registers the P2P connection state change events. **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | type | string | Yes| Event type. The value is **p2pConnectionChange**.| - | callback | Callback<[WifiP2pLinkedInfo](#wifip2plinkedinfo9)> | Yes| Callback invoked to return the P2P connection state.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| type | string | Yes| Event type. The value is **p2pConnectionChange**.| +| callback | Callback<[WifiP2pLinkedInfo](#wifip2plinkedinfo9)> | Yes| Callback invoked to return the P2P connection state.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2801000 | Operation failed.| ## wifi.off('p2pConnectionChange')9+ @@ -2420,42 +3240,57 @@ Unregisters the P2P connection state change events. **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | type | string | Yes| Event type. The value is **p2pConnectionChange**.| - | callback | Callback<[WifiP2pLinkedInfo](#wifip2plinkedinfo9)> | No| Callback for the P2P connection state change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| type | string | Yes| Event type. The value is **p2pConnectionChange**.| +| callback | Callback<[WifiP2pLinkedInfo](#wifip2plinkedinfo9)> | No| Callback for the P2P connection state change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2801000 | Operation failed.| +**Example** + ```js + import wifi from '@ohos.wifiManager'; + + var recvP2pConnectionChangeFunc = result => { + console.info("Receive p2p connection change event: " + result); + } + + // Register an event. + wifiManager.on("p2pConnectionChange", recvP2pConnectionChangeFunc); + + // Unregister an event. + wifiManager.off("p2pConnectionChange", recvP2pConnectionChangeFunc); + ``` + ## wifi.on('p2pDeviceChange')9+ on(type: "p2pDeviceChange", callback: Callback<WifiP2pDevice>): void Registers the P2P device state change events. -**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION +**Required permissions**: ohos.permission.GET_WIFI_INFO, ohos.permission.LOCATION, and ohos.permission.APPROXIMATELY_LOCATION **System capability**: SystemCapability.Communication.WiFi.P2P **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | type | string | Yes| Event type. The value is **p2pDeviceChange**.| - | callback | Callback<[WifiP2pDevice](#wifip2pdevice9)> | Yes| Callback invoked to return the P2P device state.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| type | string | Yes| Event type. The value is **p2pDeviceChange**.| +| callback | Callback<[WifiP2pDevice](#wifip2pdevice9)> | Yes| Callback invoked to return the P2P device state.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2801000 | Operation failed.| ## wifi.off('p2pDeviceChange')9+ @@ -2464,48 +3299,63 @@ off(type: "p2pDeviceChange", callback?: Callback<WifiP2pDevice>): void Unregisters the P2P device state change events. -**Required permissions**: ohos.permission.LOCATION +**Required permissions**: ohos.permission.LOCATION and ohos.permission.APPROXIMATELY_LOCATION **System capability**: SystemCapability.Communication.WiFi.P2P **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | type | string | Yes| Event type. The value is **p2pDeviceChange**.| - | callback | Callback<[WifiP2pDevice](#wifip2pdevice9)> | No| Callback for the P2P device state change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| type | string | Yes| Event type. The value is **p2pDeviceChange**.| +| callback | Callback<[WifiP2pDevice](#wifip2pdevice9)> | No| Callback for the P2P device state change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2801000 | Operation failed.| +**Example** + ```js + import wifi from '@ohos.wifiManager'; + + var recvP2pDeviceChangeFunc = result => { + console.info("Receive recv p2p device change event: " + result); + } + + // Register an event. + wifiManager.on("p2pDeviceChange", recvP2pDeviceChangeFunc); + + // Unregister an event. + wifiManager.off("p2pDeviceChange", recvP2pDeviceChangeFunc); + ``` + ## wifi.on('p2pPeerDeviceChange')9+ on(type: "p2pPeerDeviceChange", callback: Callback<WifiP2pDevice[]>): void Registers the P2P peer device state change events. -**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION +**Required permissions**: ohos.permission.GET_WIFI_INFO, ohos.permission.LOCATION, and ohos.permission.APPROXIMATELY_LOCATION **System capability**: SystemCapability.Communication.WiFi.P2P **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | type | string | Yes| Event type. The value is **p2pPeerDeviceChange**.| - | callback | Callback<[WifiP2pDevice[]](#wifip2pdevice9)> | Yes| Callback invoked to return the P2P peer device state.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| type | string | Yes| Event type. The value is **p2pPeerDeviceChange**.| +| callback | Callback<[WifiP2pDevice[]](#wifip2pdevice9)> | Yes| Callback invoked to return the P2P peer device state.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2801000 | Operation failed.| ## wifi.off('p2pPeerDeviceChange')9+ @@ -2514,25 +3364,40 @@ off(type: "p2pPeerDeviceChange", callback?: Callback<WifiP2pDevice[]>): vo Unregisters the P2P peer device state change events. -**Required permissions**: ohos.permission.LOCATION +**Required permissions**: ohos.permission.LOCATION and ohos.permission.APPROXIMATELY_LOCATION **System capability**: SystemCapability.Communication.WiFi.P2P **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | type | string | Yes| Event type. The value is **p2pPeerDeviceChange**.| - | callback | Callback<[WifiP2pDevice[]](#wifip2pdevice9)> | No| Callback for the P2P peer device state change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| type | string | Yes| Event type. The value is **p2pPeerDeviceChange**.| +| callback | Callback<[WifiP2pDevice[]](#wifip2pdevice9)> | No| Callback for the P2P peer device state change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2801000 | Operation failed.| +**Example** + ```js + import wifi from '@ohos.wifiManager'; + + var recvP2pPeerDeviceChangeFunc = result => { + console.info("Receive recv p2p peer device change event: " + result); + } + + // Register an event. + wifiManager.on("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc); + + // Unregister an event. + wifiManager.off("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc); + ``` + ## wifi.on('p2pPersistentGroupChange')9+ on(type: "p2pPersistentGroupChange", callback: Callback<void>): void @@ -2545,17 +3410,17 @@ Registers the P2P persistent group state change events. **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | type | string | Yes| Event type. The value is **p2pPersistentGroupChange**.| - | callback | Callback<void> | Yes| Callback invoked to return the P2P persistent group state.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| type | string | Yes| Event type. The value is **p2pPersistentGroupChange**.| +| callback | Callback<void> | Yes| Callback invoked to return the P2P persistent group state.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2801000 | Operation failed.| ## wifi.off('p2pPersistentGroupChange')9+ @@ -2570,19 +3435,34 @@ Unregisters the P2P persistent group state change events. **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | type | string | Yes| Event type. The value is **p2pPersistentGroupChange**.| - | callback | Callback<void> | No| Callback for the P2P persistent group state change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| type | string | Yes| Event type. The value is **p2pPersistentGroupChange**.| +| callback | Callback<void> | No| Callback for the P2P persistent group state change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2801000 | Operation failed.| +**Example** + ```js + import wifi from '@ohos.wifiManager'; + + var recvP2pPersistentGroupChangeFunc = result => { + console.info("Receive recv p2p persistent group change event: " + result); + } + + // Register an event. + wifiManager.on("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc); + + // Unregister an event. + wifiManager.off("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc); + ``` + ## wifi.on('p2pDiscoveryChange')9+ on(type: "p2pDiscoveryChange", callback: Callback<number>): void @@ -2595,10 +3475,10 @@ Registers the P2P device discovery state change events. **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | type | string | Yes| Event type. The value is **p2pDiscoveryChange**.| - | callback | Callback<number> | Yes| Callback invoked to return the P2P device discovery state.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| type | string | Yes| Event type. The value is **p2pDiscoveryChange**.| +| callback | Callback<number> | Yes| Callback invoked to return the P2P device discovery state.| **P2P discovered device states** @@ -2611,8 +3491,8 @@ Registers the P2P device discovery state change events. For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2801000 | Operation failed.| ## wifi.off('p2pDiscoveryChange')9+ @@ -2627,15 +3507,30 @@ Unregisters the P2P device discovery state change events. **Parameters** - | **Name**| **Type**| **Mandatory**| **Description**| - | -------- | -------- | -------- | -------- | - | type | string | Yes| Event type. The value is **p2pDiscoveryChange**.| - | callback | Callback<number> | No| Callback for the P2P device discovery state change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| +| **Name**| **Type**| **Mandatory**| **Description**| +| -------- | -------- | -------- | -------- | +| type | string | Yes| Event type. The value is **p2pDiscoveryChange**.| +| callback | Callback<number> | No| Callback for the P2P device discovery state change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| **Error codes** For details about the error codes, see [Wi-Fi Error Codes](../errorcodes/errorcode-wifi.md). -| **Type**| **Description**| - | -------- | -------- | +| **ID**| **Error Message**| +| -------- | -------- | | 2801000 | Operation failed.| + +**Example** + ```js + import wifi from '@ohos.wifiManager'; + + var recvP2pDiscoveryChangeFunc = result => { + console.info("Receive recv p2p discovery change event: " + result); + } + + // Register an event. + wifiManager.on("p2pDiscoveryChange", recvP2pDiscoveryChangeFunc); + + // Unregister an event. + wifiManager.off("p2pDiscoveryChange", recvP2pDiscoveryChangeFunc); + ```