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