1# @ohos.wifiManager (WLAN) 2该模块主要提供WLAN基础功能(无线接入、无线加密、无线漫游等)、P2P(peer-to-peer)服务的基础功能和WLAN消息通知的相应服务,让应用可以通过WLAN和其他设备互联互通。 3 4> **说明:** 5> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 6 7 8## 导入模块 9 10```ts 11import { wifiManager } from '@kit.ConnectivityKit'; 12``` 13 14 15## wifiManager.isWifiActive<sup>9+</sup> 16 17isWifiActive(): boolean 18 19查询WLAN开关是否已使能。 20 21**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 22 23**系统能力:** SystemCapability.Communication.WiFi.STA 24 25**返回值:** 26 27 | **类型** | **说明** | 28 | -------- | -------- | 29 | boolean | true:已使能, false:未使能。 | 30 31**错误码:** 32 33以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 34 35| **错误码ID** | **错误信息** | 36| -------- | -------- | 37| 801 | Capability not supported. | 38| 2501000 | Operation failed.| 39 40**示例:** 41 42```ts 43 import { wifiManager } from '@kit.ConnectivityKit'; 44 45 try { 46 let isWifiActive = wifiManager.isWifiActive(); 47 console.info("isWifiActive:" + isWifiActive); 48 }catch(error){ 49 console.error("failed:" + JSON.stringify(error)); 50 } 51``` 52 53## wifiManager.scan<sup>9+</sup><sup>(deprecated)</sup> 54 55scan(): void 56 57启动WLAN扫描,使用前先使能WLAN。 58 59> **说明:** 60> 从 API version 9开始支持,从API version 10开始废弃。替代接口仅向系统应用开放。 61 62**需要权限:** ohos.permission.SET_WIFI_INFO、ohos.permission.LOCATION 和 ohos.permission.APPROXIMATELY_LOCATION 63 64**系统能力:** SystemCapability.Communication.WiFi.STA 65 66**错误码:** 67 68以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 69 70| **错误码ID** | **错误信息** | 71| -------- | -------- | 72| 201 | Permission denied. | 73| 801 | Capability not supported. | 74| 2501000 | Operation failed.| 75 76**示例:** 77 78```ts 79 import { wifiManager } from '@kit.ConnectivityKit'; 80 81 try { 82 wifiManager.scan(); 83 }catch(error){ 84 console.error("failed:" + JSON.stringify(error)); 85 } 86``` 87 88 89## wifiManager.getScanResults<sup>9+</sup><sup>(deprecated)</sup> 90 91getScanResults(): Promise<Array<WifiScanInfo>> 92 93获取扫描结果,使用Promise异步回调。 94 95> **说明:** 96> 从 API version 9开始支持,从API version 10开始废弃。建议使用[wifiManager.getScanInfoList](#wifimanagergetscaninfolist10)代替。 97 98**需要权限:** ohos.permission.GET_WIFI_INFO 和 (ohos.permission.GET_WIFI_PEERS_MAC 或(ohos.permission.LOCATION 和 ohos.permission.APPROXIMATELY_LOCATION)) 99ohos.permission.GET_WIFI_PEERS_MAC权限仅系统应用可申请。 100 101**系统能力:** SystemCapability.Communication.WiFi.STA 102 103**返回值:** 104 105| **类型** | **说明** | 106| -------- | -------- | 107| Promise< Array<[WifiScanInfo](#wifiscaninfo9)> > | Promise对象。返回扫描到的热点列表。 | 108 109**错误码:** 110 111以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 112 113| **错误码ID** | **错误信息** | 114| -------- | -------- | 115| 201 | Permission denied. | 116| 801 | Capability not supported. | 117| 2501000 | Operation failed.| 118 119## wifiManager.getScanResults<sup>9+</sup><sup>(deprecated)</sup> 120 121getScanResults(callback: AsyncCallback<Array<WifiScanInfo>>): void 122 123获取扫描结果,使用callback异步回调。 124 125> **说明:** 126> 从 API version 9开始支持,从API version 10开始废弃。建议使用[wifiManager.getScanInfoList](#wifimanagergetscaninfolist10)代替。 127 128**需要权限:** ohos.permission.GET_WIFI_INFO 和 (ohos.permission.GET_WIFI_PEERS_MAC 或 (ohos.permission.LOCATION 和 ohos.permission.APPROXIMATELY_LOCATION)) 129ohos.permission.GET_WIFI_PEERS_MAC权限仅系统应用可申请。 130 131**系统能力:** SystemCapability.Communication.WiFi.STA 132 133**参数:** 134| **参数名** | **类型** | **必填** | **说明** | 135| -------- | -------- | -------- | -------- | 136| callback | AsyncCallback< Array<[WifiScanInfo](#wifiscaninfo9)>> | 是 | 回调函数。当成功时,err为0,data为扫描到的热点;否则err为非0值,data为空。 | 137 138**错误码:** 139 140以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 141 142| **错误码ID** | **错误信息** | 143| -------- | -------- | 144| 201 | Permission denied. | 145| 801 | Capability not supported. | 146| 2501000 | Operation failed.| 147 148**示例:** 149```ts 150 import { wifiManager } from '@kit.ConnectivityKit'; 151 152 wifiManager.getScanResults((err, result) => { 153 if (err) { 154 console.error("get scan info error"); 155 return; 156 } 157 158 let len = result.length; 159 console.log("wifi received scan info: " + len); 160 for (let i = 0; i < len; ++i) { 161 console.info("ssid: " + result[i].ssid); 162 console.info("bssid: " + result[i].bssid); 163 console.info("capabilities: " + result[i].capabilities); 164 console.info("securityType: " + result[i].securityType); 165 console.info("rssi: " + result[i].rssi); 166 console.info("band: " + result[i].band); 167 console.info("frequency: " + result[i].frequency); 168 console.info("channelWidth: " + result[i].channelWidth); 169 console.info("timestamp: " + result[i].timestamp); 170 } 171 }); 172 173 wifiManager.getScanResults().then(result => { 174 let len = result.length; 175 console.log("wifi received scan info: " + len); 176 for (let i = 0; i < len; ++i) { 177 console.info("ssid: " + result[i].ssid); 178 console.info("bssid: " + result[i].bssid); 179 console.info("capabilities: " + result[i].capabilities); 180 console.info("securityType: " + result[i].securityType); 181 console.info("rssi: " + result[i].rssi); 182 console.info("band: " + result[i].band); 183 console.info("frequency: " + result[i].frequency); 184 console.info("channelWidth: " + result[i].channelWidth); 185 console.info("timestamp: " + result[i].timestamp); 186 } 187 }).catch((err:number) => { 188 console.error("failed:" + JSON.stringify(err)); 189 }); 190``` 191 192## wifiManager.getScanResultsSync<sup>9+</sup><sup>(deprecated)</sup> 193 194getScanResultsSync(): Array<[WifiScanInfo](#wifiscaninfo9)> 195 196获取扫描结果,使用同步方式返回结果。 197 198> **说明:** 199> 从 API version 9开始支持,从API version 10开始废弃。建议使用[wifiManager.getScanInfoList](#wifimanagergetscaninfolist10)代替。 200 201**需要权限:** ohos.permission.GET_WIFI_INFO 和 (ohos.permission.GET_WIFI_PEERS_MAC 或 (ohos.permission.LOCATION 和 ohos.permission.APPROXIMATELY_LOCATION)) 202ohos.permission.GET_WIFI_PEERS_MAC权限仅系统应用可申请。 203 204**系统能力:** SystemCapability.Communication.WiFi.STA 205 206**返回值:** 207 208| **类型** | **说明** | 209| -------- | -------- | 210| Array<[WifiScanInfo](#wifiscaninfo9)> | 扫描结果数组。 | 211 212**错误码:** 213 214以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 215 216| **错误码ID** | **错误信息** | 217| -------- | -------- | 218| 201 | Permission denied. | 219| 801 | Capability not supported. | 220| 2501000 | Operation failed.| 221 222**示例:** 223 224```ts 225 import { wifiManager } from '@kit.ConnectivityKit'; 226 227 try { 228 let scanInfoList = wifiManager.getScanResultsSync(); 229 console.info("scanInfoList:" + JSON.stringify(scanInfoList)); 230 let len = scanInfoList.length; 231 console.log("wifi received scan info: " + len); 232 if(len > 0){ 233 for (let i = 0; i < len; ++i) { 234 console.info("ssid: " + scanInfoList[i].ssid); 235 console.info("bssid: " + scanInfoList[i].bssid); 236 console.info("capabilities: " + scanInfoList[i].capabilities); 237 console.info("securityType: " + scanInfoList[i].securityType); 238 console.info("rssi: " + scanInfoList[i].rssi); 239 console.info("band: " + scanInfoList[i].band); 240 console.info("frequency: " + scanInfoList[i].frequency); 241 console.info("channelWidth: " + scanInfoList[i].channelWidth); 242 console.info("timestamp: " + scanInfoList[i].timestamp); 243 } 244 } 245 }catch(error){ 246 console.error("failed:" + JSON.stringify(error)); 247 } 248 249``` 250 251## wifiManager.getScanInfoList<sup>10+</sup> 252 253getScanInfoList(): Array<WifiScanInfo> 254 255获取扫描结果。 256 257**需要权限:** ohos.permission.GET_WIFI_INFO 258 259**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 260 261**系统能力:** SystemCapability.Communication.WiFi.STA 262 263**返回值:** 264 265| **类型** | **说明** | 266| -------- | -------- | 267| Array<[WifiScanInfo](#wifiscaninfo9)> | 返回扫描到的热点列表。如果应用申请了ohos.permission.GET_WIFI_PEERS_MAC权限(仅系统应用可申请),则返回结果中的bssid为真实设备地址,否则为随机设备地址。 | 268 269**错误码:** 270 271以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 272 273| **错误码ID** | **错误信息** | 274| -------- | -------- | 275| 201 | Permission denied. | 276| 801 | Capability not supported. | 277| 2501000 | Operation failed.| 278 279**示例:** 280 281```ts 282 import { wifiManager } from '@kit.ConnectivityKit'; 283 284 try { 285 let scanInfoList = wifiManager.getScanInfoList(); 286 console.info("scanInfoList:" + JSON.stringify(scanInfoList)); 287 let len = scanInfoList.length; 288 console.log("wifi received scan info: " + len); 289 if(len > 0){ 290 for (let i = 0; i < len; ++i) { 291 console.info("ssid: " + scanInfoList[i].ssid); 292 console.info("bssid: " + scanInfoList[i].bssid); 293 console.info("capabilities: " + scanInfoList[i].capabilities); 294 console.info("securityType: " + scanInfoList[i].securityType); 295 console.info("rssi: " + scanInfoList[i].rssi); 296 console.info("band: " + scanInfoList[i].band); 297 console.info("frequency: " + scanInfoList[i].frequency); 298 console.info("channelWidth: " + scanInfoList[i].channelWidth); 299 console.info("timestamp: " + scanInfoList[i].timestamp); 300 console.info("supportedWifiCategory: " + scanInfoList[i].supportedWifiCategory); 301 console.info("isHiLinkNetwork: " + scanInfoList[i].isHiLinkNetwork); 302 } 303 } 304 }catch(error){ 305 console.error("failed:" + JSON.stringify(error)); 306 } 307 308``` 309 310## WifiScanInfo<sup>9+</sup> 311 312WLAN热点信息。 313 314**系统能力:** SystemCapability.Communication.WiFi.STA 315 316 317| **名称** | **类型** | **可读** | **可写** | **说明** | 318| -------- | -------- | -------- | -------- | -------- | 319| ssid | string | 是 | 否 | 热点的SSID,最大长度为32字节,编码格式为UTF-8。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 320| bssid | string | 是 | 否 | 热点的BSSID,例如:00:11:22:33:44:55。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 321| bssidType<sup>10+</sup>| [DeviceAddressType](#deviceaddresstype10) | 是 | 否 | 热点的BSSID类型。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 322| capabilities | string | 是 | 否 | 热点能力。 | 323| securityType | [WifiSecurityType](#wifisecuritytype9) | 是 | 否 | WLAN加密类型。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 324| rssi | number | 是 | 否 | 热点的信号强度(dBm)。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 325| band | number | 是 | 否 | WLAN接入点的频段,1表示2.4GHZ;2表示5GHZ。 | 326| frequency | number | 是 | 否 | WLAN接入点的频率。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 327| channelWidth | number | 是 | 否 | WLAN接入点的带宽,具体定义参见[WifiChannelWidth](#wifichannelwidth9)。 | 328| centerFrequency0 | number | 是 | 否 | 热点的中心频率。 | 329| centerFrequency1 | number | 是 | 否 | 热点的中心频率。如果热点使用两个不重叠的WLAN信道,则返回两个中心频率,分别用centerFrequency0和centerFrequency1表示。 | 330| infoElems | Array<[WifiInfoElem](#wifiinfoelem9)> | 是 | 否 | 信息元素。 | 331| timestamp | number | 是 | 否 | 时间戳。 | 332| supportedWifiCategory<sup>12+</sup> | [WifiCategory](#wificategory12) | 是 | 否 | 热点支持的最高Wi-Fi级别。 | 333| isHiLinkNetwork<sup>12+</sup> | boolean | 是 | 否| 热点是否支持hiLink,true:支持, false:不支持。 | 334 335## DeviceAddressType<sup>10+</sup> 336 337Wi-Fi 设备地址(MAC/bssid)类型。 338 339**系统能力:** SystemCapability.Communication.WiFi.Core 340 341**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 342 343| **名称** | **值** | **说明** | 344| -------- | -------- | -------- | 345| RANDOM_DEVICE_ADDRESS | 0 | 随机设备地址。 | 346| REAL_DEVICE_ADDRESS | 1 | 真实设备地址。 | 347 348## WifiSecurityType<sup>9+</sup> 349 350表示加密类型的枚举。 351 352**系统能力:** SystemCapability.Communication.WiFi.Core 353 354 355| **名称** | **值** | **说明** | 356| -------- | -------- | -------- | 357| WIFI_SEC_TYPE_INVALID | 0 | 无效加密类型。 | 358| WIFI_SEC_TYPE_OPEN | 1 | 开放加密类型。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 359| WIFI_SEC_TYPE_WEP | 2 | Wired Equivalent Privacy (WEP)加密类型。候选网络(添加网络配置信息)配置不支持该加密类型。 | 360| WIFI_SEC_TYPE_PSK | 3 | Pre-shared key (PSK)加密类型。 | 361| WIFI_SEC_TYPE_SAE | 4 | Simultaneous Authentication of Equals (SAE)加密类型。 | 362| WIFI_SEC_TYPE_EAP | 5 | EAP authentication (EAP)加密类型。 | 363| WIFI_SEC_TYPE_EAP_SUITE_B | 6 | Suite-B 192位加密类型。 | 364| WIFI_SEC_TYPE_OWE | 7 | Opportunistic Wireless Encryption (OWE)机会性无线加密类型。 | 365| WIFI_SEC_TYPE_WAPI_CERT | 8 | WAPI-Cert加密类型。 | 366| WIFI_SEC_TYPE_WAPI_PSK | 9 | WAPI-PSK加密类型。 | 367 368 369## WifiBandType<sup>10+</sup> 370 371表示WIFI频段类型的枚举。 372 373**系统能力:** SystemCapability.Communication.WiFi.STA 374 375| **名称** | **值** | **说明** | 376| -------- | -------- | -------- | 377| WIFI_BAND_NONE | 0 | 无效频段类型。 | 378| WIFI_BAND_2G | 1 | 2.4G频段类型。 | 379| WIFI_BAND_5G | 2 | 5G频段类型。 | 380| WIFI_BAND_6G | 3 | 6G频段类型。 | 381| WIFI_BAND_60G | 4 | 60G频段类型。 | 382 383## WifiStandard<sup>10+</sup> 384 385表示WIFI标准的枚举。 386 387**系统能力:** SystemCapability.Communication.WiFi.STA 388 389| **名称** | **值** | **说明** | 390| -------- | -------- | -------- | 391| WIFI_STANDARD_UNDEFINED | 0 | 无效WIFI标准类型。 | 392| WIFI_STANDARD_11A | 1 | 802.11a WiFi标准类型。 | 393| WIFI_STANDARD_11B | 2 | 802.11b WiFi标准类型。 | 394| WIFI_STANDARD_11G | 3 | 802.11g WiFi标准类型。 | 395| WIFI_STANDARD_11N | 4 | 802.11n WiFi标准类型。 | 396| WIFI_STANDARD_11AC | 5 | 802.11ac WiFi标准类型。 | 397| WIFI_STANDARD_11AX | 6 | 802.11ax WiFi标准类型。 | 398| WIFI_STANDARD_11AD | 7 | 802.11ad WiFi标准类型。 | 399 400## WifiInfoElem<sup>9+</sup> 401 402WLAN热点信息。 403 404**系统能力:** SystemCapability.Communication.WiFi.STA 405 406 407| **名称** | **类型** | **可读** | **可写** | **说明** | 408| -------- | -------- | -------- | -------- | -------- | 409| eid | number | 是 | 否 | 元素ID。 | 410| content | Uint8Array | 是 | 否 | 元素内容。 | 411 412 413## WifiChannelWidth<sup>9+</sup> 414 415表示带宽类型的枚举。 416 417**系统能力:** SystemCapability.Communication.WiFi.STA 418 419 420| **名称** | **值** | **说明** | 421| -------- | -------- | -------- | 422| WIDTH_20MHZ | 0 | 20MHZ。 | 423| WIDTH_40MHZ | 1 | 40MHZ。 | 424| WIDTH_80MHZ | 2 | 80MHZ。 | 425| WIDTH_160MHZ | 3 | 160MHZ。 | 426| WIDTH_80MHZ_PLUS | 4 | 80MHZ<sup>+</sup>。 | 427| WIDTH_INVALID | 5 | 无效值 | 428 429 430## WifiDeviceConfig<sup>9+</sup> 431 432WLAN配置信息。 433 434**系统能力:** SystemCapability.Communication.WiFi.STA 435 436 437| **名称** | **类型** | **可读** | **可写** | **说明** | 438| -------- | -------- | -------- | -------- | -------- | 439| ssid | string | 是 | 否 | 热点的SSID,最大长度为32字节,编码格式为UTF-8。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 440| bssid | string | 是 | 否 | 热点的BSSID,例如:00:11:22:33:44:55。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 441| bssidType<sup>10+</sup> | [DeviceAddressType](#deviceaddresstype10) | 是 | 否 | 热点的BSSID类型。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 442| preSharedKey | string | 是 | 否 | 热点的密钥,最大长度为64字节。<br>当securityType为WIFI_SEC_TYPE_OPEN时该字段需为空串,其他加密类型不能为空串。<br>当securityType为WIFI_SEC_TYPE_WEP时,该字段长度只允许为5、10、13、26、16和32字节其中之一,并且当字段长度为偶数时,该字段必须为纯十六进制数字构成。<br>当securityType为WIFI_SEC_TYPE_SAE时,该字段最小长度为1字节。<br>当securityType为WIFI_SEC_TYPE_PSK时,该字段最小长度为8字节。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 443| isHiddenSsid | boolean | 是 | 否 | 是否是隐藏网络。 | 444| securityType | [WifiSecurityType](#wifisecuritytype9)| 是 | 否 | 加密类型。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 445| eapConfig<sup>10+</sup> | [WifiEapConfig](#wifieapconfig10) | 是 | 否 | 可扩展身份验证协议配置。只有securityType为WIFI_SEC_TYPE_EAP时需要填写。 | 446| wapiConfig<sup>12+</sup> | [WifiWapiConfig](#wifiwapiconfig12) | 是 | 否 | WAPI身份验证协议配置。只有securityType为WIFI_SEC_TYPE_WAPI_CERT或WIFI_SEC_TYPE_WAPI_PSK时需要填写。 | 447 448## WifiEapConfig<sup>10+</sup> 449 450可扩展身份验证协议配置信息。 451 452**系统能力:** SystemCapability.Communication.WiFi.STA 453 454| **名称** | **类型** | **可读** | **可写** | **说明** | 455| -------- | -------- | -------- | -------- | -------- | 456| eapMethod | [EapMethod](#eapmethod10) | 是 | 否 | EAP认证方式。 | 457| phase2Method | [Phase2Method](#phase2method10) | 是 | 否 | 第二阶段认证方式。只有eapMethod为EAP_PEAP或EAP_TTLS时需要填写。 | 458| identity | string | 是 | 否 | 身份信息。当eapMethod为EAP_PEAP、EAP_TLS或EAP_PWD时,该字段不能为空串。 | 459| anonymousIdentity | string | 是 | 否 | 匿名身份。暂未使用。 | 460| password | string | 是 | 否 | 密码。当eapMethod为EAP_PEAP或EAP_PWD时,该字段不能为空串。 | 461| caCertAlias | string | 是 | 否 | CA 证书别名。 | 462| caPath | string | 是 | 否 | CA 证书路径。 | 463| clientCertAlias | string | 是 | 否 | 客户端证书别名。 | 464| certEntry | Uint8Array | 是 | 是 | CA 证书内容。当eapMethod为EAP_TLS时,如果该字段为空,则clientCertAlias不能为空。 | 465| certPassword | string | 是 | 是 | CA证书密码。 | 466| altSubjectMatch | string | 是 | 否 | 替代主题匹配。 | 467| domainSuffixMatch | string | 是 | 否 | 域后缀匹配。 | 468| realm | string | 是 | 否 | 通行证凭证的领域。 | 469| plmn | string | 是 | 否 | 公共陆地移动网的直通凭证提供商。 | 470| eapSubId | number | 是 | 否 | SIM卡的子ID。 | 471 472 473## WifiWapiConfig<sup>12+</sup> 474 475WAPI身份验证协议配置。 476 477**系统能力:** SystemCapability.Communication.WiFi.STA 478 479| **名称** | **类型** | **可读** | **可写** | **说明** | 480| -------- | -------- | -------- | -------- | -------- | 481| wapiPskType | [WapiPskType](#wapipsktype12)| 是 | 是 | 加密类型。 | 482| wapiAsCert | string | 否 | 是 | As证书。 | 483| wapiUserCert | string | 否 | 是 | 用户证书。 | 484 485## WapiPskType<sup>12+</sup> 486 487WAPI认证方式的枚举。 488 489**系统能力:** SystemCapability.Communication.WiFi.Core 490 491| 名称 | 值 | 说明 | 492| -------- | -------- | -------- | 493| WAPI_PSK_ASCII | 0 | ASCII类型。 | 494| WAPI_PSK_HEX | 1 | HEX类型。 | 495 496## EapMethod<sup>10+</sup> 497 498表示EAP认证方式的枚举。 499 500**系统能力:** SystemCapability.Communication.WiFi.STA 501 502| 名称 | 值 | 说明 | 503| -------- | -------- | -------- | 504| EAP_NONE | 0 | 不指定。 | 505| EAP_PEAP | 1 | PEAP类型。 | 506| EAP_TLS | 2 | TLS类型。 | 507| EAP_TTLS | 3 | TTLS类型。 | 508| EAP_PWD | 4 | PWD类型。 | 509| EAP_SIM | 5 | SIM类型。 | 510| EAP_AKA | 6 | AKA类型。 | 511| EAP_AKA_PRIME | 7 | AKA Prime类型。 | 512| EAP_UNAUTH_TLS | 8 | UNAUTH TLS类型。 | 513 514## Phase2Method<sup>10+</sup> 515 516表示第二阶段认证方式的枚举。 517 518**系统能力:** SystemCapability.Communication.WiFi.STA 519 520| 名称 | 值 | 说明 | 521| -------- | -------- | -------- | 522| PHASE2_NONE | 0 | 不指定。 | 523| PHASE2_PAP | 1 | PAP类型。 | 524| PHASE2_MSCHAP | 2 | MSCHAP类型。 | 525| PHASE2_MSCHAPV2 | 3 | MSCHAPV2类型。 | 526| PHASE2_GTC | 4 | GTC类型。 | 527| PHASE2_SIM | 5 | SIM类型。 | 528| PHASE2_AKA | 6 | AKA类型。 | 529| PHASE2_AKA_PRIME | 7 | AKA Prime类型。 | 530 531## WifiCategory<sup>12+</sup> 532 533表示热点支持的最高wifi类别。 534 535**系统能力:** SystemCapability.Communication.WiFi.STA 536 537| 名称 | 值 | 说明 | 538| -------- | -------- | -------- | 539| DEFAULT | 1 | Default。Wifi6以下的wifi类别。 | 540| WIFI6 | 2 | Wifi6。 | 541| WIFI6_PLUS | 3 | Wifi6+。 | 542 543## wifiManager.addCandidateConfig<sup>9+</sup> 544 545addCandidateConfig(config: WifiDeviceConfig): Promise<number> 546 547添加候选网络配置,使用Promise异步回调,使用前先使能WLAN。 548 549**需要权限:** ohos.permission.SET_WIFI_INFO 550 551**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 552 553**系统能力:** SystemCapability.Communication.WiFi.STA 554 555**参数:** 556 557| **参数名** | **类型** | **必填** | **说明** | 558| -------- | -------- | -------- | -------- | 559| config | [WifiDeviceConfig](#wifideviceconfig9) | 是 | WLAN配置信息。如果bssidType未指定值,则bssidType默认为随机设备地址类型。 | 560 561**返回值:** 562 563 | **类型** | **说明** | 564 | -------- | -------- | 565 | Promise<number> | Promise对象。表示网络配置ID。 | 566 567**错误码:** 568 569以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 570 571| **错误码ID** | **错误信息** | 572| -------- | ---------------------------- | 573| 201 | Permission denied. | 574| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified.<br>2. Incorrect parameter types. 3. Parameter verification failed.| 575| 801 | Capability not supported. | 576| 2501000 | Operation failed.| 577 578**示例:** 579`````ts 580 import { wifiManager } from '@kit.ConnectivityKit'; 581 582 try { 583 let config:wifiManager.WifiDeviceConfig = { 584 ssid : "****", 585 preSharedKey : "****", 586 securityType : 0 587 } 588 wifiManager.addCandidateConfig(config).then(result => { 589 console.info("result:" + JSON.stringify(result)); 590 }).catch((err:number) => { 591 console.error("failed:" + JSON.stringify(err)); 592 }); 593 }catch(error){ 594 console.error("failed:" + JSON.stringify(error)); 595 } 596````` 597 598## wifiManager.addCandidateConfig<sup>9+</sup> 599 600addCandidateConfig(config: WifiDeviceConfig, callback: AsyncCallback<number>): void 601 602添加候选网络配置,使用callback异步回调。 603 604**需要权限:** ohos.permission.SET_WIFI_INFO 605 606**系统能力:** SystemCapability.Communication.WiFi.STA 607 608**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 609 610**参数:** 611 612| **参数名** | **类型** | **必填** | **说明** | 613| -------- | -------- | -------- | -------- | 614| config | [WifiDeviceConfig](#wifideviceconfig9) | 是 | WLAN配置信息。如果bssidType未指定值,则bssidType默认为随机设备地址类型。 | 615| callback | AsyncCallback<number> | 是 | 回调函数。当操作成功时,err为0,data为添加的网络配置ID,如果data值为-1,表示添加失败。如果操作出现错误,err为非0值。 | 616 617**错误码:** 618 619以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 620 621| **错误码ID** | **错误信息** | 622| -------- | ---------------------------- | 623| 201 | Permission denied. | 624| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified.<br>2. Incorrect parameter types. 3. Parameter verification failed.| 625| 801 | Capability not supported. | 626| 2501000 | Operation failed.| 627 628**示例:** 629`````ts 630 import { wifiManager } from '@kit.ConnectivityKit'; 631 632 try { 633 let config:wifiManager.WifiDeviceConfig = { 634 ssid : "****", 635 preSharedKey : "****", 636 securityType : 0 637 } 638 wifiManager.addCandidateConfig(config,(error,result) => { 639 console.info("result:" + JSON.stringify(result)); 640 }); 641 }catch(error){ 642 console.error("failed:" + JSON.stringify(error)); 643 } 644````` 645 646## wifiManager.removeCandidateConfig<sup>9+</sup> 647 648removeCandidateConfig(networkId: number): Promise<void> 649 650移除候选网络配置,使用Promise异步回调。 651 652**需要权限:** ohos.permission.SET_WIFI_INFO 653 654**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 655 656**系统能力:** SystemCapability.Communication.WiFi.STA 657 658**参数:** 659 660 | **参数名** | **类型** | **必填** | **说明** | 661 | -------- | -------- | -------- | -------- | 662 | networkId | number | 是 | 网络配置ID。 | 663 664**返回值:** 665 666 | **类型** | **说明** | 667 | -------- | -------- | 668 | Promise<void> | Promise对象。 | 669 670**错误码:** 671 672以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 673 674| **错误码ID** | **错误信息** | 675| -------- | ---------------------------- | 676| 201 | Permission denied. | 677| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified.<br>2. Incorrect parameter types. 3. Parameter verification failed.| 678| 801 | Capability not supported. | 679| 2501000 | Operation failed.| 680| 2501001 | Wi-Fi STA disabled. | 681 682**示例:** 683 684```ts 685 import { wifiManager } from '@kit.ConnectivityKit'; 686 687 try { 688 let networkId = 0; 689 wifiManager.removeCandidateConfig(networkId).then(result => { 690 console.info("result:" + JSON.stringify(result)); 691 }).catch((err:number) => { 692 console.error("failed:" + JSON.stringify(err)); 693 }); 694 }catch(error){ 695 console.error("failed:" + JSON.stringify(error)); 696 } 697``` 698 699## wifiManager.removeCandidateConfig<sup>9+</sup> 700 701removeCandidateConfig(networkId: number, callback: AsyncCallback<void>): void 702 703移除候选网络配置,使用callback异步回调。 704 705**需要权限:** ohos.permission.SET_WIFI_INFO 706 707**系统能力:** SystemCapability.Communication.WiFi.STA 708 709**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 710 711**参数:** 712 713 | **参数名** | **类型** | **必填** | **说明** | 714 | -------- | -------- | -------- | -------- | 715 | networkId | number | 是 | 网络配置ID。 | 716 | callback | AsyncCallback<void> | 是 | 回调函数。当操作成功时,err为0。如果error为非0,表示处理出现错误。 | 717 718**错误码:** 719 720以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 721 722| **错误码ID** | **错误信息** | 723| -------- | ---------------------------- | 724| 201 | Permission denied. | 725| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified.<br>2. Incorrect parameter types. 3. Parameter verification failed. | 726| 801 | Capability not supported. | 727| 2501000 | Operation failed.| 728| 2501001 | Wi-Fi STA disabled. | 729 730**示例:** 731```ts 732 import { wifiManager } from '@kit.ConnectivityKit'; 733 734 try { 735 let networkId = 0; 736 wifiManager.removeCandidateConfig(networkId,(error,result) => { 737 console.info("result:" + JSON.stringify(result)); 738 }); 739 }catch(error){ 740 console.error("failed:" + JSON.stringify(error)); 741 } 742``` 743 744## wifiManager.getCandidateConfigs<sup>9+</sup> 745 746getCandidateConfigs(): Array<WifiDeviceConfig> 747 748获取候选网络配置。 749 750**需要权限:** 751 752API 10起:ohos.permission.GET_WIFI_INFO 753 754**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 755 756**系统能力:** SystemCapability.Communication.WiFi.STA 757 758**返回值:** 759 760 | **类型** | **说明** | 761 | -------- | -------- | 762 | Array<[WifiDeviceConfig](#wifideviceconfig9)> | 候选网络配置数组。 | 763 764**错误码:** 765 766以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 767 768| **错误码ID** | **错误信息** | 769| -------- | ---------------------------- | 770| 201 | Permission denied. | 771| 801 | Capability not supported. | 772| 2501000 | Operation failed.| 773 774**示例:** 775 776```ts 777 import { wifiManager } from '@kit.ConnectivityKit'; 778 779 try { 780 let configs = wifiManager.getCandidateConfigs(); 781 console.info("configs:" + JSON.stringify(configs)); 782 let len = configs.length; 783 console.log("result len: " + len); 784 if(len > 0){ 785 for (let i = 0; i < len; ++i) { 786 console.info("ssid: " + configs[i].ssid); 787 console.info("bssid: " + configs[i].bssid); 788 } 789 } 790 }catch(error){ 791 console.error("failed:" + JSON.stringify(error)); 792 } 793 794``` 795 796## wifiManager.connectToCandidateConfig<sup>9+</sup> 797 798connectToCandidateConfig(networkId: number): void 799 800应用使用该接口连接到自己添加的候选网络(如果当前已经连接到热点,需要先断开连接)。 801 802**需要权限:** ohos.permission.SET_WIFI_INFO 803 804**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 805 806**系统能力:** SystemCapability.Communication.WiFi.STA 807 808**参数:** 809 810 | **参数名** | **类型** | **必填** | **说明** | 811 | -------- | -------- | -------- | -------- | 812 | networkId | number | 是 | 候选网络配置的ID。 | 813 814**错误码:** 815 816以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 817 818| **错误码ID** | **错误信息** | 819| -------- | ---------------------------- | 820| 201 | Permission denied. | 821| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified.<br>2. Incorrect parameter types. 3. Parameter verification failed. | 822| 801 | Capability not supported. | 823| 2501000 | Operation failed.| 824| 2501001 | Wi-Fi STA disabled.| 825 826**示例:** 827```ts 828 import { wifiManager } from '@kit.ConnectivityKit'; 829 830 try { 831 let networkId = 0; // 实际的候选网络ID,在添加候选网络时生成,取自WifiDeviceConfig.netId 832 wifiManager.connectToCandidateConfig(networkId); 833 }catch(error){ 834 console.error("failed:" + JSON.stringify(error)); 835 } 836 837``` 838 839 840## wifiManager.getSignalLevel<sup>9+</sup> 841 842getSignalLevel(rssi: number, band: number): number 843 844查询WLAN信号强度。 845 846**需要权限:** ohos.permission.GET_WIFI_INFO 847 848**系统能力:** SystemCapability.Communication.WiFi.STA 849 850**参数:** 851 852 | **参数名** | **类型** | **必填** | **说明** | 853 | -------- | -------- | -------- | -------- | 854 | rssi | number | 是 | 热点的信号强度(dBm)。 | 855 | band | number | 是 | WLAN接入点的频段,1表示2.4GHZ;2表示5GHZ。 | 856 857**返回值:** 858 859 | **类型** | **说明** | 860 | -------- | -------- | 861 | number | 信号强度,取值范围为[0, 4]。 | 862 863**错误码:** 864 865以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 866 867| **错误码ID** | **错误信息** | 868| -------- | -------- | 869| 201 | Permission denied. | 870| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified.<br>2. Incorrect parameter types. | 871| 801 | Capability not supported. | 872| 2501000 | Operation failed.| 873 874**示例:** 875```ts 876 import { wifiManager } from '@kit.ConnectivityKit'; 877 878 try { 879 let rssi = 0; 880 let band = 0; 881 let level = wifiManager.getSignalLevel(rssi,band); 882 console.info("level:" + JSON.stringify(level)); 883 }catch(error){ 884 console.error("failed:" + JSON.stringify(error)); 885 } 886 887``` 888 889## wifiManager.getLinkedInfo<sup>9+</sup> 890 891getLinkedInfo(): Promise<WifiLinkedInfo> 892 893获取WLAN连接信息,使用Promise异步回调。 894 895**需要权限:** ohos.permission.GET_WIFI_INFO 。 896 897当macType是1 - 设备MAC地址时,获取 macAddress 还需申请ohos.permission.GET_WIFI_LOCAL_MAC权限(该权限仅系统应用可申请),无该权限时,macAddress 返回随机MAC地址。 898 899**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 900 901**系统能力:** SystemCapability.Communication.WiFi.STA 902 903**返回值:** 904 905 | 类型 | 说明 | 906 | -------- | -------- | 907 | Promise<[WifiLinkedInfo](#wifilinkedinfo9)> | Promise对象。表示WLAN连接信息。 | 908 909**错误码:** 910 911以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 912 913| **错误码ID** | **错误信息** | 914| -------- | -------- | 915| 201 | Permission denied. | 916| 801 | Capability not supported. | 917| 2501000 | Operation failed.| 918| 2501001 | Wi-Fi STA disabled.| 919 920## wifiManager.getLinkedInfo<sup>9+</sup> 921 922getLinkedInfo(callback: AsyncCallback<WifiLinkedInfo>): void 923 924获取WLAN连接信息,使用callback异步回调。 925 926**需要权限:** ohos.permission.GET_WIFI_INFO 。 927 928当macType是1 - 设备MAC地址时,获取 macAddress 还需申请ohos.permission.GET_WIFI_LOCAL_MAC权限(该权限仅系统应用可申请),无该权限时,macAddress 返回随机MAC地址。 929 930**系统能力:** SystemCapability.Communication.WiFi.STA 931 932**参数:** 933 934 | 参数名 | 类型 | 必填 | 说明 | 935 | -------- | -------- | -------- | -------- | 936 | callback | AsyncCallback<[WifiLinkedInfo](#wifilinkedinfo9)> | 是 | 回调函数。当获取成功时,err为0,data表示WLAN连接信息。如果err为非0,表示处理出现错误。 | 937 938**错误码:** 939 940以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 941 942| **错误码ID** | **错误信息** | 943| -------- | -------- | 944| 201 | Permission denied. | 945| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 946| 801 | Capability not supported. | 947| 2501000 | Operation failed.| 948| 2501001 | Wi-Fi STA disabled.| 949 950**示例:** 951```ts 952 import { wifiManager } from '@kit.ConnectivityKit'; 953 954 wifiManager.getLinkedInfo((err, data:wifiManager.WifiLinkedInfo) => { 955 if (err) { 956 console.error("get linked info error"); 957 return; 958 } 959 console.info("get wifi linked info: " + JSON.stringify(data)); 960 }); 961 962 wifiManager.getLinkedInfo().then(data => { 963 console.info("get wifi linked info: " + JSON.stringify(data)); 964 }).catch((error:number) => { 965 console.info("get linked info error"); 966 }); 967``` 968 969 970## WifiLinkedInfo<sup>9+</sup> 971 972提供WLAN连接的相关信息。 973 974**系统能力:** SystemCapability.Communication.WiFi.STA 975 976| 名称 | 类型 | 可读 | 可写 | 说明 | 977| -------- | -------- | -------- | -------- | -------- | 978| ssid | string | 是 | 否 | 热点的SSID,编码格式为UTF-8。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 979| bssid | string | 是 | 否 | 热点的BSSID。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 980| rssi | number | 是 | 否 | 热点的信号强度(dBm)。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 981| band | number | 是 | 否 | WLAN接入点的频段,1表示2.4GHZ;2表示5GHZ。 | 982| linkSpeed | number | 是 | 否 | WLAN接入点的上行速度,单位Mbps/s。 | 983| rxLinkSpeed<sup>10+</sup> | number | 是 | 否 | WLAN接入点的下行速度,单位Mbps/s。 | 984| maxSupportedTxLinkSpeed<sup>10+</sup> | number | 是 | 否 | 当前支持的最大上行速率,单位Mbps/s。 | 985| maxSupportedRxLinkSpeed<sup>10+</sup> | number | 是 | 否 | 当前支持的最大下行速率,单位Mbps/s。 | 986| frequency | number | 是 | 否 | WLAN接入点的频率。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 987| isHidden | boolean | 是 | 否 | WLAN接入点是否是隐藏网络。 | 988| isRestricted | boolean | 是 | 否 | WLAN接入点是否限制数据量。 | 989| macType | number | 是 | 否 | MAC地址类型。0 - 随机MAC地址,1 - 设备MAC地址。 | 990| macAddress | string | 是 | 否 | 设备的MAC地址。 | 991| ipAddress | number | 是 | 否 | WLAN连接的IP地址(wifi连接信息和关于本机里的状态信息可以查看)。| 992| connState | [ConnState](#connstate9) | 是 | 否 | WLAN连接状态。 | 993| channelWidth<sup>10+</sup> | [WifiChannelWidth](#wifichannelwidth9) | 是 | 否 | 当前连接热点的信道带宽。 | 994| wifiStandard<sup>10+</sup> | [WifiStandard](#wifistandard10) | 是 | 否 | 当前连接热点的Wi-Fi标准。 | 995| supportedWifiCategory<sup>12+</sup> | [WifiCategory](#wificategory12) | 是 | 否 | 热点支持的最高Wi-Fi级别。 | 996| isHiLinkNetwork<sup>12+</sup> | boolean | 是 | 否| 热点是否支持hilink,true:支持, false:不支持。 | 997 998## ConnState<sup>9+</sup> 999 1000表示WLAN连接状态的枚举。 1001 1002**系统能力:** SystemCapability.Communication.WiFi.STA 1003 1004| 名称 | 值 | 说明 | 1005| -------- | -------- | -------- | 1006| SCANNING | 0 | 设备正在搜索可用的AP。 | 1007| CONNECTING | 1 | 正在建立WLAN连接。 | 1008| AUTHENTICATING | 2 | WLAN连接正在认证中。 | 1009| OBTAINING_IPADDR | 3 | 正在获取WLAN连接的IP地址。 | 1010| CONNECTED | 4 | WLAN连接已建立。 | 1011| DISCONNECTING | 5 | WLAN连接正在断开。 | 1012| DISCONNECTED | 6 | WLAN连接已断开。 | 1013| UNKNOWN | 7 | WLAN连接建立失败。 | 1014 1015 1016## wifiManager.isConnected<sup>9+</sup> 1017 1018isConnected(): boolean 1019 1020查询WLAN是否已连接。 1021 1022**需要权限:** ohos.permission.GET_WIFI_INFO 1023 1024**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1025 1026**系统能力:** SystemCapability.Communication.WiFi.STA 1027 1028**返回值:** 1029 1030 | **类型** | **说明** | 1031 | -------- | -------- | 1032 | boolean | true:已连接, false:未连接。 | 1033 1034**错误码:** 1035 1036以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1037 1038| **错误码ID** | **错误信息** | 1039| -------- | -------- | 1040| 201 | Permission denied. | 1041| 801 | Capability not supported. | 1042| 2501000 | Operation failed.| 1043 1044**示例:** 1045```ts 1046 import { wifiManager } from '@kit.ConnectivityKit'; 1047 1048 try { 1049 let ret = wifiManager.isConnected(); 1050 console.info("isConnected:" + ret); 1051 }catch(error){ 1052 console.error("failed:" + JSON.stringify(error)); 1053 } 1054 1055``` 1056 1057 1058## wifiManager.isFeatureSupported<sup>9+</sup> 1059 1060isFeatureSupported(featureId: number): boolean 1061 1062判断设备是否支持相关WLAN特性。 1063 1064**需要权限:** ohos.permission.GET_WIFI_INFO 1065 1066**系统能力:** SystemCapability.Communication.WiFi.Core 1067 1068**参数:** 1069 1070 | **参数名** | **类型** | 必填 | **说明** | 1071 | -------- | -------- | -------- | -------- | 1072 | featureId | number | 是 | 特性ID值。 | 1073 1074**特性ID值枚举:** 1075 1076| 枚举值 | 说明 | 1077| -------- | -------- | 1078| 0x0001 | 基础结构模式特性。 | 1079| 0x0002 | 5 GHz带宽特性。 | 1080| 0x0004 | GAS/ANQP特性。 | 1081| 0x0008 | Wifi-Direct特性。 | 1082| 0x0010 | Soft AP特性。 | 1083| 0x0040 | Wi-Fi AWare组网特性。 | 1084| 0x8000 | AP STA共存特性。 | 1085| 0x8000000 | WPA3-Personal SAE特性。 | 1086| 0x10000000 | WPA3-Enterprise Suite-B | 1087| 0x20000000 | 增强开放特性。 | 1088 1089**返回值:** 1090 1091 | **类型** | **说明** | 1092 | -------- | -------- | 1093 | boolean | true:支持, false:不支持。 | 1094 1095**错误码:** 1096 1097以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1098 1099| **错误码ID** | **错误信息** | 1100 | -------- | -------- | 1101| 201 | Permission denied. | 1102| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified.<br>2. Incorrect parameter types. | 1103| 801 | Capability not supported. | 1104| 2401000 | Operation failed.| 1105 1106**示例:** 1107```ts 1108 import { wifiManager } from '@kit.ConnectivityKit'; 1109 1110 try { 1111 let featureId = 0; 1112 let ret = wifiManager.isFeatureSupported(featureId); 1113 console.info("isFeatureSupported:" + ret); 1114 }catch(error){ 1115 console.error("failed:" + JSON.stringify(error)); 1116 } 1117 1118``` 1119 1120 1121## wifiManager.getIpInfo<sup>9+</sup> 1122 1123getIpInfo(): IpInfo 1124 1125获取IPV4信息。 1126 1127**需要权限:** ohos.permission.GET_WIFI_INFO 1128 1129**系统能力:** SystemCapability.Communication.WiFi.STA 1130 1131**返回值:** 1132 1133 | **类型** | **说明** | 1134 | -------- | -------- | 1135 | [IpInfo](#ipinfo9) | IP信息。 | 1136 1137**错误码:** 1138 1139以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1140 1141| **错误码ID** | **错误信息** | 1142| -------- | -------- | 1143| 201 | Permission denied. | 1144| 801 | Capability not supported. | 1145| 2501000 | Operation failed.| 1146 1147**示例:** 1148```ts 1149 import { wifiManager } from '@kit.ConnectivityKit'; 1150 1151 try { 1152 let info = wifiManager.getIpInfo(); 1153 console.info("info:" + JSON.stringify(info)); 1154 }catch(error){ 1155 console.error("failed:" + JSON.stringify(error)); 1156 } 1157``` 1158 1159## IpInfo<sup>9+</sup> 1160 1161IPV4信息。 1162 1163**系统能力:** SystemCapability.Communication.WiFi.STA 1164 1165| **名称** | **类型** | **可读** | **可写** | **说明** | 1166| -------- | -------- | -------- | -------- | -------- | 1167| ipAddress | number | 是 | 否 | IP地址。 | 1168| gateway | number | 是 | 否 | 网关。 | 1169| netmask | number | 是 | 否 | 掩码。 | 1170| primaryDns | number | 是 | 否 | 主DNS服务器IP地址。 | 1171| secondDns | number | 是 | 否 | 备DNS服务器IP地址。 | 1172| serverIp | number | 是 | 否 | DHCP服务端IP地址。 | 1173| leaseDuration | number | 是 | 否 | IP地址租用时长,单位:秒。 | 1174 1175 1176## wifiManager.getIpv6Info<sup>10+</sup> 1177 1178getIpv6Info(): Ipv6Info 1179 1180获取IPV6信息。 1181 1182**需要权限:** ohos.permission.GET_WIFI_INFO 1183 1184**系统能力:** SystemCapability.Communication.WiFi.STA 1185 1186**返回值:** 1187 1188| **类型** | **说明** | 1189| -------- | -------- | 1190| [Ipv6Info](#ipv6info10) | Ipv6信息。 | 1191 1192**错误码:** 1193 1194以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1195 1196| **错误码ID** | **错误信息** | 1197| -------- | -------- | 1198| 201 | Permission denied. | 1199| 801 | Capability not supported. | 1200| 2501000 | Operation failed.| 1201 1202**示例:** 1203```ts 1204 import { wifiManager } from '@kit.ConnectivityKit'; 1205 1206 try { 1207 let info = wifiManager.getIpv6Info(); 1208 console.info("info:" + JSON.stringify(info)); 1209 }catch(error){ 1210 console.error("failed:" + JSON.stringify(error)); 1211 } 1212``` 1213## Ipv6Info<sup>10+</sup> 1214 1215Ipv6信息。 1216 1217**系统能力:** SystemCapability.Communication.WiFi.STA 1218 1219| **名称** | **类型** | **可读** | **可写** | **说明** | 1220| -------- | -------- | -------- | -------- | -------- | 1221| linkIpv6Address | string | 是 | 否 | 链路Ipv6地址。 | 1222| globalIpv6Address | string | 是 | 否 | 全局Ipv6地址。 | 1223| randomGlobalIpv6Address | string | 是 | 否 | 随机全局Ipv6地址。 预留字段,暂不支持。| 1224| uniqueIpv6Address<sup>12+</sup> | string | 是 | 否 | 唯一本地Ipv6地址。 | 1225| randomUniqueIpv6Address<sup>12+</sup> | string | 是 | 否 | 随机唯一本地Ipv6地址。 | 1226| gateway | string | 是 | 否 | 网关。 | 1227| netmask | string | 是 | 否 | 网络掩码。 | 1228| primaryDNS | string | 是 | 否 | 主DNS服务器Ipv6地址。 | 1229| secondDNS | string | 是 | 否 | 备DNS服务器Ipv6地址。 | 1230 1231## wifiManager.getCountryCode<sup>9+</sup> 1232 1233getCountryCode(): string 1234 1235获取国家码信息。 1236 1237**需要权限:** ohos.permission.GET_WIFI_INFO 1238 1239**系统能力:** SystemCapability.Communication.WiFi.Core 1240 1241**返回值:** 1242 1243 | **类型** | **说明** | 1244 | -------- | -------- | 1245 | string | 国家码。 | 1246 1247**错误码:** 1248 1249以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1250 1251| **错误码ID** | **错误信息** | 1252| -------- | -------- | 1253| 201 | Permission denied. | 1254| 801 | Capability not supported. | 1255| 2401000 | Operation failed.| 1256 1257**示例:** 1258```ts 1259 import { wifiManager } from '@kit.ConnectivityKit'; 1260 1261 try { 1262 let code = wifiManager.getCountryCode(); 1263 console.info("code:" + code); 1264 }catch(error){ 1265 console.error("failed:" + JSON.stringify(error)); 1266 } 1267``` 1268 1269 1270 1271 1272## wifiManager.isBandTypeSupported<sup>10+</sup> 1273 1274isBandTypeSupported(bandType: WifiBandType): boolean 1275 1276判断当前频段是否支持。 1277 1278**需要权限:** ohos.permission.GET_WIFI_INFO。 1279 1280**系统能力:** SystemCapability.Communication.WiFi.STA 1281 1282**参数:** 1283 1284 | **参数名** | **类型** | **必填** | **说明** | 1285 | -------- | -------- | -------- | -------- | 1286 | bandType | [WifiBandType](#wifibandtype10) | 是 | Wifi 频段类型。 | 1287 1288**返回值:** 1289 1290 | **类型** | **说明** | 1291 | -------- | -------- | 1292 | boolean | true:支持, false:不支持。 | 1293 1294**错误码:** 1295 1296以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1297 1298| **错误码ID** | **错误信息** | 1299| -------- | -------- | 1300| 201 | Permission denied. | 1301| 801 | Capability not supported. | 1302| 2501000 | Operation failed.| 1303 1304**示例:** 1305```ts 1306 import { wifiManager } from '@kit.ConnectivityKit'; 1307 1308 try { 1309 let type = 0; 1310 let isBandTypeSupported = wifiManager.isBandTypeSupported(type); 1311 console.info("isBandTypeSupported:" + isBandTypeSupported); 1312 }catch(error){ 1313 console.error("failed:" + JSON.stringify(error)); 1314 } 1315``` 1316 1317 1318## wifiManager.isMeteredHotspot<sup>11+</sup> 1319 1320isMeteredHotspot(): boolean 1321 1322查询设备当前连接的wifi是否是手机热点。 1323 1324**需要权限:** ohos.permission.GET_WIFI_INFO 1325 1326**系统能力:** SystemCapability.Communication.WiFi.STA 1327 1328**返回值:** 1329 1330 | **类型** | **说明** | 1331 | -------- | -------- | 1332 | boolean | true:是手机热点, false:不是手机热点。 | 1333 1334**错误码:** 1335 1336以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1337 1338| **错误码ID** | **错误信息** | 1339| -------- | -------- | 1340| 201 | Permission denied. | 1341| 801 | Capability not supported. | 1342| 2501000 | Operation failed.| 1343| 2501001 | Wi-Fi STA disabled. | 1344 1345**示例:** 1346 1347```ts 1348 import { wifiManager } from '@kit.ConnectivityKit'; 1349 1350 try { 1351 let isMeteredHotspot = wifiManager.isMeteredHotspot(); 1352 console.info("isMeteredHotspot:" + isMeteredHotspot); 1353 }catch(error){ 1354 console.error("failed:" + JSON.stringify(error)); 1355 } 1356``` 1357 1358 1359 1360## wifiManager.getP2pLinkedInfo<sup>9+</sup> 1361 1362getP2pLinkedInfo(): Promise<WifiP2pLinkedInfo> 1363 1364获取P2P连接信息,使用Promise异步回调。 1365 1366**需要权限:** ohos.permission.GET_WIFI_INFO 1367 1368获取 groupOwnerAddr 还需申请ohos.permission.GET_WIFI_LOCAL_MAC权限(该权限仅系统应用可申请),无该权限时,groupOwnerAddr 返回全零地址。 1369 1370**系统能力:** SystemCapability.Communication.WiFi.P2P 1371 1372**返回值:** 1373 1374 | 类型 | 说明 | 1375 | -------- | -------- | 1376 | Promise<[WifiP2pLinkedInfo](#wifip2plinkedinfo9)> | Promise对象。表示P2P连接信息。 | 1377 1378**错误码:** 1379 1380以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1381 1382| **错误码ID** | **错误信息** | 1383| -------- | -------- | 1384| 201 | Permission denied. | 1385| 801 | Capability not supported. | 1386| 2801000 | P2P module error. | 1387 1388 1389## wifiManager.getP2pLinkedInfo<sup>9+</sup> 1390 1391getP2pLinkedInfo(callback: AsyncCallback<WifiP2pLinkedInfo>): void 1392 1393获取P2P连接信息,使用callback异步回调。 1394 1395**需要权限:** ohos.permission.GET_WIFI_INFO 1396 1397获取 groupOwnerAddr 还需申请ohos.permission.GET_WIFI_LOCAL_MAC权限(该权限仅系统应用可申请),无该权限时,groupOwnerAddr 返回全零地址。 1398 1399**系统能力:** SystemCapability.Communication.WiFi.P2P 1400 1401**参数:** 1402 1403 | 参数名 | 类型 | 必填 | 说明 | 1404 | -------- | -------- | -------- | -------- | 1405 | callback | AsyncCallback<[WifiP2pLinkedInfo](#wifip2plinkedinfo9)> | 是 | 回调函数。当操作成功时,err为0,data表示P2P连接信息。如果err为非0,表示处理出现错误。 | 1406 1407**错误码:** 1408 1409以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1410 1411| **错误码ID** | **错误信息** | 1412| -------- | -------- | 1413| 201 | Permission denied. | 1414| 801 | Capability not supported. | 1415| 2801000 | Operation failed. | 1416| 2801001 | Wi-Fi STA disabled. | 1417 1418**示例:** 1419```ts 1420 import { wifiManager } from '@kit.ConnectivityKit'; 1421 1422 wifiManager.getP2pLinkedInfo((err, data:wifiManager.WifiP2pLinkedInfo) => { 1423 if (err) { 1424 console.error("get p2p linked info error"); 1425 return; 1426 } 1427 console.info("get wifi p2p linked info: " + JSON.stringify(data)); 1428 }); 1429 1430 wifiManager.getP2pLinkedInfo().then(data => { 1431 console.info("get wifi p2p linked info: " + JSON.stringify(data)); 1432 }); 1433``` 1434 1435 1436## WifiP2pLinkedInfo<sup>9+</sup> 1437 1438提供WLAN连接的相关信息。 1439 1440**系统能力:** SystemCapability.Communication.WiFi.P2P 1441 1442| 名称 | 类型 | 可读 | 可写 | 说明 | 1443| -------- | -------- | -------- | -------- | -------- | 1444| connectState | [P2pConnectState](#p2pconnectstate9) | 是 | 否 | P2P连接状态。 | 1445| isGroupOwner | boolean | 是 | 否 | 是否为GO。 | 1446| groupOwnerAddr | string | 是 | 否 | 群组IP地址。| 1447 1448 1449## P2pConnectState<sup>9+</sup> 1450 1451表示P2P连接状态的枚举。 1452 1453**系统能力:** SystemCapability.Communication.WiFi.P2P 1454 1455| 名称 | 值 | 说明 | 1456| -------- | -------- | -------- | 1457| DISCONNECTED | 0 | 断开状态。 | 1458| CONNECTED | 1 | 连接状态。 | 1459 1460## wifiManager.getCurrentGroup<sup>9+</sup> 1461 1462getCurrentGroup(): Promise<WifiP2pGroupInfo> 1463 1464获取P2P当前组信息,使用Promise异步回调。 1465 1466**需要权限:** 1467 1468API 10起:ohos.permission.GET_WIFI_INFO 1469 1470**系统能力:** SystemCapability.Communication.WiFi.P2P 1471 1472**返回值:** 1473 1474| 类型 | 说明 | 1475| -------- | -------- | 1476| Promise<[WifiP2pGroupInfo](#wifip2pgroupinfo9)> | Promise对象。表示当前组信息。如果应用申请了ohos.permission.GET_WIFI_PEERS_MAC权限(仅系统应用可申请),则返回结果中的deviceAddress为真实设备地址,否则为随机设备地址。 | 1477 1478**错误码:** 1479 1480以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1481 1482| **错误码ID** | **错误信息** | 1483| -------- | -------- | 1484| 201 | Permission denied. | 1485| 801 | Capability not supported. | 1486| 2801000 | Operation failed. | 1487 1488## wifiManager.getCurrentGroup<sup>9+</sup> 1489 1490getCurrentGroup(callback: AsyncCallback<WifiP2pGroupInfo>): void 1491 1492获取P2P当前组信息,使用callback异步回调。 1493 1494**需要权限:** 1495 1496API 10起:ohos.permission.GET_WIFI_INFO 1497 1498**系统能力:** SystemCapability.Communication.WiFi.P2P 1499 1500**参数:** 1501 1502| 参数名 | 类型 | 必填 | 说明 | 1503| -------- | -------- | -------- | -------- | 1504| callback | AsyncCallback<[WifiP2pGroupInfo](#wifip2pgroupinfo9)> | 是 | 回调函数。当操作成功时,err为0,data表示当前组信息。如果error为非0,表示处理出现错误。如果应用申请了ohos.permission.GET_WIFI_PEERS_MAC权限(仅系统应用可申请),则返回结果中的deviceAddress为真实设备地址,否则为随机设备地址。 | 1505 1506**错误码:** 1507 1508以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1509 1510| **错误码ID** | **错误信息** | 1511| -------- | -------- | 1512| 201 | Permission denied. | 1513| 801 | Capability not supported. | 1514| 2801000 | P2P module error. | 1515 1516**示例:** 1517```ts 1518 import { wifiManager } from '@kit.ConnectivityKit'; 1519 // p2p已经建组或者连接成功,才能正常获取到当前组信息 1520 wifiManager.getCurrentGroup((err, data:wifiManager.WifiP2pGroupInfo) => { 1521 if (err) { 1522 console.error("get current P2P group error"); 1523 return; 1524 } 1525 console.info("get current P2P group: " + JSON.stringify(data)); 1526 }); 1527 1528 wifiManager.getCurrentGroup().then(data => { 1529 console.info("get current P2P group: " + JSON.stringify(data)); 1530 }); 1531``` 1532 1533## wifiManager.getP2pPeerDevices<sup>9+</sup> 1534 1535getP2pPeerDevices(): Promise<WifiP2pDevice[]> 1536 1537获取P2P对端设备列表信息,使用Promise异步回调。 1538 1539**需要权限:** 1540 1541API 10起:ohos.permission.GET_WIFI_INFO 1542 1543**系统能力:** SystemCapability.Communication.WiFi.P2P 1544 1545**返回值:** 1546 1547| 类型 | 说明 | 1548| -------- | -------- | 1549| Promise<[WifiP2pDevice[]](#wifip2pdevice9)> | Promise对象。表示对端设备列表信息。如果应用申请了ohos.permission.GET_WIFI_PEERS_MAC权限(仅系统应用可申请),则返回结果中的deviceAddress为真实设备地址,否则为随机设备地址。 | 1550 1551**错误码:** 1552 1553以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1554 1555| **错误码ID** | **错误信息** | 1556| -------- | -------- | 1557| 201 | Permission denied. | 1558| 801 | Capability not supported. | 1559| 2801000 | Operation failed. | 1560 1561## wifiManager.getP2pPeerDevices<sup>9+</sup> 1562 1563getP2pPeerDevices(callback: AsyncCallback<WifiP2pDevice[]>): void 1564 1565获取P2P对端设备列表信息,使用callback异步回调。 1566 1567**需要权限:** 1568 1569API 10起:ohos.permission.GET_WIFI_INFO 1570 1571**系统能力:** SystemCapability.Communication.WiFi.P2P 1572 1573**参数:** 1574 1575| 参数名 | 类型 | 必填 | 说明 | 1576| -------- | -------- | -------- | -------- | 1577| callback | AsyncCallback<[WifiP2pDevice[]](#wifip2pdevice9)> | 是 | 回调函数。当操作成功时,err为0,data表示对端设备列表信息。如果err为非0,表示处理出现错误。如果应用申请了ohos.permission.GET_WIFI_PEERS_MAC权限(仅系统应用可申请),则返回结果中的deviceAddress为真实设备地址,否则为随机设备地址。 | 1578 1579**错误码:** 1580 1581以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1582 1583| **错误码ID** | **错误信息** | 1584| -------- | -------- | 1585| 201 | Permission denied. | 1586| 801 | Capability not supported. | 1587| 2801000 | P2P module error. | 1588| 2801001 | Wi-Fi STA disabled. | 1589 1590**示例:** 1591```ts 1592 import { wifiManager } from '@kit.ConnectivityKit'; 1593 // p2p发现阶段完成,才能正常获取到对端设备列表信息 1594 wifiManager.getP2pPeerDevices((err, data:wifiManager.WifiP2pDevice) => { 1595 if (err) { 1596 console.error("get P2P peer devices error"); 1597 return; 1598 } 1599 console.info("get P2P peer devices: " + JSON.stringify(data)); 1600 }); 1601 1602 wifiManager.getP2pPeerDevices().then(data => { 1603 console.info("get P2P peer devices: " + JSON.stringify(data)); 1604 }); 1605``` 1606 1607## WifiP2pDevice<sup>9+</sup> 1608 1609表示P2P设备信息。 1610 1611**系统能力:** SystemCapability.Communication.WiFi.P2P 1612 1613| 名称 | 类型 | 可读 | 可写 | 说明 | 1614| -------- | -------- | -------- | -------- | -------- | 1615| deviceName | string | 是 | 否 | 设备名称。 | 1616| deviceAddress | string | 是 | 否 | 设备MAC地址。 | 1617| deviceAddressType<sup>10+</sup> | [DeviceAddressType](#deviceaddresstype10) | 是 | 否 | 设备MAC地址类型。 | 1618| primaryDeviceType | string | 是 | 否 | 主设备类型。 | 1619| deviceStatus | [P2pDeviceStatus](#p2pdevicestatus9) | 是 | 否 | 设备状态。 | 1620| groupCapabilities | number | 是 | 否 | 群组能力。 | 1621 1622 1623## P2pDeviceStatus<sup>9+</sup> 1624 1625表示设备状态的枚举。 1626 1627**系统能力:** SystemCapability.Communication.WiFi.P2P 1628 1629| 名称 | 值 | 说明 | 1630| -------- | -------- | -------- | 1631| CONNECTED | 0 | 连接状态。 | 1632| INVITED | 1 | 邀请状态。 | 1633| FAILED | 2 | 失败状态。 | 1634| AVAILABLE | 3 | 可用状态。 | 1635| UNAVAILABLE | 4 | 不可用状态。 | 1636 1637 1638## wifiManager.getP2pLocalDevice<sup>9+</sup> 1639 1640getP2pLocalDevice(): Promise<WifiP2pDevice> 1641 1642获取P2P本端设备信息,使用Promise异步回调。 1643 1644**需要权限:** 1645 1646API 11起:ohos.permission.GET_WIFI_INFO 1647 1648**系统能力:** SystemCapability.Communication.WiFi.P2P 1649 1650**返回值:** 1651 1652 | 类型 | 说明 | 1653 | -------- | -------- | 1654 | Promise<[WifiP2pDevice](#wifip2pdevice9)> | Promise对象。表示本端设备信息。 | 1655 1656**错误码:** 1657 1658以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1659 1660| **错误码ID** | **错误信息** | 1661| -------- | -------- | 1662| 201 | Permission denied. | 1663| 801 | Capability not supported. | 1664| 2801000 | Operation failed. | 1665 1666## wifiManager.getP2pLocalDevice<sup>9+</sup> 1667 1668getP2pLocalDevice(callback: AsyncCallback<WifiP2pDevice>): void 1669 1670获取P2P本端设备信息,使用callback异步回调。 1671 1672**需要权限:** 1673 1674API 11起:ohos.permission.GET_WIFI_INFO 1675 1676**系统能力:** SystemCapability.Communication.WiFi.P2P 1677 1678**参数:** 1679 1680 | 参数名 | 类型 | 必填 | 说明 | 1681 | -------- | -------- | -------- | -------- | 1682 | callback | AsyncCallback<[WifiP2pDevice](#wifip2pdevice9)> | 是 | 回调函数。当操作成功时,err为0,data表示本端设备信息。如果error为非0,表示处理出现错误。 | 1683 1684**错误码:** 1685 1686| **错误码ID** | **错误信息** | 1687| -------- | -------- | 1688| 201 | Permission denied. | 1689| 801 | Capability not supported. | 1690| 2801000 | P2P module error. | 1691| 2801001 | Wi-Fi STA disabled. | 1692 1693**示例:** 1694```ts 1695 import { wifiManager } from '@kit.ConnectivityKit'; 1696 // p2p已经建组或者连接成功,才能正常获取到本端设备信息 1697 wifiManager.getP2pLocalDevice((err, data:wifiManager.WifiP2pDevice) => { 1698 if (err) { 1699 console.error("get P2P local device error"); 1700 return; 1701 } 1702 console.info("get P2P local device: " + JSON.stringify(data)); 1703 }); 1704 1705 wifiManager.getP2pLocalDevice().then(data => { 1706 console.info("get P2P local device: " + JSON.stringify(data)); 1707 }); 1708``` 1709 1710## wifiManager.createGroup<sup>9+</sup> 1711 1712createGroup(config: WifiP2PConfig): void 1713 1714创建群组。 1715 1716**需要权限:** ohos.permission.GET_WIFI_INFO 1717 1718**系统能力:** SystemCapability.Communication.WiFi.P2P 1719 1720**参数:** 1721 1722| **参数名** | **类型** | 必填 | **说明** | 1723| -------- | -------- | -------- | -------- | 1724| config | [WifiP2PConfig](#wifip2pconfig9) | 是 | 群组配置信息。如果DeviceAddressType未指定值,则DeviceAddressType默认为随机设备地址类型。 | 1725 1726**错误码:** 1727 1728以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1729 1730| **错误码ID** | **错误信息** | 1731| -------- | -------- | 1732| 201 | Permission denied. | 1733| 401 | Invalid parameters. Possible causes: 1. Incorrect parameter types.<br>2. Parameter verification failed. | 1734| 801 | Capability not supported. | 1735| 2801000 | Operation failed. | 1736| 2801001 | Wi-Fi STA disabled. | 1737 1738**示例:** 1739```ts 1740 import { wifiManager } from '@kit.ConnectivityKit'; 1741 1742 try { 1743 let config:wifiManager.WifiP2PConfig = { 1744 deviceAddress: "****", 1745 netId: 0, 1746 passphrase: "*****", 1747 groupName: "****", 1748 goBand: 0 1749 } 1750 wifiManager.createGroup(config); 1751 1752 }catch(error){ 1753 console.error("failed:" + JSON.stringify(error)); 1754 } 1755``` 1756 1757## WifiP2PConfig<sup>9+</sup> 1758 1759表示P2P配置信息。 1760 1761**系统能力:** SystemCapability.Communication.WiFi.P2P 1762 1763| 名称 | 类型 | 可读 | 可写 | 说明 | 1764| -------- | -------- | -------- | -------- | -------- | 1765| deviceAddress | string | 是 | 否 | 设备地址。 | 1766| deviceAddressType<sup>10+</sup>| [DeviceAddressType](#deviceaddresstype10) | 是 | 否 | 设备地址类型。 | 1767| netId | number | 是 | 否 | 网络ID。创建群组时-1表示创建临时组,-2表示创建永久组。 | 1768| passphrase | string | 是 | 否 | 群组密钥。 | 1769| groupName | string | 是 | 否 | 群组名称。 | 1770| goBand | [GroupOwnerBand](#groupownerband9) | 是 | 否 | 群组带宽。 | 1771 1772 1773## GroupOwnerBand<sup>9+</sup> 1774 1775表示群组带宽的枚举。 1776 1777**系统能力:** SystemCapability.Communication.WiFi.P2P 1778 1779| 名称 | 值 | 说明 | 1780| -------- | -------- | -------- | 1781| GO_BAND_AUTO | 0 | 自动模式。 | 1782| GO_BAND_2GHZ | 1 | 2.4GHZ。 | 1783| GO_BAND_5GHZ | 2 | 5GHZ。 | 1784 1785 1786## wifiManager.removeGroup<sup>9+</sup> 1787 1788removeGroup(): void 1789 1790移除群组。 1791 1792**需要权限:** ohos.permission.GET_WIFI_INFO 1793 1794**系统能力:** SystemCapability.Communication.WiFi.P2P 1795 1796**错误码:** 1797 1798以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1799 1800| **错误码ID** | **错误信息** | 1801| -------- | -------- | 1802| 201 | Permission denied. | 1803| 801 | Capability not supported. | 1804| 2801000 | Operation failed. | 1805| 2801001 | Wi-Fi STA disabled. | 1806 1807**示例:** 1808```ts 1809 import { wifiManager } from '@kit.ConnectivityKit'; 1810 1811 try { 1812 wifiManager.removeGroup(); 1813 }catch(error){ 1814 console.error("failed:" + JSON.stringify(error)); 1815 } 1816``` 1817 1818## wifiManager.p2pConnect<sup>9+</sup> 1819 1820p2pConnect(config: WifiP2PConfig): void 1821 1822执行P2P连接。 1823 1824**需要权限:** 1825 1826API 10起:ohos.permission.GET_WIFI_INFO 1827 1828**系统能力:** SystemCapability.Communication.WiFi.P2P 1829 1830**参数:** 1831 1832| **参数名** | **类型** | 必填 | **说明** | 1833| -------- | -------- | -------- | -------- | 1834| config | [WifiP2PConfig](#wifip2pconfig9) | 是 | 连接配置信息。如果DeviceAddressType未指定值,则DeviceAddressType默认为随机设备地址类型。 | 1835 1836**错误码:** 1837 1838以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1839 1840| **错误码ID** | **错误信息** | 1841| -------- | -------- | 1842| 201 | Permission denied. | 1843| 401 | Invalid parameters. Possible causes: 1. Incorrect parameter types.<br>2. Parameter verification failed. | 1844| 801 | Capability not supported. | 1845| 2801000 | Operation failed. | 1846| 2801001 | Wi-Fi STA disabled. | 1847 1848**示例:** 1849```ts 1850 import { wifiManager } from '@kit.ConnectivityKit'; 1851 1852 let recvP2pConnectionChangeFunc = (result:wifiManager.WifiP2pLinkedInfo) => { 1853 console.info("p2p connection change receive event: " + JSON.stringify(result)); 1854 wifiManager.getP2pLinkedInfo((err, data:wifiManager.WifiP2pLinkedInfo) => { 1855 if (err) { 1856 console.error('failed to get getP2pLinkedInfo: ' + JSON.stringify(err)); 1857 return; 1858 } 1859 console.info("get getP2pLinkedInfo: " + JSON.stringify(data)); 1860 }); 1861 } 1862 wifiManager.on("p2pConnectionChange", recvP2pConnectionChangeFunc); 1863 1864 let recvP2pDeviceChangeFunc = (result:wifiManager.WifiP2pDevice) => { 1865 console.info("p2p device change receive event: " + JSON.stringify(result)); 1866 } 1867 wifiManager.on("p2pDeviceChange", recvP2pDeviceChangeFunc); 1868 1869 let recvP2pPeerDeviceChangeFunc = (result:wifiManager.WifiP2pDevice[]) => { 1870 console.info("p2p peer device change receive event: " + JSON.stringify(result)); 1871 wifiManager.getP2pPeerDevices((err, data:wifiManager.WifiP2pDevice) => { 1872 if (err) { 1873 console.error('failed to get peer devices: ' + JSON.stringify(err)); 1874 return; 1875 } 1876 console.info("get peer devices: " + JSON.stringify(data)); 1877 let len = data.length; 1878 for (let i = 0; i < len; ++i) { 1879 if (data[i].deviceName === "my_test_device") { 1880 console.info("p2p connect to test device: " + data[i].deviceAddress); 1881 let config:wifiManager.WifiP2PConfig = { 1882 deviceAddress:data[i].deviceAddress, 1883 netId:-2, 1884 passphrase:"", 1885 groupName:"", 1886 goBand:0, 1887 } 1888 wifiManager.p2pConnect(config); 1889 } 1890 } 1891 }); 1892 } 1893 wifiManager.on("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc); 1894 1895 let recvP2pPersistentGroupChangeFunc = () => { 1896 console.info("p2p persistent group change receive event"); 1897 1898 wifiManager.getCurrentGroup((err, data:wifiManager.WifiP2pGroupInfo) => { 1899 if (err) { 1900 console.error('failed to get current group: ' + JSON.stringify(err)); 1901 return; 1902 } 1903 console.info("get current group: " + JSON.stringify(data)); 1904 }); 1905 } 1906 wifiManager.on("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc); 1907 1908 setTimeout(() => {wifiManager.off("p2pConnectionChange", recvP2pConnectionChangeFunc);}, 125 * 1000); 1909 setTimeout(() => {wifiManager.off("p2pDeviceChange", recvP2pDeviceChangeFunc);}, 125 * 1000); 1910 setTimeout(() => {wifiManager.off("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc);}, 125 * 1000); 1911 setTimeout(() => {wifiManager.off("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc);}, 125 * 1000); 1912 console.info("start discover devices -> " + wifiManager.startDiscoverDevices()); 1913``` 1914 1915## wifiManager.p2pCancelConnect<sup>9+</sup> 1916 1917p2pCancelConnect(): void 1918 1919在P2P连接过程中,取消P2P连接。 1920 1921**需要权限:** ohos.permission.GET_WIFI_INFO 1922 1923**系统能力:** SystemCapability.Communication.WiFi.P2P 1924 1925**错误码:** 1926 1927以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1928 1929| **错误码ID** | **错误信息** | 1930| -------- | -------- | 1931| 201 | Permission denied. | 1932| 801 | Capability not supported. | 1933| 2801000 | Operation failed. | 1934| 2801001 | Wi-Fi STA disabled. | 1935 1936**示例:** 1937```ts 1938 import { wifiManager } from '@kit.ConnectivityKit'; 1939 1940 try { 1941 wifiManager.p2pCancelConnect(); 1942 }catch(error){ 1943 console.error("failed:" + JSON.stringify(error)); 1944 } 1945``` 1946 1947## wifiManager.startDiscoverDevices<sup>9+</sup> 1948 1949startDiscoverDevices(): void 1950 1951开始发现设备。 1952 1953**需要权限:** 1954 1955API 10起:ohos.permission.GET_WIFI_INFO 1956 1957**系统能力:** SystemCapability.Communication.WiFi.P2P 1958 1959**错误码:** 1960 1961以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1962 1963| **错误码ID** | **错误信息** | 1964| -------- | -------- | 1965| 201 | Permission denied. | 1966| 801 | Capability not supported. | 1967| 2801000 | Operation failed. | 1968| 2801001 | Wi-Fi STA disabled. | 1969 1970**示例:** 1971```ts 1972 import { wifiManager } from '@kit.ConnectivityKit'; 1973 1974 try { 1975 wifiManager.startDiscoverDevices(); 1976 }catch(error){ 1977 console.error("failed:" + JSON.stringify(error)); 1978 } 1979``` 1980 1981## wifiManager.stopDiscoverDevices<sup>9+</sup> 1982 1983stopDiscoverDevices(): void 1984 1985停止发现设备。 1986 1987**需要权限:** ohos.permission.GET_WIFI_INFO 1988 1989**系统能力:** SystemCapability.Communication.WiFi.P2P 1990 1991**错误码:** 1992 1993以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1994 1995| **错误码ID** | **错误信息** | 1996| -------- | -------- | 1997| 201 | Permission denied. | 1998| 801 | Capability not supported. | 1999| 2801000 | Operation failed. | 2000| 2801001 | Wi-Fi STA disabled. | 2001 2002**示例:** 2003```ts 2004 import { wifiManager } from '@kit.ConnectivityKit'; 2005 2006 try { 2007 wifiManager.stopDiscoverDevices(); 2008 }catch(error){ 2009 console.error("failed:" + JSON.stringify(error)); 2010 } 2011``` 2012 2013 2014 2015## WifiP2pGroupInfo<sup>9+</sup> 2016 2017表示P2P群组相关信息。 2018 2019**系统能力:** SystemCapability.Communication.WiFi.P2P 2020 2021| 名称 | 类型 | 可读 | 可写 | 说明 | 2022| -------- | -------- | -------- | -------- | -------- | 2023| isP2pGo | boolean | 是 | 否 | 是否是群主。 | 2024| ownerInfo | [WifiP2pDevice](#wifip2pdevice9) | 是 | 否 | 群组的设备信息。 | 2025| passphrase | string | 是 | 否 | 群组密钥。 | 2026| interface | string | 是 | 否 | 接口名称。 | 2027| groupName | string | 是 | 否 | 群组名称。 | 2028| networkId | number | 是 | 否 | 网络ID。 | 2029| frequency | number | 是 | 否 | 群组的频率。 | 2030| clientDevices | [WifiP2pDevice[]](#wifip2pdevice9) | 是 | 否 | 接入的设备列表信息。 | 2031| goIpAddress | string | 是 | 否 | 群组IP地址。 | 2032 2033 2034## wifiManager.on('wifiStateChange')<sup>9+</sup> 2035 2036on(type: 'wifiStateChange', callback: Callback<number>): void 2037 2038注册WLAN状态改变事件,在业务退出时,要调用off(type: 'wifiStateChange', callback?: Callback<number>)接口去掉之前的注册回调。 2039 2040**需要权限:** ohos.permission.GET_WIFI_INFO 2041 2042**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2043 2044**系统能力:** SystemCapability.Communication.WiFi.STA 2045 2046**参数:** 2047 2048 | **参数名** | **类型** | **必填** | **说明** | 2049 | -------- | -------- | -------- | -------- | 2050 | type | string | 是 | 固定填"wifiStateChange"字符串。 | 2051 | callback | Callback<number> | 是 | 状态改变回调函数。 | 2052 2053**错误码:** 2054 2055以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2056 2057| **错误码ID** | **错误信息** | 2058| -------- | ---------------------------- | 2059| 201 | Permission denied. | 2060| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2061| 801 | Capability not supported. | 2062| 2501000 | Operation failed.| 2063 2064**状态改变事件的枚举:** 2065 2066| **枚举值** | **说明** | 2067| -------- | -------- | 2068| 0 | 未激活。 | 2069| 1 | 已激活。 | 2070| 2 | 激活中。 | 2071| 3 | 去激活中。 | 2072 2073 2074## wifiManager.off('wifiStateChange')<sup>9+</sup> 2075 2076off(type: 'wifiStateChange', callback?: Callback<number>): void 2077 2078取消注册WLAN状态改变事件。 2079 2080**需要权限:** ohos.permission.GET_WIFI_INFO 2081 2082**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2083 2084**系统能力:** SystemCapability.Communication.WiFi.STA 2085 2086**参数:** 2087 2088 | **参数名** | **类型** | **必填** | **说明** | 2089 | -------- | -------- | -------- | -------- | 2090 | type | string | 是 | 固定填"wifiStateChange"字符串。 | 2091 | callback | Callback<number> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 2092 2093**错误码:** 2094 2095以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2096 2097| **错误码ID** | **错误信息** | 2098| -------- | ---------------------------- | 2099| 201 | Permission denied. | 2100| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2101| 801 | Capability not supported. | 2102| 2501000 | Operation failed.| 2103 2104**示例:** 2105```ts 2106 import { wifiManager } from '@kit.ConnectivityKit'; 2107 2108 let recvPowerNotifyFunc = (result:number) => { 2109 console.info("Receive power state change event: " + result); 2110 } 2111 2112 // Register event 2113 wifiManager.on("wifiStateChange", recvPowerNotifyFunc); 2114 2115 // Unregister event 2116 wifiManager.off("wifiStateChange", recvPowerNotifyFunc); 2117``` 2118 2119 2120## wifiManager.on('wifiConnectionChange')<sup>9+</sup> 2121 2122on(type: 'wifiConnectionChange', callback: Callback<number>): void 2123 2124注册WLAN连接状态改变事件,在业务退出时,要调用off(type: 'wifiConnectionChange', callback?: Callback<number>)接口去掉之前的注册回调。 2125 2126**需要权限:** ohos.permission.GET_WIFI_INFO 2127 2128**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2129 2130**系统能力:** SystemCapability.Communication.WiFi.STA 2131 2132**参数:** 2133 2134 | **参数名** | **类型** | **必填** | **说明** | 2135 | -------- | -------- | -------- | -------- | 2136 | type | string | 是 | 固定填"wifiConnectionChange"字符串。 | 2137 | callback | Callback<number> | 是 | 状态改变回调函数。 | 2138 2139**连接状态改变事件的枚举:** 2140 2141| **枚举值** | **说明** | 2142| -------- | -------- | 2143| 0 | 已断开。 | 2144| 1 | 已连接。 | 2145 2146**错误码:** 2147 2148以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2149 2150| **错误码ID** | **错误信息** | 2151| -------- | ---------------------------- | 2152| 201 | Permission denied. | 2153| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2154| 801 | Capability not supported. | 2155| 2501000 | Operation failed.| 2156 2157## wifiManager.off('wifiConnectionChange')<sup>9+</sup> 2158 2159off(type: 'wifiConnectionChange', callback?: Callback<number>): void 2160 2161取消注册WLAN连接状态改变事件。 2162 2163**需要权限:** ohos.permission.GET_WIFI_INFO 2164 2165**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2166 2167**系统能力:** SystemCapability.Communication.WiFi.STA 2168 2169**参数:** 2170 2171 | **参数名** | **类型** | **必填** | **说明** | 2172 | -------- | -------- | -------- | -------- | 2173 | type | string | 是 | 固定填"wifiConnectionChange"字符串。 | 2174 | callback | Callback<number> | 否 | 连接状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 2175 2176**错误码:** 2177 2178以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2179 2180| **错误码ID** | **错误信息** | 2181| -------- | ---------------------------- | 2182| 201 | Permission denied. | 2183| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2184| 801 | Capability not supported. | 2185| 2501000 | Operation failed.| 2186 2187**示例:** 2188```ts 2189 import { wifiManager } from '@kit.ConnectivityKit'; 2190 2191 let recvWifiConnectionChangeFunc = (result:number) => { 2192 console.info("Receive wifi connection change event: " + result); 2193 } 2194 2195 // Register event 2196 wifiManager.on("wifiConnectionChange", recvWifiConnectionChangeFunc); 2197 2198 // Unregister event 2199 wifiManager.off("wifiConnectionChange", recvWifiConnectionChangeFunc); 2200``` 2201 2202## wifiManager.on('wifiScanStateChange')<sup>9+</sup> 2203 2204on(type: 'wifiScanStateChange', callback: Callback<number>): void 2205 2206注册扫描状态改变事件,在业务退出时,要调用off(type: 'wifiScanStateChange', callback?: Callback<number>)接口去掉之前的注册回调。 2207 2208**需要权限:** ohos.permission.GET_WIFI_INFO 2209 2210**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2211 2212**系统能力:** SystemCapability.Communication.WiFi.STA 2213 2214**参数:** 2215 2216 | **参数名** | **类型** | **必填** | **说明** | 2217 | -------- | -------- | -------- | -------- | 2218 | type | string | 是 | 固定填"wifiScanStateChange"字符串。 | 2219 | callback | Callback<number> | 是 | 状态改变回调函数。 | 2220 2221**扫描状态改变事件的枚举:** 2222 2223| **枚举值** | **说明** | 2224| -------- | -------- | 2225| 0 | 扫描失败。 | 2226| 1 | 扫描成功。 | 2227 2228**错误码:** 2229 2230以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2231 2232| **错误码ID** | **错误信息** | 2233| -------- | ---------------------------- | 2234| 201 | Permission denied. | 2235| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2236| 801 | Capability not supported. | 2237| 2501000 | Operation failed.| 2238 2239## wifiManager.off('wifiScanStateChange')<sup>9+</sup> 2240 2241off(type: 'wifiScanStateChange', callback?: Callback<number>): void 2242 2243取消注册扫描状态改变事件。 2244 2245**需要权限:** ohos.permission.GET_WIFI_INFO 2246 2247**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2248 2249**系统能力:** SystemCapability.Communication.WiFi.STA 2250 2251**参数:** 2252 2253| **参数名** | **类型** | **必填** | **说明** | 2254| -------- | -------- | -------- | -------- | 2255| type | string | 是 | 固定填"wifiScanStateChange"字符串。 | 2256| callback | Callback<number> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 2257 2258**错误码:** 2259 2260以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2261 2262| **错误码ID** | **错误信息** | 2263| -------- | ---------------------------- | 2264| 201 | Permission denied. | 2265| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2266| 801 | Capability not supported. | 2267| 2501000 | Operation failed.| 2268 2269**示例:** 2270```ts 2271 import { wifiManager } from '@kit.ConnectivityKit'; 2272 2273 let recvWifiScanStateChangeFunc = (result:number) => { 2274 console.info("Receive Wifi scan state change event: " + result); 2275 } 2276 2277 // Register event 2278 wifiManager.on("wifiScanStateChange", recvWifiScanStateChangeFunc); 2279 2280 // Unregister event 2281 wifiManager.off("wifiScanStateChange", recvWifiScanStateChangeFunc); 2282``` 2283 2284## wifiManager.on('wifiRssiChange')<sup>9+</sup> 2285 2286on(type: 'wifiRssiChange', callback: Callback<number>): void 2287 2288注册RSSI状态改变事件,在业务退出时,要调用off(type: 'wifiRssiChange', callback?: Callback<number>)接口去掉之前的注册回调。 2289 2290**需要权限:** ohos.permission.GET_WIFI_INFO 2291 2292**系统能力:** SystemCapability.Communication.WiFi.STA 2293 2294**参数:** 2295 2296 | **参数名** | **类型** | **必填** | **说明** | 2297 | -------- | -------- | -------- | -------- | 2298 | type | string | 是 | 固定填"wifiRssiChange"字符串。 | 2299 | callback | Callback<number> | 是 | 状态改变回调函数,返回以dBm为单位的RSSI值。 | 2300 2301**错误码:** 2302 2303以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2304 2305| **错误码ID** | **错误信息** | 2306| -------- | ---------------------------- | 2307| 201 | Permission denied. | 2308| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2309| 801 | Capability not supported. | 2310| 2501000 | Operation failed.| 2311 2312## wifiManager.off('wifiRssiChange')<sup>9+</sup> 2313 2314off(type: 'wifiRssiChange', callback?: Callback<number>): void 2315 2316取消注册RSSI状态改变事件。 2317 2318**需要权限:** ohos.permission.GET_WIFI_INFO 2319 2320**系统能力:** SystemCapability.Communication.WiFi.STA 2321 2322**参数:** 2323 2324| **参数名** | **类型** | **必填** | **说明** | 2325| -------- | -------- | -------- | -------- | 2326| type | string | 是 | 固定填"wifiRssiChange"字符串。 | 2327| callback | Callback<number> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 2328 2329**错误码:** 2330 2331以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2332 2333| **错误码ID** | **错误信息** | 2334| -------- | ---------------------------- | 2335| 201 | Permission denied. | 2336| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2337| 801 | Capability not supported. | 2338| 2501000 | Operation failed.| 2339 2340**示例:** 2341```ts 2342 import { wifiManager } from '@kit.ConnectivityKit'; 2343 2344 let recvWifiRssiChangeFunc = (result:number) => { 2345 console.info("Receive wifi rssi change event: " + result); 2346 } 2347 2348 // Register event 2349 wifiManager.on("wifiRssiChange", recvWifiRssiChangeFunc); 2350 2351 // Unregister event 2352 wifiManager.off("wifiRssiChange", recvWifiRssiChangeFunc); 2353``` 2354 2355## wifiManager.on('hotspotStateChange')<sup>9+</sup> 2356 2357on(type: 'hotspotStateChange', callback: Callback<number>): void 2358 2359注册热点状态改变事件,在业务退出时,要调用off(type: 'hotspotStateChange', callback?: Callback<number>)接口去掉之前的注册回调。 2360 2361**需要权限:** ohos.permission.GET_WIFI_INFO 2362 2363**系统能力:** SystemCapability.Communication.WiFi.AP.Core 2364 2365**参数:** 2366 2367| **参数名** | **类型** | **必填** | **说明** | 2368| -------- | -------- | -------- | -------- | 2369| type | string | 是 | 固定填"hotspotStateChange"字符串。 | 2370| callback | Callback<number> | 是 | 状态改变回调函数。 | 2371 2372**热点状态改变事件的枚举:** 2373 2374| **枚举值** | **说明** | 2375| -------- | -------- | 2376| 0 | 未激活。 | 2377| 1 | 已激活。 | 2378| 2 | 激活中。 | 2379| 3 | 去激活中。 | 2380 2381**错误码:** 2382 2383以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2384 2385| **错误码ID** | **错误信息** | 2386| -------- | ---------------------------- | 2387| 201 | Permission denied. | 2388| 202 | System API is not allowed called by Non-system application. | 2389| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2390| 801 | Capability not supported. | 2391| 2601000 | Operation failed. | 2392 2393## wifiManager.off('hotspotStateChange')<sup>9+</sup> 2394 2395off(type: 'hotspotStateChange', callback?: Callback<number>): void 2396 2397取消注册热点状态改变事件。 2398 2399**需要权限:** ohos.permission.GET_WIFI_INFO 2400 2401**系统能力:** SystemCapability.Communication.WiFi.AP.Core 2402 2403**参数:** 2404 2405| **参数名** | **类型** | **必填** | **说明** | 2406| -------- | -------- | -------- | -------- | 2407| type | string | 是 | 固定填"hotspotStateChange"字符串。 | 2408| callback | Callback<number> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 2409 2410**错误码:** 2411 2412以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2413 2414| **错误码ID** | **错误信息** | 2415| -------- | ---------------------------- | 2416| 201 | Permission denied. | 2417| 202 | System API is not allowed called by Non-system application. | 2418| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2419| 801 | Capability not supported. | 2420| 2601000 | Operation failed. | 2421 2422**示例:** 2423```ts 2424 import { wifiManager } from '@kit.ConnectivityKit'; 2425 2426 let recvHotspotStateChangeFunc = (result:number) => { 2427 console.info("Receive hotspot state change event: " + result); 2428 } 2429 2430 // Register event 2431 wifiManager.on("hotspotStateChange", recvHotspotStateChangeFunc); 2432 2433 // Unregister event 2434 wifiManager.off("hotspotStateChange", recvHotspotStateChangeFunc); 2435``` 2436 2437 2438## wifiManager.on('p2pStateChange')<sup>9+</sup> 2439 2440on(type: 'p2pStateChange', callback: Callback<number>): void 2441 2442注册P2P开关状态改变事件,在业务退出时,要调用off(type: 'p2pStateChange', callback?: Callback<number>)接口去掉之前的注册回调。 2443 2444**需要权限:** ohos.permission.GET_WIFI_INFO 2445 2446**系统能力:** SystemCapability.Communication.WiFi.P2P 2447 2448**参数:** 2449 2450| **参数名** | **类型** | **必填** | **说明** | 2451| -------- | -------- | -------- | -------- | 2452| type | string | 是 | 固定填"p2pStateChange"字符串。 | 2453| callback | Callback<number> | 是 | 状态改变回调函数。 | 2454 2455** P2P状态改变事件的枚举:** 2456 2457| **枚举值** | **说明** | 2458| -------- | -------- | 2459| 1 | 空闲。 | 2460| 2 | 打开中。 | 2461| 3 | 已打开。 | 2462| 4 | 关闭中。 | 2463| 5 | 已关闭。 | 2464 2465**错误码:** 2466 2467以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2468 2469| **错误码ID** | **错误信息** | 2470| -------- | ---------------------------- | 2471| 201 | Permission denied. | 2472| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2473| 801 | Capability not supported. | 2474| 2801000 | Operation failed. | 2475 2476## wifiManager.off('p2pStateChange')<sup>9+</sup> 2477 2478off(type: 'p2pStateChange', callback?: Callback<number>): void 2479 2480取消注册P2P开关状态改变事件。 2481 2482**需要权限:** ohos.permission.GET_WIFI_INFO 2483 2484**系统能力:** SystemCapability.Communication.WiFi.P2P 2485 2486**参数:** 2487 2488 | **参数名** | **类型** | **必填** | **说明** | 2489 | -------- | -------- | -------- | -------- | 2490 | type | string | 是 | 固定填"p2pStateChange"字符串。 | 2491 | callback | Callback<number> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 2492 2493**错误码:** 2494 2495以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2496 2497| **错误码ID** | **错误信息** | 2498| -------- | ---------------------------- | 2499| 201 | Permission denied. | 2500| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2501| 801 | Capability not supported. | 2502| 2801000 | Operation failed. | 2503 2504**示例:** 2505```ts 2506 import { wifiManager } from '@kit.ConnectivityKit'; 2507 2508 let recvP2pStateChangeFunc = (result:number) => { 2509 console.info("Receive p2p state change event: " + result); 2510 } 2511 2512 // Register event 2513 wifiManager.on("p2pStateChange", recvP2pStateChangeFunc); 2514 2515 // Unregister event 2516 wifiManager.off("p2pStateChange", recvP2pStateChangeFunc); 2517``` 2518 2519## wifiManager.on('p2pConnectionChange')<sup>9+</sup> 2520 2521on(type: 'p2pConnectionChange', callback: Callback<WifiP2pLinkedInfo>): void 2522 2523注册P2P连接状态改变事件,在业务退出时,要调用off(type: 'p2pConnectionChange', callback?: Callback<WifiP2pLinkedInfo>)接口去掉之前的注册回调。 2524 2525**需要权限:** ohos.permission.GET_WIFI_INFO 2526 2527**系统能力:** SystemCapability.Communication.WiFi.P2P 2528 2529**参数:** 2530 2531 | **参数名** | **类型** | **必填** | **说明** | 2532 | -------- | -------- | -------- | -------- | 2533 | type | string | 是 | 固定填"p2pConnectionChange"字符串。 | 2534 | callback | Callback<[WifiP2pLinkedInfo](#wifip2plinkedinfo9)> | 是 | 状态改变回调函数。 | 2535 2536**错误码:** 2537 2538以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2539 2540| **错误码ID** | **错误信息** | 2541| -------- | ---------------------------- | 2542| 201 | Permission denied. | 2543| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2544| 801 | Capability not supported. | 2545| 2801000 | Operation failed. | 2546 2547## wifiManager.off('p2pConnectionChange')<sup>9+</sup> 2548 2549off(type: 'p2pConnectionChange', callback?: Callback<WifiP2pLinkedInfo>): void 2550 2551取消注册P2P连接状态改变事件。 2552 2553**需要权限:** ohos.permission.GET_WIFI_INFO 2554 2555**系统能力:** SystemCapability.Communication.WiFi.P2P 2556 2557**参数:** 2558 2559 | **参数名** | **类型** | **必填** | **说明** | 2560 | -------- | -------- | -------- | -------- | 2561 | type | string | 是 | 固定填"p2pConnectionChange"字符串。 | 2562 | callback | Callback<[WifiP2pLinkedInfo](#wifip2plinkedinfo9)> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 2563 2564**错误码:** 2565 2566以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2567 2568| **错误码ID** | **错误信息** | 2569| -------- | ---------------------------- | 2570| 201 | Permission denied. | 2571| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2572| 801 | Capability not supported. | 2573| 2801000 | Operation failed. | 2574 2575**示例:** 2576```ts 2577 import { wifiManager } from '@kit.ConnectivityKit'; 2578 2579 let recvP2pConnectionChangeFunc = (result:wifiManager.WifiP2pLinkedInfo) => { 2580 console.info("Receive p2p connection change event: " + result); 2581 } 2582 2583 // Register event 2584 wifiManager.on("p2pConnectionChange", recvP2pConnectionChangeFunc); 2585 2586 // Unregister event 2587 wifiManager.off("p2pConnectionChange", recvP2pConnectionChangeFunc); 2588``` 2589 2590## wifiManager.on('p2pDeviceChange')<sup>9+</sup> 2591 2592on(type: 'p2pDeviceChange', callback: Callback<WifiP2pDevice>): void 2593 2594注册P2P设备状态改变事件,在业务退出时,要调用off(type: 'p2pDeviceChange', callback?: Callback<WifiP2pDevice>)接口去掉之前的注册回调。 2595 2596**需要权限:** 2597 2598API 10起:ohos.permission.GET_WIFI_INFO 2599 2600**系统能力:** SystemCapability.Communication.WiFi.P2P 2601 2602**参数:** 2603 2604 | **参数名** | **类型** | **必填** | **说明** | 2605 | -------- | -------- | -------- | -------- | 2606 | type | string | 是 | 固定填"p2pDeviceChange"字符串。 | 2607 | callback | Callback<[WifiP2pDevice](#wifip2pdevice9)> | 是 | 状态改变回调函数。 | 2608 2609**错误码:** 2610 2611以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2612 2613| **错误码ID** | **错误信息** | 2614| -------- | ---------------------------- | 2615| 201 | Permission denied. | 2616| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2617| 801 | Capability not supported. | 2618| 2801000 | Operation failed. | 2619 2620## wifiManager.off('p2pDeviceChange')<sup>9+</sup> 2621 2622off(type: 'p2pDeviceChange', callback?: Callback<WifiP2pDevice>): void 2623 2624取消注册P2P设备状态改变事件。 2625 2626**系统能力:** SystemCapability.Communication.WiFi.P2P 2627 2628**参数:** 2629 2630 | **参数名** | **类型** | **必填** | **说明** | 2631 | -------- | -------- | -------- | -------- | 2632 | type | string | 是 | 固定填"p2pDeviceChange"字符串。 | 2633 | callback | Callback<[WifiP2pDevice](#wifip2pdevice9)> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 2634 2635**错误码:** 2636 2637以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2638 2639| **错误码ID** | **错误信息** | 2640| -------- | ---------------------------- | 2641| 201<sup>10+</sup> | Permission denied. | 2642| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2643| 801 | Capability not supported. | 2644| 2801000 | Operation failed. | 2645 2646**示例:** 2647```ts 2648 import { wifiManager } from '@kit.ConnectivityKit'; 2649 2650 let recvP2pDeviceChangeFunc = (result:wifiManager.WifiP2pDevice) => { 2651 console.info("Receive p2p device change event: " + result); 2652 } 2653 2654 // Register event 2655 wifiManager.on("p2pDeviceChange", recvP2pDeviceChangeFunc); 2656 2657 // Unregister event 2658 wifiManager.off("p2pDeviceChange", recvP2pDeviceChangeFunc); 2659``` 2660 2661## wifiManager.on('p2pPeerDeviceChange')<sup>9+</sup> 2662 2663on(type: 'p2pPeerDeviceChange', callback: Callback<WifiP2pDevice[]>): void 2664 2665注册P2P对端设备状态改变事件,在业务退出时,要调用off(type: 'p2pPeerDeviceChange', callback?: Callback<WifiP2pDevice[]>)接口去掉之前的注册回调。 2666 2667**需要权限:** 2668 2669API 10起:ohos.permission.GET_WIFI_INFO 2670 2671**系统能力:** SystemCapability.Communication.WiFi.P2P 2672 2673**参数:** 2674 2675| **参数名** | **类型** | **必填** | **说明** | 2676| -------- | -------- | -------- | -------- | 2677| type | string | 是 | 固定填"p2pPeerDeviceChange"字符串。 | 2678| callback | Callback<[WifiP2pDevice[]](#wifip2pdevice9)> | 是 | 状态改变回调函数。如果应用申请了ohos.permission.GET_WIFI_PEERS_MAC权限(仅系统应用可申请),则返回结果中的deviceAddress为真实设备地址,否则为随机设备地址。 | 2679 2680**错误码:** 2681 2682以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2683 2684| **错误码ID** | **错误信息** | 2685| -------- | ---------------------------- | 2686| 201 | Permission denied. | 2687| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2688| 801 | Capability not supported. | 2689| 2801000 | Operation failed. | 2690 2691## wifiManager.off('p2pPeerDeviceChange')<sup>9+</sup> 2692 2693off(type: 'p2pPeerDeviceChange', callback?: Callback<WifiP2pDevice[]>): void 2694 2695取消注册P2P对端设备状态改变事件。 2696 2697**系统能力:** SystemCapability.Communication.WiFi.P2P 2698 2699**参数:** 2700 2701| **参数名** | **类型** | **必填** | **说明** | 2702| -------- | -------- | -------- | -------- | 2703| type | string | 是 | 固定填"p2pPeerDeviceChange"字符串。 | 2704| callback | Callback<[WifiP2pDevice[]](#wifip2pdevice9)> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。如果应用申请了ohos.permission.GET_WIFI_PEERS_MAC权限(仅系统应用可申请),则返回结果中的deviceAddress为真实设备地址,否则为随机设备地址。 | 2705 2706**错误码:** 2707 2708以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2709 2710| **错误码ID** | **错误信息** | 2711| -------- | ---------------------------- | 2712| 201<sup>10+</sup> | Permission denied. | 2713| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2714| 801 | Capability not supported. | 2715| 2801000 | Operation failed. | 2716 2717**示例:** 2718```ts 2719 import { wifiManager } from '@kit.ConnectivityKit'; 2720 2721 let recvP2pPeerDeviceChangeFunc = (result:wifiManager.WifiP2pDevice[]) => { 2722 console.info("Receive p2p peer device change event: " + result); 2723 } 2724 2725 // Register event 2726 wifiManager.on("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc); 2727 2728 // Unregister event 2729 wifiManager.off("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc); 2730``` 2731 2732## wifiManager.on('p2pPersistentGroupChange')<sup>9+</sup> 2733 2734on(type: 'p2pPersistentGroupChange', callback: Callback<void>): void 2735 2736注册P2P永久组状态改变事件,在业务退出时,要调用off(type: 'p2pPersistentGroupChange', callback?: Callback<void>)接口去掉之前的注册回调。 2737 2738**需要权限:** ohos.permission.GET_WIFI_INFO 2739 2740**系统能力:** SystemCapability.Communication.WiFi.P2P 2741 2742**参数:** 2743 2744 | **参数名** | **类型** | **必填** | **说明** | 2745 | -------- | -------- | -------- | -------- | 2746 | type | string | 是 | 固定填"p2pPersistentGroupChange"字符串。 | 2747 | callback | Callback<void> | 是 | 状态改变回调函数。 | 2748 2749**错误码:** 2750 2751以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2752 2753| **错误码ID** | **错误信息** | 2754| -------- | ---------------------------- | 2755| 201 | Permission denied. | 2756| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2757| 801 | Capability not supported. | 2758| 2801000 | Operation failed. | 2759 2760## wifiManager.off('p2pPersistentGroupChange')<sup>9+</sup> 2761 2762off(type: 'p2pPersistentGroupChange', callback?: Callback<void>): void 2763 2764取消注册P2P永久组状态改变事件。 2765 2766**需要权限:** ohos.permission.GET_WIFI_INFO 2767 2768**系统能力:** SystemCapability.Communication.WiFi.P2P 2769 2770**参数:** 2771 2772| **参数名** | **类型** | **必填** | **说明** | 2773| -------- | -------- | -------- | -------- | 2774| type | string | 是 | 固定填"p2pPersistentGroupChange"字符串。 | 2775| callback | Callback<void> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 2776 2777**错误码:** 2778 2779以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2780 2781| **错误码ID** | **错误信息** | 2782| -------- | ---------------------------- | 2783| 201 | Permission denied. | 2784| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2785| 801 | Capability not supported. | 2786| 2801000 | Operation failed. | 2787 2788**示例:** 2789```ts 2790 import { wifiManager } from '@kit.ConnectivityKit'; 2791 2792 let recvP2pPersistentGroupChangeFunc = (result:void) => { 2793 console.info("Receive p2p persistent group change event: " + result); 2794 } 2795 2796 // Register event 2797 wifiManager.on("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc); 2798 2799 // Unregister event 2800 wifiManager.off("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc); 2801``` 2802 2803## wifiManager.on('p2pDiscoveryChange')<sup>9+</sup> 2804 2805on(type: 'p2pDiscoveryChange', callback: Callback<number>): void 2806 2807注册发现设备状态改变事件,在业务退出时,要调用off(type: 'p2pDiscoveryChange', callback?: Callback<number>)接口去掉之前的注册回调。 2808 2809**需要权限:** ohos.permission.GET_WIFI_INFO 2810 2811**系统能力:** SystemCapability.Communication.WiFi.P2P 2812 2813**参数:** 2814 2815 | **参数名** | **类型** | **必填** | **说明** | 2816 | -------- | -------- | -------- | -------- | 2817 | type | string | 是 | 固定填"p2pDiscoveryChange"字符串。 | 2818 | callback | Callback<number> | 是 | 状态改变回调函数。 | 2819 2820**发现设备状态改变事件的枚举:** 2821 2822| **枚举值** | **说明** | 2823| -------- | -------- | 2824| 0 | 初始状态。 | 2825| 1 | 发现成功。 | 2826 2827**错误码:** 2828 2829以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2830 2831| **错误码ID** | **错误信息** | 2832| -------- | ---------------------------- | 2833| 201 | Permission denied. | 2834| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2835| 801 | Capability not supported. | 2836| 2801000 | Operation failed. | 2837 2838## wifiManager.off('p2pDiscoveryChange')<sup>9+</sup> 2839 2840off(type: 'p2pDiscoveryChange', callback?: Callback<number>): void 2841 2842取消注册发现设备状态改变事件。 2843 2844**需要权限:** ohos.permission.GET_WIFI_INFO 2845 2846**系统能力:** SystemCapability.Communication.WiFi.P2P 2847 2848**参数:** 2849 2850 | **参数名** | **类型** | **必填** | **说明** | 2851 | -------- | -------- | -------- | -------- | 2852 | type | string | 是 | 固定填"p2pDiscoveryChange"字符串。 | 2853 | callback | Callback<number> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 2854 2855**错误码:** 2856 2857以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2858 2859| **错误码ID** | **错误信息** | 2860| -------- | ---------------------------- | 2861| 201 | Permission denied. | 2862| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2863| 801 | Capability not supported. | 2864| 2801000 | Operation failed. | 2865 2866**示例:** 2867```ts 2868 import { wifiManager } from '@kit.ConnectivityKit'; 2869 2870 let recvP2pDiscoveryChangeFunc = (result:number) => { 2871 console.info("Receive p2p discovery change event: " + result); 2872 } 2873 2874 // Register event 2875 wifiManager.on("p2pDiscoveryChange", recvP2pDiscoveryChangeFunc); 2876 2877 // Unregister event 2878 wifiManager.off("p2pDiscoveryChange", recvP2pDiscoveryChangeFunc); 2879```