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