js-apis-wifi.md 43.0 KB
Newer Older
A
annie_wangli 已提交
1
# WLAN
W
wusongqing 已提交
2

A
Annie_wang 已提交
3
> **NOTE**<br>
A
annie_wangli 已提交
4
> The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
W
wusongqing 已提交
5 6


A
annie_wangli 已提交
7 8 9
## Modules to Import

```js
Z
zengyawen 已提交
10
import wifi from '@ohos.wifi';
W
wusongqing 已提交
11 12
```

A
annie_wangli 已提交
13
## wifi.isWifiActive
W
wusongqing 已提交
14

A
annie_wangli 已提交
15
isWifiActive(): boolean
W
wusongqing 已提交
16

Z
zengyawen 已提交
17
Checks whether the WLAN is activated.
W
wusongqing 已提交
18

A
Annie_wang 已提交
19
**Required permissions**: ohos.permission.GET_WIFI_INFO
A
annie_wangli 已提交
20

A
Annie_wang 已提交
21
**System capability**: SystemCapability.Communication.WiFi.STA
W
wusongqing 已提交
22

A
Annie_wang 已提交
23
**Return value**
A
Annie_wang 已提交
24 25 26
| **Type**| **Description**|
| -------- | -------- |
| boolean | Returns **true** if the WLAN is activated; returns **false** otherwise.|
W
wusongqing 已提交
27 28


A
annie_wangli 已提交
29
## wifi.scan
Z
zengyawen 已提交
30

A
annie_wangli 已提交
31
scan(): boolean
Z
zengyawen 已提交
32 33 34

Starts a scan for WLAN.

A
Annie_wang 已提交
35
**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.LOCATION
Z
zengyawen 已提交
36

A
Annie_wang 已提交
37
**System capability**: SystemCapability.Communication.WiFi.STA
Z
zengyawen 已提交
38

A
Annie_wang 已提交
39
**Return value**
A
Annie_wang 已提交
40 41 42
| **Type**| **Description**|
| -------- | -------- |
| boolean | Returns **true** if the scan is successful; returns **false** otherwise.|
Z
zengyawen 已提交
43 44


A
annie_wangli 已提交
45 46 47
## wifi.getScanInfos

getScanInfos(): Promise&lt;Array&lt;WifiScanInfo&gt;&gt;
Z
zengyawen 已提交
48

A
Annie_wang 已提交
49
Obtains the scan result. This API uses a promise to return the result.
Z
zengyawen 已提交
50

A
Annie_wang 已提交
51
**Required permissions**: at least one of ohos.permission.GET_WIFI_INFO, ohos.permission.GET_WIFI_PEERS_MAC, and ohos.permission.LOCATION
A
annie_wangli 已提交
52

A
Annie_wang 已提交
53
**System capability**: SystemCapability.Communication.WiFi.STA
Z
zengyawen 已提交
54

