• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.wifi (WLAN)
2
3The **WLAN** module provides basic wireless local area network (WLAN) functions, peer-to-peer (P2P) functions, and WLAN message notification services. It allows applications to communicate with other devices over WLAN.
4
5> **NOTE**
6>
7> 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.
8
9
10## Modules to Import
11
12```js
13import wifi from '@ohos.wifi';
14```
15
16## wifi.enableWifi
17
18enableWifi(): boolean
19
20Enables WLAN.
21
22**System API**: This is a system API.
23
24**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications)
25
26**System capability**: SystemCapability.Communication.WiFi.STA
27
28**Return value**
29
30  | **Type**| **Description**|
31  | -------- | -------- |
32  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
33
34
35## wifi.disableWifi
36
37disableWifi(): boolean
38
39Disables WLAN.
40
41**System API**: This is a system API.
42
43**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications)
44
45**System capability**: SystemCapability.Communication.WiFi.STA
46
47**Return value**
48
49  | **Type**| **Description**|
50  | -------- | -------- |
51  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
52
53
54## wifi.isWifiActive
55
56isWifiActive(): boolean
57
58Checks whether WLAN is enabled.
59
60**Required permissions**: ohos.permission.GET_WIFI_INFO
61
62**System capability**: SystemCapability.Communication.WiFi.STA
63
64**Return value**
65
66  | **Type**| **Description**|
67  | -------- | -------- |
68  | boolean | Returns **true** if WLAN is enabled; returns **false** otherwise.|
69
70
71## wifi.scan
72
73scan(): boolean
74
75Starts a scan for WLAN.
76
77**Required permissions**: **ohos.permission.SET_WIFI_INFO** and **ohos.permission.LOCATION**
78
79**System capability**: SystemCapability.Communication.WiFi.STA
80
81**Return value**
82
83  | **Type**| **Description**|
84  | -------- | -------- |
85  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
86
87
88## wifi.getScanInfos
89
90getScanInfos(): Promise<Array<WifiScanInfo>>
91
92Obtains the scan result. This API uses a promise to return the result.
93
94**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.GET_WIFI_PEERS_MAC (or ohos.permission.LOCATION)
95
96**System capability**: SystemCapability.Communication.WiFi.STA
97
98**Return value**
99
100  | **Type**| **Description**|
101  | -------- | -------- |
102  | Promise< Array<[WifiScanInfo](#wifiscaninfo)> > | Promise used to return the detected hotspots.|
103
104
105## wifi.getScanInfos
106
107getScanInfos(callback: AsyncCallback<Array<WifiScanInfo>>): void
108
109Obtains the scan result. This API uses an asynchronous callback to return the result.
110
111**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.GET_WIFI_PEERS_MAC (or ohos.permission.LOCATION)
112
113**System capability**: SystemCapability.Communication.WiFi.STA
114
115**Parameters**
116
117  | **Name**| **Type**| **Mandatory**| **Description**|
118  | -------- | -------- | -------- | -------- |
119  | 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.|
120
121**Example**
122  ```js
123  import wifi from '@ohos.wifi';
124
125  wifi.getScanInfos((err, result) => {
126      if (err) {
127          console.error("get scan info error");
128          return;
129      }
130
131      var len = Object.keys(result).length;
132      console.log("wifi received scan info: " + len);
133      for (var i = 0; i < len; ++i) {
134          console.info("ssid: " + result[i].ssid);
135          console.info("bssid: " + result[i].bssid);
136          console.info("capabilities: " + result[i].capabilities);
137          console.info("securityType: " + result[i].securityType);
138          console.info("rssi: " + result[i].rssi);
139          console.info("band: " + result[i].band);
140          console.info("frequency: " + result[i].frequency);
141          console.info("channelWidth: " + result[i].channelWidth);
142          console.info("timestamp: " + result[i].timestamp);
143      }
144  });
145
146  wifi.getScanInfos().then(result => {
147      var len = Object.keys(result).length;
148      console.log("wifi received scan info: " + len);
149      for (var i = 0; i < len; ++i) {
150          console.info("ssid: " + result[i].ssid);
151          console.info("bssid: " + result[i].bssid);
152          console.info("capabilities: " + result[i].capabilities);
153          console.info("securityType: " + result[i].securityType);
154          console.info("rssi: " + result[i].rssi);
155          console.info("band: " + result[i].band);
156          console.info("frequency: " + result[i].frequency);
157          console.info("channelWidth: " + result[i].channelWidth);
158          console.info("timestamp: " + result[i].timestamp);
159      }
160  });
161  ```
162
163
164## WifiScanInfo
165
166Represents WLAN hotspot information.
167
168**System capability**: SystemCapability.Communication.WiFi.STA
169
170
171| **Name**| **Type**| **Readable**| **Writable**| **Description**|
172| -------- | -------- | -------- | -------- | -------- |
173| ssid | string | Yes| No| Service set identifier (SSID) of the hotspot, in UTF-8 format.|
174| bssid | string | Yes| No| Basic service set identifier (BSSID) of the hotspot.|
175| capabilities | string | Yes| No| Hotspot capabilities.|
176| securityType | [WifiSecurityType](#wifisecuritytype) | Yes| No| WLAN security type.|
177| rssi | number | Yes| No| Received signal strength indicator (RSSI) of the hotspot, in dBm.|
178| band | number | Yes| No| Frequency band of the WLAN access point (AP).|
179| frequency | number | Yes| No| Frequency of the WLAN AP.|
180| channelWidth | number | Yes| No| Channel width of the WLAN AP.|
181| timestamp | number | Yes| No| Timestamp.|
182
183
184## WifiSecurityType
185
186Enumerates the WLAN security types.
187
188**System capability**: SystemCapability.Communication.WiFi.Core
189
190
191| **Name**| **Value**| **Description**|
192| -------- | -------- | -------- |
193| WIFI_SEC_TYPE_INVALID | 0 | Invalid security type.|
194| WIFI_SEC_TYPE_OPEN | 1 | Open security type.|
195| WIFI_SEC_TYPE_WEP | 2 | Wired Equivalent Privacy (WEP).|
196| WIFI_SEC_TYPE_PSK | 3 | Pre-shared key (PSK).|
197| WIFI_SEC_TYPE_SAE | 4 | Simultaneous Authentication of Equals (SAE).|
198
199
200## wifi.getScanInfosSync<sup>9+</sup>
201
202getScanInfosSync(): &nbsp;Array&lt;[WifiScanInfo](#wifiscaninfo)&gt;
203
204Obtains the scan result. This API returns the result synchronously.
205
206**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.GET_WIFI_PEERS_MAC (or ohos.permission.LOCATION)
207
208**System capability**: SystemCapability.Communication.WiFi.STA
209
210**Return value**
211
212  | **Type**| **Description**|
213  | -------- | -------- |
214  | &nbsp;Array&lt;[WifiScanInfo](#wifiscaninfo)&gt; | Scan result obtained.|
215
216
217## wifi.addDeviceConfig
218
219addDeviceConfig(config: WifiDeviceConfig): Promise&lt;number&gt;
220
221Adds network configuration. This API uses a promise to return the result.
222
223**System API**: This is a system API.
224
225**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.SET_WIFI_CONFIG
226
227**System capability**: SystemCapability.Communication.WiFi.STA
228
229**Parameters**
230
231  | **Name**| **Type**| **Mandatory**| **Description**|
232  | -------- | -------- | -------- | -------- |
233  | config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration to add.|
234
235**Return value**
236
237  | **Type**| **Description**|
238  | -------- | -------- |
239  | Promise&lt;number&gt; | Promise used to return the WLAN configuration ID. If **-1** is returned, the network configuration fails to be added.|
240
241## WifiDeviceConfig
242
243Represents the WLAN configuration.
244
245**System capability**: SystemCapability.Communication.WiFi.STA
246
247
248| **Name**| **Type**| **Readable**| **Writable**| **Description**|
249| -------- | -------- | -------- | -------- | -------- |
250| ssid | string | Yes| No| SSID of the hotspot, in UTF-8 format.|
251| bssid | string | Yes| No| BSSID of the hotspot.|
252| preSharedKey | string | Yes| No| PSK of the hotspot.|
253| isHiddenSsid | boolean | Yes| No| Whether the network is hidden.|
254| securityType | [WifiSecurityType](#wifisecuritytype) | Yes| No| Security type.|
255| creatorUid | number | Yes| No| ID of the creator.<br> **System API**: This is a system API.|
256| disableReason | number | Yes| No| Reason for disabling WLAN.<br> **System API**: This is a system API.|
257| netId | number | Yes| No| Network ID.<br> **System API**: This is a system API.|
258| randomMacType | number | Yes| No| Random MAC type.<br> **System API**: This is a system API.|
259| randomMacAddr | string | Yes| No| Random MAC address.<br> **System API**: This is a system API.|
260| ipType | [IpType](#iptype7) | Yes| No| IP address type.<br> **System API**: This is a system API.|
261| staticIp | [IpConfig](#ipconfig7) | Yes| No| Static IP address configuration.<br> **System API**: This is a system API.|
262
263
264## IpType<sup>7+</sup>
265
266Enumerate the IP address types.
267
268**System API**: This is a system API.
269
270**System capability**: SystemCapability.Communication.WiFi.STA
271
272
273| Name| Value| Description|
274| -------- | -------- | -------- |
275| STATIC | 0 | Static IP address.|
276| DHCP | 1 | IP address allocated by DHCP.|
277| UNKNOWN | 2 | Not specified.|
278
279
280## IpConfig<sup>7+</sup>
281
282Represents IP configuration information.
283
284**System API**: This is a system API.
285
286**System capability**: SystemCapability.Communication.WiFi.STA
287
288| **Name**| **Type**| **Readable**| **Writable**| **Description**|
289| -------- | -------- | -------- | -------- | -------- |
290| ipAddress | number | Yes| No| IP address.|
291| gateway | number | Yes| No| Gateway.|
292| dnsServers | number[] | Yes| No| Domain name server (DNS) information.|
293| domains | Array&lt;string&gt; | Yes| No| Domain information.|
294
295
296## wifi.addDeviceConfig
297
298addDeviceConfig(config: WifiDeviceConfig, callback: AsyncCallback&lt;number&gt;): void
299
300Adds network configuration. This API uses an asynchronous callback to return the result.
301
302**System API**: This is a system API.
303
304**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.SET_WIFI_CONFIG
305
306**System capability**: SystemCapability.Communication.WiFi.STA
307
308**Parameters**
309
310  | **Name**| **Type**| **Mandatory**| **Description**|
311  | -------- | -------- | -------- | -------- |
312  | config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration to add.|
313  | callback | AsyncCallback&lt;number&gt; | 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.|
314
315
316## wifi.addUntrustedConfig<sup>7+</sup>
317
318addUntrustedConfig(config: WifiDeviceConfig): Promise&lt;boolean&gt;
319
320Adds the configuration of an untrusted network. This API uses a promise to return the result.
321
322**Required permissions**: ohos.permission.SET_WIFI_INFO
323
324**System capability**: SystemCapability.Communication.WiFi.STA
325
326**Parameters**
327
328  | **Name**| **Type**| **Mandatory**| **Description**|
329  | -------- | -------- | -------- | -------- |
330  | config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration to add.|
331
332**Return value**
333
334  | **Type**| **Description**|
335  | -------- | -------- |
336  | Promise&lt;boolean&gt; | Promise used to return the result. If the operation is successful, **true** is returned; otherwise, **false** is returned.|
337
338
339## wifi.addUntrustedConfig<sup>7+</sup>
340
341addUntrustedConfig(config: WifiDeviceConfig, callback: AsyncCallback&lt;boolean&gt;): void
342
343Adds the configuration of an untrusted network. This API uses an asynchronous callback to return the result.
344
345**Required permissions**: ohos.permission.SET_WIFI_INFO
346
347**System capability**: SystemCapability.Communication.WiFi.STA
348
349**Parameters**
350
351  | **Name**| **Type**| **Mandatory**| **Description**|
352  | -------- | -------- | -------- | -------- |
353  | config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration to add.|
354  | callback | AsyncCallback&lt;boolean&gt; | 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.|
355
356
357## wifi.removeUntrustedConfig<sup>7+</sup>
358
359removeUntrustedConfig(config: WifiDeviceConfig): Promise&lt;boolean&gt;
360
361Removes the configuration of an untrusted network. This API uses a promise to return the result.
362
363**Required permissions**: ohos.permission.SET_WIFI_INFO
364
365**System capability**: SystemCapability.Communication.WiFi.STA
366
367**Parameters**
368
369  | **Name**| **Type**| **Mandatory**| **Description**|
370  | -------- | -------- | -------- | -------- |
371| config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration to remove.|
372
373**Return value**
374
375  | **Type**| **Description**|
376  | -------- | -------- |
377  | Promise&lt;boolean&gt; | Promise used to return the result. If the operation is successful, **true** is returned; otherwise, **false** is returned.|
378
379
380## wifi.removeUntrustedConfig<sup>7+</sup>
381
382removeUntrustedConfig(config: WifiDeviceConfig, callback: AsyncCallback&lt;boolean&gt;): void
383
384Removes the configuration of an untrusted network. This API uses an asynchronous callback to return the result.
385
386**Required permissions**: ohos.permission.SET_WIFI_INFO
387
388**System capability**: SystemCapability.Communication.WiFi.STA
389
390**Parameters**
391
392  | **Name**| **Type**| **Mandatory**| **Description**|
393  | -------- | -------- | -------- | -------- |
394| config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration to remove.|
395  | callback | AsyncCallback&lt;boolean&gt; | 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.|
396
397
398## wifi.connectToNetwork
399
400connectToNetwork(networkId: number): boolean
401
402Connects to the specified network.
403
404**System API**: This is a system API.
405
406**Required permissions**: ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications)
407
408**System capability**: SystemCapability.Communication.WiFi.STA
409
410**Parameters**
411
412  | **Name**| **Type**| **Mandatory**| **Description**|
413  | -------- | -------- | -------- | -------- |
414  | networkId | number | Yes| Network configuration ID.|
415
416**Return value**
417
418  | **Type**| **Description**|
419  | -------- | -------- |
420  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
421
422
423## wifi.connectToDevice
424
425connectToDevice(config: WifiDeviceConfig): boolean
426
427Connects to the specified network.
428
429**System API**: This is a system API.
430
431**Required permissions**: ohos.permission.SET_WIFI_INFO, ohos.permission.SET_WIFI_CONFIG, and ohos.permissio.MANAGE_WIFI_CONNECTION (available only to system applications)
432
433**System capability**:
434  SystemCapability.Communication.WiFi.STA
435
436**Parameters**
437
438  | **Name**| **Type**| **Mandatory**| **Description**|
439  | -------- | -------- | -------- | -------- |
440| config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration.|
441
442**Return value**
443
444  | **Type**| **Description**|
445  | -------- | -------- |
446  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
447
448
449## wifi.disconnect
450
451disconnect(): boolean
452
453Disconnects the network.
454
455**System API**: This is a system API.
456
457**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications)
458
459**System capability**:
460  SystemCapability.Communication.WiFi.STA
461
462**Return value**
463
464  | **Type**| **Description**|
465  | -------- | -------- |
466  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
467
468
469## wifi.getSignalLevel
470
471getSignalLevel(rssi: number, band: number): number
472
473Obtains the WLAN signal level.
474
475**Required permissions**: ohos.permission.GET_WIFI_INFO
476
477**System capability**: SystemCapability.Communication.WiFi.STA
478
479**Parameters**
480
481  | **Name**| **Type**| **Mandatory**| **Description**|
482  | -------- | -------- | -------- | -------- |
483  | rssi | number | Yes| RSSI of the hotspot, in dBm.|
484  | band | number | Yes| Frequency band of the WLAN AP.|
485
486**Return value**
487
488  | **Type**| **Description**|
489  | -------- | -------- |
490  | number | Signal level obtained. The value range is [0, 4].|
491
492
493## wifi.getLinkedInfo
494
495getLinkedInfo(): Promise&lt;WifiLinkedInfo&gt;
496
497Obtains WLAN connection information. This API uses a promise to return the result.
498
499**Required permissions**: ohos.permission.GET_WIFI_INFO
500
501**System capability**: SystemCapability.Communication.WiFi.STA
502
503**Return value**
504
505  | Type| Description|
506  | -------- | -------- |
507  | Promise&lt;[WifiLinkedInfo](#wifilinkedinfo)&gt; | Promise used to return the WLAN connection information.|
508
509
510## wifi.getLinkedInfo
511
512getLinkedInfo(callback: AsyncCallback&lt;WifiLinkedInfo&gt;): void
513
514Obtains WLAN connection information. This API uses an asynchronous callback to return the result.
515
516**Required permissions**: ohos.permission.GET_WIFI_INFO
517
518**System capability**: SystemCapability.Communication.WiFi.STA
519
520**Parameters**
521
522  | Name| Type| Mandatory| Description|
523  | -------- | -------- | -------- | -------- |
524  | callback | AsyncCallback&lt;[WifiLinkedInfo](#wifilinkedinfo)&gt; | 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.|
525
526**Example**
527  ```js
528  import wifi from '@ohos.wifi';
529
530  wifi.getLinkedInfo((err, data) => {
531      if (err) {
532          console.error("get linked info error");
533          return;
534      }
535      console.info("get wifi linked info: " + JSON.stringify(data));
536  });
537
538  wifi.getLinkedInfo().then(data => {
539      console.info("get wifi linked info: " + JSON.stringify(data));
540  }).catch(error => {
541      console.info("get linked info error");
542  });
543  ```
544
545
546## WifiLinkedInfo
547
548Represents the WLAN connection information.
549
550**System capability**: SystemCapability.Communication.WiFi.STA
551
552| Name| Type| Readable| Writable| Description|
553| -------- | -------- | -------- | -------- | -------- |
554| ssid | string | Yes| No| SSID of the hotspot, in UTF-8 format.|
555| bssid | string | Yes| No| BSSID of the hotspot.|
556| networkId | number | Yes| No| Network configuration ID.<br> **System API**: This is a system API.|
557| rssi | number | Yes| No| RSSI of the hotspot, in dBm.|
558| band | number | Yes| No| Frequency band of the WLAN AP.|
559| linkSpeed | number | Yes| No| Speed of the WLAN AP.|
560| frequency | number | Yes| No| Frequency of the WLAN AP.|
561| isHidden | boolean | Yes| No| Whether to hide the WLAN AP.|
562| isRestricted | boolean | Yes| No| Whether to restrict data volume at the WLAN AP.|
563| chload | number | Yes| No| Channel load. A larger value indicates a higher load.<br> **System API**: This is a system API.|
564| snr | number | Yes| No| Signal-to-noise ratio (SNR).<br> **System API**: This is a system API.|
565| macAddress | string | Yes| No| MAC address of the device.|
566| ipAddress | number | Yes| No| IP address of the device that sets up the WLAN connection.|
567| suppState | [SuppState](#suppstate) | Yes| No| Supplicant state.<br> **System API**: This is a system API.|
568| connState | [ConnState](#connstate) | Yes| No| WLAN connection state.|
569
570
571## ConnState
572
573Enumerates the WLAN connection states.
574
575**System capability**: SystemCapability.Communication.WiFi.STA
576
577| Name| Value| Description|
578| -------- | -------- | -------- |
579| SCANNING | 0 | The device is scanning for available APs.|
580| CONNECTING | 1 | A WLAN connection is being established.|
581| AUTHENTICATING | 2 | An authentication is being performed for a WLAN connection.|
582| OBTAINING_IPADDR | 3 | The IP address of the WLAN connection is being acquired.|
583| CONNECTED | 4 | A WLAN connection is established.|
584| DISCONNECTING | 5 | The WLAN connection is being disconnected.|
585| DISCONNECTED | 6 | The WLAN connection is disconnected.|
586| UNKNOWN | 7 | Failed to set up the WLAN connection.|
587
588
589## SuppState
590
591Enumerates the supplicant states.
592
593**System API**: This is a system API.
594
595**System capability**: SystemCapability.Communication.WiFi.STA
596
597| Name| Value| Description|
598| -------- | -------- | -------- |
599| DISCONNECTED | 0 | The supplicant is disconnected from the AP.|
600| INTERFACE_DISABLED | 1 | The network interface is disabled.|
601| INACTIVE | 2 | The supplicant is inactive.|
602| SCANNING | 3 | The supplicant is scanning for a WLAN connection.|
603| AUTHENTICATING | 4 | The supplicant is being authenticated.|
604| ASSOCIATING | 5 | The supplicant is being associated with an AP.|
605| ASSOCIATED | 6 | The supplicant is associated with an AP.|
606| FOUR_WAY_HANDSHAKE | 7 | A four-way handshake is being performed for the supplicant.|
607| GROUP_HANDSHAKE | 8 | A group handshake is being performed for the supplicant.|
608| COMPLETED | 9 | The authentication is complete.|
609| UNINITIALIZED | 10 | The supplicant failed to set up the connection.|
610| INVALID | 11 | Invalid value.|
611
612
613## wifi.isConnected<sup>7+</sup>
614
615isConnected(): boolean
616
617Checks whether the WLAN is connected.
618
619**Required permissions**: ohos.permission.GET_WIFI_INFO
620
621**System capability**: SystemCapability.Communication.WiFi.STA
622
623**Return value**
624
625  | **Type**| **Description**|
626  | -------- | -------- |
627  | boolean | Returns **true** if the WLAN is connected; returns **false** otherwise.|
628
629
630## wifi.getSupportedFeatures<sup>7+</sup>
631
632getSupportedFeatures(): number
633
634Obtains the features supported by this device.
635
636**System API**: This is a system API.
637
638**Required permissions**: ohos.permission.GET_WIFI_INFO
639
640**System capability**: SystemCapability.Communication.WiFi.Core
641
642**Return value**
643
644  | **Type**| **Description**|
645  | -------- | -------- |
646  | number | Feature value. |
647
648**Feature IDs**
649
650| Value| Description|
651| -------- | -------- |
652| 0x0001 | WLAN infrastructure mode|
653| 0x0002 | 5 GHz feature|
654| 0x0004 | Generic Advertisement Service (GAS)/Access Network Query Protocol (ANQP) feature|
655| 0x0008 | Wi-Fi Direct|
656| 0x0010 | SoftAP|
657| 0x0040 | Wi-Fi AWare|
658| 0x8000 | WLAN AP/STA concurrency|
659| 0x8000000 | WPA3 Personal (WPA-3 SAE)|
660| 0x10000000 | WPA3-Enterprise Suite B |
661| 0x20000000 | Enhanced open feature|
662
663
664## wifi.isFeatureSupported<sup>7+</sup>
665
666isFeatureSupported(featureId: number): boolean
667
668Checks whether the device supports the specified WLAN feature.
669
670**Required permissions**: ohos.permission.GET_WIFI_INFO
671
672**System capability**: SystemCapability.Communication.WiFi.Core
673
674**Parameters**
675
676
677  | **Name**| **Type**| Mandatory| **Description**|
678  | -------- | -------- | -------- | -------- |
679  | featureId | number | Yes| Feature ID.|
680
681**Return value**
682
683  | **Type**| **Description**|
684  | -------- | -------- |
685  | boolean | Returns **true** if the feature is supported; returns **false** otherwise.|
686
687
688## wifi.getDeviceMacAddress<sup>7+</sup>
689
690getDeviceMacAddress(): string[]
691
692Obtains the device MAC address.
693
694**System API**: This is a system API.
695
696**Required permissions**: ohos.permission.GET_WIFI_LOCAL_MAC and ohos.permission.GET_WIFI_INFO (available only to system applications)
697
698**System capability**: SystemCapability.Communication.WiFi.STA
699
700**Return value**
701
702  | **Type**| **Description**|
703  | -------- | -------- |
704  | string[] | MAC address obtained.|
705
706
707## wifi.getIpInfo<sup>7+</sup>
708
709getIpInfo(): IpInfo
710
711Obtains IP information.
712
713**Required permissions**: ohos.permission.GET_WIFI_INFO
714
715**System capability**: SystemCapability.Communication.WiFi.STA
716
717**Return value**
718
719  | **Type**| **Description**|
720  | -------- | -------- |
721  | [IpInfo](#ipinfo7) | IP information obtained.|
722
723
724## IpInfo<sup>7+</sup>
725
726Represents IP information.
727
728**System capability**: SystemCapability.Communication.WiFi.STA
729
730| **Name**| **Type**| **Readable**| **Writable**| **Description**|
731| -------- | -------- | -------- | -------- | -------- |
732| ipAddress | number | Yes| No| IP address.|
733| gateway | number | Yes| No| Gateway.|
734| netmask | number | Yes| No| Subnet mask.|
735| primaryDns | number | Yes| No| IP address of the preferred DNS server.|
736| secondDns | number | Yes| No| IP address of the alternate DNS server.|
737| serverIp | number | Yes| No| IP address of the DHCP server.|
738| leaseDuration | number | Yes| No| Lease duration of the IP address.|
739
740
741## wifi.getCountryCode<sup>7+</sup>
742
743getCountryCode(): string
744
745Obtains the country code.
746
747**Required permissions**: ohos.permission.GET_WIFI_INFO
748
749**System capability**: SystemCapability.Communication.WiFi.Core
750
751**Return value**
752
753  | **Type**| **Description**|
754  | -------- | -------- |
755  | string | Country code obtained.|
756
757
758## wifi.reassociate<sup>7+</sup>
759
760reassociate(): boolean
761
762Re-associates with the network.
763
764**System API**: This is a system API.
765
766**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications)
767
768**System capability**: SystemCapability.Communication.WiFi.STA
769
770**Return value**
771
772  | **Type**| **Description**|
773  | -------- | -------- |
774  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
775
776
777## wifi.reconnect<sup>7+</sup>
778
779reconnect(): boolean
780
781Reconnects to the network.
782
783**System API**: This is a system API.
784
785**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications)
786
787**System capability**: SystemCapability.Communication.WiFi.STA
788
789**Return value**
790
791  | **Type**| **Description**|
792  | -------- | -------- |
793  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
794
795
796## wifi.getDeviceConfigs<sup>7+</sup>
797
798getDeviceConfigs(): &nbsp;Array&lt;[WifiDeviceConfig](#wifideviceconfig)&gt;
799
800Obtains network configuration.
801
802**System API**: This is a system API.
803
804**Required permissions**: ohos.permission.GET_WIFI_INFO, ohos.permission.LOCATION, and ohos.permission.GET_WIFI_CONFIG
805
806**System capability**: SystemCapability.Communication.WiFi.STA
807
808**Return value**
809
810  | **Type**| **Description**|
811  | -------- | -------- |
812  | &nbsp;Array&lt;[WifiDeviceConfig](#wifideviceconfig)&gt; | Array of network configuration obtained.|
813
814
815## wifi.updateNetwork<sup>7+</sup>
816
817updateNetwork(config: WifiDeviceConfig): number
818
819Updates network configuration.
820
821**System API**: This is a system API.
822
823**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.SET_WIFI_CONFIG
824
825**System capability**: SystemCapability.Communication.WiFi.STA
826
827**Parameters**
828
829  | **Name**| **Type**| **Mandatory**| **Description**|
830  | -------- | -------- | -------- | -------- |
831| config | [WifiDeviceConfig](#wifideviceconfig) | Yes| New WLAN configuration.|
832
833**Return value**
834
835  | **Type**| **Description**|
836  | -------- | -------- |
837  | number | ID of the updated network configuration. The value **-1** indicates that the operation has failed.|
838
839
840## wifi.disableNetwork<sup>7+</sup>
841
842disableNetwork(netId: number): boolean
843
844Disables network configuration.
845
846**System API**: This is a system API.
847
848**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications)
849
850**System capability**: SystemCapability.Communication.WiFi.STA
851
852**Parameters**
853
854  | **Name**| **Type**| **Mandatory**| **Description**|
855  | -------- | -------- | -------- | -------- |
856  | netId | number | Yes| ID of the network configuration to disable.|
857
858**Return value**
859
860  | **Type**| **Description**|
861  | -------- | -------- |
862  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
863
864
865## wifi.removeAllNetwork<sup>7+</sup>
866
867removeAllNetwork(): boolean
868
869Removes the configuration of all networks.
870
871**System API**: This is a system API.
872
873**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications)
874
875**System capability**: SystemCapability.Communication.WiFi.STA
876
877**Return value**
878
879  | **Type**| **Description**|
880  | -------- | -------- |
881  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
882
883
884## wifi.removeDevice<sup>7+</sup>
885
886removeDevice(id: number): boolean
887
888Removes the specified network configuration.
889
890**System API**: This is a system API.
891
892**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications)
893
894**System capability**: SystemCapability.Communication.WiFi.STA
895
896**Parameters**
897
898  | **Name**| **Type**| **Mandatory**| **Description**|
899  | -------- | -------- | -------- | -------- |
900| id | number | Yes| ID of the network configuration to remove.|
901
902**Return value**
903
904  | **Type**| **Description**|
905  | -------- | -------- |
906  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
907
908
909## wifi.enableHotspot<sup>7+</sup>
910
911enableHotspot(): boolean
912
913Enables this hotspot.
914
915**System API**: This is a system API.
916
917**Required permissions**: ohos.permission.MANAGE_WIFI_HOTSPOT (available only to system applications)
918
919**System capability**: SystemCapability.Communication.WiFi.AP.Core
920
921**Return value**
922
923  | **Type**| **Description**|
924  | -------- | -------- |
925  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
926
927
928## wifi.disableHotspot<sup>7+</sup>
929
930disableHotspot(): boolean
931
932Disables this hotspot.
933
934**System API**: This is a system API.
935
936**Required permissions**: ohos.permission.MANAGE_WIFI_HOTSPOT (available only to system applications)
937
938**System capability**: SystemCapability.Communication.WiFi.AP.Core
939
940**Return value**
941
942  | **Type**| **Description**|
943  | -------- | -------- |
944  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
945
946
947## wifi.isHotspotDualBandSupported<sup>7+</sup>
948
949isHotspotDualBandSupported(): boolean
950
951Checks whether the hotspot supports dual band.
952
953**System API**: This is a system API.
954
955**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.MANAGE_WIFI_HOTSPOT (available only to system applications)
956
957**System capability**: SystemCapability.Communication.WiFi.AP.Core
958
959**Return value**
960
961  | **Type**| **Description**|
962  | -------- | -------- |
963  | boolean | Returns **true** if the feature is supported; returns **false** otherwise.|
964
965
966## wifi.isHotspotActive<sup>7+</sup>
967
968isHotspotActive(): boolean
969
970Checks whether this hotspot is active.
971
972**System API**: This is a system API.
973
974**Required permissions**: ohos.permission.GET_WIFI_INFO
975
976**System capability**: SystemCapability.Communication.WiFi.AP.Core
977
978**Return value**
979
980  | **Type**| **Description**|
981  | -------- | -------- |
982  | boolean | Returns **true** if the hotspot is active; returns **false** otherwise.|
983
984
985## wifi.setHotspotConfig<sup>7+</sup>
986
987setHotspotConfig(config: HotspotConfig): boolean
988
989Sets hotspot configuration.
990
991**System API**: This is a system API.
992
993**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.GET_WIFI_CONFIG
994
995**System capability**: SystemCapability.Communication.WiFi.AP.Core
996
997**Parameters**
998
999  | **Name**| **Type**| **Mandatory**| **Description**|
1000  | -------- | -------- | -------- | -------- |
1001  | config | [HotspotConfig](#hotspotconfig7) | Yes| Hotspot configuration to set.|
1002
1003**Return value**
1004
1005  | **Type**| **Description**|
1006  | -------- | -------- |
1007  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
1008
1009
1010## HotspotConfig<sup>7+</sup>
1011
1012Represents the hotspot configuration.
1013
1014**System API**: This is a system API.
1015
1016**System capability**: SystemCapability.Communication.WiFi.AP.Core
1017
1018| **Name**| **Type**| **Readable**| **Writable**| **Description**|
1019| -------- | -------- | -------- | -------- | -------- |
1020| ssid | string | Yes| No| SSID of the hotspot, in UTF-8 format.|
1021| securityType | [WifiSecurityType](#wifisecuritytype) | Yes| No| Security type.|
1022| 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.|
1023| preSharedKey | string | Yes| No| PSK of the hotspot.|
1024| maxConn | number | Yes| No| Maximum number of connections allowed.|
1025
1026
1027## wifi.getHotspotConfig<sup>7+</sup>
1028
1029getHotspotConfig(): HotspotConfig
1030
1031obtains hotspot configuration.
1032
1033**System API**: This is a system API.
1034
1035**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.GET_WIFI_CONFIG
1036
1037**System capability**: SystemCapability.Communication.WiFi.AP.Core
1038
1039**Return value**
1040
1041  | **Type**| **Description**|
1042  | -------- | -------- |
1043  | [HotspotConfig](#hotspotconfig7) | Hotspot configuration obtained.|
1044
1045
1046## wifi.getStations<sup>7+</sup>
1047
1048getStations(): &nbsp;Array&lt;[StationInfo](#stationinfo7)&gt;
1049
1050Obtains information about the connected stations.
1051
1052**System API**: This is a system API.
1053
1054**Required permissions**: ohos.permission.GET_WIFI_INFO, ohos.permission.LOCATION, and ohos.permission.MANAGE_WIFI_HOTSPOT (available only to system applications)
1055
1056**System capability**: SystemCapability.Communication.WiFi.AP.Core
1057
1058**Return value**
1059
1060  | **Type**| **Description**|
1061  | -------- | -------- |
1062  | &nbsp;Array&lt;[StationInfo](#stationinfo7)&gt; | Connected stations obtained.|
1063
1064
1065## StationInfo<sup>7+</sup>
1066
1067Represents the station information.
1068
1069**System API**: This is a system API.
1070
1071**System capability**: SystemCapability.Communication.WiFi.AP.Core
1072
1073| **Name**| **Type**| **Readable**| **Writable**| **Description**|
1074| -------- | -------- | -------- | -------- | -------- |
1075| name | string | Yes| No| Device name.|
1076| macAddress | string | Yes| No| MAC address.|
1077| ipAddress | string | Yes| No| IP address.|
1078
1079
1080## wifi.getP2pLinkedInfo<sup>8+</sup>
1081
1082getP2pLinkedInfo(): Promise&lt;WifiP2pLinkedInfo&gt;
1083
1084Obtains P2P link information. This API uses a promise to return the result.
1085
1086**Required permissions**: ohos.permission.GET_WIFI_INFO
1087
1088**System capability**: SystemCapability.Communication.WiFi.P2P
1089
1090**Return value**
1091
1092  | Type| Description|
1093  | -------- | -------- |
1094  | Promise&lt;[WifiP2pLinkedInfo](#wifip2plinkedinfo8)&gt; | Promise used to return the P2P link information obtained.|
1095
1096
1097
1098## WifiP2pLinkedInfo<sup>8+</sup>
1099
1100Represents the P2P link information.
1101
1102**System capability**: SystemCapability.Communication.WiFi.P2P
1103
1104| Name| Type| Readable| Writable| Description|
1105| -------- | -------- | -------- | -------- | -------- |
1106| connectState | [P2pConnectState](#p2pconnectstate8) | Yes| No| P2P connection state.|
1107| isGroupOwner | boolean | Yes| No| Whether the device is the group owner.|
1108| groupOwnerAddr | string | Yes| No| MAC address of the group.
1109
1110
1111## P2pConnectState<sup>8+</sup>
1112
1113Enumerates the P2P connection states.
1114
1115**System capability**: SystemCapability.Communication.WiFi.P2P
1116
1117| Name| Value| Description|
1118| -------- | -------- | -------- |
1119| DISCONNECTED | 0 | Disconnected.|
1120| CONNECTED | 1 | Connected.|
1121
1122
1123## wifi.getP2pLinkedInfo<sup>8+</sup>
1124
1125getP2pLinkedInfo(callback: AsyncCallback&lt;WifiP2pLinkedInfo&gt;): void
1126
1127Obtains P2P link information. This API uses an asynchronous callback to return the result.
1128
1129**Required permissions**: ohos.permission.GET_WIFI_INFO
1130
1131**System capability**: SystemCapability.Communication.WiFi.P2P
1132
1133**Parameters**
1134
1135  | Name| Type| Mandatory| Description|
1136  | -------- | -------- | -------- | -------- |
1137  | callback | AsyncCallback&lt;[WifiP2pLinkedInfo](#wifip2plinkedinfo8)&gt; | 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.|
1138
1139
1140## wifi.getCurrentGroup<sup>8+</sup>
1141
1142getCurrentGroup(): Promise&lt;WifiP2pGroupInfo&gt;
1143
1144Obtains the current P2P group information. This API uses a promise to return the result.
1145
1146**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION
1147
1148**System capability**: SystemCapability.Communication.WiFi.P2P
1149
1150**Return value**
1151
1152  | Type| Description|
1153  | -------- | -------- |
1154  | Promise&lt;[WifiP2pGroupInfo](#wifip2pgroupinfo8)&gt; | Promise used to return the P2P group information obtained.|
1155
1156
1157## wifi.getCurrentGroup<sup>8+</sup>
1158
1159getCurrentGroup(callback: AsyncCallback&lt;WifiP2pGroupInfo&gt;): void
1160
1161Obtains the current P2P group information. This API uses an asynchronous callback to return the result.
1162
1163**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION
1164
1165**System capability**: SystemCapability.Communication.WiFi.P2P
1166
1167**Parameters**
1168
1169  | Name| Type| Mandatory| Description|
1170  | -------- | -------- | -------- | -------- |
1171  | callback | AsyncCallback&lt;[WifiP2pGroupInfo](#wifip2pgroupinfo8)&gt; | 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.|
1172
1173
1174## wifi.getP2pPeerDevices<sup>8+</sup>
1175
1176getP2pPeerDevices(): Promise&lt;WifiP2pDevice[]&gt;
1177
1178Obtains the peer device list in the P2P connection. This API uses a promise to return the result.
1179
1180**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION
1181
1182**System capability**: SystemCapability.Communication.WiFi.P2P
1183
1184**Return value**
1185
1186  | Type| Description|
1187  | -------- | -------- |
1188  | Promise&lt;[WifiP2pDevice[]](#wifip2pdevice8)&gt; | Promise used to return the peer device list.|
1189
1190
1191## wifi.getP2pPeerDevices<sup>8+</sup>
1192
1193getP2pPeerDevices(callback: AsyncCallback&lt;WifiP2pDevice[]&gt;): void
1194
1195Obtains the peer device list in the P2P connection. This API uses an asynchronous callback to return the result.
1196
1197**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION
1198
1199**System capability**: SystemCapability.Communication.WiFi.P2P
1200
1201**Parameters**
1202
1203  | Name| Type| Mandatory| Description|
1204  | -------- | -------- | -------- | -------- |
1205  | callback | AsyncCallback&lt;[WifiP2pDevice[]](#wifip2pdevice8)&gt; | 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.|
1206
1207
1208## WifiP2pDevice<sup>8+</sup>
1209
1210Represents the P2P device information.
1211
1212**System capability**: SystemCapability.Communication.WiFi.P2P
1213
1214| Name| Type| Readable| Writable| Description|
1215| -------- | -------- | -------- | -------- | -------- |
1216| deviceName | string | Yes| No| Device name.|
1217| deviceAddress | string | Yes| No| MAC address of the device.|
1218| primaryDeviceType | string | Yes| No| Type of the primary device.|
1219| deviceStatus | [P2pDeviceStatus](#p2pdevicestatus8) | Yes| No| Device status.|
1220| groupCapabilitys | number | Yes| No| Group capabilities.|
1221
1222
1223## P2pDeviceStatus<sup>8+</sup>
1224
1225Enumerates the P2P device states.
1226
1227**System capability**: SystemCapability.Communication.WiFi.P2P
1228
1229| Name| Value| Description|
1230| -------- | -------- | -------- |
1231| CONNECTED | 0 | Connected.|
1232| INVITED | 1 | Invited.|
1233| FAILED | 2 | Failed.|
1234| AVAILABLE | 3 | Available.|
1235| UNAVAILABLE | 4 | Unavailable.|
1236
1237
1238## wifi.createGroup<sup>8+</sup>
1239
1240createGroup(config: WifiP2PConfig): boolean
1241
1242Creates a P2P group.
1243
1244**Required permissions**: ohos.permission.GET_WIFI_INFO
1245
1246**System capability**: SystemCapability.Communication.WiFi.P2P
1247
1248**Parameters**
1249
1250  | **Name**| **Type**| Mandatory| **Description**|
1251  | -------- | -------- | -------- | -------- |
1252  | config | [WifiP2PConfig](#wifip2pconfig8) | Yes| Group configuration.|
1253
1254**Return value**
1255
1256  | Type| Description|
1257  | -------- | -------- |
1258  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
1259
1260
1261## WifiP2PConfig<sup>8+</sup>
1262
1263Represents P2P group configuration.
1264
1265**System capability**: SystemCapability.Communication.WiFi.P2P
1266
1267| Name| Type| Readable| Writable| Description|
1268| -------- | -------- | -------- | -------- | -------- |
1269| deviceAddress | string | Yes| No| Device address.|
1270| netId | number | Yes| No| Network ID. The value **-1** indicates a temporary group, and **-2** indicates a persistent group.|
1271| passphrase | string | Yes| No| Passphrase of the group.|
1272| groupName | string | Yes| No| Name of the group.|
1273| goBand | [GroupOwnerBand](#groupownerband8) | Yes| No| Frequency band of the group.|
1274
1275
1276## GroupOwnerBand<sup>8+</sup>
1277
1278Enumerates the P2P group frequency bands.
1279
1280**System capability**: SystemCapability.Communication.WiFi.P2P
1281
1282| Name| Value| Description|
1283| -------- | -------- | -------- |
1284| GO_BAND_AUTO | 0 | Auto.|
1285| GO_BAND_2GHZ | 1 | 2 GHz.|
1286| GO_BAND_5GHZ | 2 | 5 GHz.|
1287
1288
1289## wifi.removeGroup<sup>8+</sup>
1290
1291removeGroup(): boolean
1292
1293Removes this P2P group.
1294
1295**Required permissions**: ohos.permission.GET_WIFI_INFO
1296
1297**System capability**: SystemCapability.Communication.WiFi.P2P
1298
1299**Return value**
1300
1301  | Type| Description|
1302  | -------- | -------- |
1303  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
1304
1305
1306## wifi.p2pConnect<sup>8+</sup>
1307
1308p2pConnect(config: WifiP2PConfig): boolean
1309
1310Sets up a P2P connection.
1311
1312**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION
1313
1314**System capability**: SystemCapability.Communication.WiFi.P2P
1315
1316**Parameters**
1317
1318
1319  | **Name**| **Type**| Mandatory| **Description**|
1320  | -------- | -------- | -------- | -------- |
1321  | config | [WifiP2PConfig](#wifip2pconfig8) | Yes| P2P group configuration.|
1322
1323**Return value**
1324
1325  | Type| Description|
1326  | -------- | -------- |
1327  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
1328
1329
1330**Example**
1331  ```js
1332  import wifi from '@ohos.wifi';
1333
1334  var recvP2pConnectionChangeFunc = result => {
1335      console.info("p2p connection change receive event: " + JSON.stringify(result));
1336      wifi.getP2pLinkedInfo((err, data) => {
1337          if (err) {
1338              console.error('failed to get getP2pLinkedInfo: ' + JSON.stringify(err));
1339              return;
1340          }
1341          console.info("get getP2pLinkedInfo: " + JSON.stringify(data));
1342      });
1343  }
1344  wifi.on("p2pConnectionChange", recvP2pConnectionChangeFunc);
1345
1346  var recvP2pDeviceChangeFunc = result => {
1347      console.info("p2p device change receive event: " + JSON.stringify(result));
1348  }
1349  wifi.on("p2pDeviceChange", recvP2pDeviceChangeFunc);
1350
1351  var recvP2pPeerDeviceChangeFunc = result => {
1352      console.info("p2p peer device change receive event: " + JSON.stringify(result));
1353      wifi.getP2pPeerDevices((err, data) => {
1354          if (err) {
1355              console.error('failed to get peer devices: ' + JSON.stringify(err));
1356              return;
1357          }
1358          console.info("get peer devices: " + JSON.stringify(data));
1359          var len = Object.keys(data).length;
1360          for (var i = 0; i < len; ++i) {
1361              if (data[i].deviceName === "my_test_device") {
1362                  console.info("p2p connect to test device: " + data[i].deviceAddress);
1363                  var config = {
1364                      "deviceAddress":data[i].deviceAddress,
1365                      "netId":-2,
1366                      "passphrase":"",
1367                      "groupName":"",
1368                      "goBand":0,
1369                  }
1370                  wifi.p2pConnect(config);
1371              }
1372          }
1373      });
1374  }
1375  wifi.on("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc);
1376
1377  var recvP2pPersistentGroupChangeFunc = () => {
1378      console.info("p2p persistent group change receive event");
1379
1380      wifi.getCurrentGroup((err, data) => {
1381          if (err) {
1382              console.error('failed to get current group: ' + JSON.stringify(err));
1383              return;
1384          }
1385          console.info("get current group: " + JSON.stringify(data));
1386      });
1387  }
1388  wifi.on("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc);
1389
1390  setTimeout(function() {wifi.off("p2pConnectionChange", recvP2pConnectionChangeFunc);}, 125 * 1000);
1391  setTimeout(function() {wifi.off("p2pDeviceChange", recvP2pDeviceChangeFunc);}, 125 * 1000);
1392  setTimeout(function() {wifi.off("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc);}, 125 * 1000);
1393  setTimeout(function() {wifi.off("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc);}, 125 * 1000);
1394  console.info("start discover devices -> " + wifi.startDiscoverDevices());
1395  ```
1396
1397## wifi.p2pCancelConnect<sup>8+</sup>
1398
1399p2pCancelConnect(): boolean
1400
1401Cancels this P2P connection.
1402
1403**Required permissions**: ohos.permission.GET_WIFI_INFO
1404
1405**System capability**: SystemCapability.Communication.WiFi.P2P
1406
1407**Return value**
1408
1409  | Type| Description|
1410  | -------- | -------- |
1411  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
1412
1413
1414## wifi.startDiscoverDevices<sup>8+</sup>
1415
1416startDiscoverDevices(): boolean
1417
1418Starts to discover devices.
1419
1420**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION
1421
1422**System capability**: SystemCapability.Communication.WiFi.P2P
1423
1424**Return value**
1425
1426  | Type| Description|
1427  | -------- | -------- |
1428  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
1429
1430
1431## wifi.stopDiscoverDevices<sup>8+</sup>
1432
1433stopDiscoverDevices(): boolean
1434
1435Stops discovering devices.
1436
1437**Required permissions**: ohos.permission.GET_WIFI_INFO
1438
1439**System capability**: SystemCapability.Communication.WiFi.P2P
1440
1441**Return value**
1442
1443  | Type| Description|
1444  | -------- | -------- |
1445  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
1446
1447
1448## wifi.deletePersistentGroup<sup>8+</sup>
1449
1450deletePersistentGroup(netId: number): boolean
1451
1452Deletes a persistent group.
1453
1454**System API**: This is a system API.
1455
1456**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION
1457
1458**System capability**: SystemCapability.Communication.WiFi.P2P
1459
1460**Parameters**
1461
1462
1463  | **Name**| **Type**| Mandatory| **Description**|
1464  | -------- | -------- | -------- | -------- |
1465  | netId | number | Yes| ID of the group to delete.|
1466
1467**Return value**
1468
1469  | Type| Description|
1470  | -------- | -------- |
1471  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
1472
1473
1474## WifiP2pGroupInfo<sup>8+</sup>
1475
1476Represents the P2P group information.
1477
1478**System capability**: SystemCapability.Communication.WiFi.P2P
1479
1480| Name| Type| Readable| Writable| Description|
1481| -------- | -------- | -------- | -------- | -------- |
1482| isP2pGo | boolean | Yes| No| Whether the device is the group owner.|
1483| ownerInfo | [WifiP2pDevice](#wifip2pdevice8) | Yes| No| Device information of the group.|
1484| passphrase | string | Yes| No| Passphrase of the group.|
1485| interface | string | Yes| No| Interface name.|
1486| groupName | string | Yes| No| Group name.|
1487| networkId | number | Yes| No| Network ID.|
1488| frequency | number | Yes| No| Frequency of the group.|
1489| clientDevices | [WifiP2pDevice[]](#wifip2pdevice8) | Yes| No| List of connected devices.|
1490| goIpAddress | string | Yes| No| IP address of the group.|
1491
1492
1493## wifi.setDeviceName<sup>8+</sup>
1494
1495setDeviceName(devName: string): boolean
1496
1497Sets the device name.
1498
1499**System API**: This is a system API.
1500
1501**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications)
1502
1503**System capability**: SystemCapability.Communication.WiFi.P2P
1504
1505**Parameters**
1506
1507  | **Name**| **Type**| **Mandatory**| **Description**|
1508  | -------- | -------- | -------- | -------- |
1509  | devName | string | Yes| Device name to set.|
1510
1511**Return value**
1512
1513  | **Type**| **Description**|
1514  | -------- | -------- |
1515  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
1516
1517
1518## wifi.on('wifiStateChange')<sup>7+</sup>
1519
1520on(type: "wifiStateChange", callback: Callback&lt;number&gt;): void
1521
1522Registers the WLAN state change events.
1523
1524**Required permissions**: ohos.permission.GET_WIFI_INFO
1525
1526**System capability**: SystemCapability.Communication.WiFi.STA
1527
1528**Parameters**
1529
1530  | **Name**| **Type**| **Mandatory**| **Description**|
1531  | -------- | -------- | -------- | -------- |
1532  | type | string | Yes| Event type. The value is **wifiStateChange**.|
1533  | callback | Callback&lt;number&gt; | Yes| Callback invoked to return the WLAN state.|
1534
1535**WLAN states**
1536
1537| **Value**| **Description**|
1538| -------- | -------- |
1539| 0 | Deactivated|
1540| 1 | Activated|
1541| 2 | Activating|
1542| 3 | Deactivating|
1543
1544
1545## wifi.off('wifiStateChange')<sup>7+</sup>
1546
1547off(type: "wifiStateChange", callback?: Callback&lt;number&gt;): void
1548
1549Unregisters the WLAN state change events.
1550
1551**Required permissions**: ohos.permission.GET_WIFI_INFO
1552
1553**System capability**: SystemCapability.Communication.WiFi.STA
1554
1555**Parameters**
1556
1557  | **Name**| **Type**| **Mandatory**| **Description**|
1558  | -------- | -------- | -------- | -------- |
1559  | type | string | Yes| Event type. The value is **wifiStateChange**.|
1560  | callback | Callback&lt;number&gt; | No| Callback for the WLAN state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.|
1561
1562**Example**
1563  ```js
1564  import wifi from '@ohos.wifi';
1565
1566  var recvPowerNotifyFunc = result => {
1567      console.info("Receive power state change event: " + result);
1568  }
1569
1570  // Register an event.
1571  wifi.on("wifiStateChange", recvPowerNotifyFunc);
1572
1573  // Unregister an event.
1574  wifi.off("wifiStateChange", recvPowerNotifyFunc);
1575  ```
1576
1577
1578## wifi.on('wifiConnectionChange')<sup>7+</sup>
1579
1580on(type: "wifiConnectionChange", callback: Callback&lt;number&gt;): void
1581
1582Registers the WLAN connection state change events.
1583
1584**Required permissions**: ohos.permission.GET_WIFI_INFO
1585
1586**System capability**: SystemCapability.Communication.WiFi.STA
1587
1588**Parameters**
1589
1590  | **Name**| **Type**| **Mandatory**| **Description**|
1591  | -------- | -------- | -------- | -------- |
1592  | type | string | Yes| Event type. The value is **wifiConnectionChange**.|
1593  | callback | Callback&lt;number&gt; | Yes| Callback invoked to return the WLAN connection state.|
1594
1595**WLAN connection states**
1596
1597| **Value**| **Description**|
1598| -------- | -------- |
1599| 0 | Disconnected.|
1600| 1 | Connected.|
1601
1602
1603## wifi.off('wifiConnectionChange')<sup>7+</sup>
1604
1605off(type: "wifiConnectionChange", callback?: Callback&lt;number&gt;): void
1606
1607Unregisters the WLAN connection state change events.
1608
1609**Required permissions**: ohos.permission.GET_WIFI_INFO
1610
1611**System capability**: SystemCapability.Communication.WiFi.STA
1612
1613**Parameters**
1614
1615  | **Name**| **Type**| **Mandatory**| **Description**|
1616  | -------- | -------- | -------- | -------- |
1617  | type | string | Yes| Event type. The value is **wifiConnectionChange**.|
1618  | callback | Callback&lt;number&gt; | No| Callback for the WLAN connection state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.|
1619
1620
1621## wifi.on('wifiScanStateChange')<sup>7+</sup>
1622
1623on(type: "wifiScanStateChange", callback: Callback&lt;number&gt;): void
1624
1625Registers the WLAN scan state change events.
1626
1627**Required permissions**: ohos.permission.GET_WIFI_INFO
1628
1629**System capability**: SystemCapability.Communication.WiFi.STA
1630
1631**Parameters**
1632
1633  | **Name**| **Type**| **Mandatory**| **Description**|
1634  | -------- | -------- | -------- | -------- |
1635  | type | string | Yes| Event type. The value is **wifiScanStateChange**.|
1636  | callback | Callback&lt;number&gt; | Yes| Callback invoked to return the WLAN scan state.|
1637
1638**WLAN scan states**
1639
1640| **Value**| **Description**|
1641| -------- | -------- |
1642| 0 | Scan failed.|
1643| 1 | Scan successful.|
1644
1645
1646## wifi.off('wifiScanStateChange')<sup>7+</sup>
1647
1648off(type: "wifiScanStateChange", callback?: Callback&lt;number&gt;): void
1649
1650Unregisters the WLAN scan state change events.
1651
1652**Required permissions**: ohos.permission.GET_WIFI_INFO
1653
1654**System capability**: SystemCapability.Communication.WiFi.STA
1655
1656**Parameters**
1657
1658| **Name**| **Type**| **Mandatory**| **Description**|
1659| -------- | -------- | -------- | -------- |
1660| type | string | Yes| Event type. The value is **wifiScanStateChange**.|
1661| callback | Callback&lt;number&gt; | No| Callback for the WLAN scan state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.|
1662
1663
1664## wifi.on('wifiRssiChange')<sup>7+</sup>
1665
1666on(type: "wifiRssiChange", callback: Callback&lt;number&gt;): void
1667
1668Registers the RSSI change events.
1669
1670**Required permissions**: ohos.permission.GET_WIFI_INFO
1671
1672**System capability**: SystemCapability.Communication.WiFi.STA
1673
1674**Parameters**
1675
1676  | **Name**| **Type**| **Mandatory**| **Description**|
1677  | -------- | -------- | -------- | -------- |
1678  | type | string | Yes| Event type. The value is **wifiRssiChange**.|
1679  | callback | Callback&lt;number&gt; | Yes| Callback invoked to return the RSSI, in dBm.|
1680
1681
1682## wifi.off('wifiRssiChange')<sup>7+</sup>
1683
1684off(type: "wifiRssiChange", callback?: Callback&lt;number&gt;): void
1685
1686Unregisters the RSSI change events.
1687
1688**Required permissions**: ohos.permission.GET_WIFI_INFO
1689
1690**System capability**: SystemCapability.Communication.WiFi.STA
1691
1692**Parameters**
1693
1694  | **Name**| **Type**| **Mandatory**| **Description**|
1695  | -------- | -------- | -------- | -------- |
1696  | type | string | Yes| Event type. The value is **wifiRssiChange**.|
1697| callback | Callback&lt;number&gt; | No| Callback for the RSSI. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.|
1698
1699
1700## wifi.on('hotspotStateChange')<sup>7+</sup>
1701
1702on(type: "hotspotStateChange", callback: Callback&lt;number&gt;): void
1703
1704Registers the hotspot state change events.
1705
1706**Required permissions**: ohos.permission.GET_WIFI_INFO
1707
1708**System capability**: SystemCapability.Communication.WiFi.AP.Core
1709
1710**Parameters**
1711
1712  | **Name**| **Type**| **Mandatory**| **Description**|
1713  | -------- | -------- | -------- | -------- |
1714  | type | string | Yes| Event type. The value is **hotspotStateChange**.|
1715  | callback | Callback&lt;number&gt; | Yes| Callback invoked to return the hotspot state.|
1716
1717**Hotspot states**
1718
1719| **Value**| **Description**|
1720| -------- | -------- |
1721| 0 | Deactivated|
1722| 1 | Activated|
1723| 2 | Activating|
1724| 3 | Deactivating|
1725
1726
1727## wifi.off('hotspotStateChange')<sup>7+</sup>
1728
1729off(type: "hotspotStateChange", callback?: Callback&lt;number&gt;): void
1730
1731Unregisters the hotspot state change events.
1732
1733**Required permissions**: ohos.permission.GET_WIFI_INFO
1734
1735**System capability**: SystemCapability.Communication.WiFi.AP.Core
1736
1737**Parameters**
1738
1739  | **Name**| **Type**| **Mandatory**| **Description**|
1740  | -------- | -------- | -------- | -------- |
1741  | type | string | Yes| Event type. The value is **hotspotStateChange**.|
1742| callback | Callback&lt;number&gt; | No| Callback for the hotspot state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.|
1743
1744
1745## wifi.on('p2pStateChange')<sup>8+</sup>
1746
1747on(type: "p2pStateChange", callback: Callback&lt;number&gt;): void
1748
1749Registers the P2P state change events.
1750
1751**Required permissions**: ohos.permission.GET_WIFI_INFO
1752
1753**System capability**: SystemCapability.Communication.WiFi.P2P
1754
1755**Parameters**
1756
1757  | **Name**| **Type**| **Mandatory**| **Description**|
1758  | -------- | -------- | -------- | -------- |
1759  | type | string | Yes| Event type. The value is **p2pStateChange**.|
1760  | callback | Callback&lt;number&gt; | Yes| Callback invoked to return the P2P state.|
1761
1762**P2P states**
1763
1764| **Value**| **Description**|
1765| -------- | -------- |
1766| 1 | Available|
1767| 2 | Opening|
1768| 3 | Opened|
1769| 4 | Closing|
1770| 5 | Closed|
1771
1772## wifi.off('p2pStateChange')<sup>8+</sup>
1773
1774off(type: "p2pStateChange", callback?: Callback&lt;number&gt;): void
1775
1776Unregisters the P2P state change events.
1777
1778**Required permissions**: ohos.permission.GET_WIFI_INFO
1779
1780**System capability**: SystemCapability.Communication.WiFi.P2P
1781
1782**Parameters**
1783
1784  | **Name**| **Type**| **Mandatory**| **Description**|
1785  | -------- | -------- | -------- | -------- |
1786  | type | string | Yes| Event type. The value is **p2pStateChange**.|
1787| callback | Callback&lt;number&gt; | No| Callback for the P2P state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.|
1788
1789
1790  ## wifi.on('p2pConnectionChange')<sup>8+</sup>
1791
1792on(type: "p2pConnectionChange", callback: Callback&lt;WifiP2pLinkedInfo&gt;): void
1793
1794Registers the P2P connection state change events.
1795
1796**Required permissions**: ohos.permission.GET_WIFI_INFO
1797
1798**System capability**: SystemCapability.Communication.WiFi.P2P
1799
1800**Parameters**
1801
1802  | **Name**| **Type**| **Mandatory**| **Description**|
1803  | -------- | -------- | -------- | -------- |
1804  | type | string | Yes| Event type. The value is **p2pConnectionChange**.|
1805  | callback | Callback&lt;[WifiP2pLinkedInfo](#wifip2plinkedinfo8)&gt; | Yes| Callback invoked to return the P2P connection state.|
1806
1807
1808## wifi.off('p2pConnectionChange')<sup>8+</sup>
1809
1810off(type: "p2pConnectionChange", callback?: Callback&lt;WifiP2pLinkedInfo&gt;): void
1811
1812Unregisters the P2P connection state change events.
1813
1814**Required permissions**: ohos.permission.GET_WIFI_INFO
1815
1816**System capability**: SystemCapability.Communication.WiFi.P2P
1817
1818**Parameters**
1819
1820  | **Name**| **Type**| **Mandatory**| **Description**|
1821  | -------- | -------- | -------- | -------- |
1822  | type | string | Yes| Event type. The value is **p2pConnectionChange**.|
1823| callback | Callback&lt;[WifiP2pLinkedInfo](#wifip2plinkedinfo8)&gt; | No| Callback for the P2P connection state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.|
1824
1825
1826## wifi.on('p2pDeviceChange')<sup>8+</sup>
1827
1828on(type: "p2pDeviceChange", callback: Callback&lt;WifiP2pDevice&gt;): void
1829
1830Registers the P2P device state change events.
1831
1832**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION
1833
1834**System capability**: SystemCapability.Communication.WiFi.P2P
1835
1836**Parameters**
1837
1838  | **Name**| **Type**| **Mandatory**| **Description**|
1839  | -------- | -------- | -------- | -------- |
1840  | type | string | Yes| Event type. The value is **p2pDeviceChange**.|
1841  | callback | Callback&lt;[WifiP2pDevice](#wifip2pdevice8)&gt; | Yes| Callback invoked to return the P2P device state.|
1842
1843
1844## wifi.off('p2pDeviceChange')<sup>8+</sup>
1845
1846off(type: "p2pDeviceChange", callback?: Callback&lt;WifiP2pDevice&gt;): void
1847
1848Unregisters the P2P device state change events.
1849
1850**Required permissions**: ohos.permission.LOCATION
1851
1852**System capability**: SystemCapability.Communication.WiFi.P2P
1853
1854**Parameters**
1855
1856  | **Name**| **Type**| **Mandatory**| **Description**|
1857  | -------- | -------- | -------- | -------- |
1858  | type | string | Yes| Event type. The value is **p2pDeviceChange**.|
1859| callback | Callback&lt;[WifiP2pDevice](#wifip2pdevice8)&gt; | No| Callback for the P2P device state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.|
1860
1861
1862## wifi.on('p2pPeerDeviceChange')<sup>8+</sup>
1863
1864on(type: "p2pPeerDeviceChange", callback: Callback&lt;WifiP2pDevice[]&gt;): void
1865
1866Registers the P2P peer device state change events.
1867
1868**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION
1869
1870**System capability**: SystemCapability.Communication.WiFi.P2P
1871
1872**Parameters**
1873
1874  | **Name**| **Type**| **Mandatory**| **Description**|
1875  | -------- | -------- | -------- | -------- |
1876  | type | string | Yes| Event type. The value is **p2pPeerDeviceChange**.|
1877| callback | Callback&lt;[WifiP2pDevice[]](#wifip2pdevice8)&gt; | Yes| Callback invoked to return the P2P peer device state.|
1878
1879
1880## wifi.off('p2pPeerDeviceChange')<sup>8+</sup>
1881
1882off(type: "p2pPeerDeviceChange", callback?: Callback&lt;WifiP2pDevice[]&gt;): void
1883
1884Unregisters the P2P peer device state change events.
1885
1886**Required permissions**: ohos.permission.LOCATION
1887
1888**System capability**: SystemCapability.Communication.WiFi.P2P
1889
1890**Parameters**
1891
1892  | **Name**| **Type**| **Mandatory**| **Description**|
1893  | -------- | -------- | -------- | -------- |
1894  | type | string | Yes| Event type. The value is **p2pPeerDeviceChange**.|
1895| callback | Callback&lt;[WifiP2pDevice[]](#wifip2pdevice8)&gt; | No| Callback for the peer device state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.|
1896
1897
1898## wifi.on('p2pPersistentGroupChange')<sup>8+</sup>
1899
1900on(type: "p2pPersistentGroupChange", callback: Callback&lt;void&gt;): void
1901
1902Registers the P2P persistent group state change events.
1903
1904**Required permissions**: ohos.permission.GET_WIFI_INFO
1905
1906**System capability**: SystemCapability.Communication.WiFi.P2P
1907
1908**Parameters**
1909
1910  | **Name**| **Type**| **Mandatory**| **Description**|
1911  | -------- | -------- | -------- | -------- |
1912  | type | string | Yes| Event type. The value is **p2pPersistentGroupChange**.|
1913  | callback | Callback&lt;void&gt; | Yes| Callback invoked to return the P2P persistent group state.|
1914
1915
1916## wifi.off('p2pPersistentGroupChange')<sup>8+</sup>
1917
1918off(type: "p2pPersistentGroupChange", callback?: Callback&lt;void&gt;): void
1919
1920Unregisters the P2P persistent group state change events.
1921
1922**Required permissions**: ohos.permission.GET_WIFI_INFO
1923
1924**System capability**: SystemCapability.Communication.WiFi.P2P
1925
1926**Parameters**
1927
1928  | **Name**| **Type**| **Mandatory**| **Description**|
1929  | -------- | -------- | -------- | -------- |
1930  | type | string | Yes| Event type. The value is **p2pPersistentGroupChange**.|
1931| callback | Callback&lt;void&gt; | No| Callback for the P2P persistent group state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.|
1932
1933
1934## wifi.on('p2pDiscoveryChange')<sup>8+</sup>
1935
1936on(type: "p2pDiscoveryChange", callback: Callback&lt;number&gt;): void
1937
1938Registers the P2P device discovery state change events.
1939
1940**Required permissions**: ohos.permission.GET_WIFI_INFO
1941
1942**System capability**: SystemCapability.Communication.WiFi.P2P
1943
1944**Parameters**
1945
1946  | **Name**| **Type**| **Mandatory**| **Description**|
1947  | -------- | -------- | -------- | -------- |
1948  | type | string | Yes| Event type. The value is **p2pDiscoveryChange**.|
1949  | callback | Callback&lt;number&gt; | Yes| Callback invoked to return the P2P device discovery state.|
1950
1951**P2P discovered device states**
1952
1953| **Value**| **Description**|
1954| -------- | -------- |
1955| 0 | Initial state.|
1956| 1 | Discovered.|
1957
1958
1959## wifi.off('p2pDiscoveryChange')<sup>8+</sup>
1960
1961off(type: "p2pDiscoveryChange", callback?: Callback&lt;number&gt;): void
1962
1963Unregisters the P2P device discovery state change events.
1964
1965**Required permissions**: ohos.permission.GET_WIFI_INFO
1966
1967**System capability**: SystemCapability.Communication.WiFi.P2P
1968
1969**Parameters**
1970
1971  | **Name**| **Type**| **Mandatory**| **Description**|
1972  | -------- | -------- | -------- | -------- |
1973  | type | string | Yes| Event type. The value is **p2pDiscoveryChange**.|
1974| callback | Callback&lt;number&gt; | No| Callback for the P2P device discovery state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.|
1975