1# @ohos.wifi (WLAN)(系统接口)
2
3该模块主要提供WLAN基础功能、P2P(peer-to-peer)功能和WLAN消息通知的相应服务,让应用可以通过WLAN和其他设备互联互通。
4
5> **说明:**
6>
7> 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8> 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.wifi (WLAN)](js-apis-wifi.md)
9> 从API Version 9 开始,该接口不再维护,推荐使用[`@ohos.wifiManager (WLAN)`](js-apis-wifiManager-sys.md)等相关接口。
10
11
12## 导入模块
13
14```ts
15import wifi from '@ohos.wifi';
16```
17
18## wifi.enableWifi
19
20enableWifi(): boolean
21
22使能WLAN。
23
24**系统接口:** 此接口为系统接口。
25
26**需要权限:** ohos.permission.SET_WIFI_INFOohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。
27
28**系统能力:** SystemCapability.Communication.WiFi.STA
29
30**返回值:**
31
32  | **类型** | **说明** |
33  | -------- | -------- |
34  | boolean | true:操作成功, false:操作失败。|
35
36**示例:**
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
52去使能WLAN。
53
54**系统接口:** 此接口为系统接口。
55
56**需要权限:** ohos.permission.SET_WIFI_INFOohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。
57
58**系统能力:** SystemCapability.Communication.WiFi.STA
59
60**返回值:**
61
62  | **类型** | **说明** |
63  | -------- | -------- |
64  | boolean | true:操作成功, false:操作失败。|
65
66**示例:**
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
84添加网络配置,使用Promise异步回调。
85
86**系统接口:** 此接口为系统接口。
87
88**需要权限:** ohos.permission.SET_WIFI_INFOohos.permission.SET_WIFI_CONFIG
89
90**系统能力:** SystemCapability.Communication.WiFi.STA
91
92**参数:**
93
94  | **参数名** | **类型** | **必填** | **说明** |
95  | -------- | -------- | -------- | -------- |
96  | config | [WifiDeviceConfig](#wifideviceconfig) | 是 | WLAN配置信息。 |
97
98**返回值:**
99
100  | **类型** | **说明** |
101  | -------- | -------- |
102  | Promise<number> | Promise对象。返回添加的网络配置ID,如果值为-1表示添加失败。 |
103
104  **示例:**
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
139WLAN配置信息。
140
141**系统能力:** SystemCapability.Communication.WiFi.STA
142
143
144| **名称** | **类型** | **可读** | **可写** | **说明** |
145| -------- | -------- | -------- | -------- | -------- |
146| creatorUid | number | 是 | 否 | 创建用户的ID。 <br /> **系统接口:** 此接口为系统接口。 |
147| disableReason | number | 是 | 否 | 禁用原因。 <br /> **系统接口:** 此接口为系统接口。 |
148| netId | number | 是 | 否 | 分配的网络ID。 <br /> **系统接口:** 此接口为系统接口。 |
149| randomMacType | number | 是 | 否 | 随机MAC类型。 <br /> **系统接口:** 此接口为系统接口。 |
150| randomMacAddr | string | 是 | 否 | 随机MAC地址。 <br /> **系统接口:** 此接口为系统接口。 |
151| ipType | [IpType](#iptype7) | 是 | 否 | IP地址类型。 <br /> **系统接口:** 此接口为系统接口。 |
152| staticIp | [IpConfig](#ipconfig7) | 是 | 否 | 静态IP配置信息。 <br /> **系统接口:** 此接口为系统接口。 |
153
154
155## IpType<sup>7+</sup>
156
157表示IP类型的枚举。
158
159**系统接口:** 此接口为系统接口。
160
161**系统能力:** SystemCapability.Communication.WiFi.STA
162
163
164| 名称 | 值 | 说明 |
165| -------- | -------- | -------- |
166| STATIC | 0 | 静态IP。 |
167| DHCP | 1 | 通过DHCP获取。 |
168| UNKNOWN | 2 | 未指定。 |
169
170
171## IpConfig<sup>7+</sup>
172
173IP配置信息。
174
175**系统接口:** 此接口为系统接口。
176
177**系统能力:** SystemCapability.Communication.WiFi.STA
178
179| **名称** | **类型** | **可读** | **可写** | **说明** |
180| -------- | -------- | -------- | -------- | -------- |
181| ipAddress | number | 是 | 否 | IP地址。 |
182| gateway | number | 是 | 否 | 网关。 |
183| dnsServers | number[] | 是 | 否 | DNS服务器。 |
184| domains | Array&lt;string&gt; | 是 | 否 | 域信息。 |
185
186
187## wifi.addDeviceConfig
188
189addDeviceConfig(config: WifiDeviceConfig, callback: AsyncCallback&lt;number&gt;): void
190
191添加网络配置,使用callback异步回调。
192
193**系统接口:** 此接口为系统接口。
194
195**需要权限:** ohos.permission.SET_WIFI_INFOohos.permission.SET_WIFI_CONFIG
196
197**系统能力:** SystemCapability.Communication.WiFi.STA
198
199**参数:**
200
201  | **参数名** | **类型** | **必填** | **说明** |
202  | -------- | -------- | -------- | -------- |
203  | config | [WifiDeviceConfig](#wifideviceconfig) | 是 | WLAN配置信息。 |
204  | callback | AsyncCallback&lt;number&gt; | 是 | 回调函数。当操作成功时,err为0,data为添加的网络配置ID,如果data值为-1,表示添加失败。当error为非0,表示处理出现错误。 |
205
206**示例:**
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
244连接到指定网络。
245
246**系统接口:** 此接口为系统接口。
247
248**需要权限:** ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。
249
250**系统能力:** SystemCapability.Communication.WiFi.STA
251
252**参数:**
253
254  | **参数名** | **类型** | **必填** | **说明** |
255  | -------- | -------- | -------- | -------- |
256  | networkId | number | 是 | 待连接的网络配置ID。 |
257
258**返回值:**
259
260  | **类型** | **说明** |
261  | -------- | -------- |
262  | boolean | true:操作成功,&nbsp;false:操作失败。 |
263
264**示例:**
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
281连接到指定网络。
282
283**系统接口:** 此接口为系统接口。
284
285**需要权限:** ohos.permission.SET_WIFI_INFOohos.permission.SET_WIFI_CONFIGohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。
286
287**系统能力:**
288  SystemCapability.Communication.WiFi.STA
289
290**参数:**
291
292  | **参数名** | **类型** | **必填** | **说明** |
293  | -------- | -------- | -------- | -------- |
294  | config | [WifiDeviceConfig](#wifideviceconfig) | 是 | WLAN配置信息。 |
295
296**返回值:**
297
298  | **类型** | **说明** |
299  | -------- | -------- |
300  | boolean | true:操作成功,&nbsp;false:操作失败。 |
301
302**示例:**
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
337断开连接的网络。
338
339**系统接口:** 此接口为系统接口。
340
341**需要权限:** ohos.permission.SET_WIFI_INFOohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。
342
343**系统能力:**
344  SystemCapability.Communication.WiFi.STA
345
346**返回值:**
347
348  | **类型** | **说明** |
349  | -------- | -------- |
350  | boolean | true:操作成功,&nbsp;false:操作失败。 |
351
352**示例:**
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
366提供WLAN连接的相关信息。
367
368**系统能力:** SystemCapability.Communication.WiFi.STA
369
370| 名称 | 类型 | 可读 | 可写 | 说明 |
371| -------- | -------- | -------- | -------- | -------- |
372| networkId | number | 是 | 否 | 网络配置ID。 <br /> **系统接口:** 此接口为系统接口。 |
373| chload | number | 是 | 否 | 连接负载,值越大表示负载约高。 <br /> **系统接口:** 此接口为系统接口。 |
374| snr | number | 是 | 否 | 信噪比。 <br /> **系统接口:** 此接口为系统接口。 |
375| suppState | [SuppState](#suppstate) | 是 | 否 | 请求状态。 <br /> **系统接口:** 此接口为系统接口。 |
376
377
378## SuppState
379
380表示请求状态的枚举。
381
382**系统接口:** 此接口为系统接口。
383
384**系统能力:** SystemCapability.Communication.WiFi.STA
385
386| 名称 | 值 | 说明 |
387| -------- | -------- | -------- |
388| DISCONNECTED | 0 | 已断开。 |
389| INTERFACE_DISABLED | 1 | 接口禁用。 |
390| INACTIVE | 2 | 未激活。 |
391| SCANNING | 3 | 扫描中。 |
392| AUTHENTICATING | 4 | 认证中。 |
393| ASSOCIATING | 5 | 关联中。 |
394| ASSOCIATED | 6 | 已关联。 |
395| FOUR_WAY_HANDSHAKE | 7 | 四次握手。 |
396| GROUP_HANDSHAKE | 8 | 组握手。 |
397| COMPLETED | 9 | 所有认证已完成。 |
398| UNINITIALIZED | 10 | 连接建立失败。 |
399| INVALID | 11 | 无效值。 |
400
401
402
403## wifi.getSupportedFeatures<sup>7+</sup>
404
405getSupportedFeatures(): number
406
407查询设备支持的特性。
408
409**系统接口:** 此接口为系统接口。
410
411**需要权限:** ohos.permission.GET_WIFI_INFO
412
413**系统能力:** SystemCapability.Communication.WiFi.Core
414
415**返回值:**
416
417  | **类型** | **说明** |
418  | -------- | -------- |
419  | number | 支持的特性值。 |
420
421**特性ID值枚举:**
422
423| 枚举值 | 说明 |
424| -------- | -------- |
425| 0x0001 | 基础结构模式特性。 |
426| 0x0002 | 5&nbsp;GHz带宽特性。 |
427| 0x0004 | GAS/ANQP特性。 |
428| 0x0008 | Wifi-Direct特性。 |
429| 0x0010 | Soft&nbsp;AP特性。 |
430| 0x0040 | Wi-Fi&nbsp;AWare组网特性。 |
431| 0x8000 | AP&nbsp;STA共存特性。 |
432| 0x8000000 | WPA3-Personal&nbsp;SAE特性。 |
433| 0x10000000 | WPA3-Enterprise&nbsp;Suite-B |
434| 0x20000000 | 增强开放特性。 |
435
436
437
438
439## wifi.getDeviceMacAddress<sup>7+</sup>
440
441getDeviceMacAddress(): string[]
442
443获取设备的MAC地址。
444
445**系统接口:** 此接口为系统接口。
446
447**需要权限:** ohos.permission.GET_WIFI_LOCAL_MACohos.permission.GET_WIFI_INFO,仅系统应用可用。
448
449**系统能力:** SystemCapability.Communication.WiFi.STA
450
451**返回值:**
452
453  | **类型** | **说明** |
454  | -------- | -------- |
455  | string[] | MAC地址。 |
456
457**示例:**
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
475重新关联网络。
476
477**系统接口:** 此接口为系统接口。
478
479**需要权限:** ohos.permission.SET_WIFI_INFOohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。
480
481**系统能力:** SystemCapability.Communication.WiFi.STA
482
483**返回值:**
484
485  | **类型** | **说明** |
486  | -------- | -------- |
487  | boolean | true:操作成功,&nbsp;false:操作失败。 |
488
489**示例:**
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
504重新连接网络。
505
506**系统接口:** 此接口为系统接口。
507
508**需要权限:** ohos.permission.SET_WIFI_INFOohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。
509
510**系统能力:** SystemCapability.Communication.WiFi.STA
511
512**返回值:**
513
514  | **类型** | **说明** |
515  | -------- | -------- |
516  | boolean | true:操作成功,&nbsp;false:操作失败。 |
517
518**示例:**
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
533获取网络配置。
534
535**系统接口:** 此接口为系统接口。
536
537**需要权限:** ohos.permission.GET_WIFI_INFOohos.permission.LOCATIONohos.permission.GET_WIFI_CONFIG
538
539**系统能力:** SystemCapability.Communication.WiFi.STA
540
541**返回值:**
542
543  | **类型** | **说明** |
544  | -------- | -------- |
545  | &nbsp;Array&lt;[WifiDeviceConfig](#wifideviceconfig)&gt; | 网络配置信息的数组。 |
546
547**示例:**
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
563更新网络配置。
564
565**系统接口:** 此接口为系统接口。
566
567**需要权限:** ohos.permission.SET_WIFI_INFOohos.permission.SET_WIFI_CONFIG
568
569**系统能力:** SystemCapability.Communication.WiFi.STA
570
571**参数:**
572
573  | **参数名** | **类型** | **必填** | **说明** |
574  | -------- | -------- | -------- | -------- |
575  | config | [WifiDeviceConfig](#wifideviceconfig) | 是 | WLAN配置信息。 |
576
577**返回值:**
578
579  | **类型** | **说明** |
580  | -------- | -------- |
581  | number | 返回更新的网络配置ID,如果值为-1表示更新失败。 |
582
583**示例:**
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
618去使能网络配置。
619
620**系统接口:** 此接口为系统接口。
621
622**需要权限:** ohos.permission.SET_WIFI_INFOohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。
623
624**系统能力:** SystemCapability.Communication.WiFi.STA
625
626**参数:**
627
628  | **参数名** | **类型** | **必填** | **说明** |
629  | -------- | -------- | -------- | -------- |
630  | netId | number | 是 | 网络配置ID。 |
631
632**返回值:**
633
634  | **类型** | **说明** |
635  | -------- | -------- |
636  | boolean | true:操作成功,&nbsp;false:操作失败。 |
637
638**示例:**
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
654移除所有网络配置。
655
656**系统接口:** 此接口为系统接口。
657
658**需要权限:** ohos.permission.SET_WIFI_INFOohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。
659
660**系统能力:** SystemCapability.Communication.WiFi.STA
661
662**返回值:**
663
664  | **类型** | **说明** |
665  | -------- | -------- |
666  | boolean | true:操作成功,&nbsp;false:操作失败。 |
667
668**示例:**
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
683移除指定的网络配置。
684
685**系统接口:** 此接口为系统接口。
686
687**需要权限:** ohos.permission.SET_WIFI_INFOohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。
688
689**系统能力:** SystemCapability.Communication.WiFi.STA
690
691**参数:**
692
693  | **参数名** | **类型** | **必填** | **说明** |
694  | -------- | -------- | -------- | -------- |
695  | id | number | 是 | 网络配置ID。 |
696
697**返回值:**
698
699  | **类型** | **说明** |
700  | -------- | -------- |
701  | boolean | true:操作成功,&nbsp;false:操作失败。 |
702
703**示例:**
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
719使能热点。
720
721**系统接口:** 此接口为系统接口。
722
723**需要权限:** ohos.permission.MANAGE_WIFI_HOTSPOT,仅系统应用可用。
724
725**系统能力:** SystemCapability.Communication.WiFi.AP.Core
726
727**返回值:**
728
729  | **类型** | **说明** |
730  | -------- | -------- |
731  | boolean | true:操作成功,&nbsp;false:操作失败。|
732
733**示例:**
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
748去使能热点。
749
750**系统接口:** 此接口为系统接口。
751
752**需要权限:** ohos.permission.MANAGE_WIFI_HOTSPOT,仅系统应用可用。
753
754**系统能力:** SystemCapability.Communication.WiFi.AP.Core
755
756**返回值:**
757
758  | **类型** | **说明** |
759  | -------- | -------- |
760  | boolean | true:操作成功,&nbsp;false:操作失败。|
761
762**示例:**
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
777热点是否支持双频。
778
779**系统接口:** 此接口为系统接口。
780
781**需要权限:** ohos.permission.GET_WIFI_INFOohos.permission.MANAGE_WIFI_HOTSPOT,仅系统应用可用。
782
783**系统能力:** SystemCapability.Communication.WiFi.AP.Core
784
785**返回值:**
786
787  | **类型** | **说明** |
788  | -------- | -------- |
789  | boolean | true:支持,&nbsp;false:不支持。|
790
791**示例:**
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
807热点是否已使能。
808
809**系统接口:** 此接口为系统接口。
810
811**需要权限:** ohos.permission.GET_WIFI_INFO
812
813**系统能力:** SystemCapability.Communication.WiFi.AP.Core
814
815**返回值:**
816
817  | **类型** | **说明** |
818  | -------- | -------- |
819  | boolean | true:已使能,&nbsp;false:未使能。|
820
821**示例:**
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
837设置热点配置信息。
838
839**系统接口:** 此接口为系统接口。
840
841**需要权限:** ohos.permission.SET_WIFI_INFOohos.permission.GET_WIFI_CONFIG
842
843**系统能力:** SystemCapability.Communication.WiFi.AP.Core
844
845**参数:**
846
847  | **参数名** | **类型** | **必填** | **说明** |
848  | -------- | -------- | -------- | -------- |
849  | config | [HotspotConfig](#hotspotconfig7) | 是 | 热点配置信息。 |
850
851**返回值:**
852
853  | **类型** | **说明** |
854  | -------- | -------- |
855  | boolean | true:操作成功,&nbsp;false:操作失败。 |
856
857**示例:**
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
878热点配置信息。
879
880**系统接口:** 此接口为系统接口。
881
882**系统能力:** SystemCapability.Communication.WiFi.AP.Core
883
884| **名称** | **类型** | **可读** | **可写** | **说明** |
885| -------- | -------- | -------- | -------- | -------- |
886| ssid | string | 是 | 否 | 热点的SSID,编码格式为UTF-8。 |
887| securityType | [WifiSecurityType](js-apis-wifi.md#wifisecuritytype) | 是 | 否 | 加密类型。 |
888| band | number | 是 | 否 | 热点的带宽。1: 2.4G, 2: 5G, 3: 双模频段 |
889| preSharedKey | string | 是 | 否 | 热点的密钥。 |
890| maxConn | number | 是 | 否 | 最大设备连接数。 |
891
892
893## wifi.getHotspotConfig<sup>7+</sup>
894
895getHotspotConfig(): HotspotConfig
896
897获取热点配置信息。
898
899**系统接口:** 此接口为系统接口。
900
901**需要权限:** ohos.permission.GET_WIFI_INFOohos.permission.GET_WIFI_CONFIG
902
903**系统能力:** SystemCapability.Communication.WiFi.AP.Core
904
905**返回值:**
906
907  | **类型** | **说明** |
908  | -------- | -------- |
909  | [HotspotConfig](#hotspotconfig7) | 热点的配置信息。 |
910
911**示例:**
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
927获取连接的设备。
928
929**系统接口:** 此接口为系统接口。
930
931**需要权限:** ohos.permission.GET_WIFI_INFOohos.permission.LOCATIONohos.permission.MANAGE_WIFI_HOTSPOT,仅系统应用可用。
932
933**系统能力:** SystemCapability.Communication.WiFi.AP.Core
934
935**返回值:**
936
937  | **类型** | **说明** |
938  | -------- | -------- |
939  | &nbsp;Array&lt;[StationInfo](#stationinfo7)&gt; | 连接的设备数组。 |
940
941**示例:**
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
955接入的设备信息。
956
957**系统接口:** 此接口为系统接口。
958
959**系统能力:** SystemCapability.Communication.WiFi.AP.Core
960
961| **名称** | **类型** | **可读** | **可写** | **说明** |
962| -------- | -------- | -------- | -------- | -------- |
963| name | string | 是 | 否 | 设备名称。 |
964| macAddress | string | 是 | 否 | MAC地址。 |
965| ipAddress | string | 是 | 否 | IP地址。 |
966
967
968## wifi.deletePersistentGroup<sup>8+</sup>
969
970deletePersistentGroup(netId: number): boolean
971
972删除永久组。
973
974**系统接口:** 此接口为系统接口。
975
976**需要权限:** ohos.permission.SET_WIFI_INFOohos.permission.MANAGE_WIFI_CONNECTION
977
978**系统能力:** SystemCapability.Communication.WiFi.P2P
979
980**参数:**
981
982
983  | **参数名** | **类型** | 必填 | **说明** |
984  | -------- | -------- | -------- | -------- |
985  | netId | number | 是 | 组的ID。 |
986
987**返回值:**
988
989  | 类型 | 说明 |
990  | -------- | -------- |
991  | boolean | true:操作执行成功,操作执行失败。 |
992
993**示例:**
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
1010设置设备名称。
1011
1012**系统接口:** 此接口为系统接口。
1013
1014**需要权限:** ohos.permission.SET_WIFI_INFOohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。
1015
1016**系统能力:** SystemCapability.Communication.WiFi.P2P
1017
1018**参数:**
1019
1020  | **参数名** | **类型** | **必填** | **说明** |
1021  | -------- | -------- | -------- | -------- |
1022  | devName | string | 是 | 设备名称。 |
1023
1024**返回值:**
1025
1026  | **类型** | **说明** |
1027  | -------- | -------- |
1028  | boolean | true:操作成功,&nbsp;false:操作失败。 |
1029
1030**示例:**
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
1046注册WIFI流更改事件。
1047
1048**系统接口:** 此接口为系统接口。
1049
1050**需要权限:** ohos.permission.MANAGE_WIFI_CONNECTION
1051
1052**系统能力:** SystemCapability.Communication.WiFi.STA
1053
1054**参数:**
1055
1056  | **参数名** | **类型** | **必填** | **说明** |
1057  | -------- | -------- | -------- | -------- |
1058  | type | string | 是 | 固定填"streamChange"字符串。 |
1059  | callback | Callback&lt;number&gt; | 是 | 状态改变回调函数,返回0:无,1:向下,2:向上,3:双向。 |
1060
1061## wifi.off('streamChange')<sup>7+</sup>
1062
1063off(type: "streamChange", callback?: Callback&lt;number&gt;): void
1064
1065取消注册WIFI流更改事件。
1066
1067**系统接口:** 此接口为系统接口。
1068
1069**需要权限:** ohos.permission.MANAGE_WIFI_CONNECTION
1070
1071**系统能力:** SystemCapability.Communication.WiFi.STA
1072
1073**参数:**
1074
1075| **参数名** | **类型** | **必填** | **说明** |
1076| -------- | -------- | -------- | -------- |
1077| type | string | 是 | 固定填"streamChange"字符串。 |
1078| callback | Callback&lt;number&gt; | 否| 状态改变回调函数,返回0:无,1:向下,2:向上,3:双向。 |
1079
1080**示例:**
1081```ts
1082import wifi from '@ohos.wifi';
1083
1084let recvStreamChangeFunc = (result:number) => {
1085    console.info("Receive stream change event: " + result);
1086}
1087
1088// Register event
1089wifi.on("streamChange", recvStreamChangeFunc);
1090
1091// Unregister 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
1101注册wifi热点sta加入事件。
1102
1103**需要权限:** ohos.permission.MANAGE_WIFI_HOTSPOT
1104
1105**系统接口:** 此接口为系统接口。
1106
1107**系统能力:** SystemCapability.Communication.WiFi.AP.Core
1108
1109**参数:**
1110
1111  | **参数名** | **类型** | **必填** | **说明** |
1112  | -------- | -------- | -------- | -------- |
1113  | type | string | 是 | 固定填"hotspotStaJoin"字符串。 |
1114  | callback | Callback&lt;StationInfo&gt; | 是 | 状态改变回调函数。 |
1115
1116## wifi.off('hotspotStaJoin')<sup>7+</sup>
1117
1118off(type: "hotspotStaJoin", callback?: Callback&lt;StationInfo&gt;): void
1119
1120取消注册wifi热点sta加入事件。
1121
1122**需要权限:** ohos.permission.MANAGE_WIFI_HOTSPOT
1123
1124**系统接口:** 此接口为系统接口。
1125
1126**系统能力:** SystemCapability.Communication.WiFi.AP.Core
1127
1128**参数:**
1129
1130  | **参数名** | **类型** | **必填** | **说明** |
1131  | -------- | -------- | -------- | -------- |
1132  | type | string | 是 | 固定填"hotspotStaJoin"字符串。 |
1133  | callback | Callback&lt;StationInfo&gt; | 否 | 状态改变回调函数。 |
1134
1135  **示例:**
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 event
1144wifi.on("hotspotStaJoin", recvHotspotStaJoinFunc);
1145
1146// Unregister 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
1155注册wifi热点sta离开事件。
1156
1157**需要权限:** ohos.permission.MANAGE_WIFI_HOTSPOT
1158
1159**系统接口:** 此接口为系统接口。
1160
1161**系统能力:** SystemCapability.Communication.WiFi.AP.Core
1162
1163**参数:**
1164
1165  | **参数名** | **类型** | **必填** | **说明** |
1166  | -------- | -------- | -------- | -------- |
1167  | type | string | 是 | 固定填"hotspotStaLeave"字符串。 |
1168  | callback | Callback&lt;StationInf]&gt; | 是 | 状态改变回调函数。 |
1169
1170## wifi.off('hotspotStaLeave')<sup>7+</sup>
1171
1172off(type: "hotspotStaLeave", callback?: Callback&lt;StationInfo&gt;): void
1173
1174取消注册wifi热点sta离开事件。
1175
1176**需要权限:** ohos.permission.MANAGE_WIFI_HOTSPOT
1177
1178**系统接口:** 此接口为系统接口。
1179
1180**系统能力:** SystemCapability.Communication.WiFi.AP.Core
1181
1182**参数:**
1183
1184  | **参数名** | **类型** | **必填** | **说明** |
1185  | -------- | -------- | -------- | -------- |
1186  | type | string | 是 | 固定填"hotspotStaLeave"字符串。 |
1187  | callback | Callback&lt;StationInf]&gt; | 否 | 状态改变回调函数。 |
1188
1189  **示例:**
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 event
1198wifi.on("hotspotStaLeave", recvHotspotStaLeaveFunc);
1199
1200// Unregister event
1201wifi.off("hotspotStaLeave", recvHotspotStaLeaveFunc);
1202
1203```
1204
1205