1# @ohos.wifi (WLAN) (System API)
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> This topic describes only the system APIs provided by the module. For details about its public APIs, see [@ohos.wifi (WLAN)](js-apis-wifi.md).
9> The APIs of this module are no longer maintained since API version 9. You are advised to use [@ohos.wifiManager (WLAN)](js-apis-wifiManager-sys.md).
10
11
12## Modules to Import
13
14```ts
15import wifi from '@ohos.wifi';
16```
17
18## wifi.enableWifi
19
20enableWifi(): boolean
21
22Enables WLAN.
23
24**System API**: This is a system API.
25
26**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications)
27
28**System capability**: SystemCapability.Communication.WiFi.STA
29
30**Return value**
31
32  | **Type**| **Description**|
33  | -------- | -------- |
34  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
35
36**Example**
37
38```ts
39import wifi from '@ohos.wifi';
40
41try {
42	wifi.enableWifi();
43}catch(error){
44	console.error("failed:" + JSON.stringify(error));
45}
46```
47
48## wifi.disableWifi
49
50disableWifi(): boolean
51
52Disables WLAN.
53
54**System API**: This is a system API.
55
56**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications)
57
58**System capability**: SystemCapability.Communication.WiFi.STA
59
60**Return value**
61
62  | **Type**| **Description**|
63  | -------- | -------- |
64  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
65
66**Example**
67
68```ts
69import wifi from '@ohos.wifi';
70
71try {
72	wifi.disableWifi();
73}catch(error){
74	console.error("failed:" + JSON.stringify(error));
75}
76
77```
78
79
80## wifi.addDeviceConfig
81
82addDeviceConfig(config: WifiDeviceConfig): Promise<number>
83
84Adds network configuration. This API uses a promise to return the result.
85
86**System API**: This is a system API.
87
88**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.SET_WIFI_CONFIG
89
90**System capability**: SystemCapability.Communication.WiFi.STA
91
92**Parameters**
93
94  | **Name**| **Type**| **Mandatory**| **Description**|
95  | -------- | -------- | -------- | -------- |
96  | config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration to add.|
97
98**Return value**
99
100  | **Type**| **Description**|
101  | -------- | -------- |
102| Promise<number> | Promise used to return the WLAN configuration ID. If **-1** is returned, the network configuration fails to be added.|
103
104  **Example**
105
106```ts
107import wifi from '@ohos.wifi';
108
109try {
110	let config:wifi.WifiDeviceConfig = {
111		ssid : "****",
112		bssid:  "****",
113		preSharedKey: "****",
114		isHiddenSsid: false,
115		securityType: 0,
116		creatorUid: 0,
117		disableReason: 0,
118		netId: 0,
119		randomMacType: 0,
120		randomMacAddr:  "****",
121		ipType: 0,
122		staticIp: {
123			ipAddress: 0,
124			gateway: 0,
125			dnsServers: [],
126			domains: []
127		}
128	}
129	wifi.addDeviceConfig(config).then(result => {
130		console.info("result:" + JSON.stringify(result));
131	});
132}catch(error){
133	console.error("failed:" + JSON.stringify(error));
134}
135```
136
137## WifiDeviceConfig
138
139Represents the WLAN configuration.
140
141**System capability**: SystemCapability.Communication.WiFi.STA
142
143
144| **Name**| **Type**| **Readable**| **Writable**| **Description**|
145| -------- | -------- | -------- | -------- | -------- |
146| creatorUid | number | Yes| No| ID of the creator.<br>**System API**: This is a system API.|
147| disableReason | number | Yes| No| Reason for disabling WLAN.<br>**System API**: This is a system API.|
148| netId | number | Yes| No| Network ID.<br>**System API**: This is a system API.|
149| randomMacType | number | Yes| No| Random MAC type.<br>**System API**: This is a system API.|
150| randomMacAddr | string | Yes| No| Random MAC address.<br>**System API**: This is a system API.|
151| ipType | [IpType](#iptype7) | Yes| No| IP address type.<br>**System API**: This is a system API.|
152| staticIp | [IpConfig](#ipconfig7) | Yes| No| Static IP address configuration.<br>**System API**: This is a system API.|
153
154
155## IpType<sup>7+</sup>
156
157Enumerates the IP address types.
158
159**System API**: This is a system API.
160
161**System capability**: SystemCapability.Communication.WiFi.STA
162
163
164| Name| Value| Description|
165| -------- | -------- | -------- |
166| STATIC | 0 | Static IP address.|
167| DHCP | 1 | IP address allocated by DHCP.|
168| UNKNOWN | 2 | Not specified.|
169
170
171## IpConfig<sup>7+</sup>
172
173Represents IP configuration information.
174
175**System API**: This is a system API.
176
177**System capability**: SystemCapability.Communication.WiFi.STA
178
179| **Name**| **Type**| **Readable**| **Writable**| **Description**|
180| -------- | -------- | -------- | -------- | -------- |
181| ipAddress | number | Yes| No| IP address.|
182| gateway | number | Yes| No| Gateway.|
183| dnsServers | number[] | Yes| No| Domain name server (DNS) information.|
184| domains | Array&lt;string&gt; | Yes| No| Domain information.|
185
186
187## wifi.addDeviceConfig
188
189addDeviceConfig(config: WifiDeviceConfig, callback: AsyncCallback&lt;number&gt;): void
190
191Adds network configuration. This API uses an asynchronous callback to return the result.
192
193**System API**: This is a system API.
194
195**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.SET_WIFI_CONFIG
196
197**System capability**: SystemCapability.Communication.WiFi.STA
198
199**Parameters**
200
201  | **Name**| **Type**| **Mandatory**| **Description**|
202  | -------- | -------- | -------- | -------- |
203  | config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration to add.|
204  | callback | AsyncCallback&lt;number&gt; | Yes| Callback used 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.|
205
206**Example**
207
208```ts
209import wifi from '@ohos.wifi';
210
211try {
212	let config:wifi.WifiDeviceConfig = {
213		ssid : "****",
214		bssid:  "****",
215		preSharedKey: "****",
216		isHiddenSsid: false,
217		securityType: 0,
218		creatorUid: 0,
219		disableReason: 0,
220		netId: 0,
221		randomMacType: 0,
222		randomMacAddr:  "****",
223		ipType: 0,
224		staticIp: {
225			ipAddress: 0,
226			gateway: 0,
227			dnsServers: [],
228			domains: []
229		}
230	}
231	wifi.addDeviceConfig(config,(error,result) => {
232		console.info("result:" + JSON.stringify(result));
233	});
234}catch(error){
235	console.error("failed:" + JSON.stringify(error));
236}
237```
238
239
240## wifi.connectToNetwork
241
242connectToNetwork(networkId: number): boolean
243
244Connects to the specified network.
245
246**System API**: This is a system API.
247
248**Required permissions**: ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications)
249
250**System capability**: SystemCapability.Communication.WiFi.STA
251
252**Parameters**
253
254  | **Name**| **Type**| **Mandatory**| **Description**|
255  | -------- | -------- | -------- | -------- |
256  | networkId | number | Yes| Network configuration ID.|
257
258**Return value**
259
260  | **Type**| **Description**|
261  | -------- | -------- |
262  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
263
264**Example**
265
266```ts
267import wifi from '@ohos.wifi';
268
269try {
270	let networkId = 0;
271	wifi.connectToNetwork(networkId);
272}catch(error){
273	console.error("failed:" + JSON.stringify(error));
274}
275```
276
277## wifi.connectToDevice
278
279connectToDevice(config: WifiDeviceConfig): boolean
280
281Connects to the specified network.
282
283**System API**: This is a system API.
284
285**Required permissions**: ohos.permission.SET_WIFI_INFO, ohos.permission.SET_WIFI_CONFIG, and ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications)
286
287**System capability**:
288  SystemCapability.Communication.WiFi.STA
289
290**Parameters**
291
292  | **Name**| **Type**| **Mandatory**| **Description**|
293  | -------- | -------- | -------- | -------- |
294| config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration.|
295
296**Return value**
297
298  | **Type**| **Description**|
299  | -------- | -------- |
300  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
301
302**Example**
303```ts
304import wifi from '@ohos.wifi';
305
306try {
307	let config:wifi.WifiDeviceConfig = {
308		ssid : "****",
309		bssid:  "****",
310		preSharedKey: "****",
311		isHiddenSsid: false,
312		securityType: 3,
313		creatorUid: 0,
314		disableReason: 0,
315		netId: 0,
316		randomMacType: 0,
317		randomMacAddr:  "****",
318		ipType: 0,
319		staticIp: {
320		ipAddress: 0,
321			gateway: 0,
322			dnsServers: [],
323			domains: []
324		}
325	}
326	wifi.connectToDevice(config);
327
328}catch(error){
329	console.error("failed:" + JSON.stringify(error));
330}
331```
332
333## wifi.disconnect
334
335disconnect(): boolean
336
337Disconnects the network.
338
339**System API**: This is a system API.
340
341**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications)
342
343**System capability**:
344  SystemCapability.Communication.WiFi.STA
345
346**Return value**
347
348  | **Type**| **Description**|
349  | -------- | -------- |
350  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
351
352**Example**
353```ts
354import wifi from '@ohos.wifi';
355
356try {
357	wifi.disconnect();
358}catch(error){
359	console.error("failed:" + JSON.stringify(error));
360}
361```
362
363
364## WifiLinkedInfo
365
366Represents the WLAN connection information.
367
368**System capability**: SystemCapability.Communication.WiFi.STA
369
370| Name| Type| Readable| Writable| Description|
371| -------- | -------- | -------- | -------- | -------- |
372| networkId | number | Yes| No| Network configuration ID.<br>**System API**: This is a system API.|
373| chload | number | Yes| No| Channel load. A larger value indicates a higher load.<br>**System API**: This is a system API.|
374| snr | number | Yes| No| Signal-to-noise ratio (SNR).<br>**System API**: This is a system API.|
375| suppState | [SuppState](#suppstate) | Yes| No| Supplicant state.<br>**System API**: This is a system API.|
376
377
378## SuppState
379
380Enumerates the supplicant states.
381
382**System API**: This is a system API.
383
384**System capability**: SystemCapability.Communication.WiFi.STA
385
386| Name| Value| Description|
387| -------- | -------- | -------- |
388| DISCONNECTED | 0 | The supplicant is disconnected from the AP.|
389| INTERFACE_DISABLED | 1 | The network interface is disabled.|
390| INACTIVE | 2 | The supplicant is inactive.|
391| SCANNING | 3 | The supplicant is scanning for a WLAN connection.|
392| AUTHENTICATING | 4 | The supplicant is being authenticated.|
393| ASSOCIATING | 5 | The supplicant is being associated with an AP.|
394| ASSOCIATED | 6 | The supplicant is associated with an AP.|
395| FOUR_WAY_HANDSHAKE | 7 | A four-way handshake is being performed for the supplicant.|
396| GROUP_HANDSHAKE | 8 | A group handshake is being performed for the supplicant.|
397| COMPLETED | 9 | The authentication is complete.|
398| UNINITIALIZED | 10 | The supplicant failed to set up the connection.|
399| INVALID | 11 | Invalid value.|
400
401
402
403## wifi.getSupportedFeatures<sup>7+</sup>
404
405getSupportedFeatures(): number
406
407Obtains the features supported by this device.
408
409**System API**: This is a system API.
410
411**Required permissions**: ohos.permission.GET_WIFI_INFO
412
413**System capability**: SystemCapability.Communication.WiFi.Core
414
415**Return value**
416
417  | **Type**| **Description**|
418  | -------- | -------- |
419  | number | Feature value. |
420
421**Feature IDs**
422
423| Value| Description|
424| -------- | -------- |
425| 0x0001 | WLAN infrastructure mode|
426| 0x0002 | 5 GHz feature|
427| 0x0004 | Generic Advertisement Service (GAS)/Access Network Query Protocol (ANQP) feature|
428| 0x0008 | Wi-Fi Direct|
429| 0x0010 | SoftAP|
430| 0x0040 | Wi-Fi AWare|
431| 0x8000 | WLAN AP/STA concurrency|
432| 0x8000000 | WPA3 Personal (WPA-3 SAE)|
433| 0x10000000 | WPA3-Enterprise Suite B |
434| 0x20000000 | Enhanced open feature|
435
436
437
438
439## wifi.getDeviceMacAddress<sup>7+</sup>
440
441getDeviceMacAddress(): string[]
442
443Obtains the device MAC address.
444
445**System API**: This is a system API.
446
447**Required permissions**: ohos.permission.GET_WIFI_LOCAL_MAC and ohos.permission.GET_WIFI_INFO (available only to system applications)
448
449**System capability**: SystemCapability.Communication.WiFi.STA
450
451**Return value**
452
453  | **Type**| **Description**|
454  | -------- | -------- |
455  | string[] | MAC address obtained.|
456
457**Example**
458```ts
459import wifi from '@ohos.wifi';
460
461try {
462	let ret = wifi.getDeviceMacAddress();
463	console.info("deviceMacAddress:" + JSON.stringify(ret));
464}catch(error){
465	console.error("failed:" + JSON.stringify(error));
466}
467
468```
469
470
471## wifi.reassociate<sup>7+</sup>
472
473reassociate(): boolean
474
475Re-associates with the network.
476
477**System API**: This is a system API.
478
479**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications)
480
481**System capability**: SystemCapability.Communication.WiFi.STA
482
483**Return value**
484
485  | **Type**| **Description**|
486  | -------- | -------- |
487  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
488
489**Example**
490```ts
491import wifi from '@ohos.wifi';
492
493try {
494	wifi.reassociate();
495}catch(error){
496	console.error("failed:" + JSON.stringify(error));
497}
498```
499
500## wifi.reconnect<sup>7+</sup>
501
502reconnect(): boolean
503
504Reconnects to the network.
505
506**System API**: This is a system API.
507
508**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications)
509
510**System capability**: SystemCapability.Communication.WiFi.STA
511
512**Return value**
513
514  | **Type**| **Description**|
515  | -------- | -------- |
516  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
517
518**Example**
519```ts
520import wifi from '@ohos.wifi';
521
522try {
523	wifi.reconnect();
524}catch(error){
525	console.error("failed:" + JSON.stringify(error));
526}
527```
528
529## wifi.getDeviceConfigs<sup>7+</sup>
530
531getDeviceConfigs(): &nbsp;Array&lt;[WifiDeviceConfig](#wifideviceconfig)&gt;
532
533Obtains network configuration.
534
535**System API**: This is a system API.
536
537**Required permissions**: ohos.permission.GET_WIFI_INFO, ohos.permission.LOCATION, and ohos.permission.GET_WIFI_CONFIG
538
539**System capability**: SystemCapability.Communication.WiFi.STA
540
541**Return value**
542
543  | **Type**| **Description**|
544  | -------- | -------- |
545  | &nbsp;Array&lt;[WifiDeviceConfig](#wifideviceconfig)&gt; | Array of network configuration obtained.|
546
547**Example**
548```ts
549import wifi from '@ohos.wifi';
550
551try {
552	let configs = wifi.getDeviceConfigs();
553	console.info("configs:" + JSON.stringify(configs));
554}catch(error){
555	console.error("failed:" + JSON.stringify(error));
556}
557```
558
559## wifi.updateNetwork<sup>7+</sup>
560
561updateNetwork(config: WifiDeviceConfig): number
562
563Updates network configuration.
564
565**System API**: This is a system API.
566
567**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.SET_WIFI_CONFIG
568
569**System capability**: SystemCapability.Communication.WiFi.STA
570
571**Parameters**
572
573  | **Name**| **Type**| **Mandatory**| **Description**|
574  | -------- | -------- | -------- | -------- |
575| config | [WifiDeviceConfig](#wifideviceconfig) | Yes| New WLAN configuration.|
576
577**Return value**
578
579  | **Type**| **Description**|
580  | -------- | -------- |
581  | number | ID of the updated network configuration. The value **-1** indicates that the operation has failed.|
582
583**Example**
584```ts
585import wifi from '@ohos.wifi';
586
587try {
588	let config:wifi.WifiDeviceConfig = {
589		ssid : "****",
590		bssid:  "****",
591		preSharedKey: "****",
592		isHiddenSsid: false,
593		securityType: 3,
594		creatorUid: 0,
595		disableReason: 0,
596		netId: 0,
597		randomMacType: 0,
598		randomMacAddr:  "****",
599		ipType: 0,
600		staticIp: {
601			ipAddress: 0,
602			gateway: 0,
603			dnsServers: [],
604			domains: []
605		}
606	}
607	let ret = wifi.updateNetwork(config);
608	console.error("ret:" + ret);
609}catch(error){
610	console.error("failed:" + JSON.stringify(error));
611}
612```
613
614## wifi.disableNetwork<sup>7+</sup>
615
616disableNetwork(netId: number): boolean
617
618Disables network configuration.
619
620**System API**: This is a system API.
621
622**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications)
623
624**System capability**: SystemCapability.Communication.WiFi.STA
625
626**Parameters**
627
628  | **Name**| **Type**| **Mandatory**| **Description**|
629  | -------- | -------- | -------- | -------- |
630  | netId | number | Yes| ID of the network configuration to disable.|
631
632**Return value**
633
634  | **Type**| **Description**|
635  | -------- | -------- |
636  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
637
638**Example**
639```ts
640import wifi from '@ohos.wifi';
641
642try {
643	let netId = 0;
644	wifi.disableNetwork(netId);
645}catch(error){
646	console.error("failed:" + JSON.stringify(error));
647}
648```
649
650## wifi.removeAllNetwork<sup>7+</sup>
651
652removeAllNetwork(): boolean
653
654Removes the configuration of all networks.
655
656**System API**: This is a system API.
657
658**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications)
659
660**System capability**: SystemCapability.Communication.WiFi.STA
661
662**Return value**
663
664  | **Type**| **Description**|
665  | -------- | -------- |
666  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
667
668**Example**
669```ts
670import wifi from '@ohos.wifi';
671
672try {
673	wifi.removeAllNetwork();
674}catch(error){
675	console.error("failed:" + JSON.stringify(error));
676}
677```
678
679## wifi.removeDevice<sup>7+</sup>
680
681removeDevice(id: number): boolean
682
683Removes the specified network configuration.
684
685**System API**: This is a system API.
686
687**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications)
688
689**System capability**: SystemCapability.Communication.WiFi.STA
690
691**Parameters**
692
693  | **Name**| **Type**| **Mandatory**| **Description**|
694  | -------- | -------- | -------- | -------- |
695| id | number | Yes| ID of the network configuration to remove.|
696
697**Return value**
698
699  | **Type**| **Description**|
700  | -------- | -------- |
701  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
702
703**Example**
704```ts
705import wifi from '@ohos.wifi';
706
707try {
708	let id = 0;
709	wifi.removeDevice(id);
710}catch(error){
711	console.error("failed:" + JSON.stringify(error));
712}
713```
714
715## wifi.enableHotspot<sup>7+</sup>
716
717enableHotspot(): boolean
718
719Enables this hotspot.
720
721**System API**: This is a system API.
722
723**Required permissions**: ohos.permission.MANAGE_WIFI_HOTSPOT (available only to system applications)
724
725**System capability**: SystemCapability.Communication.WiFi.AP.Core
726
727**Return value**
728
729  | **Type**| **Description**|
730  | -------- | -------- |
731  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
732
733**Example**
734```ts
735import wifi from '@ohos.wifi';
736
737try {
738	wifi.enableHotspot();
739}catch(error){
740	console.error("failed:" + JSON.stringify(error));
741}
742```
743
744## wifi.disableHotspot<sup>7+</sup>
745
746disableHotspot(): boolean
747
748Disables this hotspot.
749
750**System API**: This is a system API.
751
752**Required permissions**: ohos.permission.MANAGE_WIFI_HOTSPOT (available only to system applications)
753
754**System capability**: SystemCapability.Communication.WiFi.AP.Core
755
756**Return value**
757
758  | **Type**| **Description**|
759  | -------- | -------- |
760  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
761
762**Example**
763```ts
764import wifi from '@ohos.wifi';
765
766try {
767	wifi.disableHotspot();
768}catch(error){
769	console.error("failed:" + JSON.stringify(error));
770}
771```
772
773## wifi.isHotspotDualBandSupported<sup>7+</sup>
774
775isHotspotDualBandSupported(): boolean
776
777Checks whether the hotspot supports dual band.
778
779**System API**: This is a system API.
780
781**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.MANAGE_WIFI_HOTSPOT (available only to system applications)
782
783**System capability**: SystemCapability.Communication.WiFi.AP.Core
784
785**Return value**
786
787  | **Type**| **Description**|
788  | -------- | -------- |
789  | boolean | Returns **true** if the feature is supported; returns **false** otherwise.|
790
791**Example**
792```ts
793import wifi from '@ohos.wifi';
794
795try {
796	let ret = wifi.isHotspotDualBandSupported();
797	console.info("result:" + ret);
798}catch(error){
799	console.error("failed:" + JSON.stringify(error));
800}
801```
802
803## wifi.isHotspotActive<sup>7+</sup>
804
805isHotspotActive(): boolean
806
807Checks whether this hotspot is active.
808
809**System API**: This is a system API.
810
811**Required permissions**: ohos.permission.GET_WIFI_INFO
812
813**System capability**: SystemCapability.Communication.WiFi.AP.Core
814
815**Return value**
816
817  | **Type**| **Description**|
818  | -------- | -------- |
819| boolean | Returns **true** if the hotspot is active; returns **false** otherwise.|
820
821**Example**
822```ts
823import wifi from '@ohos.wifi';
824
825try {
826	let ret = wifi.isHotspotActive();
827	console.info("result:" + ret);
828}catch(error){
829	console.error("failed:" + JSON.stringify(error));
830}
831```
832
833## wifi.setHotspotConfig<sup>7+</sup>
834
835setHotspotConfig(config: HotspotConfig): boolean
836
837Sets hotspot configuration.
838
839**System API**: This is a system API.
840
841**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.GET_WIFI_CONFIG
842
843**System capability**: SystemCapability.Communication.WiFi.AP.Core
844
845**Parameters**
846
847  | **Name**| **Type**| **Mandatory**| **Description**|
848  | -------- | -------- | -------- | -------- |
849  | config | [HotspotConfig](#hotspotconfig7) | Yes| Hotspot configuration to set.|
850
851**Return value**
852
853  | **Type**| **Description**|
854  | -------- | -------- |
855  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
856
857**Example**
858```ts
859import wifi from '@ohos.wifi';
860
861try {
862	let config:wifi.HotspotConfig = {
863		ssid: "****",
864		securityType: 3,
865		band: 0,
866		preSharedKey: "****",
867		maxConn: 0
868	}
869	let ret = wifi.setHotspotConfig(config);
870	console.info("result:" + ret);
871}catch(error){
872	console.error("failed:" + JSON.stringify(error));
873}
874```
875
876## HotspotConfig<sup>7+</sup>
877
878Represents the hotspot configuration.
879
880**System API**: This is a system API.
881
882**System capability**: SystemCapability.Communication.WiFi.AP.Core
883
884| **Name**| **Type**| **Readable**| **Writable**| **Description**|
885| -------- | -------- | -------- | -------- | -------- |
886| ssid | string | Yes| No| SSID of the hotspot, in UTF-8 format.|
887| securityType | [WifiSecurityType](js-apis-wifi.md#wifisecuritytype) | Yes| No| Security type.|
888| 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.|
889| preSharedKey | string | Yes| No| PSK of the hotspot.|
890| maxConn | number | Yes| No| Maximum number of connections allowed.|
891
892
893## wifi.getHotspotConfig<sup>7+</sup>
894
895getHotspotConfig(): HotspotConfig
896
897Obtains hotspot configuration.
898
899**System API**: This is a system API.
900
901**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.GET_WIFI_CONFIG
902
903**System capability**: SystemCapability.Communication.WiFi.AP.Core
904
905**Return value**
906
907  | **Type**| **Description**|
908  | -------- | -------- |
909  | [HotspotConfig](#hotspotconfig7) | Hotspot configuration obtained.|
910
911**Example**
912```ts
913import wifi from '@ohos.wifi';
914
915try {
916	let config = wifi.getHotspotConfig();
917	console.info("result:" + JSON.stringify(config));
918}catch(error){
919	console.error("failed:" + JSON.stringify(error));
920}
921```
922
923## wifi.getStations<sup>7+</sup>
924
925getStations(): &nbsp;Array&lt;[StationInfo](#stationinfo7)&gt;
926
927Obtains information about the connected stations.
928
929**System API**: This is a system API.
930
931**Required permissions**: ohos.permission.GET_WIFI_INFO, ohos.permission.LOCATION, and ohos.permission.MANAGE_WIFI_HOTSPOT (available only to system applications)
932
933**System capability**: SystemCapability.Communication.WiFi.AP.Core
934
935**Return value**
936
937  | **Type**| **Description**|
938  | -------- | -------- |
939  | &nbsp;Array&lt;[StationInfo](#stationinfo7)&gt; | Connected stations obtained.|
940
941**Example**
942```ts
943import wifi from '@ohos.wifi';
944
945try {
946	let stations = wifi.getStations();
947	console.info("result:" + JSON.stringify(stations));
948}catch(error){
949	console.error("failed:" + JSON.stringify(error));
950}
951```
952
953## StationInfo<sup>7+</sup>
954
955Represents the station information.
956
957**System API**: This is a system API.
958
959**System capability**: SystemCapability.Communication.WiFi.AP.Core
960
961| **Name**| **Type**| **Readable**| **Writable**| **Description**|
962| -------- | -------- | -------- | -------- | -------- |
963| name | string | Yes| No| Device name.|
964| macAddress | string | Yes| No| MAC address.|
965| ipAddress | string | Yes| No| IP address.|
966
967
968## wifi.deletePersistentGroup<sup>8+</sup>
969
970deletePersistentGroup(netId: number): boolean
971
972Deletes a persistent group.
973
974**System API**: This is a system API.
975
976**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION
977
978**System capability**: SystemCapability.Communication.WiFi.P2P
979
980**Parameters**
981
982
983  | **Name**| **Type**| Mandatory| **Description**|
984  | -------- | -------- | -------- | -------- |
985  | netId | number | Yes| ID of the group to delete.|
986
987**Return value**
988
989  | Type| Description|
990  | -------- | -------- |
991  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
992
993**Example**
994```ts
995import wifi from '@ohos.wifi';
996
997try {
998	let netId = 0;
999	wifi.deletePersistentGroup(netId);
1000}catch(error){
1001	console.error("failed:" + JSON.stringify(error));
1002}
1003```
1004
1005
1006## wifi.setDeviceName<sup>8+</sup>
1007
1008setDeviceName(devName: string): boolean
1009
1010Sets the device name.
1011
1012**System API**: This is a system API.
1013
1014**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications)
1015
1016**System capability**: SystemCapability.Communication.WiFi.P2P
1017
1018**Parameters**
1019
1020  | **Name**| **Type**| **Mandatory**| **Description**|
1021  | -------- | -------- | -------- | -------- |
1022  | devName | string | Yes| Device name to set.|
1023
1024**Return value**
1025
1026  | **Type**| **Description**|
1027  | -------- | -------- |
1028  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
1029
1030**Example**
1031```ts
1032import wifi from '@ohos.wifi';
1033
1034try {
1035	let name = "****";
1036	wifi.setDeviceName(name);
1037}catch(error){
1038	console.error("failed:" + JSON.stringify(error));
1039}
1040```
1041
1042## wifi.on('streamChange')<sup>7+</sup>
1043
1044on(type: "streamChange", callback: Callback&lt;number&gt;): void
1045
1046Subscribes to Wi-Fi stream changes.
1047
1048**System API**: This is a system API.
1049
1050**Required permissions**: ohos.permission.MANAGE_WIFI_CONNECTION
1051
1052**System capability**: SystemCapability.Communication.WiFi.STA
1053
1054**Parameters**
1055
1056  | **Name**| **Type**| **Mandatory**| **Description**|
1057  | -------- | -------- | -------- | -------- |
1058  | type | string | Yes| Event type, which has a fixed value of **streamChange**.|
1059| callback | Callback&lt;number&gt; | Yes| Callback used to return the Wi-Fi stream change, which can be any of the following values:<br>- **0**: No stream.<br>- **1**: Downward.<br>- **2**: Upward.<br>- **3**: Bidirectional. |
1060
1061## wifi.off('streamChange')<sup>7+</sup>
1062
1063off(type: "streamChange", callback?: Callback&lt;number&gt;): void
1064
1065Unsubscribes from Wi-Fi stream changes.
1066
1067**System API**: This is a system API.
1068
1069**Required permissions**: ohos.permission.MANAGE_WIFI_CONNECTION
1070
1071**System capability**: SystemCapability.Communication.WiFi.STA
1072
1073**Parameters**
1074
1075| **Name**| **Type**| **Mandatory**| **Description**|
1076| -------- | -------- | -------- | -------- |
1077| type | string | Yes| Event type, which has a fixed value of **streamChange**.|
1078| callback | Callback&lt;number&gt; | No| Callback to unregister. The Wi-Fi stream change can be any of the following values:<br>- **0**: No stream.<br>- **1**: Downward.<br>- **2**: Upward.<br>- **3**: Bidirectional.|
1079
1080**Example**
1081```ts
1082import wifi from '@ohos.wifi';
1083
1084let recvStreamChangeFunc = (result:number) => {
1085    console.info("Receive stream change event: " + result);
1086}
1087
1088// Register an event.
1089wifi.on("streamChange", recvStreamChangeFunc);
1090
1091// Unregister an event.
1092wifi.off("streamChange", recvStreamChangeFunc);
1093
1094```
1095
1096
1097## wifi.on('hotspotStaJoin')<sup>7+</sup>
1098
1099on(type: "hotspotStaJoin", callback: Callback&lt;StationInfo&gt;): void
1100
1101Subscribes to the event of an STA joining a Wi-Fi hotspot.
1102
1103**Required permissions**: ohos.permission.MANAGE_WIFI_HOTSPOT
1104
1105**System API**: This is a system API.
1106
1107**System capability**: SystemCapability.Communication.WiFi.AP.Core
1108
1109**Parameters**
1110
1111  | **Name**| **Type**| **Mandatory**| **Description**|
1112  | -------- | -------- | -------- | -------- |
1113  | type | string | Yes| Event type, which has a fixed value of **hotspotStaJoin**.|
1114| callback | Callback&lt;StationInfo&gt; | Yes| Callback invoked when an STA is connected to a Wi-Fi hotspot.|
1115
1116## wifi.off('hotspotStaJoin')<sup>7+</sup>
1117
1118off(type: "hotspotStaJoin", callback?: Callback&lt;StationInfo&gt;): void
1119
1120Unsubscribes from the event of an STA joining a Wi-Fi hotspot.
1121
1122**Required permissions**: ohos.permission.MANAGE_WIFI_HOTSPOT
1123
1124**System API**: This is a system API.
1125
1126**System capability**: SystemCapability.Communication.WiFi.AP.Core
1127
1128**Parameters**
1129
1130  | **Name**| **Type**| **Mandatory**| **Description**|
1131  | -------- | -------- | -------- | -------- |
1132  | type | string | Yes| Event type, which has a fixed value of **hotspotStaJoin**.|
1133| callback | Callback&lt;StationInfo&gt; | No| Callback to unregister.|
1134
1135  **Example**
1136```ts
1137import wifi from '@ohos.wifi';
1138
1139let recvHotspotStaJoinFunc = (result:wifi.StationInfo) => {
1140    console.info("Receive hotspot sta join event: " + result);
1141}
1142
1143// Register an event.
1144wifi.on("hotspotStaJoin", recvHotspotStaJoinFunc);
1145
1146// Unregister an event.
1147wifi.off("hotspotStaJoin", recvHotspotStaJoinFunc);
1148
1149```
1150
1151## wifi.on('hotspotStaLeave')<sup>7+</sup>
1152
1153on(type: "hotspotStaLeave", callback: Callback&lt;StationInfo&gt;): void
1154
1155Subscribes to the event of an STA leaving a Wi-Fi hotspot.
1156
1157**Required permissions**: ohos.permission.MANAGE_WIFI_HOTSPOT
1158
1159**System API**: This is a system API.
1160
1161**System capability**: SystemCapability.Communication.WiFi.AP.Core
1162
1163**Parameters**
1164
1165  | **Name**| **Type**| **Mandatory**| **Description**|
1166  | -------- | -------- | -------- | -------- |
1167  | type | string | Yes| Event type, which has a fixed value of **hotspotStaLeave**.|
1168| callback | Callback&lt;StationInf]&gt; | Yes| Callback invoked when an STA is disconnected from a Wi-Fi hotspot.|
1169
1170## wifi.off('hotspotStaLeave')<sup>7+</sup>
1171
1172off(type: "hotspotStaLeave", callback?: Callback&lt;StationInfo&gt;): void
1173
1174Unsubscribes from the event of an STA leaving a Wi-Fi hotspot.
1175
1176**Required permissions**: ohos.permission.MANAGE_WIFI_HOTSPOT
1177
1178**System API**: This is a system API.
1179
1180**System capability**: SystemCapability.Communication.WiFi.AP.Core
1181
1182**Parameters**
1183
1184  | **Name**| **Type**| **Mandatory**| **Description**|
1185  | -------- | -------- | -------- | -------- |
1186  | type | string | Yes| Event type, which has a fixed value of **hotspotStaLeave**.|
1187| callback | Callback&lt;StationInf]&gt; | No| Callback to unregister.|
1188
1189  **Example**
1190```ts
1191import wifi from '@ohos.wifi';
1192
1193let recvHotspotStaLeaveFunc = (result:wifi.StationInfo) => {
1194    console.info("Receive hotspot sta leave event: " + result);
1195}
1196
1197// Register an event.
1198wifi.on("hotspotStaLeave", recvHotspotStaLeaveFunc);
1199
1200// Unregister an event.
1201wifi.off("hotspotStaLeave", recvHotspotStaLeaveFunc);
1202
1203```
1204