A
Annie_wang 已提交
55
**Return value**
A
Annie_wang 已提交
56 57 58
| **Type**| **Description**|
| -------- | -------- |
| Promise&lt;&nbsp;Array&lt;[WifiScanInfo](#wifiscaninfo)&gt;&nbsp;&gt; | Promise used to return the hotspots detected.|
Z
zengyawen 已提交
59 60


A
annie_wangli 已提交
61
## wifi.getScanInfos
Z
zengyawen 已提交
62

A
annie_wangli 已提交
63
getScanInfos(callback: AsyncCallback&lt;Array&lt;WifiScanInfo&gt;&gt;): void
Z
zengyawen 已提交
64

A
Annie_wang 已提交
65
Obtains the scan result. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
66

A
Annie_wang 已提交
67
**Required permissions**: at least one of ohos.permission.GET_WIFI_INFO, ohos.permission.GET_WIFI_PEERS_MAC, and ohos.permission.LOCATION
A
annie_wangli 已提交
68

A
Annie_wang 已提交
69
**System capability**: SystemCapability.Communication.WiFi.STA
A
annie_wangli 已提交
70

A
Annie_wang 已提交
71
**Parameters**
A
Annie_wang 已提交
72 73 74
| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;&nbsp;Array&lt;[WifiScanInfo](#wifiscaninfo)&gt;&gt; | Yes| Callback invoked to return the hotspots detected.|
A
annie_wangli 已提交
75

A
Annie_wang 已提交
76
**Example**
A
annie_wangli 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
  ```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

Represents WLAN hotspot information.

| **Name**| **Type**| **Readable/Writable**| **Description**|
| -------- | -------- | -------- | -------- |
| ssid | string | Read only| Hotspot service set identifier (SSID), in UTF-8 format.|
| bssid | string | Read only| Basic service set identifier (BSSID) of the hotspot.|
| capabilities | string | Read only| Hotspot capabilities.|
| securityType | [WifiSecurityType](#WifiSecurityType) | Read only| WLAN security type.|
A
Annie_wang 已提交
129
| rssi | number | Read only| Received signal strength indicator (RSSI) of the hotspot, in dBm.|
A
annie_wangli 已提交
130 131 132
| band | number | Read only| Frequency band of the WLAN access point (AP).|
| frequency | number | Read only| Frequency of the WLAN AP.|
| channelWidth | number | Read only| Bandwidth of the WLAN AP.|
A
Annie_wang 已提交
133 134
| centerFrequency0 | number | Read only| Center frequency.|
| centerFrequency1 | number | Read only| Center frequency.|
A
annie_wangli 已提交
135
| timestamp | number | Read only| Timestamp.|
W
wusongqing 已提交
136 137


A
annie_wangli 已提交
138
## WifiSecurityType
W
wusongqing 已提交
139

A
annie_wangli 已提交
140
Enumerates the WLAN security types.
W
wusongqing 已提交
141

A
annie_wangli 已提交
142 143
| **Name**| **Default Value**| **Description**|
| -------- | -------- | -------- |
A
annie_wangli 已提交
144 145 146 147 148
| WIFI_SEC_TYPE_INVALID | 0 | Invalid security type|
| WIFI_SEC_TYPE_OPEN | 1 | Open security type|
| WIFI_SEC_TYPE_WEP | 2 | Wired Equivalent Privacy (WEP)|
| WIFI_SEC_TYPE_PSK | 3 | Pre-shared key (PSK)|
| WIFI_SEC_TYPE_SAE | 4 | Simultaneous Authentication of Equals (SAE)|
W
wusongqing 已提交
149 150


A
annie_wangli 已提交
151 152 153 154
## wifi.addUntrustedConfig<sup>7+</sup>

addUntrustedConfig(config: WifiDeviceConfig): Promise&lt;boolean&gt;

A
Annie_wang 已提交
155
Adds untrusted WLAN configuration. This API uses a promise to return the result.
A
annie_wangli 已提交
156

A
Annie_wang 已提交
157
**Required permissions**:
A
annie_wangli 已提交
158 159
  ohos.permission.SET_WIFI_INFO

A
Annie_wang 已提交
160
**System capability**:
A
annie_wangli 已提交
161 162
  SystemCapability.Communication.WiFi.STA

A
Annie_wang 已提交
163
**Parameters**
A
Annie_wang 已提交
164 165 166
| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| config | [WifiDeviceConfig](#WifiDeviceConfig) | Yes| WLAN configuration to add.|
A
annie_wangli 已提交
167

A
Annie_wang 已提交
168
**Return value**
A
Annie_wang 已提交
169 170 171
| **Type**| **Description**|
| -------- | -------- |
| Promise&lt;boolean&gt; | Promise used to return the operation result. The value **true** indicates that the operation is successful; **false** indicates the opposite.|
A
annie_wangli 已提交
172 173 174 175 176 177 178

## WifiDeviceConfig

Represents the WLAN configuration.

| **Name**| **Type**| **Readable/Writable**| **Description**|
| -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
179
| ssid | string | Read only| Hotspot service set identifier (SSID), in UTF-8 format.|
A
annie_wangli 已提交
180 181 182
| bssid | string | Read only| BSSID of the hotspot.|
| preSharedKey | string | Read only| Private key of the hotspot.|
| isHiddenSsid | boolean | Read only| Whether to hide the network.|
A
Annie_wang 已提交
183
| securityType | [WifiSecurityType](#WifiSecurityType) | Read only| Security type.|
A
annie_wangli 已提交
184 185 186 187 188 189


## wifi.addUntrustedConfig<sup>7+</sup>

addUntrustedConfig(config: WifiDeviceConfig, callback: AsyncCallback&lt;boolean&gt;): void

A
Annie_wang 已提交
190
Adds untrusted WLAN configuration. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
191

A
Annie_wang 已提交
192
**Required permissions**: ohos.permission.SET_WIFI_INFO
A
annie_wangli 已提交
193

A
Annie_wang 已提交
194
**System capability**: SystemCapability.Communication.WiFi.STA
A
annie_wangli 已提交
195

A
Annie_wang 已提交
196
**Parameters**
A
Annie_wang 已提交
197 198 199 200
| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| config | [WifiDeviceConfig](#WifiDeviceConfig) | Yes| WLAN configuration to add.|
| callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return the operation result. The value **true** indicates that the operation is successful; **false** indicates the opposite.|
A
annie_wangli 已提交
201 202 203 204 205 206


## wifi.removeUntrustedConfig<sup>7+</sup>

removeUntrustedConfig(config: WifiDeviceConfig): Promise&lt;boolean&gt;

A
Annie_wang 已提交
207
Removes untrusted WLAN configuration. This API uses a promise to return the result.
A
annie_wangli 已提交
208

A
Annie_wang 已提交
209
**Required permissions**: ohos.permission.SET_WIFI_INFO
A
annie_wangli 已提交
210

A
Annie_wang 已提交
211
**System capability**: SystemCapability.Communication.WiFi.STA
A
annie_wangli 已提交
212

A
Annie_wang 已提交
213
**Parameters**
A
Annie_wang 已提交
214 215 216
| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| config | [WifiDeviceConfig](#WifiDeviceConfig) | Yes| WLAN configuration to remove. |
A
annie_wangli 已提交
217

A
Annie_wang 已提交
218
**Return value**
A
Annie_wang 已提交
219 220 221
| **Type**| **Description**|
| -------- | -------- |
| Promise&lt;boolean&gt; | Promise used to return the operation result. The value **true** indicates that the operation is successful; **false** indicates the opposite.|
A
annie_wangli 已提交
222 223 224 225 226 227


## wifi.removeUntrustedConfig<sup>7+</sup>

removeUntrustedConfig(config: WifiDeviceConfig, callback: AsyncCallback&lt;boolean&gt;): void

A
Annie_wang 已提交
228
Removes untrusted WLAN configuration. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
229

A
Annie_wang 已提交
230
**Required permissions**: ohos.permission.SET_WIFI_INFO
W
wusongqing 已提交
231

A
Annie_wang 已提交
232
**System capability**: SystemCapability.Communication.WiFi.STA
Z
zengyawen 已提交
233

A
Annie_wang 已提交
234
**Parameters**
A
Annie_wang 已提交
235 236 237 238
| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| config | [WifiDeviceConfig](#WifiDeviceConfig) | Yes| WLAN configuration to remove. |
| callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return the operation result. The value **true** indicates that the operation is successful; **false** indicates the opposite.|
Z
zengyawen 已提交
239 240


A
annie_wangli 已提交
241 242 243 244
## wifi.getSignalLevel

getSignalLevel(rssi: number, band: number): number

A
Annie_wang 已提交
245
Obtains the WLAN signal level.
A
annie_wangli 已提交
246

A
Annie_wang 已提交
247
**Required permissions**: ohos.permission.GET_WIFI_INFO
A
annie_wangli 已提交
248

A
Annie_wang 已提交
249
**System capability**: SystemCapability.Communication.WiFi.STA
A
annie_wangli 已提交
250

A
Annie_wang 已提交
251
**Parameters**
A
Annie_wang 已提交
252 253 254 255
| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| rssi | number | Yes| RSSI of the hotspot, in dBm. |
| band | number | Yes| Frequency band of the WLAN AP.|
A
annie_wangli 已提交
256

A
Annie_wang 已提交
257
**Return value**
A
Annie_wang 已提交
258 259 260
| **Type**| **Description**|
| -------- | -------- |
| number | Signal level obtained. The value range is [0,&nbsp;4].|
A
annie_wangli 已提交
261 262 263 264 265


## wifi.getLinkedInfo

getLinkedInfo(): Promise&lt;WifiLinkedInfo&gt;
Z
zengyawen 已提交
266

A
Annie_wang 已提交
267
Obtains WLAN connection information. This API uses a promise to return the result.
Z
zengyawen 已提交
268

A
Annie_wang 已提交
269
**Required permissions**: ohos.permission.GET_WIFI_INFO
A
annie_wangli 已提交
270

A
Annie_wang 已提交
271
**System capability**: SystemCapability.Communication.WiFi.STA
Z
zengyawen 已提交
272

A
Annie_wang 已提交
273
**Return value**
A
Annie_wang 已提交
274 275 276
| Type| Description|
| -------- | -------- |
| Promise&lt;[WifiLinkedInfo](#WifiLinkedInfo)&gt; | Promise used to return the WLAN connection information obtained.|
Z
zengyawen 已提交
277 278


A
annie_wangli 已提交
279
## wifi.getLinkedInfo
Z
zengyawen 已提交
280

A
annie_wangli 已提交
281
getLinkedInfo(callback: AsyncCallback&lt;WifiLinkedInfo&gt;): void
Z
zengyawen 已提交
282

A
Annie_wang 已提交
283
Obtains WLAN connection information. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
284

A
Annie_wang 已提交
285
**Required permissions**: ohos.permission.GET_WIFI_INFO
A
annie_wangli 已提交
286

A
Annie_wang 已提交
287
**System capability**: SystemCapability.Communication.WiFi.STA
A
annie_wangli 已提交
288

A
Annie_wang 已提交
289
**Parameters**
A
Annie_wang 已提交
290 291 292
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;[WifiLinkedInfo](#WifiLinkedInfo)&gt; | Yes| Callback invoked to return the WLAN connection information obtained.|
A
annie_wangli 已提交
293

A
Annie_wang 已提交
294
**Example**
A
annie_wangli 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
  ```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

Represents the WLAN connection information.

| Name| Type| Readable/Writable| Description|
| -------- | -------- | -------- | -------- |
A
annie_wangli 已提交
320
| ssid | string | Read only| Hotspot SSID, in UTF-8 format.|
A
annie_wangli 已提交
321
| bssid | string | Read only| BSSID of the hotspot.|
A
Annie_wang 已提交
322
| rssi | number | Read only| RSSI of the hotspot, in dBm. |
A
annie_wangli 已提交
323 324 325
| band | number | Read only| Frequency band of the WLAN AP.|
| linkSpeed | number | Read only| Speed of the WLAN AP.|
| frequency | number | Read only| Frequency of the WLAN AP.|
A
Annie_wang 已提交
326
| isHidden | boolean | Read only| Whether to hide the WLAN AP.|
A
Annie_wang 已提交
327
| isRestricted | boolean | Read only| Whether to restrict data volume at the WLAN AP.|
A
annie_wangli 已提交
328 329 330 331 332 333
| macAddress | string | Read only| MAC address of the device.|
| ipAddress | number | Read only| IP address of the device that sets up the WLAN connection.|
| connState | [ConnState](#ConnState) | Read only| WLAN connection state.|


## ConnState
Z
zengyawen 已提交
334 335 336

Enumerates the WLAN connection states.

A
annie_wangli 已提交
337 338 339 340 341 342 343 344 345 346
| Name| Default Value| Description|
| -------- | -------- | -------- |
| SCANNING | 0 | The device is scanning for available APs.|
| CONNECTING | 1 | A WLAN connection is being established.|
| AUTHENTICATING | 2 | An authentication is being performed for a WLAN connection.|
| OBTAINING_IPADDR | 3 | The IP address of the WLAN connection is being acquired.|
| CONNECTED | 4 | A WLAN connection is established.|
| DISCONNECTING | 5 | The WLAN connection is being disconnected.|
| DISCONNECTED | 6 | The WLAN connection is disconnected.|
| UNKNOWN | 7 | Failed to set up a WLAN connection.|
Z
zengyawen 已提交
347 348


A
annie_wangli 已提交
349
## wifi.isConnected<sup>7+</sup>
Z
zengyawen 已提交
350

A
annie_wangli 已提交
351 352 353
isConnected(): boolean

Checks whether the WLAN is connected.
Z
zengyawen 已提交
354

A
Annie_wang 已提交
355
**Required permissions**: ohos.permission.GET_WIFI_INFO
Z
zengyawen 已提交
356

A
Annie_wang 已提交
357
**System capability**: SystemCapability.Communication.WiFi.STA
Z
zengyawen 已提交
358

A
Annie_wang 已提交
359
**Return value**
A
Annie_wang 已提交
360 361 362
| **Type**| **Description**|
| -------- | -------- |
| boolean | Returns **true** if the WLAN is connected; returns **false** otherwise.|
A
annie_wangli 已提交
363 364 365 366 367


## wifi.isFeatureSupported<sup>7+</sup>

isFeatureSupported(featureId: number): boolean
Z
zengyawen 已提交
368 369 370

Checks whether the device supports the specified WLAN feature.

A
Annie_wang 已提交
371
**Required permissions**: ohos.permission.GET_WIFI_INFO
A
annie_wangli 已提交
372

A
Annie_wang 已提交
373 374 375
**System capability**: SystemCapability.Communication.WiFi.Core

**Parameters**
A
annie_wangli 已提交
376

A
Annie_wang 已提交
377 378 379
| **Name**| **Type**| Mandatory| **Description**|
| -------- | -------- | -------- | -------- |
| featureId | number | Yes| Feature ID.|
A
annie_wangli 已提交
380

A
Annie_wang 已提交
381
**Return value**
A
Annie_wang 已提交
382 383 384
| **Type**| **Description**|
| -------- | -------- |
| boolean | Returns **true** if the feature is supported; returns **false** otherwise.|
A
annie_wangli 已提交
385

A
Annie_wang 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398 399
**Feature IDs**

| Value| Description|
| -------- | -------- |
| 0x0001 | WLAN infrastructure mode|
| 0x0002 | 5 GHz bandwidth|
| 0x0004 | Generic Advertisement Service (GAS)/Access Network Query Protocol (ANQP) feature|
| 0x0008 | Wi-Fi Direct|
| 0x0010 | SoftAP|
| 0x0040 | Wi-Fi AWare|
| 0x8000 | WLAN AP/STA concurrency|
| 0x8000000 | WPA3 Personal (WPA-3 SAE)|
| 0x10000000 | WPA3-Enterprise&nbsp;Suite-B |
| 0x20000000 | Enhanced open feature|
A
annie_wangli 已提交
400 401 402 403 404 405 406 407


## wifi.getIpInfo<sup>7+</sup>

getIpInfo(): IpInfo

Obtains IP information.

A
Annie_wang 已提交
408
**Required permissions**: ohos.permission.GET_WIFI_INFO
A
annie_wangli 已提交
409

A
Annie_wang 已提交
410
**System capability**: SystemCapability.Communication.WiFi.STA
A
annie_wangli 已提交
411

A
Annie_wang 已提交
412
**Return value**
A
Annie_wang 已提交
413 414 415
| **Type**| **Description**|
| -------- | -------- |
| [IpInfo](#IpInfo) | IP information obtained.|
A
annie_wangli 已提交
416 417 418 419 420 421 422 423


## IpInfo<sup>7+</sup>

Represents IP information.

| **Name**| **Type**| **Readable/Writable**| **Description**|
| -------- | -------- | -------- | -------- |
A
annie_wangli 已提交
424 425 426 427 428 429 430
| ipAddress | number | Read only| IP address|
| gateway | number | Read only| Gateway|
| netmask | number | Read only| Subnet mask|
| primaryDns | number | Read only| IP address of the preferred DNS server|
| secondDns | number | Read only| IP address of the alternate DNS server|
| serverIp | number | Read only| IP address of the DHCP server|
| leaseDuration | number | Read only| Lease duration of the IP address|
A
annie_wangli 已提交
431 432 433 434 435 436 437 438


## wifi.getCountryCode<sup>7+</sup>

getCountryCode(): string

Obtains the country code.

A
Annie_wang 已提交
439
**Required permissions**: ohos.permission.GET_WIFI_INFO
A
annie_wangli 已提交
440

A
Annie_wang 已提交
441
**System capability**: SystemCapability.Communication.WiFi.Core
A
annie_wangli 已提交
442

A
Annie_wang 已提交
443
**Return value**
A
Annie_wang 已提交
444 445 446
| **Type**| **Description**|
| -------- | -------- |
| string | Country code obtained.|
A
annie_wangli 已提交
447 448 449 450 451 452


## wifi.getP2pLinkedInfo<sup>8+</sup>

getP2pLinkedInfo(): Promise&lt;WifiP2pLinkedInfo&gt;

A
Annie_wang 已提交
453
Obtains peer-to-peer (P2P) connection information. This API uses a promise to return the result.
A
annie_wangli 已提交
454

A
Annie_wang 已提交
455
**Required permissions**: ohos.permission.GET_WIFI_INFO
A
annie_wangli 已提交
456

A
Annie_wang 已提交
457
**System capability**: SystemCapability.Communication.WiFi.P2P
A
annie_wangli 已提交
458

A
Annie_wang 已提交
459
**Return value**
A
Annie_wang 已提交
460 461 462
| Type| Description|
| -------- | -------- |
| Promise&lt;[WifiP2pLinkedInfo](#WifiP2pLinkedInfo)&gt; | Promise used to return the P2P connection information obtained.|
A
annie_wangli 已提交
463 464 465 466 467 468


## wifi.getP2pLinkedInfo<sup>8+</sup>

getP2pLinkedInfo(callback: AsyncCallback&lt;WifiP2pLinkedInfo&gt;): void

A
Annie_wang 已提交
469
Obtains P2P connection information. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
470

A
Annie_wang 已提交
471
**Required permissions**: ohos.permission.GET_WIFI_INFO
A
annie_wangli 已提交
472

A
Annie_wang 已提交
473
**System capability**: SystemCapability.Communication.WiFi.P2P
A
annie_wangli 已提交
474

A
Annie_wang 已提交
475
**Parameters**
A
Annie_wang 已提交
476 477 478
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;[WifiP2pLinkedInfo](#WifiP2pLinkedInfo)&gt; | Yes| Callback used to return the P2P connection information obtained.|
A
annie_wangli 已提交
479 480 481 482 483 484 485 486 487


## WifiP2pLinkedInfo<sup>8+</sup>

Represents the WLAN connection information.

| Name| Type| Readable/Writable| Description|
| -------- | -------- | -------- | -------- |
| connectState | [P2pConnectState](#P2pConnectState) | Read only| P2P connection state.|
A
Annie_wang 已提交
488 489
| isGroupOwner | boolean | Read only| Whether it is a group owner.|
| groupOwnerAddr | string | Read only| MAC address of the group owner.|
A
annie_wangli 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505


## P2pConnectState<sup>8+</sup>

Enumerates the P2P connection states.

| Name| Default Value| Description|
| -------- | -------- | -------- |
| DISCONNECTED | 0 | Disconnected|
| CONNECTED | 1 | Connected|


## wifi.getCurrentGroup<sup>8+</sup>

getCurrentGroup(): Promise&lt;WifiP2pGroupInfo&gt;

A
Annie_wang 已提交
506
Obtains the current P2P group information. This API uses a promise to return the result.
A
annie_wangli 已提交
507

A
Annie_wang 已提交
508
**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION
A
annie_wangli 已提交
509

A
Annie_wang 已提交
510
**System capability**: SystemCapability.Communication.WiFi.P2P
A
annie_wangli 已提交
511

A
Annie_wang 已提交
512
**Return value**
A
Annie_wang 已提交
513 514 515
| Type| Description|
| -------- | -------- |
| Promise&lt;[WifiP2pGroupInfo](#WifiP2pGroupInfo)&gt; | Promise used to return the P2P group information obtained.|
A
annie_wangli 已提交
516 517 518 519 520 521


## wifi.getCurrentGroup<sup>8+</sup>

getCurrentGroup(callback: AsyncCallback&lt;WifiP2pGroupInfo&gt;): void

A
Annie_wang 已提交
522
Obtains the P2P group information. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
523

A
Annie_wang 已提交
524
**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION
A
annie_wangli 已提交
525

A
Annie_wang 已提交
526
**System capability**: SystemCapability.Communication.WiFi.P2P
A
annie_wangli 已提交
527

A
Annie_wang 已提交
528
**Parameters**
A
Annie_wang 已提交
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;[WifiP2pGroupInfo](#WifiP2pGroupInfo)&gt; | Yes| Callback used to return the P2P group information obtained.|

## wifi.getP2pGroups<sup>9+</sup>

getP2pGroups(): Promise&lt;Array&lt;WifiP2pGroupInfo&gt;

Obtains information about all P2P groups. This API uses a promise to return the result.

**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION

**System capability**: SystemCapability.Communication.WiFi.P2P

**Return value**
| Type| Description|
| -------- | -------- |
| Promise&lt;&nbsp;Array&lt;[WifiP2pGroupInfo](#WifiP2pGroupInfo)&gt;&nbsp;&gt; | Information about all created P2P groups obtained.|

## wifi.getP2pGroups<sup>9+</sup>

getP2pGroups(callback: AsyncCallback&lt;Array&lt;WifiP2pGroupInfo&gt;): void

Obtains information about all P2P groups. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION

**System capability**: SystemCapability.Communication.WiFi.P2P
A
annie_wangli 已提交
557

A
Annie_wang 已提交
558 559 560 561
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;&nbsp;Array&lt;[WifiP2pGroupInfo](#WifiP2pGroupInfo)&gt;&gt; | Yes| Callback invoked to return the P2P group information obtained.|
A
annie_wangli 已提交
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588

## WifiP2pGroupInfo<sup>8+</sup>

Represents the P2P group information.

| Name| Type| Readable/Writable| Description|
| -------- | -------- | -------- | -------- |
| isP2pGo | boolean | Read only| Whether it is a group.|
| ownerInfo | [WifiP2pDevice](#WifiP2pDevice) | Read only| Device information of the group.|
| passphrase | string | Read only| Private key of the group.|
| interface | string | Read only| Interface name.|
| groupName | string | Read only| Group name.|
| networkId | number | Read only| Network ID.|
| frequency | number | Read only| Frequency of the group.|
| clientDevices | [WifiP2pDevice[]](#WifiP2pDevice) | Read only| List of connected devices.|
| goIpAddress | string | Read only| IP address of the group.|

## WifiP2pDevice<sup>8+</sup>

Represents the P2P device information.

| Name| Type| Readable/Writable| Description|
| -------- | -------- | -------- | -------- |
| deviceName | string | Read only| Device name.|
| deviceAddress | string | Read only| MAC address of the device.|
| primaryDeviceType | string | Read only| Type of the primary device.|
| deviceStatus | [P2pDeviceStatus](#P2pDeviceStatus) | Read only| Device status.|
A
Annie_wang 已提交
589
| groupCapabilitys | number | Read only| Group capabilities.|
A
annie_wangli 已提交
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607

## P2pDeviceStatus<sup>8+</sup>

Enumerates the device states.

| Name| Default Value| Description|
| -------- | -------- | -------- |
| CONNECTED | 0 | Connected|
| INVITED | 1 | Invited|
| FAILED | 2 | Failed|
| AVAILABLE | 3 | Available|
| UNAVAILABLE | 4 | Unavailable|


## wifi.getP2pPeerDevices<sup>8+</sup>

getP2pPeerDevices(): Promise&lt;WifiP2pDevice[]&gt;

A
Annie_wang 已提交
608
Obtains the list of peer devices in a P2P connection. This API uses a promise to return the result.
A
annie_wangli 已提交
609

A
Annie_wang 已提交
610
**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION
A
annie_wangli 已提交
611

A
Annie_wang 已提交
612
**System capability**: SystemCapability.Communication.WiFi.P2P
A
annie_wangli 已提交
613

A
Annie_wang 已提交
614
**Return value**
A
Annie_wang 已提交
615 616 617
| Type| Description|
| -------- | -------- |
| Promise&lt;[WifiP2pDevice[]](#WifiP2pDevice)&gt; | Promise used to return the peer device list obtained.|
A
annie_wangli 已提交
618 619 620 621 622 623


## wifi.getP2pPeerDevices<sup>8+</sup>

getP2pPeerDevices(callback: AsyncCallback&lt;WifiP2pDevice[]&gt;): void

A
Annie_wang 已提交
624
Obtains the list of peer devices in a P2P connection. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
625

A
Annie_wang 已提交
626
**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION
A
annie_wangli 已提交
627

A
Annie_wang 已提交
628
**System capability**: SystemCapability.Communication.WiFi.P2P
A
annie_wangli 已提交
629

A
Annie_wang 已提交
630
**Parameters**
A
Annie_wang 已提交
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;[WifiP2pDevice[]](#WifiP2pDevice)&gt; | Yes| Callback used to return the peer device list obtained.|

## wifi.getP2pLocalDevice<sup>9+</sup>

getP2pLocalDevice(): Promise&lt;WifiP2pDevice&gt;

Obtains local device information. This API uses a promise to return the result.

**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.GET_WIFI_CONFIG

**System capability**: SystemCapability.Communication.WiFi.P2P

**Return value**
| Type| Description|
| -------- | -------- |
| Promise&lt;[WifiP2pDevice](#WifiP2pDevice)&gt; | Promise used to return the local device information obtained.|


## wifi.getP2pLocalDevice<sup>8+</sup>

getP2pLocalDevice(callback: AsyncCallback&lt;WifiP2pDevice&gt;): void

Obtains local device information. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.GET_WIFI_CONFIG

**System capability**: SystemCapability.Communication.WiFi.P2P
A
annie_wangli 已提交
660

A
Annie_wang 已提交
661 662 663 664
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;[WifiP2pDevice](#WifiP2pDevice)&gt; | Yes| Callback invoked to returnthe local device information obtained.|
A
annie_wangli 已提交
665 666 667 668 669 670 671

## wifi.createGroup<sup>8+</sup>

createGroup(config: WifiP2PConfig): boolean;

Creates a P2P group.

A
Annie_wang 已提交
672 673 674
**Required permissions**: ohos.permission.GET_WIFI_INFO

**System capability**: SystemCapability.Communication.WiFi.P2P
A
annie_wangli 已提交
675

A
Annie_wang 已提交
676
**Parameters**
A
annie_wangli 已提交
677

A
Annie_wang 已提交
678 679 680
| **Name**| **Type**| Mandatory| **Description**|
| -------- | -------- | -------- | -------- |
| config | [WifiP2PConfig](#WifiP2PConfig) | Yes| Group configuration.|
A
annie_wangli 已提交
681

A
Annie_wang 已提交
682
**Return value**
A
Annie_wang 已提交
683 684 685
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
A
annie_wangli 已提交
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713

## WifiP2PConfig<sup>8+</sup>

Represents P2P configuration.

| Name| Type| Readable/Writable| Description|
| -------- | -------- | -------- | -------- |
| deviceAddress | string | Read only| Device address.|
| netId | number | Read only| Network ID. The value **-1** indicates that a temporary group, and **-2** indicates that a persistent group.|
| passphrase | string | Read only| Private key of the group.|
| groupName | string | Read only| Name of the group.|
| goBand | [GroupOwnerBand](#GroupOwnerBand) | Read only| Bandwidth of the group.|


## GroupOwnerBand<sup>8+</sup>

Enumerates the P2P group bandwidths.

| Name| Default Value| Description|
| -------- | -------- | -------- |
| GO_BAND_AUTO | 0 | Auto|
| GO_BAND_2GHZ | 1 | 2 GHz|
| GO_BAND_5GHZ | 2 | 5 GHz|

## wifi.removeGroup<sup>8+</sup>

removeGroup(): boolean;

A
Annie_wang 已提交
714
Removes this P2P group.
A
annie_wangli 已提交
715

A
Annie_wang 已提交
716
**Required permissions**: ohos.permission.GET_WIFI_INFO
A
annie_wangli 已提交
717

A
Annie_wang 已提交
718
**System capability**: SystemCapability.Communication.WiFi.P2P
A
annie_wangli 已提交
719

A
Annie_wang 已提交
720
**Return value**
A
Annie_wang 已提交
721 722 723
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
A
annie_wangli 已提交
724 725 726 727 728 729 730 731


## wifi.p2pConnect<sup>8+</sup>

p2pConnect(config: WifiP2PConfig): boolean;

Sets up a P2P connection.

A
Annie_wang 已提交
732
**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION
A
annie_wangli 已提交
733

A
Annie_wang 已提交
734 735 736
**System capability**: SystemCapability.Communication.WiFi.P2P

**Parameters**
A
annie_wangli 已提交
737

A
Annie_wang 已提交
738 739 740
| **Name**| **Type**| Mandatory| **Description**|
| -------- | -------- | -------- | -------- |
| config | [WifiP2PConfig](#WifiP2PConfig) | Yes| Connection configuration.|
A
annie_wangli 已提交
741

A
Annie_wang 已提交
742
**Return value**
A
Annie_wang 已提交
743 744 745
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
A
annie_wangli 已提交
746 747


A
Annie_wang 已提交
748
**Example**
A
annie_wangli 已提交
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
  ```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 = result => {
      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.p2pCancelConnect<sup>8+</sup>

p2pCancelConnect(): boolean;

Cancels this P2P connection.

A
Annie_wang 已提交
821
**Required permissions**: ohos.permission.GET_WIFI_INFO
A
annie_wangli 已提交
822

A
Annie_wang 已提交
823
**System capability**: SystemCapability.Communication.WiFi.P2P
A
annie_wangli 已提交
824

A
Annie_wang 已提交
825
**Return value**
A
Annie_wang 已提交
826 827 828
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
A
annie_wangli 已提交
829 830 831 832 833 834 835 836


## wifi.startDiscoverDevices<sup>8+</sup>

startDiscoverDevices(): boolean;

Starts to discover devices.

A
Annie_wang 已提交
837
**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION
A
annie_wangli 已提交
838

A
Annie_wang 已提交
839
**System capability**: SystemCapability.Communication.WiFi.P2P
A
annie_wangli 已提交
840

A
Annie_wang 已提交
841
**Return value**
A
Annie_wang 已提交
842 843 844
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
A
annie_wangli 已提交
845 846 847 848 849 850 851 852


## wifi.stopDiscoverDevices<sup>8+</sup>

stopDiscoverDevices(): boolean;

Stops discovering devices.

A
Annie_wang 已提交
853
**Required permissions**: ohos.permission.GET_WIFI_INFO
A
annie_wangli 已提交
854

A
Annie_wang 已提交
855
**System capability**: SystemCapability.Communication.WiFi.P2P
A
annie_wangli 已提交
856

A
Annie_wang 已提交
857
**Return value**
A
Annie_wang 已提交
858 859 860
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
A
annie_wangli 已提交
861 862 863 864 865


## wifi.on('wifiStateChange')<sup>7+</sup>

on(type: "wifiStateChange", callback: Callback&lt;number&gt;): void
Z
zengyawen 已提交
866 867 868

Registers the WLAN state change events.

A
Annie_wang 已提交
869
**Required permissions**: ohos.permission.GET_WIFI_INFO
A
annie_wangli 已提交
870

A
Annie_wang 已提交
871
**System capability**: SystemCapability.Communication.WiFi.STA
A
annie_wangli 已提交
872

A
Annie_wang 已提交
873
**Parameters**
A
Annie_wang 已提交
874 875 876 877
| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value is **wifiStateChange**.|
| callback | Callback&lt;number&gt; | Yes| Callback invoked to return the WLAN state.|
A
annie_wangli 已提交
878

A
Annie_wang 已提交
879 880 881 882 883 884 885 886
**WLAN states** 

| **Value**| **Description**|
| -------- | -------- |
| 0 | Deactivated|
| 1 | Activated|
| 2 | Activating|
| 3 | Deactivating|
A
annie_wangli 已提交
887 888 889 890 891


## wifi.off('wifiStateChange')<sup>7+</sup>

off(type: "wifiStateChange", callback?: Callback&lt;number&gt;): void
Z
zengyawen 已提交
892 893 894

Unregisters the WLAN state change events.

A
Annie_wang 已提交
895
**Required permissions**: ohos.permission.GET_WIFI_INFO
A
annie_wangli 已提交
896

A
Annie_wang 已提交
897
**System capability**: SystemCapability.Communication.WiFi.STA
A
annie_wangli 已提交
898

A
Annie_wang 已提交
899
**Parameters**
A
Annie_wang 已提交
900 901 902 903
| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value is **wifiStateChange**.|
| callback | Callback&lt;number&gt; | No| Callback used to return the WLAN state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.|
A
annie_wangli 已提交
904

A
Annie_wang 已提交
905
**Example**
A
annie_wangli 已提交
906 907 908 909 910 911 912 913
  ```js
  import wifi from '@ohos.wifi';
  
  var WIFI_POWER_STATE = "wifiStateChange";
  var recvPowerNotifyFunc = result => {
      console.info("Receive power state change event: " + result);
  }
  
A
Annie_wang 已提交
914
  // Register an event.
A
annie_wangli 已提交
915 916
  wifi.on(WIFI_POWER_STATE, recvPowerNotifyFunc);
  
A
Annie_wang 已提交
917
  // Unregister an event.
A
annie_wangli 已提交
918 919 920 921 922 923 924
  wifi.off(WIFI_POWER_STATE, recvPowerNotifyFunc);
  ```


## wifi.on('wifiConnectionChange')<sup>7+</sup>

on(type: "wifiConnectionChange", callback: Callback&lt;number&gt;): void
Z
zengyawen 已提交
925 926 927

Registers the WLAN connection state change events.

A
Annie_wang 已提交
928
**Required permissions**: ohos.permission.GET_WIFI_INFO
A
annie_wangli 已提交
929

A
Annie_wang 已提交
930
**System capability**: SystemCapability.Communication.WiFi.STA
A
annie_wangli 已提交
931

A
Annie_wang 已提交
932
**Parameters**
A
Annie_wang 已提交
933 934 935 936
| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value is **wifiConnectionChange**.|
| callback | Callback&lt;number&gt; | Yes| Callback invoked to return the WLAN connection state.|
A
annie_wangli 已提交
937

A
Annie_wang 已提交
938 939 940 941 942 943
**WLAN connection states**

| **Value**| **Description**|
| -------- | -------- |
| 0 | Disconnected|
| 1 | Connected|
A
annie_wangli 已提交
944 945 946 947 948


## wifi.off('wifiConnectionChange')<sup>7+</sup>

off(type: "wifiConnectionChange", callback?: Callback&lt;number&gt;): void
Z
zengyawen 已提交
949 950 951

Unregisters the WLAN connection state change events.

A
Annie_wang 已提交
952
**Required permissions**: ohos.permission.GET_WIFI_INFO
A
annie_wangli 已提交
953

A
Annie_wang 已提交
954
**System capability**: SystemCapability.Communication.WiFi.STA
A
annie_wangli 已提交
955

A
Annie_wang 已提交
956
**Parameters**
A
Annie_wang 已提交
957 958 959 960
| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value is **wifiConnectionChange**.|
| callback | Callback&lt;number&gt; | No| Callback used to return the WLAN connection state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.|
A
annie_wangli 已提交
961 962 963 964 965


## wifi.on('wifiScanStateChange')<sup>7+</sup>

on(type: "wifiScanStateChange", callback: Callback&lt;number&gt;): void
Z
zengyawen 已提交
966 967 968

Registers the WLAN scan state change events.

A
Annie_wang 已提交
969
**Required permissions**: ohos.permission.GET_WIFI_INFO
A
annie_wangli 已提交
970

A
Annie_wang 已提交
971
**System capability**: SystemCapability.Communication.WiFi.STA
A
annie_wangli 已提交
972

A
Annie_wang 已提交
973
**Parameters**
A
Annie_wang 已提交
974 975 976 977
| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value is **wifiScanStateChange**.|
| callback | Callback&lt;number&gt; | Yes| Callback invoked to return the WLAN scan state.|
A
annie_wangli 已提交
978

A
Annie_wang 已提交
979 980 981 982 983 984
**WLAN scan states**

| **Value**| **Description**|
| -------- | -------- |
| 0 | Scan failed|
| 1 | Scan successful|
A
annie_wangli 已提交
985 986 987 988 989


## wifi.off('wifiScanStateChange')<sup>7+</sup>

off(type: "wifiScanStateChange", callback?: Callback&lt;number&gt;): void
Z
zengyawen 已提交
990 991 992

Unregisters the WLAN scan state change events.

A
Annie_wang 已提交
993
**Required permissions**: ohos.permission.GET_WIFI_INFO
A
annie_wangli 已提交
994

A
Annie_wang 已提交
995
**System capability**: SystemCapability.Communication.WiFi.STA
A
annie_wangli 已提交
996

A
Annie_wang 已提交
997
**Parameters**
A
annie_wangli 已提交
998 999 1000 1001 1002 1003 1004 1005 1006 1007

| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value is **wifiScanStateChange**.|
| callback | Callback&lt;number&gt; | No| Callback used to return the WLAN scan state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.|


## wifi.on('wifiRssiChange')<sup>7+</sup>

on(type: "wifiRssiChange", callback: Callback&lt;number&gt;): void
Z
zengyawen 已提交
1008

A
Annie_wang 已提交
1009
Registers the RSSI change events.
Z
zengyawen 已提交
1010

A
Annie_wang 已提交
1011
**Required permissions**: ohos.permission.GET_WIFI_INFO
A
annie_wangli 已提交
1012

A
Annie_wang 已提交
1013
**System capability**: SystemCapability.Communication.WiFi.STA
A
annie_wangli 已提交
1014

A
Annie_wang 已提交
1015
**Parameters**
A
Annie_wang 已提交
1016 1017 1018 1019
| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value is **wifiRssiChange**.|
| callback | Callback&lt;number&gt; | Yes| Callback invoked to return the RSSI, in dBm.|
A
annie_wangli 已提交
1020 1021 1022 1023 1024


## wifi.off('wifiRssiChange')<sup>7+</sup>

off(type: "wifiRssiChange", callback?: Callback&lt;number&gt;): void
Z
zengyawen 已提交
1025

A
Annie_wang 已提交
1026
Unregisters the RSSI change events.
Z
zengyawen 已提交
1027

A
Annie_wang 已提交
1028
**Required permissions**: ohos.permission.GET_WIFI_INFO
A
annie_wangli 已提交
1029

A
Annie_wang 已提交
1030
**System capability**: SystemCapability.Communication.WiFi.STA
A
annie_wangli 已提交
1031

A
Annie_wang 已提交
1032
**Parameters**
A
Annie_wang 已提交
1033 1034 1035 1036
| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value is **wifiRssiChange**.|
| callback | Callback&lt;number&gt; | No| Callback used to return the RSSI, in dBm. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.|
A
annie_wangli 已提交
1037 1038 1039 1040 1041


## wifi.on('hotspotStateChange')<sup>7+</sup>

on(type: "hotspotStateChange", callback: Callback&lt;number&gt;): void
Z
zengyawen 已提交
1042 1043 1044

Registers the hotspot state change events.

A
Annie_wang 已提交
1045
**Required permissions**: ohos.permission.GET_WIFI_INFO
A
annie_wangli 已提交
1046

A
Annie_wang 已提交
1047
**System capability**: SystemCapability.Communication.WiFi.AP.Core
A
annie_wangli 已提交
1048

A
Annie_wang 已提交
1049
**Parameters**
A
Annie_wang 已提交
1050 1051 1052 1053
| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value is **hotspotStateChange**.|
| callback | Callback&lt;number&gt; | Yes| Callback invoked to return the hotspot state.|
A
annie_wangli 已提交
1054

A
Annie_wang 已提交
1055 1056 1057 1058 1059 1060 1061 1062
**Hotspot states**

| **Value**| **Description**|
| -------- | -------- |
| 0 | Deactivated|
| 1 | Activated|
| 2 | Activating|
| 3 | Deactivating|
A
annie_wangli 已提交
1063 1064 1065 1066 1067


## wifi.off('hotspotStateChange')<sup>7+</sup>

off(type: "hotspotStateChange", callback?: Callback&lt;number&gt;): void
Z
zengyawen 已提交
1068 1069 1070

Unregisters the hotspot state change events.

A
Annie_wang 已提交
1071
**Required permissions**: ohos.permission.GET_WIFI_INFO
A
annie_wangli 已提交
1072

A
Annie_wang 已提交
1073
**System capability**: SystemCapability.Communication.WiFi.AP.Core
A
annie_wangli 已提交
1074

A
Annie_wang 已提交
1075
**Parameters**
A
Annie_wang 已提交
1076 1077 1078 1079
| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value is **hotspotStateChange**.|
| callback | Callback&lt;number&gt; | No| Callback used to return the hotspot state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.|
A
annie_wangli 已提交
1080 1081 1082 1083 1084 1085


## wifi.on('p2pStateChange')<sup>8+</sup>

on(type: "p2pStateChange", callback: Callback&lt;number&gt;): void

A
Annie_wang 已提交
1086
Registers the P2P state change events.
A
annie_wangli 已提交
1087

A
Annie_wang 已提交
1088
**Required permissions**: ohos.permission.GET_WIFI_INFO
A
annie_wangli 已提交
1089

A
Annie_wang 已提交
1090
**System capability**: SystemCapability.Communication.WiFi.P2P
A
annie_wangli 已提交
1091

A
Annie_wang 已提交
1092
**Parameters**
A
Annie_wang 已提交
1093 1094 1095 1096
| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value is **p2pStateChange**.|
| callback | Callback&lt;number&gt; | Yes| Callback invoked to return the P2P state.|
A
annie_wangli 已提交
1097

A
Annie_wang 已提交
1098 1099 1100 1101 1102 1103 1104 1105 1106
**P2P states**

| **Value**| **Description**|
| -------- | -------- |
| 1 | Available|
| 2 | Opening|
| 3 | Opened|
| 4 | Closing|
| 5 | Closed|
A
annie_wangli 已提交
1107 1108 1109 1110 1111 1112 1113

## wifi.off('p2pStateChange')<sup>8+</sup>

off(type: "p2pStateChange", callback?: Callback&lt;number&gt;): void

Unregisters the P2P state change events.

A
Annie_wang 已提交
1114
**Required permissions**: ohos.permission.GET_WIFI_INFO
A
annie_wangli 已提交
1115

A
Annie_wang 已提交
1116
**System capability**: SystemCapability.Communication.WiFi.P2P
A
annie_wangli 已提交
1117

A
Annie_wang 已提交
1118
**Parameters**
A
Annie_wang 已提交
1119 1120 1121 1122
| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value is **p2pStateChange**.|
| callback | Callback&lt;number&gt; | No| Callback used to return the P2P state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.|
A
annie_wangli 已提交
1123 1124 1125 1126 1127 1128 1129 1130


  ## wifi.on('p2pConnectionChange')<sup>8+</sup>

on(type: "p2pConnectionChange", callback: Callback&lt;WifiP2pLinkedInfo&gt;): void

Registers the P2P connection state change events.

A
Annie_wang 已提交
1131
**Required permissions**: ohos.permission.GET_WIFI_INFO
A
annie_wangli 已提交
1132

A
Annie_wang 已提交
1133
**System capability**: SystemCapability.Communication.WiFi.P2P
A
annie_wangli 已提交
1134

A
Annie_wang 已提交
1135
**Parameters**
A
Annie_wang 已提交
1136 1137 1138 1139
| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value is **p2pConnectionChange**.|
| callback | Callback&lt;[WifiP2pLinkedInfo](#WifiP2pLinkedInfo)&gt; | Yes| Callback invoked to return the P2P connection state.|
A
annie_wangli 已提交
1140 1141 1142 1143 1144 1145 1146 1147


## wifi.off('p2pConnectionChange')<sup>8+</sup>

off(type: "p2pConnectionChange", callback?: Callback&lt;WifiP2pLinkedInfo&gt;): void

Unregisters the P2P connection state change events.

A
Annie_wang 已提交
1148
**Required permissions**: ohos.permission.GET_WIFI_INFO
A
annie_wangli 已提交
1149

A
Annie_wang 已提交
1150
**System capability**: SystemCapability.Communication.WiFi.P2P
A
annie_wangli 已提交
1151

A
Annie_wang 已提交
1152
**Parameters**
A
Annie_wang 已提交
1153 1154 1155 1156
| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value is **p2pConnectionChange**.|
| callback | Callback&lt;[WifiP2pLinkedInfo](#WifiP2pLinkedInfo)&gt; | No| Callback used to return the P2P connection state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.|
A
annie_wangli 已提交
1157 1158 1159 1160 1161 1162 1163 1164


## wifi.on('p2pDeviceChange')<sup>8+</sup>

on(type: "p2pDeviceChange", callback: Callback&lt;WifiP2pDevice&gt;): void

Registers the P2P device state change events.

A
Annie_wang 已提交
1165
**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION
A
annie_wangli 已提交
1166

A
Annie_wang 已提交
1167
**System capability**: SystemCapability.Communication.WiFi.P2P
A
annie_wangli 已提交
1168

A
Annie_wang 已提交
1169
**Parameters**
A
Annie_wang 已提交
1170 1171 1172 1173
| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value is **p2pDeviceChange**.|
| callback | Callback&lt;[WifiP2pDevice](#WifiP2pDevice)&gt; | Yes| Callback invoked to return the P2P device state.|
A
annie_wangli 已提交
1174 1175 1176 1177 1178 1179 1180 1181


## wifi.off('p2pDeviceChange')<sup>8+</sup>

off(type: "p2pDeviceChange", callback?: Callback&lt;WifiP2pDevice&gt;): void

Unregisters the P2P device state change events.

A
Annie_wang 已提交
1182
**Required permissions**: ohos.permission.LOCATION
A
annie_wangli 已提交
1183

A
Annie_wang 已提交
1184
**System capability**: SystemCapability.Communication.WiFi.P2P
A
annie_wangli 已提交
1185

A
Annie_wang 已提交
1186
**Parameters**
A
Annie_wang 已提交
1187 1188 1189 1190
| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value is **p2pDeviceChange**.|
| callback | Callback&lt;[WifiP2pDevice](#WifiP2pDevice)&gt; | No| Callback used to return the P2P device state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.|
A
annie_wangli 已提交
1191 1192 1193 1194 1195 1196 1197 1198


## wifi.on('p2pPeerDeviceChange')<sup>8+</sup>

on(type: "p2pPeerDeviceChange", callback: Callback&lt;WifiP2pDevice[]&gt;): void

Registers the P2P peer device state change events.

A
Annie_wang 已提交
1199
**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION
A
annie_wangli 已提交
1200

A
Annie_wang 已提交
1201
**System capability**: SystemCapability.Communication.WiFi.P2P
A
annie_wangli 已提交
1202

A
Annie_wang 已提交
1203
**Parameters**
A
Annie_wang 已提交
1204 1205 1206 1207
| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value is **p2pPeerDeviceChange**.|
| callback | Callback&lt;[WifiP2pDevice[]](#WifiP2pDevice)&gt; | Yes| Callback invoked to return the P2P peer device state.|
A
annie_wangli 已提交
1208 1209 1210 1211 1212 1213 1214 1215


## wifi.off('p2pPeerDeviceChange')<sup>8+</sup>

off(type: "p2pPeerDeviceChange", callback?: Callback&lt;WifiP2pDevice[]&gt;): void

Unregisters the P2P peer device state change events.

A
Annie_wang 已提交
1216
**Required permissions**: ohos.permission.LOCATION
A
annie_wangli 已提交
1217

A
Annie_wang 已提交
1218
**System capability**: SystemCapability.Communication.WiFi.P2P
A
annie_wangli 已提交
1219

A
Annie_wang 已提交
1220
**Parameters**
A
Annie_wang 已提交
1221 1222 1223 1224
| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value is **p2pPeerDeviceChange**.|
| callback | Callback&lt;[WifiP2pDevice[]](#WifiP2pDevice)&gt; | No| Callback used to return the P2P peer device state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.|
A
annie_wangli 已提交
1225 1226 1227 1228 1229 1230 1231 1232


## wifi.on('p2pPersistentGroupChange')<sup>8+</sup>

on(type: "p2pPersistentGroupChange", callback: Callback&lt;void&gt;): void

Registers the P2P persistent group state change events.

A
Annie_wang 已提交
1233
**Required permissions**: ohos.permission.GET_WIFI_INFO
A
annie_wangli 已提交
1234

A
Annie_wang 已提交
1235
**System capability**: SystemCapability.Communication.WiFi.P2P
A
annie_wangli 已提交
1236

A
Annie_wang 已提交
1237
**Parameters**
A
Annie_wang 已提交
1238 1239 1240 1241
| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value is **p2pPersistentGroupChange**.|
| callback | Callback&lt;void&gt; | Yes| Callback invoked to return the P2P persistent group state.|
A
annie_wangli 已提交
1242 1243 1244 1245 1246 1247 1248 1249


## wifi.off('p2pPersistentGroupChange')<sup>8+</sup>

off(type: "p2pPersistentGroupChange", callback?: Callback&lt;void&gt;): void

Unregisters the P2P persistent group state change events.

A
Annie_wang 已提交
1250
**Required permissions**: ohos.permission.GET_WIFI_INFO
A
annie_wangli 已提交
1251

A
Annie_wang 已提交
1252
**System capability**: SystemCapability.Communication.WiFi.P2P
A
annie_wangli 已提交
1253

A
Annie_wang 已提交
1254
**Parameters**
A
Annie_wang 已提交
1255 1256 1257 1258
| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value is **p2pPersistentGroupChange**.|
| callback | Callback&lt;void&gt; | No| Callback used to return the P2P persistent group state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.|
A
annie_wangli 已提交
1259 1260 1261 1262 1263 1264


## wifi.on('p2pDiscoveryChange')<sup>8+</sup>

on(type: "p2pDiscoveryChange", callback: Callback&lt;number&gt;): void

A
Annie_wang 已提交
1265
Registers the P2P device discovery state change events.
A
annie_wangli 已提交
1266

A
Annie_wang 已提交
1267
**Required permissions**: ohos.permission.GET_WIFI_INFO
A
annie_wangli 已提交
1268

A
Annie_wang 已提交
1269
**System capability**: SystemCapability.Communication.WiFi.P2P
A
annie_wangli 已提交
1270

A
Annie_wang 已提交
1271
**Parameters**
A
Annie_wang 已提交
1272 1273 1274 1275
| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value is **p2pDiscoveryChange**.|
| callback | Callback&lt;number&gt; | Yes| Callback invoked to return the P2P device discovery state.|
A
annie_wangli 已提交
1276

A
Annie_wang 已提交
1277 1278 1279 1280 1281 1282
**P2P discovered device states**

| **Value**| **Description**|
| -------- | -------- |
| 0 | Initial state|
| 1 | Discovered|
A
annie_wangli 已提交
1283 1284 1285 1286 1287 1288


## wifi.off('p2pDiscoveryChange')<sup>8+</sup>

off(type: "p2pDiscoveryChange", callback?: Callback&lt;number&gt;): void

A
Annie_wang 已提交
1289
Unregisters the P2P device discovery state change events.
A
annie_wangli 已提交
1290

A
Annie_wang 已提交
1291
**Required permissions**: ohos.permission.GET_WIFI_INFO
W
wusongqing 已提交
1292

A
Annie_wang 已提交
1293
**System capability**: SystemCapability.Communication.WiFi.P2P
W
wusongqing 已提交
1294

A
Annie_wang 已提交
1295
**Parameters**
A
Annie_wang 已提交
1296 1297 1298 1299
| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value is **p2pDiscoveryChange**.|
| callback | Callback&lt;number&gt; | No| Callback used to return the P2P device discovery state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.|