1# @ohos.enterprise.wifiManager(Wi-Fi管理)
2
3本模块提供企业设备Wi-Fi管理能力,包括查询Wi-Fi开启状态等。
4
5> **说明:**
6>
7> 本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8>
9> 本模块接口仅可在Stage模型下使用。
10>
11> 本模块接口仅对[设备管理应用](../../mdm/mdm-kit-guide.md#功能介绍)开放,需将设备管理应用激活后调用,实现相应功能。
12>
13> 全局通用限制类策略由restrictions统一提供,若要全局禁用Wi-Fi,请参考[@ohos.enterprise.restrictions(限制类策略)](js-apis-enterprise-restrictions.md)。
14
15## 导入模块
16
17```ts
18import { wifiManager } from '@kit.MDMKit';
19```
20
21## wifiManager.isWifiActiveSync
22
23isWifiActiveSync(admin: Want): boolean
24
25指定设备管理应用查询Wi-Fi开启状态。
26
27**需要权限:** ohos.permission.ENTERPRISE_MANAGE_WIFI
28
29**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
30
31
32
33**参数:**
34
35| 参数名 | 类型                                                    | 必填 | 说明           |
36| ------ | ------------------------------------------------------- | ---- | -------------- |
37| admin  | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是   | 设备管理应用。 |
38
39**返回值:**
40
41| 类型    | 说明                                                       |
42| ------- | ---------------------------------------------------------- |
43| boolean | 返回Wi-Fi开启状态,true表示Wi-Fi开启,false表示Wi-Fi关闭。 |
44
45**错误码**:
46
47以下的错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。
48
49| 错误码ID | 错误信息                                                     |
50| -------- | ------------------------------------------------------------ |
51| 9200001  | The application is not an administrator application of the device. |
52| 9200002  | The administrator application does not have permission to manage the device. |
53| 201      | Permission verification failed. The application does not have the permission required to call the API. |
54| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
55
56**示例:**
57
58```ts
59import { Want } from '@kit.AbilityKit';
60let wantTemp: Want = {
61  bundleName: 'com.example.myapplication',
62  abilityName: 'EntryAbility',
63};
64
65try {
66  let result: boolean = wifiManager.isWifiActiveSync(wantTemp);
67  console.info(`Succeeded in query is wifi active or not, result : ${result}`);
68} catch (err) {
69  console.error(`Failed to query is wifi active or not. Code: ${err.code}, message: ${err.message}`);
70}
71```
72
73## wifiManager.setWifiProfileSync
74
75setWifiProfileSync(admin: Want, profile: WifiProfile): void
76
77指定设备管理应用为设备配置Wi-Fi,使连接到指定网络。
78
79**需要权限:** ohos.permission.ENTERPRISE_MANAGE_WIFI
80
81**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
82
83
84
85**参数:**
86
87| 参数名  | 类型                                                    | 必填 | 说明           |
88| ------- | ------------------------------------------------------- | ---- | -------------- |
89| admin   | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是   | 设备管理应用。 |
90| profile | [WifiProfile](#wifiprofile)                             | 是   | WLAN配置信息。 |
91
92**错误码**:
93
94以下的错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。
95
96| 错误码ID | 错误信息                                                     |
97| -------- | ------------------------------------------------------------ |
98| 9200001  | The application is not an administrator application of the device. |
99| 9200002  | The administrator application does not have permission to manage the device. |
100| 201      | Permission verification failed. The application does not have the permission required to call the API. |
101| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
102
103**示例:**
104
105```ts
106import { Want } from '@kit.AbilityKit';
107import { BusinessError } from '@kit.BasicServicesKit';
108let wantTemp: Want = {
109  bundleName: 'com.example.myapplication',
110  abilityName: 'EntryAbility',
111};
112let profile: wifiManager.WifiProfile = {
113  'ssid': 'name',
114  'preSharedKey': 'passwd',
115  'securityType': wifiManager.WifiSecurityType.WIFI_SEC_TYPE_PSK
116};
117
118try {
119  wifiManager.setWifiProfileSync(wantTemp, profile);
120  console.info('Succeeded in setting wifi profile.');
121} catch (err) {
122  console.error(`Failed to set wifi profile. Code: ${err.code}, message: ${err.message}`);
123}
124```
125
126## WifiProfile
127
128WLAN配置信息。
129
130**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
131
132
133
134| 名称          | 类型                                  | 必填 | 说明                          |
135| ------------- | ------------------------------------- | ---- | ----------------------------- |
136| ssid          | string                                | 是   | 热点的SSID,编码格式为UTF-8。 |
137| bssid         | string                                | 否   | 热点的BSSID。                 |
138| preSharedKey  | string                                | 是   | 热点的密钥。                  |
139| isHiddenSsid  | boolean                               | 否   | 是否是隐藏网络。              |
140| securityType  | [WifiSecurityType](#wifisecuritytype) | 是   | 加密类型。                    |
141| creatorUid    | number                                | 否   | 创建用户的ID。                |
142| disableReason | number                                | 否   | 禁用原因。                    |
143| netId         | number                                | 否   | 分配的网络ID。                |
144| randomMacType | number                                | 否   | 随机MAC类型。                 |
145| randomMacAddr | string                                | 否   | 随机MAC地址。                 |
146| ipType        | [IpType](#iptype)                     | 否   | IP地址类型。                  |
147| staticIp      | [IpProfile](#ipprofile)               | 否   | 静态IP配置信息。              |
148| eapProfile    | [WifiEapProfile](#wifieapprofile)     | 否   | 可扩展身份验证协议配置。      |
149
150## WifiSecurityType
151
152表示加密类型的枚举。
153
154**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
155
156
157
158| 名称                      | 值   | 说明                                                         |
159| ------------------------- | ---- | ------------------------------------------------------------ |
160| WIFI_SEC_TYPE_INVALID     | 0    | 无效加密类型。                                               |
161| WIFI_SEC_TYPE_OPEN        | 1    | 开放加密类型。                                               |
162| WIFI_SEC_TYPE_WEP         | 2    | Wired Equivalent Privacy (WEP)加密类型。      |
163| WIFI_SEC_TYPE_PSK         | 3    | Pre-shared key (PSK)加密类型。                     |
164| WIFI_SEC_TYPE_SAE         | 4    | Simultaneous Authentication of Equals (SAE)加密类型。 |
165| WIFI_SEC_TYPE_EAP         | 5    | EAP加密类型。                                                |
166| WIFI_SEC_TYPE_EAP_SUITE_B | 6    | Suite-B 192位加密类型。                                      |
167| WIFI_SEC_TYPE_OWE         | 7    | 机会性无线加密类型。                                         |
168| WIFI_SEC_TYPE_WAPI_CERT   | 8    | WAPI-Cert加密类型。                                          |
169| WIFI_SEC_TYPE_WAPI_PSK    | 9    | WAPI-PSK加密类型。                                           |
170
171## IpType
172
173表示IP类型的枚举。
174
175**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
176
177
178
179| 名称    | 值   | 说明           |
180| ------- | ---- | -------------- |
181| STATIC  | 0    | 静态IP。       |
182| DHCP    | 1    | 通过DHCP获取。 |
183| UNKNOWN | 2    | 未指定。       |
184
185## IpProfile
186
187IP配置信息。
188
189**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
190
191
192
193| 名称         | 类型                | 必填 | 说明        |
194| ------------ | ------------------- | ---- | ----------- |
195| ipAddress    | number              | 是   | IP地址。    |
196| gateway      | number              | 是   | 网关。      |
197| prefixLength | number              | 是   | 掩码。      |
198| dnsServers   | number[]            | 是   | DNS服务器。 |
199| domains      | Array<string> | 是   | 域信息。    |
200
201## WifiEapProfile
202
203可扩展身份验证协议配置信息。
204
205**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
206
207
208
209| 名称              | 类型                          | 必填 | 说明                             |
210| ----------------- | ----------------------------- | ---- | -------------------------------- |
211| eapMethod         | [EapMethod](#eapmethod)       | 是   | AP认证方式。                     |
212| phase2Method      | [Phase2Method](#phase2method) | 是   | 第二阶段认证方式。               |
213| identity          | string                        | 是   | 身份信息。                       |
214| anonymousIdentity | string                        | 是   | 匿名身份。                       |
215| password          | string                        | 是   | 密码。                           |
216| caCertAliases     | string                        | 是   | CA 证书别名。                    |
217| caPath            | string                        | 是   | CA 证书路径。                    |
218| clientCertAliases | string                        | 是   | 客户端证书别名。                 |
219| certEntry         | Uint8Array                    | 是   | CA 证书内容。                    |
220| certPassword      | string                        | 是   | CA证书密码。                     |
221| altSubjectMatch   | string                        | 是   | 替代主题匹配。                   |
222| domainSuffixMatch | string                        | 是   | 域后缀匹配。                     |
223| realm             | string                        | 是   | 通行证凭证的领域。               |
224| plmn              | string                        | 是   | 公共陆地移动网的直通凭证提供商。 |
225| eapSubId          | number                        | 是   | SIM卡的子ID。                    |
226
227## EapMethod
228
229表示EAP认证方式的枚举。
230
231**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
232
233
234
235| 名称           | 值   | 说明             |
236| -------------- | ---- | ---------------- |
237| EAP_NONE       | 0    | 不指定。         |
238| EAP_PEAP       | 1    | PEAP类型。       |
239| EAP_TLS        | 2    | TLS类型。        |
240| EAP_TTLS       | 3    | TTLS类型。       |
241| EAP_PWD        | 4    | PWD类型。        |
242| EAP_SIM        | 5    | SIM类型。        |
243| EAP_AKA        | 6    | AKA类型。        |
244| EAP_AKA_PRIME  | 7    | AKA Prime类型。  |
245| EAP_UNAUTH_TLS | 8    | UNAUTH TLS类型。 |
246
247## Phase2Method
248
249表示第二阶段认证方式的枚举。
250
251**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
252
253
254
255| 名称             | 值   | 说明            |
256| ---------------- | ---- | --------------- |
257| PHASE2_NONE      | 0    | 不指定。        |
258| PHASE2_PAP       | 1    | PAP类型。       |
259| PHASE2_MSCHAP    | 2    | MSCHAP类型。    |
260| PHASE2_MSCHAPV2  | 3    | MSCHAPV2类型。  |
261| PHASE2_GTC       | 4    | GTC类型。       |
262| PHASE2_SIM       | 5    | SIM类型。       |
263| PHASE2_AKA       | 6    | AKA类型。       |
264| PHASE2_AKA_PRIME | 7    | AKA Prime类型。 |
265