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