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 | Operation failed because the service is being closed.| 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 | 是 | 否 | 是否是隐藏网络。true:是隐藏网络,false:不是隐藏网络。 | 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时,该字段不能为空串,最大长度为128字节。 | 494| caCertAlias | string | 是 | 否 | CA 证书别名。 | 495| caPath | string | 是 | 否 | CA 证书路径。 | 496| clientCertAlias | string | 是 | 否 | 客户端证书别名。 | 497| certEntry | Uint8Array | 是 | 是 | CA 证书内容。当eapMethod为EAP_TLS时,如果该字段为空,则clientCertAlias不能为空。 | 498| certPassword | string | 是 | 是 | CA证书密码,最大长度为128字节。 | 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## wifiManager.getLinkedInfoSync<sup>18+</sup> 1227 1228getLinkedInfoSync(): WifiLinkedInfo; 1229 1230获取WLAN连接信息,使用同步方式返回结果。 1231 1232**需要权限:** ohos.permission.GET_WIFI_INFO 。 1233 1234当macType是1 - 设备MAC地址时,获取 macAddress 还需申请ohos.permission.GET_WIFI_LOCAL_MAC权限(该权限仅系统应用可申请),无该权限时,macAddress 返回随机MAC地址。 1235 1236**系统能力:** SystemCapability.Communication.WiFi.STA 1237 1238**返回值:** 1239 1240 | 类型 | 说明 | 1241 | -------- | -------- | 1242 | [WifiLinkedInfo](#wifilinkedinfo9) | 表示WLAN连接信息。 | 1243 1244**错误码:** 1245 1246以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1247 1248| **错误码ID** | **错误信息** | 1249| -------- | -------- | 1250| 201 | Permission denied. | 1251| 801 | Capability not supported. | 1252| 2501000 | Operation failed.| 1253| 2501001 | Wi-Fi STA disabled.| 1254 1255**示例:** 1256```ts 1257 import { wifiManager } from '@kit.ConnectivityKit'; 1258 try { 1259 let linkInfo = wifiManager.getLinkedInfoSync(); 1260 console.info("get linked info:" + JSON.stringify(linkInfo)); 1261 } catch(error) { 1262 console.error("get linked info failed:" + JSON.stringify(error)); 1263 } 1264``` 1265 1266## WifiLinkedInfo<sup>9+</sup> 1267 1268提供WLAN连接的相关信息。 1269 1270**系统能力:** SystemCapability.Communication.WiFi.STA 1271 1272| 名称 | 类型 | 可读 | 可写 | 说明 | 1273| -------- | -------- | -------- | -------- | -------- | 1274| ssid | string | 是 | 否 | 热点的SSID,编码格式为UTF-8。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 1275| bssid | string | 是 | 否 | 热点的BSSID。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 1276| rssi | number | 是 | 否 | 热点的信号强度(dBm)。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 1277| band | number | 是 | 否 | WLAN接入点的频段,1表示2.4GHZ;2表示5GHZ。 | 1278| linkSpeed | number | 是 | 否 | WLAN接入点的上行速度,单位Mbps/s。 | 1279| rxLinkSpeed<sup>10+</sup> | number | 是 | 否 | WLAN接入点的下行速度,单位Mbps/s。 | 1280| maxSupportedTxLinkSpeed<sup>10+</sup> | number | 是 | 否 | 当前支持的最大上行速率,单位Mbps/s。 | 1281| maxSupportedRxLinkSpeed<sup>10+</sup> | number | 是 | 否 | 当前支持的最大下行速率,单位Mbps/s。 | 1282| frequency | number | 是 | 否 | WLAN接入点的频率。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 1283| isHidden | boolean | 是 | 否 | WLAN接入点是否是隐藏网络, true:是隐藏网络,false:不是隐藏网络。 | 1284| isRestricted | boolean | 是 | 否 | WLAN接入点是否限制数据量,true: 限制,false:不限制。 | 1285| macType | number | 是 | 否 | MAC地址类型。0 - 随机MAC地址,1 - 设备MAC地址。 | 1286| macAddress | string | 是 | 否 | 设备的MAC地址。 | 1287| ipAddress | number | 是 | 否 | WLAN连接的IP地址(wifi连接信息和关于本机里的状态信息可以查看)。| 1288| connState | [ConnState](#connstate9) | 是 | 否 | WLAN连接状态。 | 1289| channelWidth<sup>10+</sup> | [WifiChannelWidth](#wifichannelwidth9) | 是 | 否 | 当前连接热点的信道带宽。 | 1290| wifiStandard<sup>10+</sup> | [WifiStandard](#wifistandard10) | 是 | 否 | 当前连接热点的Wi-Fi标准。 | 1291| supportedWifiCategory<sup>12+</sup> | [WifiCategory](#wificategory12) | 是 | 否 | 热点支持的最高Wi-Fi级别。 | 1292| isHiLinkNetwork<sup>12+</sup> | boolean | 是 | 否| 热点是否支持hilink,true:支持, false:不支持。 | 1293| wifiLinkType<sup>18+</sup> | [WifiLinkType](#wifilinktype18) | 是 | 否| Wi-Fi7连接类型。 | 1294 1295 1296## WifiLinkType<sup>18+</sup> 1297 1298枚举,Wi-Fi7连接类型。 1299 1300**系统能力:** SystemCapability.Communication.WiFi.STA 1301 1302| 名称 | 值 | 说明 | 1303| -------- | -------- | -------- | 1304| DEFAULT_LINK | 0 | 默认连接类型。 | 1305| WIFI7_SINGLE_LINK | 1 | Wi-Fi7单链连接。 | 1306| WIFI7_MLSR | 2 | Wi-Fi7 MLSR(multi-link single-radio,多链路多天线)连接。 | 1307| WIFI7_EMLSR | 3 | Wi-Fi7 EMLSR(enhanced multi-link single-radio, 增强型多链路单天线)连接。 | 1308| WIFI7_STR | 4 | Wi-Fi7 STR(Simultaneous Tx and Rx, 同时发送和接收)连接。 | 1309 1310## ConnState<sup>9+</sup> 1311 1312表示WLAN连接状态的枚举。 1313 1314**系统能力:** SystemCapability.Communication.WiFi.STA 1315 1316| 名称 | 值 | 说明 | 1317| -------- | -------- | -------- | 1318| SCANNING | 0 | 设备正在搜索可用的AP。 | 1319| CONNECTING | 1 | 正在建立WLAN连接。 | 1320| AUTHENTICATING | 2 | WLAN连接正在认证中。 | 1321| OBTAINING_IPADDR | 3 | 正在获取WLAN连接的IP地址。 | 1322| CONNECTED | 4 | WLAN连接已建立。 | 1323| DISCONNECTING | 5 | WLAN连接正在断开。 | 1324| DISCONNECTED | 6 | WLAN连接已断开。 | 1325| UNKNOWN | 7 | WLAN连接建立失败。 | 1326 1327 1328## wifiManager.isConnected<sup>9+</sup> 1329 1330isConnected(): boolean 1331 1332查询WLAN是否已连接。 1333 1334**需要权限:** ohos.permission.GET_WIFI_INFO 1335 1336**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1337 1338**系统能力:** SystemCapability.Communication.WiFi.STA 1339 1340**返回值:** 1341 1342 | **类型** | **说明** | 1343 | -------- | -------- | 1344 | boolean | true:已连接, false:未连接。 | 1345 1346**错误码:** 1347 1348以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1349 1350| **错误码ID** | **错误信息** | 1351| -------- | -------- | 1352| 201 | Permission denied. | 1353| 801 | Capability not supported. | 1354| 2501000 | Operation failed.| 1355 1356**示例:** 1357```ts 1358 import { wifiManager } from '@kit.ConnectivityKit'; 1359 1360 try { 1361 let ret = wifiManager.isConnected(); 1362 console.info("isConnected:" + ret); 1363 }catch(error){ 1364 console.error("failed:" + JSON.stringify(error)); 1365 } 1366 1367``` 1368 1369 1370## wifiManager.disconnect<sup>15+</sup> 1371 1372disconnect(): void 1373 1374断开WLAN连接。 1375 1376**需要权限:** ohos.permission.SET_WIFI_INFO 和 (ohos.permission.MANAGE_WIFI_CONNECTION 仅系统应用可用 或 1377 ohos.permission.MANAGE_ENTERPRISE_WIFI_CONNECTION 仅企业应用可用) 1378 1379**系统能力:** SystemCapability.Communication.WiFi.STA 1380 1381**错误码:** 1382 1383以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1384 1385| **错误码ID** | **错误信息** | 1386| -------- | -------- | 1387| 201 | Permission denied. | 1388| 801 | Capability not supported. | 1389| 2501000 | Operation failed.| 1390| 2501001 | Wi-Fi STA disabled.| 1391 1392**示例:** 1393```ts 1394 import { wifiManager } from '@kit.ConnectivityKit'; 1395 1396 try { 1397 wifiManager.disconnect(); 1398 }catch(error){ 1399 console.error("failed:" + JSON.stringify(error)); 1400 } 1401 1402``` 1403 1404 1405## wifiManager.isFeatureSupported<sup>9+</sup> 1406 1407isFeatureSupported(featureId: number): boolean 1408 1409判断设备是否支持相关WLAN特性。 1410 1411**需要权限:** ohos.permission.GET_WIFI_INFO 1412 1413**系统能力:** SystemCapability.Communication.WiFi.Core 1414 1415**参数:** 1416 1417 | **参数名** | **类型** | 必填 | **说明** | 1418 | -------- | -------- | -------- | -------- | 1419 | featureId | number | 是 | 特性ID值。 | 1420 1421**特性ID值枚举:** 1422 1423| 枚举值 | 说明 | 1424| -------- | -------- | 1425| 0x0001 | 基础结构模式特性。 | 1426| 0x0002 | 5 GHz带宽特性。 | 1427| 0x0004 | GAS/ANQP特性。 | 1428| 0x0008 | Wifi-Direct特性。 | 1429| 0x0010 | Soft AP特性。 | 1430| 0x0040 | Wi-Fi AWare组网特性。 | 1431| 0x8000 | AP STA共存特性。 | 1432| 0x8000000 | WPA3-Personal SAE特性。 | 1433| 0x10000000 | WPA3-Enterprise Suite-B | 1434| 0x20000000 | 增强开放特性。 | 1435 1436**返回值:** 1437 1438 | **类型** | **说明** | 1439 | -------- | -------- | 1440 | boolean | true:支持, false:不支持。 | 1441 1442**错误码:** 1443 1444以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1445 1446| **错误码ID** | **错误信息** | 1447 | -------- | -------- | 1448| 201 | Permission denied. | 1449| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified.<br>2. Incorrect parameter types. | 1450| 801 | Capability not supported. | 1451| 2401000 | Operation failed.| 1452 1453**示例:** 1454```ts 1455 import { wifiManager } from '@kit.ConnectivityKit'; 1456 1457 try { 1458 let featureId = 0; 1459 let ret = wifiManager.isFeatureSupported(featureId); 1460 console.info("isFeatureSupported:" + ret); 1461 }catch(error){ 1462 console.error("failed:" + JSON.stringify(error)); 1463 } 1464 1465``` 1466 1467 1468## wifiManager.getIpInfo<sup>9+</sup> 1469 1470getIpInfo(): IpInfo 1471 1472获取IPV4信息。 1473 1474**需要权限:** ohos.permission.GET_WIFI_INFO 1475 1476**系统能力:** SystemCapability.Communication.WiFi.STA 1477 1478**返回值:** 1479 1480 | **类型** | **说明** | 1481 | -------- | -------- | 1482 | [IpInfo](#ipinfo9) | IP信息。 | 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.getIpInfo(); 1500 console.info("info:" + JSON.stringify(info)); 1501 }catch(error){ 1502 console.error("failed:" + JSON.stringify(error)); 1503 } 1504``` 1505 1506## IpInfo<sup>9+</sup> 1507 1508IPV4信息。 1509 1510**系统能力:** SystemCapability.Communication.WiFi.STA 1511 1512| **名称** | **类型** | **可读** | **可写** | **说明** | 1513| -------- | -------- | -------- | -------- | -------- | 1514| ipAddress | number | 是 | 否 | IP地址。 | 1515| gateway | number | 是 | 否 | 网关。 | 1516| netmask | number | 是 | 否 | 掩码。 | 1517| primaryDns | number | 是 | 否 | 主DNS服务器IP地址。 | 1518| secondDns | number | 是 | 否 | 备DNS服务器IP地址。 | 1519| serverIp | number | 是 | 否 | DHCP服务端IP地址。 | 1520| leaseDuration | number | 是 | 否 | IP地址租用时长,单位:秒。 | 1521 1522 1523## wifiManager.getIpv6Info<sup>10+</sup> 1524 1525getIpv6Info(): Ipv6Info 1526 1527获取IPV6信息。 1528 1529**需要权限:** ohos.permission.GET_WIFI_INFO 1530 1531**系统能力:** SystemCapability.Communication.WiFi.STA 1532 1533**返回值:** 1534 1535| **类型** | **说明** | 1536| -------- | -------- | 1537| [Ipv6Info](#ipv6info10) | Ipv6信息。 | 1538 1539**错误码:** 1540 1541以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1542 1543| **错误码ID** | **错误信息** | 1544| -------- | -------- | 1545| 201 | Permission denied. | 1546| 801 | Capability not supported. | 1547| 2501000 | Operation failed.| 1548 1549**示例:** 1550```ts 1551 import { wifiManager } from '@kit.ConnectivityKit'; 1552 1553 try { 1554 let info = wifiManager.getIpv6Info(); 1555 console.info("info:" + JSON.stringify(info)); 1556 }catch(error){ 1557 console.error("failed:" + JSON.stringify(error)); 1558 } 1559``` 1560## Ipv6Info<sup>10+</sup> 1561 1562Ipv6信息。 1563 1564**系统能力:** SystemCapability.Communication.WiFi.STA 1565 1566| **名称** | **类型** | **可读** | **可写** | **说明** | 1567| -------- | -------- | -------- | -------- | -------- | 1568| linkIpv6Address | string | 是 | 否 | 链路Ipv6地址。 | 1569| globalIpv6Address | string | 是 | 否 | 全局Ipv6地址。 | 1570| randomGlobalIpv6Address | string | 是 | 否 | 随机全局Ipv6地址。 预留字段,暂不支持。| 1571| uniqueIpv6Address<sup>12+</sup> | string | 是 | 否 | 唯一本地Ipv6地址。 | 1572| randomUniqueIpv6Address<sup>12+</sup> | string | 是 | 否 | 随机唯一本地Ipv6地址。 | 1573| gateway | string | 是 | 否 | 网关。 | 1574| netmask | string | 是 | 否 | 网络掩码。 | 1575| primaryDNS | string | 是 | 否 | 主DNS服务器Ipv6地址。 | 1576| secondDNS | string | 是 | 否 | 备DNS服务器Ipv6地址。 | 1577 1578## wifiManager.getCountryCode<sup>9+</sup> 1579 1580getCountryCode(): string 1581 1582获取国家码信息。 1583 1584**需要权限:** ohos.permission.GET_WIFI_INFO 1585 1586**系统能力:** SystemCapability.Communication.WiFi.Core 1587 1588**返回值:** 1589 1590 | **类型** | **说明** | 1591 | -------- | -------- | 1592 | string | 国家码。 | 1593 1594**错误码:** 1595 1596以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1597 1598| **错误码ID** | **错误信息** | 1599| -------- | -------- | 1600| 201 | Permission denied. | 1601| 801 | Capability not supported. | 1602| 2401000 | Operation failed.| 1603 1604**示例:** 1605```ts 1606 import { wifiManager } from '@kit.ConnectivityKit'; 1607 1608 try { 1609 let code = wifiManager.getCountryCode(); 1610 console.info("code:" + code); 1611 }catch(error){ 1612 console.error("failed:" + JSON.stringify(error)); 1613 } 1614``` 1615 1616 1617 1618 1619## wifiManager.isBandTypeSupported<sup>10+</sup> 1620 1621isBandTypeSupported(bandType: WifiBandType): boolean 1622 1623判断当前频段是否支持。 1624 1625**需要权限:** ohos.permission.GET_WIFI_INFO。 1626 1627**系统能力:** SystemCapability.Communication.WiFi.STA 1628 1629**参数:** 1630 1631 | **参数名** | **类型** | **必填** | **说明** | 1632 | -------- | -------- | -------- | -------- | 1633 | bandType | [WifiBandType](#wifibandtype10) | 是 | Wifi 频段类型。 | 1634 1635**返回值:** 1636 1637 | **类型** | **说明** | 1638 | -------- | -------- | 1639 | boolean | true:支持, false:不支持。 | 1640 1641**错误码:** 1642 1643以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1644 1645| **错误码ID** | **错误信息** | 1646| -------- | -------- | 1647| 201 | Permission denied. | 1648| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified.<br>2. Incorrect parameter types. 3. Parameter verification failed. | 1649| 801 | Capability not supported. | 1650| 2501000 | Operation failed.| 1651 1652**示例:** 1653```ts 1654 import { wifiManager } from '@kit.ConnectivityKit'; 1655 1656 try { 1657 let type = 0; 1658 let isBandTypeSupported = wifiManager.isBandTypeSupported(type); 1659 console.info("isBandTypeSupported:" + isBandTypeSupported); 1660 }catch(error){ 1661 console.error("failed:" + JSON.stringify(error)); 1662 } 1663``` 1664 1665 1666## wifiManager.isMeteredHotspot<sup>11+</sup> 1667 1668isMeteredHotspot(): boolean 1669 1670查询设备当前连接的wifi是否是手机热点。 1671 1672**需要权限:** ohos.permission.GET_WIFI_INFO 1673 1674**系统能力:** SystemCapability.Communication.WiFi.STA 1675 1676**返回值:** 1677 1678 | **类型** | **说明** | 1679 | -------- | -------- | 1680 | boolean | true:是手机热点, false:不是手机热点。 | 1681 1682**错误码:** 1683 1684以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1685 1686| **错误码ID** | **错误信息** | 1687| -------- | -------- | 1688| 201 | Permission denied. | 1689| 801 | Capability not supported. | 1690| 2501000 | Operation failed.| 1691| 2501001 | Wi-Fi STA disabled. | 1692 1693**示例:** 1694 1695```ts 1696 import { wifiManager } from '@kit.ConnectivityKit'; 1697 1698 try { 1699 let isMeteredHotspot = wifiManager.isMeteredHotspot(); 1700 console.info("isMeteredHotspot:" + isMeteredHotspot); 1701 }catch(error){ 1702 console.error("failed:" + JSON.stringify(error)); 1703 } 1704``` 1705 1706 1707## wifiManager.isHotspotActive<sup>15+</sup> 1708 1709isHotspotActive(): boolean 1710 1711热点是否已使能。 1712 1713**需要权限:** ohos.permission.GET_WIFI_INFO 1714 1715**系统能力:** SystemCapability.Communication.WiFi.AP.Core 1716 1717**返回值:** 1718 1719 | **类型** | **说明** | 1720 | -------- | -------- | 1721 | boolean | true:已使能, false:未使能。| 1722 1723**错误码:** 1724 1725以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1726 1727| **错误码ID** | **错误信息** | 1728| -------- | -------- | 1729| 201 | Permission denied. | 1730| 801 | Capability not supported. | 1731| 2601000 | Operation failed. | 1732 1733**示例:** 1734```ts 1735 import { wifiManager } from '@kit.ConnectivityKit'; 1736 1737 try { 1738 let ret = wifiManager.isHotspotActive(); 1739 console.info("result:" + ret); 1740 } catch(error) { 1741 console.error("failed:" + JSON.stringify(error)); 1742 } 1743``` 1744 1745 1746## wifiManager.getP2pLinkedInfo<sup>9+</sup> 1747 1748getP2pLinkedInfo(): Promise<WifiP2pLinkedInfo> 1749 1750获取P2P连接信息,使用Promise异步回调。 1751 1752**需要权限:** ohos.permission.GET_WIFI_INFO 1753 1754获取 groupOwnerAddr 还需申请ohos.permission.GET_WIFI_LOCAL_MAC权限(该权限仅系统应用可申请),无该权限时,groupOwnerAddr 返回全零地址。 1755 1756**系统能力:** SystemCapability.Communication.WiFi.P2P 1757 1758**返回值:** 1759 1760 | 类型 | 说明 | 1761 | -------- | -------- | 1762 | Promise<[WifiP2pLinkedInfo](#wifip2plinkedinfo9)> | Promise对象。表示P2P连接信息。 | 1763 1764**错误码:** 1765 1766以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1767 1768| **错误码ID** | **错误信息** | 1769| -------- | -------- | 1770| 201 | Permission denied. | 1771| 801 | Capability not supported. | 1772| 2801000 | Operation failed. | 1773 1774 1775## wifiManager.getP2pLinkedInfo<sup>9+</sup> 1776 1777getP2pLinkedInfo(callback: AsyncCallback<WifiP2pLinkedInfo>): void 1778 1779获取P2P连接信息,使用callback异步回调。 1780 1781**需要权限:** ohos.permission.GET_WIFI_INFO 1782 1783获取 groupOwnerAddr 还需申请ohos.permission.GET_WIFI_LOCAL_MAC权限(该权限仅系统应用可申请),无该权限时,groupOwnerAddr 返回全零地址。 1784 1785**系统能力:** SystemCapability.Communication.WiFi.P2P 1786 1787**参数:** 1788 1789 | 参数名 | 类型 | 必填 | 说明 | 1790 | -------- | -------- | -------- | -------- | 1791 | callback | AsyncCallback<[WifiP2pLinkedInfo](#wifip2plinkedinfo9)> | 是 | 回调函数。当操作成功时,err为0,data表示P2P连接信息。如果err为非0,表示处理出现错误。 | 1792 1793**错误码:** 1794 1795以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1796 1797| **错误码ID** | **错误信息** | 1798| -------- | -------- | 1799| 201 | Permission denied. | 1800| 801 | Capability not supported. | 1801| 2801000 | Operation failed. | 1802| 2801001 | Wi-Fi STA disabled. | 1803 1804**示例:** 1805```ts 1806 import { wifiManager } from '@kit.ConnectivityKit'; 1807 1808 wifiManager.getP2pLinkedInfo((err, data:wifiManager.WifiP2pLinkedInfo) => { 1809 if (err) { 1810 console.error("get p2p linked info error"); 1811 return; 1812 } 1813 console.info("get wifi p2p linked info: " + JSON.stringify(data)); 1814 }); 1815 1816 wifiManager.getP2pLinkedInfo().then(data => { 1817 console.info("get wifi p2p linked info: " + JSON.stringify(data)); 1818 }); 1819``` 1820 1821 1822## WifiP2pLinkedInfo<sup>9+</sup> 1823 1824提供WLAN连接的相关信息。 1825 1826**系统能力:** SystemCapability.Communication.WiFi.P2P 1827 1828| 名称 | 类型 | 可读 | 可写 | 说明 | 1829| -------- | -------- | -------- | -------- | -------- | 1830| connectState | [P2pConnectState](#p2pconnectstate9) | 是 | 否 | P2P连接状态。 | 1831| isGroupOwner | boolean | 是 | 否 | true:是GO,false:不是GO。| 1832| groupOwnerAddr | string | 是 | 否 | 群组IP地址。| 1833 1834 1835## P2pConnectState<sup>9+</sup> 1836 1837表示P2P连接状态的枚举。 1838 1839**系统能力:** SystemCapability.Communication.WiFi.P2P 1840 1841| 名称 | 值 | 说明 | 1842| -------- | -------- | -------- | 1843| DISCONNECTED | 0 | 断开状态。 | 1844| CONNECTED | 1 | 连接状态。 | 1845 1846## wifiManager.getCurrentGroup<sup>9+</sup> 1847 1848getCurrentGroup(): Promise<WifiP2pGroupInfo> 1849 1850获取P2P当前组信息,使用Promise异步回调。 1851 1852**需要权限:** 1853 1854API 10起:ohos.permission.GET_WIFI_INFO 1855 1856**系统能力:** SystemCapability.Communication.WiFi.P2P 1857 1858**返回值:** 1859 1860| 类型 | 说明 | 1861| -------- | -------- | 1862| Promise<[WifiP2pGroupInfo](#wifip2pgroupinfo9)> | Promise对象。表示当前组信息。如果应用申请了ohos.permission.GET_WIFI_PEERS_MAC权限(仅系统应用可申请),则返回结果中的deviceAddress为真实设备地址,否则为随机设备地址。 | 1863 1864**错误码:** 1865 1866以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1867 1868| **错误码ID** | **错误信息** | 1869| -------- | -------- | 1870| 201 | Permission denied. | 1871| 801 | Capability not supported. | 1872| 2801000 | Operation failed. | 1873 1874## wifiManager.getCurrentGroup<sup>9+</sup> 1875 1876getCurrentGroup(callback: AsyncCallback<WifiP2pGroupInfo>): void 1877 1878获取P2P当前组信息,使用callback异步回调。 1879 1880**需要权限:** 1881 1882API 10起:ohos.permission.GET_WIFI_INFO 1883 1884**系统能力:** SystemCapability.Communication.WiFi.P2P 1885 1886**参数:** 1887 1888| 参数名 | 类型 | 必填 | 说明 | 1889| -------- | -------- | -------- | -------- | 1890| callback | AsyncCallback<[WifiP2pGroupInfo](#wifip2pgroupinfo9)> | 是 | 回调函数。当操作成功时,err为0,data表示当前组信息。如果error为非0,表示处理出现错误。如果应用申请了ohos.permission.GET_WIFI_PEERS_MAC权限(仅系统应用可申请),则返回结果中的deviceAddress为真实设备地址,否则为随机设备地址。 | 1891 1892**错误码:** 1893 1894以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1895 1896| **错误码ID** | **错误信息** | 1897| -------- | -------- | 1898| 201 | Permission denied. | 1899| 801 | Capability not supported. | 1900| 2801000 | Operation failed. | 1901 1902**示例:** 1903```ts 1904 import { wifiManager } from '@kit.ConnectivityKit'; 1905 // p2p已经建组或者连接成功,才能正常获取到当前组信息 1906 wifiManager.getCurrentGroup((err, data:wifiManager.WifiP2pGroupInfo) => { 1907 if (err) { 1908 console.error("get current P2P group error"); 1909 return; 1910 } 1911 console.info("get current P2P group: " + JSON.stringify(data)); 1912 }); 1913 1914 wifiManager.getCurrentGroup().then(data => { 1915 console.info("get current P2P group: " + JSON.stringify(data)); 1916 }); 1917``` 1918 1919## wifiManager.getP2pPeerDevices<sup>9+</sup> 1920 1921getP2pPeerDevices(): Promise<WifiP2pDevice[]> 1922 1923获取P2P对端设备列表信息,使用Promise异步回调。 1924 1925**需要权限:** 1926 1927API 10起:ohos.permission.GET_WIFI_INFO 1928 1929**系统能力:** SystemCapability.Communication.WiFi.P2P 1930 1931**返回值:** 1932 1933| 类型 | 说明 | 1934| -------- | -------- | 1935| Promise<[WifiP2pDevice[]](#wifip2pdevice9)> | Promise对象。表示对端设备列表信息。如果应用申请了ohos.permission.GET_WIFI_PEERS_MAC权限(仅系统应用可申请),则返回结果中的deviceAddress为真实设备地址,否则为随机设备地址。 | 1936 1937**错误码:** 1938 1939以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1940 1941| **错误码ID** | **错误信息** | 1942| -------- | -------- | 1943| 201 | Permission denied. | 1944| 801 | Capability not supported. | 1945| 2801000 | Operation failed. | 1946 1947## wifiManager.getP2pPeerDevices<sup>9+</sup> 1948 1949getP2pPeerDevices(callback: AsyncCallback<WifiP2pDevice[]>): void 1950 1951获取P2P对端设备列表信息,使用callback异步回调。 1952 1953**需要权限:** 1954 1955API 10起:ohos.permission.GET_WIFI_INFO 1956 1957**系统能力:** SystemCapability.Communication.WiFi.P2P 1958 1959**参数:** 1960 1961| 参数名 | 类型 | 必填 | 说明 | 1962| -------- | -------- | -------- | -------- | 1963| callback | AsyncCallback<[WifiP2pDevice[]](#wifip2pdevice9)> | 是 | 回调函数。当操作成功时,err为0,data表示对端设备列表信息。如果err为非0,表示处理出现错误。如果应用申请了ohos.permission.GET_WIFI_PEERS_MAC权限(仅系统应用可申请),则返回结果中的deviceAddress为真实设备地址,否则为随机设备地址。 | 1964 1965**错误码:** 1966 1967以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1968 1969| **错误码ID** | **错误信息** | 1970| -------- | -------- | 1971| 201 | Permission denied. | 1972| 801 | Capability not supported. | 1973| 2801000 | Operation failed. | 1974| 2801001 | Wi-Fi STA disabled. | 1975 1976**示例:** 1977```ts 1978 import { wifiManager } from '@kit.ConnectivityKit'; 1979 // p2p发现阶段完成,才能正常获取到对端设备列表信息 1980 wifiManager.getP2pPeerDevices((err, data:wifiManager.WifiP2pDevice[]) => { 1981 if (err) { 1982 console.error("get P2P peer devices error"); 1983 return; 1984 } 1985 console.info("get P2P peer devices: " + JSON.stringify(data)); 1986 }); 1987 1988 wifiManager.getP2pPeerDevices().then(data => { 1989 console.info("get P2P peer devices: " + JSON.stringify(data)); 1990 }); 1991``` 1992 1993## WifiP2pDevice<sup>9+</sup> 1994 1995表示P2P设备信息。 1996 1997**系统能力:** SystemCapability.Communication.WiFi.P2P 1998 1999| 名称 | 类型 | 可读 | 可写 | 说明 | 2000| -------- | -------- | -------- | -------- | -------- | 2001| deviceName | string | 是 | 否 | 设备名称。 | 2002| deviceAddress | string | 是 | 否 | 设备MAC地址。 | 2003| deviceAddressType<sup>10+</sup> | [DeviceAddressType](#deviceaddresstype10) | 是 | 否 | 设备MAC地址类型。 | 2004| primaryDeviceType | string | 是 | 否 | 主设备类型。 | 2005| deviceStatus | [P2pDeviceStatus](#p2pdevicestatus9) | 是 | 否 | 设备状态。 | 2006| groupCapabilities | number | 是 | 否 | 群组能力。 | 2007 2008 2009## P2pDeviceStatus<sup>9+</sup> 2010 2011表示设备状态的枚举。 2012 2013**系统能力:** SystemCapability.Communication.WiFi.P2P 2014 2015| 名称 | 值 | 说明 | 2016| -------- | -------- | -------- | 2017| CONNECTED | 0 | 连接状态。 | 2018| INVITED | 1 | 邀请状态。 | 2019| FAILED | 2 | 失败状态。 | 2020| AVAILABLE | 3 | 可用状态。 | 2021| UNAVAILABLE | 4 | 不可用状态。 | 2022 2023 2024## wifiManager.getP2pLocalDevice<sup>9+</sup> 2025 2026getP2pLocalDevice(): Promise<WifiP2pDevice> 2027 2028获取P2P本端设备信息,使用Promise异步回调。 2029 2030**需要权限:** 2031 2032API 11起:ohos.permission.GET_WIFI_INFO 2033 2034**系统能力:** SystemCapability.Communication.WiFi.P2P 2035 2036**返回值:** 2037 2038 | 类型 | 说明 | 2039 | -------- | -------- | 2040 | Promise<[WifiP2pDevice](#wifip2pdevice9)> | Promise对象。表示本端设备信息。 | 2041 2042**错误码:** 2043 2044以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2045 2046| **错误码ID** | **错误信息** | 2047| -------- | -------- | 2048| 201 | Permission denied. | 2049| 801 | Capability not supported. | 2050| 2801000 | Operation failed. | 2051 2052## wifiManager.getP2pLocalDevice<sup>9+</sup> 2053 2054getP2pLocalDevice(callback: AsyncCallback<WifiP2pDevice>): void 2055 2056获取P2P本端设备信息,使用callback异步回调。 2057 2058**需要权限:** 2059 2060API 11起:ohos.permission.GET_WIFI_INFO 2061 2062**系统能力:** SystemCapability.Communication.WiFi.P2P 2063 2064**参数:** 2065 2066 | 参数名 | 类型 | 必填 | 说明 | 2067 | -------- | -------- | -------- | -------- | 2068 | callback | AsyncCallback<[WifiP2pDevice](#wifip2pdevice9)> | 是 | 回调函数。当操作成功时,err为0,data表示本端设备信息。如果error为非0,表示处理出现错误。 | 2069 2070**错误码:** 2071 2072| **错误码ID** | **错误信息** | 2073| -------- | -------- | 2074| 201 | Permission denied. | 2075| 801 | Capability not supported. | 2076| 2801000 | Operation failed. | 2077| 2801001 | Wi-Fi STA disabled. | 2078 2079**示例:** 2080```ts 2081 import { wifiManager } from '@kit.ConnectivityKit'; 2082 // p2p已经建组或者连接成功,才能正常获取到本端设备信息 2083 wifiManager.getP2pLocalDevice((err, data:wifiManager.WifiP2pDevice) => { 2084 if (err) { 2085 console.error("get P2P local device error"); 2086 return; 2087 } 2088 console.info("get P2P local device: " + JSON.stringify(data)); 2089 }); 2090 2091 wifiManager.getP2pLocalDevice().then(data => { 2092 console.info("get P2P local device: " + JSON.stringify(data)); 2093 }); 2094``` 2095 2096## wifiManager.createGroup<sup>9+</sup> 2097 2098createGroup(config: WifiP2PConfig): void 2099 2100创建群组。 2101 2102**需要权限:** ohos.permission.GET_WIFI_INFO 2103 2104**系统能力:** SystemCapability.Communication.WiFi.P2P 2105 2106**参数:** 2107 2108| **参数名** | **类型** | 必填 | **说明** | 2109| -------- | -------- | -------- | -------- | 2110| config | [WifiP2PConfig](#wifip2pconfig9) | 是 | 群组配置信息。如果DeviceAddressType未指定值,则DeviceAddressType默认为随机设备地址类型。 | 2111 2112**错误码:** 2113 2114以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2115 2116| **错误码ID** | **错误信息** | 2117| -------- | -------- | 2118| 201 | Permission denied. | 2119| 401 | Invalid parameters. Possible causes: 1. Incorrect parameter types.<br>2. Parameter verification failed. | 2120| 801 | Capability not supported. | 2121| 2801000 | Operation failed. | 2122| 2801001 | Wi-Fi STA disabled. | 2123 2124**示例:** 2125```ts 2126 import { wifiManager } from '@kit.ConnectivityKit'; 2127 2128 try { 2129 let config:wifiManager.WifiP2PConfig = { 2130 deviceAddress: "****", 2131 netId: 0, 2132 passphrase: "*****", 2133 groupName: "****", 2134 goBand: 0 2135 } 2136 wifiManager.createGroup(config); 2137 2138 }catch(error){ 2139 console.error("failed:" + JSON.stringify(error)); 2140 } 2141``` 2142 2143## WifiP2PConfig<sup>9+</sup> 2144 2145表示P2P配置信息。 2146 2147**系统能力:** SystemCapability.Communication.WiFi.P2P 2148 2149| 名称 | 类型 | 可读 | 可写 | 说明 | 2150| -------- | -------- | -------- | -------- | -------- | 2151| deviceAddress | string | 是 | 否 | 设备地址。 | 2152| deviceAddressType<sup>10+</sup>| [DeviceAddressType](#deviceaddresstype10) | 是 | 否 | 设备地址类型。 | 2153| netId | number | 是 | 否 | 网络ID。创建群组时-1表示创建临时组,-2表示创建永久组。 | 2154| passphrase | string | 是 | 否 | 群组密钥。 | 2155| groupName | string | 是 | 否 | 群组名称。 | 2156| goBand | [GroupOwnerBand](#groupownerband9) | 是 | 否 | 群组带宽。 | 2157 2158 2159## GroupOwnerBand<sup>9+</sup> 2160 2161表示群组带宽的枚举。 2162 2163**系统能力:** SystemCapability.Communication.WiFi.P2P 2164 2165| 名称 | 值 | 说明 | 2166| -------- | -------- | -------- | 2167| GO_BAND_AUTO | 0 | 自动模式。 | 2168| GO_BAND_2GHZ | 1 | 2.4GHZ。 | 2169| GO_BAND_5GHZ | 2 | 5GHZ。 | 2170 2171 2172## wifiManager.removeGroup<sup>9+</sup> 2173 2174removeGroup(): void 2175 2176移除群组。 2177 2178**需要权限:** ohos.permission.GET_WIFI_INFO 2179 2180**系统能力:** SystemCapability.Communication.WiFi.P2P 2181 2182**错误码:** 2183 2184以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2185 2186| **错误码ID** | **错误信息** | 2187| -------- | -------- | 2188| 201 | Permission denied. | 2189| 801 | Capability not supported. | 2190| 2801000 | Operation failed. | 2191| 2801001 | Wi-Fi STA disabled. | 2192 2193**示例:** 2194```ts 2195 import { wifiManager } from '@kit.ConnectivityKit'; 2196 2197 try { 2198 wifiManager.removeGroup(); 2199 }catch(error){ 2200 console.error("failed:" + JSON.stringify(error)); 2201 } 2202``` 2203 2204## wifiManager.p2pConnect<sup>9+</sup> 2205 2206p2pConnect(config: WifiP2PConfig): void 2207 2208执行P2P连接。 2209 2210**需要权限:** 2211 2212API 10起:ohos.permission.GET_WIFI_INFO 2213 2214**系统能力:** SystemCapability.Communication.WiFi.P2P 2215 2216**参数:** 2217 2218| **参数名** | **类型** | 必填 | **说明** | 2219| -------- | -------- | -------- | -------- | 2220| config | [WifiP2PConfig](#wifip2pconfig9) | 是 | 连接配置信息。如果DeviceAddressType未指定值,则DeviceAddressType默认为随机设备地址类型。 | 2221 2222**错误码:** 2223 2224以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2225 2226| **错误码ID** | **错误信息** | 2227| -------- | -------- | 2228| 201 | Permission denied. | 2229| 401 | Invalid parameters. Possible causes: 1. Incorrect parameter types.<br>2. Parameter verification failed. | 2230| 801 | Capability not supported. | 2231| 2801000 | Operation failed. | 2232| 2801001 | Wi-Fi STA disabled. | 2233 2234**示例:** 2235```ts 2236 import { wifiManager } from '@kit.ConnectivityKit'; 2237 2238 let recvP2pConnectionChangeFunc = (result:wifiManager.WifiP2pLinkedInfo) => { 2239 console.info("p2p connection change receive event: " + JSON.stringify(result)); 2240 wifiManager.getP2pLinkedInfo((err, data:wifiManager.WifiP2pLinkedInfo) => { 2241 if (err) { 2242 console.error('failed to get getP2pLinkedInfo: ' + JSON.stringify(err)); 2243 return; 2244 } 2245 console.info("get getP2pLinkedInfo: " + JSON.stringify(data)); 2246 }); 2247 } 2248 wifiManager.on("p2pConnectionChange", recvP2pConnectionChangeFunc); 2249 2250 let recvP2pDeviceChangeFunc = (result:wifiManager.WifiP2pDevice) => { 2251 console.info("p2p device change receive event: " + JSON.stringify(result)); 2252 } 2253 wifiManager.on("p2pDeviceChange", recvP2pDeviceChangeFunc); 2254 2255 let recvP2pPeerDeviceChangeFunc = (result:wifiManager.WifiP2pDevice[]) => { 2256 console.info("p2p peer device change receive event: " + JSON.stringify(result)); 2257 wifiManager.getP2pPeerDevices((err, data:wifiManager.WifiP2pDevice[]) => { 2258 if (err) { 2259 console.error('failed to get peer devices: ' + JSON.stringify(err)); 2260 return; 2261 } 2262 console.info("get peer devices: " + JSON.stringify(data)); 2263 let len = data.length; 2264 for (let i = 0; i < len; ++i) { 2265 if (data[i].deviceName === "my_test_device") { 2266 console.info("p2p connect to test device: " + data[i].deviceAddress); 2267 let config:wifiManager.WifiP2PConfig = { 2268 deviceAddress:data[i].deviceAddress, 2269 netId:-2, 2270 passphrase:"", 2271 groupName:"", 2272 goBand:0, 2273 } 2274 wifiManager.p2pConnect(config); 2275 } 2276 } 2277 }); 2278 } 2279 wifiManager.on("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc); 2280 2281 let recvP2pPersistentGroupChangeFunc = () => { 2282 console.info("p2p persistent group change receive event"); 2283 2284 wifiManager.getCurrentGroup((err, data:wifiManager.WifiP2pGroupInfo) => { 2285 if (err) { 2286 console.error('failed to get current group: ' + JSON.stringify(err)); 2287 return; 2288 } 2289 console.info("get current group: " + JSON.stringify(data)); 2290 }); 2291 } 2292 wifiManager.on("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc); 2293 2294 setTimeout(() => {wifiManager.off("p2pConnectionChange", recvP2pConnectionChangeFunc);}, 125 * 1000); 2295 setTimeout(() => {wifiManager.off("p2pDeviceChange", recvP2pDeviceChangeFunc);}, 125 * 1000); 2296 setTimeout(() => {wifiManager.off("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc);}, 125 * 1000); 2297 setTimeout(() => {wifiManager.off("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc);}, 125 * 1000); 2298 console.info("start discover devices -> " + wifiManager.startDiscoverDevices()); 2299``` 2300 2301## wifiManager.p2pCancelConnect<sup>9+</sup> 2302 2303p2pCancelConnect(): void 2304 2305在P2P连接过程中,取消P2P连接。 2306 2307**需要权限:** ohos.permission.GET_WIFI_INFO 2308 2309**系统能力:** SystemCapability.Communication.WiFi.P2P 2310 2311**错误码:** 2312 2313以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2314 2315| **错误码ID** | **错误信息** | 2316| -------- | -------- | 2317| 201 | Permission denied. | 2318| 801 | Capability not supported. | 2319| 2801000 | Operation failed. | 2320| 2801001 | Wi-Fi STA disabled. | 2321 2322**示例:** 2323```ts 2324 import { wifiManager } from '@kit.ConnectivityKit'; 2325 2326 try { 2327 wifiManager.p2pCancelConnect(); 2328 }catch(error){ 2329 console.error("failed:" + JSON.stringify(error)); 2330 } 2331``` 2332 2333## wifiManager.startDiscoverDevices<sup>9+</sup> 2334 2335startDiscoverDevices(): void 2336 2337开始发现设备。 2338 2339**需要权限:** 2340 2341API 10起:ohos.permission.GET_WIFI_INFO 2342 2343**系统能力:** SystemCapability.Communication.WiFi.P2P 2344 2345**错误码:** 2346 2347以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2348 2349| **错误码ID** | **错误信息** | 2350| -------- | -------- | 2351| 201 | Permission denied. | 2352| 801 | Capability not supported. | 2353| 2801000 | Operation failed. | 2354| 2801001 | Wi-Fi STA disabled. | 2355 2356**示例:** 2357```ts 2358 import { wifiManager } from '@kit.ConnectivityKit'; 2359 2360 try { 2361 wifiManager.startDiscoverDevices(); 2362 }catch(error){ 2363 console.error("failed:" + JSON.stringify(error)); 2364 } 2365``` 2366 2367## wifiManager.stopDiscoverDevices<sup>9+</sup> 2368 2369stopDiscoverDevices(): void 2370 2371停止发现设备。 2372 2373**需要权限:** ohos.permission.GET_WIFI_INFO 2374 2375**系统能力:** SystemCapability.Communication.WiFi.P2P 2376 2377**错误码:** 2378 2379以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2380 2381| **错误码ID** | **错误信息** | 2382| -------- | -------- | 2383| 201 | Permission denied. | 2384| 801 | Capability not supported. | 2385| 2801000 | Operation failed. | 2386| 2801001 | Wi-Fi STA disabled. | 2387 2388**示例:** 2389```ts 2390 import { wifiManager } from '@kit.ConnectivityKit'; 2391 2392 try { 2393 wifiManager.stopDiscoverDevices(); 2394 }catch(error){ 2395 console.error("failed:" + JSON.stringify(error)); 2396 } 2397``` 2398## wifiManager.getMultiLinkedInfo<sup>18+</sup> 2399 2400getMultiLinkedInfo(): Array<WifiLinkedInfo> 2401 2402获取MLO(Multi-Link Operation,多链路操作)Wi-Fi连接信息。 2403 2404**需要权限:** ohos.permission.GET_WIFI_INFO 2405 2406> **注意:** 2407> - 当macType是1(设备MAC地址),获取 macAddress 还需申请ohos.permission.GET_WIFI_LOCAL_MAC权限(该权限仅系统应用可申请),无该权限时,macAddress 返回为空。 2408> - 如果应用申请了ohos.permission.GET_WIFI_PEERS_MAC权限(该权限仅系统应用可申请),则返回结果中的bssid为真实mac地址,否则为随机设备地址。 2409 2410**系统能力:** SystemCapability.Communication.WiFi.STA 2411 2412**返回值:** 2413 2414 | **类型** | **说明** | 2415 | -------- | -------- | 2416 | Array<[WifiLinkedInfo](#wifilinkedinfo9)> | Wi-Fi连接信息。| 2417 2418**错误码:** 2419 2420以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[WIFI错误码](errorcode-wifi.md)。 2421 2422| **错误码ID** | **错误信息** | 2423| -------- | -------- | 2424| 201 | Permission denied. | 2425| 801 | Capability not supported. | 2426| 2501000 | Operation failed. | 2427| 2501001 | Wi-Fi STA disabled. | 2428 2429**示例:** 2430```ts 2431import { wifiManager } from '@kit.ConnectivityKit'; 2432 2433 try { 2434 let linkedInfo = wifiManager.getMultiLinkedInfo(); 2435 console.info("linkedInfo:" + JSON.stringify(linkedInfo)); 2436 }catch(error){ 2437 console.error("failed:" + JSON.stringify(error)); 2438 } 2439``` 2440## WifiP2pGroupInfo<sup>9+</sup> 2441 2442表示P2P群组相关信息。 2443 2444**系统能力:** SystemCapability.Communication.WiFi.P2P 2445 2446| 名称 | 类型 | 可读 | 可写 | 说明 | 2447| -------- | -------- | -------- | -------- | -------- | 2448| isP2pGo | boolean | 是 | 否 | 是否是群主。true:是群主,false:不是群主。 | 2449| ownerInfo | [WifiP2pDevice](#wifip2pdevice9) | 是 | 否 | 群组的设备信息。 | 2450| passphrase | string | 是 | 否 | 群组密钥。 | 2451| interface | string | 是 | 否 | 接口名称。 | 2452| groupName | string | 是 | 否 | 群组名称。 | 2453| networkId | number | 是 | 否 | 网络ID。 | 2454| frequency | number | 是 | 否 | 群组的频率。 | 2455| clientDevices | [WifiP2pDevice[]](#wifip2pdevice9) | 是 | 否 | 接入的设备列表信息。 | 2456| goIpAddress | string | 是 | 否 | 群组IP地址。 | 2457 2458 2459## wifiManager.on('wifiStateChange')<sup>9+</sup> 2460 2461on(type: 'wifiStateChange', callback: Callback<number>): void 2462 2463注册WLAN状态改变事件,在业务退出时,要调用off(type: 'wifiStateChange', callback?: Callback<number>)接口去掉之前的注册回调。 2464 2465**需要权限:** ohos.permission.GET_WIFI_INFO 2466 2467**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2468 2469**系统能力:** SystemCapability.Communication.WiFi.STA 2470 2471**参数:** 2472 2473 | **参数名** | **类型** | **必填** | **说明** | 2474 | -------- | -------- | -------- | -------- | 2475 | type | string | 是 | 固定填"wifiStateChange"字符串。 | 2476 | callback | Callback<number> | 是 | 状态改变回调函数。 | 2477 2478**错误码:** 2479 2480以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2481 2482| **错误码ID** | **错误信息** | 2483| -------- | ---------------------------- | 2484| 201 | Permission denied. | 2485| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2486| 801 | Capability not supported. | 2487| 2501000 | Operation failed.| 2488 2489**状态改变事件的枚举:** 2490 2491| **枚举值** | **说明** | 2492| -------- | -------- | 2493| 0 | 未激活。 | 2494| 1 | 已激活。 | 2495| 2 | 激活中。 | 2496| 3 | 去激活中。 | 2497 2498 2499## wifiManager.off('wifiStateChange')<sup>9+</sup> 2500 2501off(type: 'wifiStateChange', callback?: Callback<number>): void 2502 2503取消注册WLAN状态改变事件。 2504 2505**需要权限:** ohos.permission.GET_WIFI_INFO 2506 2507**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2508 2509**系统能力:** SystemCapability.Communication.WiFi.STA 2510 2511**参数:** 2512 2513 | **参数名** | **类型** | **必填** | **说明** | 2514 | -------- | -------- | -------- | -------- | 2515 | type | string | 是 | 固定填"wifiStateChange"字符串。 | 2516 | callback | Callback<number> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 2517 2518**错误码:** 2519 2520以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2521 2522| **错误码ID** | **错误信息** | 2523| -------- | ---------------------------- | 2524| 201 | Permission denied. | 2525| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2526| 801 | Capability not supported. | 2527| 2501000 | Operation failed.| 2528 2529**示例:** 2530```ts 2531 import { wifiManager } from '@kit.ConnectivityKit'; 2532 2533 let recvPowerNotifyFunc = (result:number) => { 2534 console.info("Receive power state change event: " + result); 2535 } 2536 2537 // Register event 2538 wifiManager.on("wifiStateChange", recvPowerNotifyFunc); 2539 2540 // Unregister event 2541 wifiManager.off("wifiStateChange", recvPowerNotifyFunc); 2542``` 2543 2544 2545## wifiManager.on('wifiConnectionChange')<sup>9+</sup> 2546 2547on(type: 'wifiConnectionChange', callback: Callback<number>): void 2548 2549注册WLAN连接状态改变事件,在业务退出时,要调用off(type: 'wifiConnectionChange', callback?: Callback<number>)接口去掉之前的注册回调。 2550 2551**需要权限:** ohos.permission.GET_WIFI_INFO 2552 2553**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2554 2555**系统能力:** SystemCapability.Communication.WiFi.STA 2556 2557**参数:** 2558 2559 | **参数名** | **类型** | **必填** | **说明** | 2560 | -------- | -------- | -------- | -------- | 2561 | type | string | 是 | 固定填"wifiConnectionChange"字符串。 | 2562 | callback | Callback<number> | 是 | 状态改变回调函数。 | 2563 2564**连接状态改变事件的枚举:** 2565 2566| **枚举值** | **说明** | 2567| -------- | -------- | 2568| 0 | 已断开。 | 2569| 1 | 已连接。 | 2570 2571**错误码:** 2572 2573以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2574 2575| **错误码ID** | **错误信息** | 2576| -------- | ---------------------------- | 2577| 201 | Permission denied. | 2578| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2579| 801 | Capability not supported. | 2580| 2501000 | Operation failed.| 2581 2582## wifiManager.off('wifiConnectionChange')<sup>9+</sup> 2583 2584off(type: 'wifiConnectionChange', callback?: Callback<number>): void 2585 2586取消注册WLAN连接状态改变事件。 2587 2588**需要权限:** ohos.permission.GET_WIFI_INFO 2589 2590**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2591 2592**系统能力:** SystemCapability.Communication.WiFi.STA 2593 2594**参数:** 2595 2596 | **参数名** | **类型** | **必填** | **说明** | 2597 | -------- | -------- | -------- | -------- | 2598 | type | string | 是 | 固定填"wifiConnectionChange"字符串。 | 2599 | callback | Callback<number> | 否 | 连接状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 2600 2601**错误码:** 2602 2603以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2604 2605| **错误码ID** | **错误信息** | 2606| -------- | ---------------------------- | 2607| 201 | Permission denied. | 2608| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2609| 801 | Capability not supported. | 2610| 2501000 | Operation failed.| 2611 2612**示例:** 2613```ts 2614 import { wifiManager } from '@kit.ConnectivityKit'; 2615 2616 let recvWifiConnectionChangeFunc = (result:number) => { 2617 console.info("Receive wifi connection change event: " + result); 2618 } 2619 2620 // Register event 2621 wifiManager.on("wifiConnectionChange", recvWifiConnectionChangeFunc); 2622 2623 // Unregister event 2624 wifiManager.off("wifiConnectionChange", recvWifiConnectionChangeFunc); 2625``` 2626 2627## wifiManager.on('wifiScanStateChange')<sup>9+</sup> 2628 2629on(type: 'wifiScanStateChange', callback: Callback<number>): void 2630 2631注册扫描状态改变事件,在业务退出时,要调用off(type: 'wifiScanStateChange', callback?: Callback<number>)接口去掉之前的注册回调。 2632 2633**需要权限:** ohos.permission.GET_WIFI_INFO 2634 2635**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2636 2637**系统能力:** SystemCapability.Communication.WiFi.STA 2638 2639**参数:** 2640 2641 | **参数名** | **类型** | **必填** | **说明** | 2642 | -------- | -------- | -------- | -------- | 2643 | type | string | 是 | 固定填"wifiScanStateChange"字符串。 | 2644 | callback | Callback<number> | 是 | 状态改变回调函数。 | 2645 2646**扫描状态改变事件的枚举:** 2647 2648| **枚举值** | **说明** | 2649| -------- | -------- | 2650| 0 | 扫描失败。 | 2651| 1 | 扫描成功。 | 2652 2653**错误码:** 2654 2655以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2656 2657| **错误码ID** | **错误信息** | 2658| -------- | ---------------------------- | 2659| 201 | Permission denied. | 2660| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2661| 801 | Capability not supported. | 2662| 2501000 | Operation failed.| 2663 2664## wifiManager.off('wifiScanStateChange')<sup>9+</sup> 2665 2666off(type: 'wifiScanStateChange', callback?: Callback<number>): void 2667 2668取消注册扫描状态改变事件。 2669 2670**需要权限:** ohos.permission.GET_WIFI_INFO 2671 2672**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2673 2674**系统能力:** SystemCapability.Communication.WiFi.STA 2675 2676**参数:** 2677 2678| **参数名** | **类型** | **必填** | **说明** | 2679| -------- | -------- | -------- | -------- | 2680| type | string | 是 | 固定填"wifiScanStateChange"字符串。 | 2681| callback | Callback<number> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 2682 2683**错误码:** 2684 2685以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2686 2687| **错误码ID** | **错误信息** | 2688| -------- | ---------------------------- | 2689| 201 | Permission denied. | 2690| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2691| 801 | Capability not supported. | 2692| 2501000 | Operation failed.| 2693 2694**示例:** 2695```ts 2696 import { wifiManager } from '@kit.ConnectivityKit'; 2697 2698 let recvWifiScanStateChangeFunc = (result:number) => { 2699 console.info("Receive Wifi scan state change event: " + result); 2700 } 2701 2702 // Register event 2703 wifiManager.on("wifiScanStateChange", recvWifiScanStateChangeFunc); 2704 2705 // Unregister event 2706 wifiManager.off("wifiScanStateChange", recvWifiScanStateChangeFunc); 2707``` 2708 2709## wifiManager.on('wifiRssiChange')<sup>9+</sup> 2710 2711on(type: 'wifiRssiChange', callback: Callback<number>): void 2712 2713注册RSSI状态改变事件,在业务退出时,要调用off(type: 'wifiRssiChange', callback?: Callback<number>)接口去掉之前的注册回调。 2714 2715**需要权限:** ohos.permission.GET_WIFI_INFO 2716 2717**系统能力:** SystemCapability.Communication.WiFi.STA 2718 2719**参数:** 2720 2721 | **参数名** | **类型** | **必填** | **说明** | 2722 | -------- | -------- | -------- | -------- | 2723 | type | string | 是 | 固定填"wifiRssiChange"字符串。 | 2724 | callback | Callback<number> | 是 | 状态改变回调函数,返回以dBm为单位的RSSI值。 | 2725 2726**错误码:** 2727 2728以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2729 2730| **错误码ID** | **错误信息** | 2731| -------- | ---------------------------- | 2732| 201 | Permission denied. | 2733| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2734| 801 | Capability not supported. | 2735| 2501000 | Operation failed.| 2736 2737## wifiManager.off('wifiRssiChange')<sup>9+</sup> 2738 2739off(type: 'wifiRssiChange', callback?: Callback<number>): void 2740 2741取消注册RSSI状态改变事件。 2742 2743**需要权限:** ohos.permission.GET_WIFI_INFO 2744 2745**系统能力:** SystemCapability.Communication.WiFi.STA 2746 2747**参数:** 2748 2749| **参数名** | **类型** | **必填** | **说明** | 2750| -------- | -------- | -------- | -------- | 2751| type | string | 是 | 固定填"wifiRssiChange"字符串。 | 2752| callback | Callback<number> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 2753 2754**错误码:** 2755 2756以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2757 2758| **错误码ID** | **错误信息** | 2759| -------- | ---------------------------- | 2760| 201 | Permission denied. | 2761| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2762| 801 | Capability not supported. | 2763| 2501000 | Operation failed.| 2764 2765**示例:** 2766```ts 2767 import { wifiManager } from '@kit.ConnectivityKit'; 2768 2769 let recvWifiRssiChangeFunc = (result:number) => { 2770 console.info("Receive wifi rssi change event: " + result); 2771 } 2772 2773 // Register event 2774 wifiManager.on("wifiRssiChange", recvWifiRssiChangeFunc); 2775 2776 // Unregister event 2777 wifiManager.off("wifiRssiChange", recvWifiRssiChangeFunc); 2778``` 2779 2780## wifiManager.on('hotspotStateChange')<sup>9+</sup> 2781 2782on(type: 'hotspotStateChange', callback: Callback<number>): void 2783 2784注册热点状态改变事件,在业务退出时,要调用off(type: 'hotspotStateChange', callback?: Callback<number>)接口去掉之前的注册回调。 2785 2786**需要权限:** ohos.permission.GET_WIFI_INFO 2787 2788**系统能力:** SystemCapability.Communication.WiFi.AP.Core 2789 2790**参数:** 2791 2792| **参数名** | **类型** | **必填** | **说明** | 2793| -------- | -------- | -------- | -------- | 2794| type | string | 是 | 固定填"hotspotStateChange"字符串。 | 2795| callback | Callback<number> | 是 | 状态改变回调函数。 | 2796 2797**热点状态改变事件的枚举:** 2798 2799| **枚举值** | **说明** | 2800| -------- | -------- | 2801| 0 | 未激活。 | 2802| 1 | 已激活。 | 2803| 2 | 激活中。 | 2804| 3 | 去激活中。 | 2805 2806**错误码:** 2807 2808以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2809 2810| **错误码ID** | **错误信息** | 2811| -------- | ---------------------------- | 2812| 201 | Permission denied. | 2813| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2814| 801 | Capability not supported. | 2815| 2601000 | Operation failed. | 2816 2817## wifiManager.off('hotspotStateChange')<sup>9+</sup> 2818 2819off(type: 'hotspotStateChange', callback?: Callback<number>): void 2820 2821取消注册热点状态改变事件。 2822 2823**需要权限:** ohos.permission.GET_WIFI_INFO 2824 2825**系统能力:** SystemCapability.Communication.WiFi.AP.Core 2826 2827**参数:** 2828 2829| **参数名** | **类型** | **必填** | **说明** | 2830| -------- | -------- | -------- | -------- | 2831| type | string | 是 | 固定填"hotspotStateChange"字符串。 | 2832| callback | Callback<number> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 2833 2834**错误码:** 2835 2836以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2837 2838| **错误码ID** | **错误信息** | 2839| -------- | ---------------------------- | 2840| 201 | Permission denied. | 2841| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2842| 801 | Capability not supported. | 2843| 2601000 | Operation failed. | 2844 2845**示例:** 2846```ts 2847 import { wifiManager } from '@kit.ConnectivityKit'; 2848 2849 let recvHotspotStateChangeFunc = (result:number) => { 2850 console.info("Receive hotspot state change event: " + result); 2851 } 2852 2853 // Register event 2854 wifiManager.on("hotspotStateChange", recvHotspotStateChangeFunc); 2855 2856 // Unregister event 2857 wifiManager.off("hotspotStateChange", recvHotspotStateChangeFunc); 2858``` 2859 2860 2861## wifiManager.on('p2pStateChange')<sup>9+</sup> 2862 2863on(type: 'p2pStateChange', callback: Callback<number>): void 2864 2865注册P2P开关状态改变事件,在业务退出时,要调用off(type: 'p2pStateChange', callback?: Callback<number>)接口去掉之前的注册回调。 2866 2867**需要权限:** ohos.permission.GET_WIFI_INFO 2868 2869**系统能力:** SystemCapability.Communication.WiFi.P2P 2870 2871**参数:** 2872 2873| **参数名** | **类型** | **必填** | **说明** | 2874| -------- | -------- | -------- | -------- | 2875| type | string | 是 | 固定填"p2pStateChange"字符串。 | 2876| callback | Callback<number> | 是 | 状态改变回调函数。 | 2877 2878** P2P状态改变事件的枚举:** 2879 2880| **枚举值** | **说明** | 2881| -------- | -------- | 2882| 1 | 空闲。 | 2883| 2 | 打开中。 | 2884| 3 | 已打开。 | 2885| 4 | 关闭中。 | 2886| 5 | 已关闭。 | 2887 2888**错误码:** 2889 2890以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2891 2892| **错误码ID** | **错误信息** | 2893| -------- | ---------------------------- | 2894| 201 | Permission denied. | 2895| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2896| 801 | Capability not supported. | 2897| 2801000 | Operation failed. | 2898 2899## wifiManager.off('p2pStateChange')<sup>9+</sup> 2900 2901off(type: 'p2pStateChange', callback?: Callback<number>): void 2902 2903取消注册P2P开关状态改变事件。 2904 2905**需要权限:** ohos.permission.GET_WIFI_INFO 2906 2907**系统能力:** SystemCapability.Communication.WiFi.P2P 2908 2909**参数:** 2910 2911 | **参数名** | **类型** | **必填** | **说明** | 2912 | -------- | -------- | -------- | -------- | 2913 | type | string | 是 | 固定填"p2pStateChange"字符串。 | 2914 | callback | Callback<number> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 2915 2916**错误码:** 2917 2918以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2919 2920| **错误码ID** | **错误信息** | 2921| -------- | ---------------------------- | 2922| 201 | Permission denied. | 2923| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2924| 801 | Capability not supported. | 2925| 2801000 | Operation failed. | 2926 2927**示例:** 2928```ts 2929 import { wifiManager } from '@kit.ConnectivityKit'; 2930 2931 let recvP2pStateChangeFunc = (result:number) => { 2932 console.info("Receive p2p state change event: " + result); 2933 } 2934 2935 // Register event 2936 wifiManager.on("p2pStateChange", recvP2pStateChangeFunc); 2937 2938 // Unregister event 2939 wifiManager.off("p2pStateChange", recvP2pStateChangeFunc); 2940``` 2941 2942## wifiManager.on('p2pConnectionChange')<sup>9+</sup> 2943 2944on(type: 'p2pConnectionChange', callback: Callback<WifiP2pLinkedInfo>): void 2945 2946注册P2P连接状态改变事件,在业务退出时,要调用off(type: 'p2pConnectionChange', callback?: Callback<WifiP2pLinkedInfo>)接口去掉之前的注册回调。 2947 2948**需要权限:** ohos.permission.GET_WIFI_INFO 2949 2950**系统能力:** SystemCapability.Communication.WiFi.P2P 2951 2952**参数:** 2953 2954 | **参数名** | **类型** | **必填** | **说明** | 2955 | -------- | -------- | -------- | -------- | 2956 | type | string | 是 | 固定填"p2pConnectionChange"字符串。 | 2957 | callback | Callback<[WifiP2pLinkedInfo](#wifip2plinkedinfo9)> | 是 | 状态改变回调函数。 | 2958 2959**错误码:** 2960 2961以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2962 2963| **错误码ID** | **错误信息** | 2964| -------- | ---------------------------- | 2965| 201 | Permission denied. | 2966| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2967| 801 | Capability not supported. | 2968| 2801000 | Operation failed. | 2969 2970## wifiManager.off('p2pConnectionChange')<sup>9+</sup> 2971 2972off(type: 'p2pConnectionChange', callback?: Callback<WifiP2pLinkedInfo>): void 2973 2974取消注册P2P连接状态改变事件。 2975 2976**需要权限:** ohos.permission.GET_WIFI_INFO 2977 2978**系统能力:** SystemCapability.Communication.WiFi.P2P 2979 2980**参数:** 2981 2982 | **参数名** | **类型** | **必填** | **说明** | 2983 | -------- | -------- | -------- | -------- | 2984 | type | string | 是 | 固定填"p2pConnectionChange"字符串。 | 2985 | callback | Callback<[WifiP2pLinkedInfo](#wifip2plinkedinfo9)> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 2986 2987**错误码:** 2988 2989以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 2990 2991| **错误码ID** | **错误信息** | 2992| -------- | ---------------------------- | 2993| 201 | Permission denied. | 2994| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2995| 801 | Capability not supported. | 2996| 2801000 | Operation failed. | 2997 2998**示例:** 2999```ts 3000 import { wifiManager } from '@kit.ConnectivityKit'; 3001 3002 let recvP2pConnectionChangeFunc = (result:wifiManager.WifiP2pLinkedInfo) => { 3003 console.info("Receive p2p connection change event: " + result); 3004 } 3005 3006 // Register event 3007 wifiManager.on("p2pConnectionChange", recvP2pConnectionChangeFunc); 3008 3009 // Unregister event 3010 wifiManager.off("p2pConnectionChange", recvP2pConnectionChangeFunc); 3011``` 3012 3013## wifiManager.on('p2pDeviceChange')<sup>9+</sup> 3014 3015on(type: 'p2pDeviceChange', callback: Callback<WifiP2pDevice>): void 3016 3017注册P2P设备状态改变事件,在业务退出时,要调用off(type: 'p2pDeviceChange', callback?: Callback<WifiP2pDevice>)接口去掉之前的注册回调。 3018 3019**需要权限:** 3020 3021API 10起:ohos.permission.GET_WIFI_INFO 3022 3023**系统能力:** SystemCapability.Communication.WiFi.P2P 3024 3025**参数:** 3026 3027 | **参数名** | **类型** | **必填** | **说明** | 3028 | -------- | -------- | -------- | -------- | 3029 | type | string | 是 | 固定填"p2pDeviceChange"字符串。 | 3030 | callback | Callback<[WifiP2pDevice](#wifip2pdevice9)> | 是 | 状态改变回调函数。 | 3031 3032**错误码:** 3033 3034以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 3035 3036| **错误码ID** | **错误信息** | 3037| -------- | ---------------------------- | 3038| 201 | Permission denied. | 3039| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 3040| 801 | Capability not supported. | 3041| 2801000 | Operation failed. | 3042 3043## wifiManager.off('p2pDeviceChange')<sup>9+</sup> 3044 3045off(type: 'p2pDeviceChange', callback?: Callback<WifiP2pDevice>): void 3046 3047取消注册P2P设备状态改变事件。 3048 3049**系统能力:** SystemCapability.Communication.WiFi.P2P 3050 3051**参数:** 3052 3053 | **参数名** | **类型** | **必填** | **说明** | 3054 | -------- | -------- | -------- | -------- | 3055 | type | string | 是 | 固定填"p2pDeviceChange"字符串。 | 3056 | callback | Callback<[WifiP2pDevice](#wifip2pdevice9)> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 3057 3058**错误码:** 3059 3060以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 3061 3062| **错误码ID** | **错误信息** | 3063| -------- | ---------------------------- | 3064| 201<sup>10+</sup> | Permission denied. | 3065| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 3066| 801 | Capability not supported. | 3067| 2801000 | Operation failed. | 3068 3069**示例:** 3070```ts 3071 import { wifiManager } from '@kit.ConnectivityKit'; 3072 3073 let recvP2pDeviceChangeFunc = (result:wifiManager.WifiP2pDevice) => { 3074 console.info("Receive p2p device change event: " + result); 3075 } 3076 3077 // Register event 3078 wifiManager.on("p2pDeviceChange", recvP2pDeviceChangeFunc); 3079 3080 // Unregister event 3081 wifiManager.off("p2pDeviceChange", recvP2pDeviceChangeFunc); 3082``` 3083 3084## wifiManager.on('p2pPeerDeviceChange')<sup>9+</sup> 3085 3086on(type: 'p2pPeerDeviceChange', callback: Callback<WifiP2pDevice[]>): void 3087 3088注册P2P对端设备状态改变事件,在业务退出时,要调用off(type: 'p2pPeerDeviceChange', callback?: Callback<WifiP2pDevice[]>)接口去掉之前的注册回调。 3089 3090**需要权限:** 3091 3092API 10起:ohos.permission.GET_WIFI_INFO 3093 3094**系统能力:** SystemCapability.Communication.WiFi.P2P 3095 3096**参数:** 3097 3098| **参数名** | **类型** | **必填** | **说明** | 3099| -------- | -------- | -------- | -------- | 3100| type | string | 是 | 固定填"p2pPeerDeviceChange"字符串。 | 3101| callback | Callback<[WifiP2pDevice[]](#wifip2pdevice9)> | 是 | 状态改变回调函数。如果应用申请了ohos.permission.GET_WIFI_PEERS_MAC权限(仅系统应用可申请),则返回结果中的deviceAddress为真实设备地址,否则为随机设备地址。 | 3102 3103**错误码:** 3104 3105以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 3106 3107| **错误码ID** | **错误信息** | 3108| -------- | ---------------------------- | 3109| 201 | Permission denied. | 3110| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 3111| 801 | Capability not supported. | 3112| 2801000 | Operation failed. | 3113 3114## wifiManager.off('p2pPeerDeviceChange')<sup>9+</sup> 3115 3116off(type: 'p2pPeerDeviceChange', callback?: Callback<WifiP2pDevice[]>): void 3117 3118取消注册P2P对端设备状态改变事件。 3119 3120**系统能力:** SystemCapability.Communication.WiFi.P2P 3121 3122**参数:** 3123 3124| **参数名** | **类型** | **必填** | **说明** | 3125| -------- | -------- | -------- | -------- | 3126| type | string | 是 | 固定填"p2pPeerDeviceChange"字符串。 | 3127| callback | Callback<[WifiP2pDevice[]](#wifip2pdevice9)> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。如果应用申请了ohos.permission.GET_WIFI_PEERS_MAC权限(仅系统应用可申请),则返回结果中的deviceAddress为真实设备地址,否则为随机设备地址。 | 3128 3129**错误码:** 3130 3131以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 3132 3133| **错误码ID** | **错误信息** | 3134| -------- | ---------------------------- | 3135| 201<sup>10+</sup> | Permission denied. | 3136| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 3137| 801 | Capability not supported. | 3138| 2801000 | Operation failed. | 3139 3140**示例:** 3141```ts 3142 import { wifiManager } from '@kit.ConnectivityKit'; 3143 3144 let recvP2pPeerDeviceChangeFunc = (result:wifiManager.WifiP2pDevice[]) => { 3145 console.info("Receive p2p peer device change event: " + result); 3146 } 3147 3148 // Register event 3149 wifiManager.on("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc); 3150 3151 // Unregister event 3152 wifiManager.off("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc); 3153``` 3154 3155## wifiManager.on('p2pPersistentGroupChange')<sup>9+</sup> 3156 3157on(type: 'p2pPersistentGroupChange', callback: Callback<void>): void 3158 3159注册P2P永久组状态改变事件,在业务退出时,要调用off(type: 'p2pPersistentGroupChange', callback?: Callback<void>)接口去掉之前的注册回调。 3160 3161**需要权限:** ohos.permission.GET_WIFI_INFO 3162 3163**系统能力:** SystemCapability.Communication.WiFi.P2P 3164 3165**参数:** 3166 3167 | **参数名** | **类型** | **必填** | **说明** | 3168 | -------- | -------- | -------- | -------- | 3169 | type | string | 是 | 固定填"p2pPersistentGroupChange"字符串。 | 3170 | callback | Callback<void> | 是 | 状态改变回调函数。 | 3171 3172**错误码:** 3173 3174以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 3175 3176| **错误码ID** | **错误信息** | 3177| -------- | ---------------------------- | 3178| 201 | Permission denied. | 3179| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 3180| 801 | Capability not supported. | 3181| 2801000 | Operation failed. | 3182 3183## wifiManager.off('p2pPersistentGroupChange')<sup>9+</sup> 3184 3185off(type: 'p2pPersistentGroupChange', callback?: Callback<void>): void 3186 3187取消注册P2P永久组状态改变事件。 3188 3189**需要权限:** ohos.permission.GET_WIFI_INFO 3190 3191**系统能力:** SystemCapability.Communication.WiFi.P2P 3192 3193**参数:** 3194 3195| **参数名** | **类型** | **必填** | **说明** | 3196| -------- | -------- | -------- | -------- | 3197| type | string | 是 | 固定填"p2pPersistentGroupChange"字符串。 | 3198| callback | Callback<void> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 3199 3200**错误码:** 3201 3202以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 3203 3204| **错误码ID** | **错误信息** | 3205| -------- | ---------------------------- | 3206| 201 | Permission denied. | 3207| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 3208| 801 | Capability not supported. | 3209| 2801000 | Operation failed. | 3210 3211**示例:** 3212```ts 3213 import { wifiManager } from '@kit.ConnectivityKit'; 3214 3215 let recvP2pPersistentGroupChangeFunc = (result:void) => { 3216 console.info("Receive p2p persistent group change event: " + result); 3217 } 3218 3219 // Register event 3220 wifiManager.on("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc); 3221 3222 // Unregister event 3223 wifiManager.off("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc); 3224``` 3225 3226## wifiManager.on('p2pDiscoveryChange')<sup>9+</sup> 3227 3228on(type: 'p2pDiscoveryChange', callback: Callback<number>): void 3229 3230注册发现设备状态改变事件,在业务退出时,要调用off(type: 'p2pDiscoveryChange', callback?: Callback<number>)接口去掉之前的注册回调。 3231 3232**需要权限:** ohos.permission.GET_WIFI_INFO 3233 3234**系统能力:** SystemCapability.Communication.WiFi.P2P 3235 3236**参数:** 3237 3238 | **参数名** | **类型** | **必填** | **说明** | 3239 | -------- | -------- | -------- | -------- | 3240 | type | string | 是 | 固定填"p2pDiscoveryChange"字符串。 | 3241 | callback | Callback<number> | 是 | 状态改变回调函数。 | 3242 3243**发现设备状态改变事件的枚举:** 3244 3245| **枚举值** | **说明** | 3246| -------- | -------- | 3247| 0 | 初始状态。 | 3248| 1 | 发现成功。 | 3249 3250**错误码:** 3251 3252以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 3253 3254| **错误码ID** | **错误信息** | 3255| -------- | ---------------------------- | 3256| 201 | Permission denied. | 3257| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 3258| 801 | Capability not supported. | 3259| 2801000 | Operation failed. | 3260 3261## wifiManager.off('p2pDiscoveryChange')<sup>9+</sup> 3262 3263off(type: 'p2pDiscoveryChange', callback?: Callback<number>): void 3264 3265取消注册发现设备状态改变事件。 3266 3267**需要权限:** ohos.permission.GET_WIFI_INFO 3268 3269**系统能力:** SystemCapability.Communication.WiFi.P2P 3270 3271**参数:** 3272 3273 | **参数名** | **类型** | **必填** | **说明** | 3274 | -------- | -------- | -------- | -------- | 3275 | type | string | 是 | 固定填"p2pDiscoveryChange"字符串。 | 3276 | callback | Callback<number> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 3277 3278**错误码:** 3279 3280以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 3281 3282| **错误码ID** | **错误信息** | 3283| -------- | ---------------------------- | 3284| 201 | Permission denied. | 3285| 401 | Invalid parameters. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 3286| 801 | Capability not supported. | 3287| 2801000 | Operation failed. | 3288 3289**示例:** 3290```ts 3291 import { wifiManager } from '@kit.ConnectivityKit'; 3292 3293 let recvP2pDiscoveryChangeFunc = (result:number) => { 3294 console.info("Receive p2p discovery change event: " + result); 3295 } 3296 3297 // Register event 3298 wifiManager.on("p2pDiscoveryChange", recvP2pDiscoveryChangeFunc); 3299 3300 // Unregister event 3301 wifiManager.off("p2pDiscoveryChange", recvP2pDiscoveryChangeFunc); 3302```