1# @ohos.wifiManager (WLAN) 2该模块主要提供WLAN基础功能、P2P(peer-to-peer)功能和WLAN消息通知的相应服务,让应用可以通过WLAN和其他设备互联互通。 3 4> **说明:** 5> 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 6 7 8## 导入模块 9 10```ts 11import wifiManager from '@ohos.wifiManager'; 12``` 13 14## wifiManager.enableWifi<sup>9+</sup> 15 16enableWifi(): void 17 18使能WLAN,异步接口,是否打开成功需要注册并监听wifiStateChange的回调。 19 20**系统接口:** 此接口为系统接口。 21 22**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION 仅系统应用可用。 23 24**系统能力:** SystemCapability.Communication.WiFi.STA 25 26**错误码:** 27 28以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 29 30| **错误码ID** | **错误信息** | 31 | -------- | -------- | 32| 2501000 | Operation failed.| 33| 2501003 | Failed to enable Wi-Fi.| 34 35**示例:** 36 37```ts 38 import wifiManager from '@ohos.wifiManager'; 39 40 try { 41 wifiManager.enableWifi(); 42 }catch(error){ 43 console.error("failed:" + JSON.stringify(error)); 44 } 45``` 46 47## wifiManager.disableWifi<sup>9+</sup> 48 49disableWifi(): void 50 51去使能WLAN,异步接口,是否关闭成功需要注册并监听wifiStateChange的回调。 52 53**系统接口:** 此接口为系统接口。 54 55**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 56 57**系统能力:** SystemCapability.Communication.WiFi.STA 58 59**错误码:** 60 61以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 62 63| **错误码ID** | **错误信息** | 64 | -------- | -------- | 65| 2501000 | Operation failed.| 66| 2501004 | Failed to disable Wi-Fi.| 67 68**示例:** 69 70```ts 71 import wifiManager from '@ohos.wifiManager'; 72 73 try { 74 wifiManager.disableWifi(); 75 }catch(error){ 76 console.error("failed:" + JSON.stringify(error)); 77 } 78``` 79 80## wifiManager.isWifiActive<sup>9+</sup> 81 82isWifiActive(): boolean 83 84查询WLAN是否已使能。 85 86**需要权限:** ohos.permission.GET_WIFI_INFO 87 88**系统能力:** SystemCapability.Communication.WiFi.STA 89 90**返回值:** 91 92 | **类型** | **说明** | 93 | -------- | -------- | 94 | boolean | true:已使能, false:未使能。 | 95 96**错误码:** 97 98以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 99 100| **错误码ID** | **错误信息** | 101 | -------- | -------- | 102| 2501000 | Operation failed.| 103 104**示例:** 105 106```ts 107 import wifiManager from '@ohos.wifiManager'; 108 109 try { 110 let isWifiActive = wifiManager.isWifiActive(); 111 console.info("isWifiActive:" + isWifiActive); 112 }catch(error){ 113 console.error("failed:" + JSON.stringify(error)); 114 } 115``` 116 117## wifiManager.scan<sup>9+</sup> 118 119scan(): void 120 121启动WLAN扫描。 122 123**需要权限:** ohos.permission.SET_WIFI_INFO、ohos.permission.LOCATION 和 ohos.permission.APPROXIMATELY_LOCATION 124 125**系统能力:** SystemCapability.Communication.WiFi.STA 126 127**错误码:** 128 129以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 130 131| **错误码ID** | **错误信息** | 132 | -------- | -------- | 133| 2501000 | Operation failed.| 134 135**示例:** 136 137```ts 138 import wifiManager from '@ohos.wifiManager'; 139 140 try { 141 wifiManager.scan(); 142 }catch(error){ 143 console.error("failed:" + JSON.stringify(error)); 144 } 145``` 146 147## wifiManager.startScan<sup>10+</sup> 148 149startScan(): void 150 151**系统接口:** 此接口为系统接口。 152 153启动WLAN扫描。 154 155**需要权限:** ohos.permission.SET_WIFI_INFO 和ohos.permission.MANAGE_WIFI_CONNECTION 156 157**系统能力:** SystemCapability.Communication.WiFi.STA 158 159**错误码:** 160 161以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 162 163| **错误码ID** | **错误信息** | 164 | -------- | -------- | 165| 2501000 | Operation failed.| 166 167**示例:** 168 169```ts 170 import wifiManager from '@ohos.wifiManager'; 171 172 try { 173 wifiManager.startScan(); 174 }catch(error){ 175 console.error("failed:" + JSON.stringify(error)); 176 } 177``` 178## wifiManager.getScanResults<sup>9+</sup> 179 180getScanResults(): Promise<Array<WifiScanInfo>> 181 182获取扫描结果,使用Promise异步回调。 183 184**需要权限:** ohos.permission.GET_WIFI_INFO 和 (ohos.permission.GET_WIFI_PEERS_MAC 或(ohos.permission.LOCATION 和 ohos.permission.APPROXIMATELY_LOCATION)) 185 186**系统能力:** SystemCapability.Communication.WiFi.STA 187 188**返回值:** 189 190| **类型** | **说明** | 191| -------- | -------- | 192| Promise< Array<[WifiScanInfo](#wifiscaninfo)> > | Promise对象。返回扫描到的热点列表。 | 193 194**错误码:** 195 196以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 197 198| **错误码ID** | **错误信息** | 199| -------- | -------- | 200| 2501000 | Operation failed.| 201 202## wifiManager.getScanResults<sup>9+</sup> 203 204getScanResults(callback: AsyncCallback<Array<WifiScanInfo>>): void 205 206获取扫描结果,使用callback异步回调。 207 208**需要权限:** ohos.permission.GET_WIFI_INFO 和 (ohos.permission.GET_WIFI_PEERS_MAC 或 (ohos.permission.LOCATION 和 ohos.permission.APPROXIMATELY_LOCATION)) 209 210**系统能力:** SystemCapability.Communication.WiFi.STA 211 212**参数:** 213| **参数名** | **类型** | **必填** | **说明** | 214| -------- | -------- | -------- | -------- | 215| callback | AsyncCallback< Array<[WifiScanInfo](#wifiscaninfo)>> | 是 | 回调函数。当成功时,err为0,data为扫描到的热点;否则err为非0值,data为空。 | 216 | Array<[WifiScanInfo](#wifiscaninfo)> | 返回扫描到的热点列表。 | 217 218**错误码:** 219 220以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 221 222| **错误码ID** | **错误信息** | 223| -------- | -------- | 224| 2501000 | Operation failed.| 225 226**示例:** 227```ts 228 import wifiManager from '@ohos.wifiManager'; 229 230 wifiManager.getScanResults((err, result) => { 231 if (err) { 232 console.error("get scan info error"); 233 return; 234 } 235 236 let len = result.length; 237 console.log("wifi received scan info: " + len); 238 for (let i = 0; i < len; ++i) { 239 console.info("ssid: " + result[i].ssid); 240 console.info("bssid: " + result[i].bssid); 241 console.info("capabilities: " + result[i].capabilities); 242 console.info("securityType: " + result[i].securityType); 243 console.info("rssi: " + result[i].rssi); 244 console.info("band: " + result[i].band); 245 console.info("frequency: " + result[i].frequency); 246 console.info("channelWidth: " + result[i].channelWidth); 247 console.info("timestamp: " + result[i].timestamp); 248 } 249 }); 250 251 wifiManager.getScanResults().then(result => { 252 let len = result.length; 253 console.log("wifi received scan info: " + len); 254 for (let i = 0; i < len; ++i) { 255 console.info("ssid: " + result[i].ssid); 256 console.info("bssid: " + result[i].bssid); 257 console.info("capabilities: " + result[i].capabilities); 258 console.info("securityType: " + result[i].securityType); 259 console.info("rssi: " + result[i].rssi); 260 console.info("band: " + result[i].band); 261 console.info("frequency: " + result[i].frequency); 262 console.info("channelWidth: " + result[i].channelWidth); 263 console.info("timestamp: " + result[i].timestamp); 264 } 265 }); 266``` 267 268## wifiManager.getScanResultsSync<sup>9+</sup> 269 270getScanResultsSync(): Array<[WifiScanInfo](#wifiscaninfo)> 271 272获取扫描结果,使用同步方式返回结果。 273 274**需要权限:** ohos.permission.GET_WIFI_INFO 和 (ohos.permission.GET_WIFI_PEERS_MAC 或 (ohos.permission.LOCATION 和 ohos.permission.APPROXIMATELY_LOCATION)) 275 276**系统能力:** SystemCapability.Communication.WiFi.STA 277 278**返回值:** 279 280| **类型** | **说明** | 281| -------- | -------- | 282| Array<[WifiScanInfo](#wifiscaninfo)> | 扫描结果数组。 | 283 284**错误码:** 285 286以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 287 288| **错误码ID** | **错误信息** | 289 | -------- | -------- | 290| 2501000 | Operation failed.| 291 292**示例:** 293 294```ts 295 import wifiManager from '@ohos.wifiManager'; 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获取扫描结果。 326 327**需要权限:** ohos.permission.GET_WIFI_INFO 328 329**系统能力:** SystemCapability.Communication.WiFi.STA 330 331**返回值:** 332 333| **类型** | **说明** | 334| -------- | -------- | 335| Array<[WifiScanInfo](#wifiscaninfo)> | 返回扫描到的热点列表。如果应用申请了ohos.permission.GET_WIFI_PEERS_MAC权限,则返回结果中的bssid为真实设备地址,否则为随机设备地址。 | 336 337**错误码:** 338 339以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 340 341| **错误码ID** | **错误信息** | 342 | -------- | -------- | 343| 2501000 | Operation failed.| 344 345**示例:** 346 347```ts 348 import wifiManager from '@ohos.wifiManager'; 349 350 try { 351 let scanInfoList = wifiManager.getScanInfoList(); 352 console.info("scanInfoList:" + JSON.stringify(scanInfoList)); 353 let len = scanInfoList.length; 354 console.log("wifi received scan info: " + len); 355 if(len > 0){ 356 for (let i = 0; i < len; ++i) { 357 console.info("ssid: " + scanInfoList[i].ssid); 358 console.info("bssid: " + scanInfoList[i].bssid); 359 console.info("capabilities: " + scanInfoList[i].capabilities); 360 console.info("securityType: " + scanInfoList[i].securityType); 361 console.info("rssi: " + scanInfoList[i].rssi); 362 console.info("band: " + scanInfoList[i].band); 363 console.info("frequency: " + scanInfoList[i].frequency); 364 console.info("channelWidth: " + scanInfoList[i].channelWidth); 365 console.info("timestamp: " + scanInfoList[i].timestamp); 366 } 367 } 368 }catch(error){ 369 console.error("failed:" + JSON.stringify(error)); 370 } 371 372``` 373 374## WifiScanInfo<sup>9+</sup> 375 376WLAN热点信息。 377 378**系统能力:** SystemCapability.Communication.WiFi.STA 379 380 381| **名称** | **类型** | **可读** | **可写** | **说明** | 382| -------- | -------- | -------- | -------- | -------- | 383| ssid | string | 是 | 否 | 热点的SSID,编码格式为UTF-8。 | 384| bssid | string | 是 | 否 | 热点的BSSID。 | 385| bssidType<sup>10+</sup>| DeviceAddressType | 是 | 否 | 热点的BSSID类型。 | 386| capabilities | string | 是 | 否 | 热点能力。 | 387| securityType | [WifiSecurityType](#wifisecuritytype) | 是 | 否 | WLAN加密类型。 | 388| rssi | number | 是 | 否 | 热点的信号强度(dBm)。 | 389| band | number | 是 | 否 | WLAN接入点的频段。 | 390| frequency | number | 是 | 否 | WLAN接入点的频率。 | 391| channelWidth | number | 是 | 否 | WLAN接入点的带宽。 | 392| centerFrequency0 | number | 是 | 否 | 热点的中心频率。 | 393| centerFrequency1 | number | 是 | 否 | 热点的中心频率。如果热点使用两个不重叠的WLAN信道,则返回两个中心频率,分别用centerFrequency0和centerFrequency1表示。 | 394| infoElems | Array<[WifiInfoElem](#wifiinfoelem9)> | 是 | 否 | 信息元素。 | 395| timestamp | number | 是 | 否 | 时间戳。 | 396 397## DeviceAddressType <sup>10+</sup> 398 399wifi 设备地址(mac/bissid)类型。 400 401**系统能力:** SystemCapability.Communication.WiFi.Core 402 403| **名称** | **值** | **说明** | 404| -------- | -------- | -------- | 405| RANDOM_DEVICE_ADDRESS | 0 | 随机设备地址。 | 406| REAL_DEVICE_ADDRESS | 1 | 真实设备地址。 | 407 408## WifiSecurityType<sup>9+</sup> 409 410表示加密类型的枚举。 411 412**系统能力:** SystemCapability.Communication.WiFi.Core 413 414 415| **名称** | **值** | **说明** | 416| -------- | -------- | -------- | 417| WIFI_SEC_TYPE_INVALID | 0 | 无效加密类型。 | 418| WIFI_SEC_TYPE_OPEN | 1 | 开放加密类型。 | 419| WIFI_SEC_TYPE_WEP | 2 | Wired Equivalent Privacy (WEP)加密类型。 | 420| WIFI_SEC_TYPE_PSK | 3 | Pre-shared key (PSK)加密类型。 | 421| WIFI_SEC_TYPE_SAE | 4 | Simultaneous Authentication of Equals (SAE)加密类型。 | 422| WIFI_SEC_TYPE_EAP<sup>9+</sup> | 5 | EAP加密类型。 | 423| WIFI_SEC_TYPE_EAP_SUITE_B<sup>9+</sup> | 6 | Suite-B 192位加密类型。 | 424| WIFI_SEC_TYPE_OWE<sup>9+</sup> | 7 | 机会性无线加密类型。 | 425| WIFI_SEC_TYPE_WAPI_CERT<sup>9+</sup> | 8 | WAPI-Cert加密类型。 | 426| WIFI_SEC_TYPE_WAPI_PSK<sup>9+</sup> | 9 | WAPI-PSK加密类型。 | 427 428 429## WifiBandType<sup>10+</sup> 430 431表示WIFI频段类型的枚举。 432 433**系统能力:** SystemCapability.Communication.WiFi.STA 434 435| **名称** | **值** | **说明** | 436| -------- | -------- | -------- | 437| WIFI_BAND_NONE | 0 | 无效频段类型。 | 438| WIFI_BAND_2G | 1 | 2.4G频段类型。 | 439| WIFI_BAND_5G | 2 | 5G频段类型。 | 440| WIFI_BAND_6G | 3 | 6G频段类型。 | 441| WIFI_BAND_60G | 4 | 60G频段类型。 | 442 443## WifiStandard<sup>10+</sup> 444 445表示WIFI标准的枚举。 446 447**系统能力:** SystemCapability.Communication.WiFi.STA 448 449| **名称** | **值** | **说明** | 450| -------- | -------- | -------- | 451| WIFI_STANDARD_UNDEFINED | 0 | 无效WIFI标准类型。 | 452| WIFI_STANDARD_11A | 1 | 802.11a WiFi标准类型。 | 453| WIFI_STANDARD_11B | 2 | 802.11b WiFi标准类型。 | 454| WIFI_STANDARD_11G | 3 | 802.11g WiFi标准类型。 | 455| WIFI_STANDARD_11N | 4 | 802.11n WiFi标准类型。 | 456| WIFI_STANDARD_11AC | 5 | 802.11ac WiFi标准类型。 | 457| WIFI_STANDARD_11AX | 6 | 802.11ax WiFi标准类型。 | 458| WIFI_STANDARD_11AD | 7 | 802.11ad WiFi标准类型。 | 459 460## WifiInfoElem<sup>9+</sup> 461 462WLAN热点信息。 463 464**系统能力:** SystemCapability.Communication.WiFi.STA 465 466 467| **名称** | **类型** | **可读** | **可写** | **说明** | 468| -------- | -------- | -------- | -------- | -------- | 469| eid | number | 是 | 否 | 元素ID。 | 470| content | Uint8Array | 是 | 否 | 元素内容。 | 471 472 473## WifiChannelWidth<sup>9+</sup> 474 475表示带宽类型的枚举。 476 477**系统能力:** SystemCapability.Communication.WiFi.STA 478 479 480| **名称** | **值** | **说明** | 481| -------- | -------- | -------- | 482| WIDTH_20MHZ | 0 | 20MHZ。 | 483| WIDTH_40MHZ | 1 | 40MHZ。 | 484| WIDTH_80MHZ | 2 | 80MHZ。 | 485| WIDTH_160MHZ | 3 | 160MHZ。 | 486| WIDTH_80MHZ_PLUS | 4 | 80MHZ<sup>+</sup>。 | 487| WIDTH_INVALID | 5 | 无效值 | 488 489## wifiManager.setScanAlwaysAllowed<sup>10+</sup> 490 491setScanAlwaysAllowed(isScanAlwaysAllowed: boolean): void 492 493设置是否始终允许扫描。 494 495**系统接口:** 此接口为系统接口。 496 497**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.SET_WIFI_CONFIG 498 499**系统能力:** SystemCapability.Communication.WiFi.STA 500 501**参数:** 502 503| **参数名** | **类型** | **必填** | **说明** | 504| -------- | -------- | -------- | -------- | 505| isScanAlwaysAllowed | boolean | 是 | 是否始终允许扫描。 | 506 507**错误码:** 508 509以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 510 511| **错误码ID** | **错误信息** | 512 | -------- | -------- | 513| 2501000 | Operation failed.| 514 515```ts 516 import wifiManager from '@ohos.wifiManager'; 517 518 try { 519 let isScanAlwaysAllowed = true; 520 wifiManager.setScanAlwaysAllowed(isScanAlwaysAllowed); 521 }catch(error){ 522 console.error("failed:" + JSON.stringify(error)); 523 } 524``` 525 526## wifiManager.getScanAlwaysAllowed<sup>10+</sup> 527 528getScanAlwaysAllowed(): boolean 529 530获取是否始终允许扫描。 531 532**系统接口:** 此接口为系统接口。 533 534**需要权限:** ohos.permission.GET_WIFI_INFO 和 ohos.permission.GET_WIFI_CONFIG 535 536**系统能力:** SystemCapability.Communication.WiFi.STA 537 538**返回值:** 539 540| **类型** | **说明** | 541| -------- | -------- | 542| boolean| 是否始终允许扫描。 true 表示允许触发扫描,false表示在禁用wifi时不允许触发扫描| 543 544**错误码:** 545 546以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 547 548| **错误码ID** | **错误信息** | 549 | -------- | -------- | 550| 2501000 | Operation failed.| 551 552**示例:** 553 554```ts 555 import wifiManager from '@ohos.wifiManager'; 556 557 try { 558 let isScanAlwaysAllowed = wifiManager.getScanAlwaysAllowed(); 559 console.info("isScanAlwaysAllowed:" + isScanAlwaysAllowed); 560 }catch(error){ 561 console.error("failed:" + JSON.stringify(error)); 562 } 563``` 564 565## wifiManager.addDeviceConfig<sup>9+</sup> 566 567addDeviceConfig(config: WifiDeviceConfig): Promise<number> 568 569添加网络配置,使用Promise异步回调。 570 571**系统接口:** 此接口为系统接口。 572 573**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.SET_WIFI_CONFIG 574 575**系统能力:** SystemCapability.Communication.WiFi.STA 576 577**参数:** 578 579| **参数名** | **类型** | **必填** | **说明** | 580| -------- | -------- | -------- | -------- | 581| config | [WifiDeviceConfig](#wifideviceconfig) | 是 | WLAN配置信息。如果bssidType未指定值,则bssidType默认为随机设备地址类型。 | 582 583**返回值:** 584 585 | **类型** | **说明** | 586 | -------- | -------- | 587 | Promise<number> | Promise对象。返回添加的网络配置ID,如果值为-1表示添加失败。 | 588 589**错误码:** 590 591以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 592 593| **错误码ID** | **错误信息** | 594 | -------- | -------- | 595| 2501000 | Operation failed.| 596 597**示例:** 598 599```ts 600 import wifiManager from '@ohos.wifiManager'; 601 602 try { 603 let config:wifiManager.WifiDeviceConfig = { 604 ssid : "****", 605 preSharedKey : "****", 606 securityType : 0 607 } 608 wifiManager.addDeviceConfig(config).then(result => { 609 console.info("result:" + JSON.stringify(result)); 610 }); 611 }catch(error){ 612 console.error("failed:" + JSON.stringify(error)); 613 } 614``` 615 616## WifiDeviceConfig<sup>9+</sup> 617 618WLAN配置信息。 619 620**系统能力:** SystemCapability.Communication.WiFi.STA 621 622 623| **名称** | **类型** | **可读** | **可写** | **说明** | 624| -------- | -------- | -------- | -------- | -------- | 625| ssid | string | 是 | 否 | 热点的SSID,编码格式为UTF-8。 | 626| bssid | string | 是 | 否 | 热点的BSSID。 | 627| bssidType<sup>10+</sup> | DeviceAddressType | 是 | 否 | 热点的BSSID类型。 | 628| preSharedKey | string | 是 | 否 | 热点的密钥。 | 629| isHiddenSsid | boolean | 是 | 否 | 是否是隐藏网络。 | 630| securityType | [WifiSecurityType](#wifisecuritytype) | 是 | 否 | 加密类型。 | 631| creatorUid | number | 是 | 否 | 创建用户的ID。 <br /> **系统接口:** 此接口为系统接口。 | 632| disableReason | number | 是 | 否 | 禁用原因。 <br /> **系统接口:** 此接口为系统接口。 | 633| netId | number | 是 | 否 | 分配的网络ID。 <br /> **系统接口:** 此接口为系统接口。 | 634| randomMacType | number | 是 | 否 | MAC地址类型。0 - 随机MAC地址,1 - 设备MAC地址 <br /> **系统接口:** 此接口为系统接口。 | 635| randomMacAddr | string | 是 | 否 | MAC地址。<br /> **系统接口:** 此接口为系统接口。 | 636| ipType | [IpType](#iptype9) | 是 | 否 | IP地址类型。 <br /> **系统接口:** 此接口为系统接口。 | 637| staticIp | [IpConfig](#ipconfig9) | 是 | 否 | 静态IP配置信息。 <br /> **系统接口:** 此接口为系统接口。 | 638| eapConfig<sup>10+</sup> | [WifiEapConfig](#wifieapconfig10) | 是 | 否 | 可扩展身份验证协议配置。 | 639| proxyConfig<sup>10+</sup> | WifiProxyConfig | 是 | 否 | 代理配置。 <br /> **系统接口:** 此接口为系统接口。| 640 641## IpType<sup>9+</sup> 642 643表示IP类型的枚举。 644 645**系统接口:** 此接口为系统接口。 646 647**系统能力:** SystemCapability.Communication.WiFi.STA 648 649 650| 名称 | 值 | 说明 | 651| -------- | -------- | -------- | 652| STATIC | 0 | 静态IP。 | 653| DHCP | 1 | 通过DHCP获取。 | 654| UNKNOWN | 2 | 未指定。 | 655 656 657## IpConfig<sup>9+</sup> 658 659IP配置信息。 660 661**系统接口:** 此接口为系统接口。 662 663**系统能力:** SystemCapability.Communication.WiFi.STA 664 665| **名称** | **类型** | **可读** | **可写** | **说明** | 666| -------- | -------- | -------- | -------- | -------- | 667| ipAddress | number | 是 | 否 | IP地址。 | 668| gateway | number | 是 | 否 | 网关。 | 669| prefixLength | number | 是 | 否 | 掩码。 | 670| dnsServers | number[] | 是 | 否 | DNS服务器。 | 671| domains | Array<string> | 是 | 否 | 域信息。 | 672 673 674## WifiEapConfig<sup>10+</sup> 675 676可扩展身份验证协议配置信息。 677 678**系统能力:** SystemCapability.Communication.WiFi.STA 679 680| **名称** | **类型** | **可读** | **可写** | **说明** | 681| -------- | -------- | -------- | -------- | -------- | 682| eapMethod | [EapMethod](#eapmethod10) | 是 | 否 | EAP认证方式。 | 683| phase2Method | [Phase2Method](#phase2method10) | 是 | 否 | 第二阶段认证方式。 | 684| identity | string | 是 | 否 | 身份信息。 | 685| anonymousIdentity | string | 是 | 否 | 匿名身份。 | 686| password | string | 是 | 否 | 密码。 | 687| caCertAlias | string | 是 | 否 | CA 证书别名。 | 688| caPath | string | 是 | 否 | CA 证书路径。 | 689| clientCertAlias | string | 是 | 否 | 客户端证书别名。 | 690| certEntry | Uint8Array | 是 | 是 | CA 证书内容。 | 691| certPassword | string | 是 | 是 | CA证书密码。 | 692| altSubjectMatch | string | 是 | 否 | 替代主题匹配。 | 693| domainSuffixMatch | string | 是 | 否 | 域后缀匹配。 | 694| realm | string | 是 | 否 | 通行证凭证的领域。 | 695| plmn | string | 是 | 否 | 公共陆地移动网的直通凭证提供商。 | 696| eapSubId | number | 是 | 否 | SIM卡的子ID。 | 697 698 699## EapMethod<sup>10+</sup> 700 701表示EAP认证方式的枚举。 702 703**系统能力:** SystemCapability.Communication.WiFi.STA 704 705| 名称 | 值 | 说明 | 706| -------- | -------- | -------- | 707| EAP_NONE | 0 | 不指定。 | 708| EAP_PEAP | 1 | PEAP类型。 | 709| EAP_TLS | 2 | TLS类型。 | 710| EAP_TTLS | 3 | TTLS类型。 | 711| EAP_PWD | 4 | PWD类型。 | 712| EAP_SIM | 5 | SIM类型。 | 713| EAP_AKA | 6 | AKA类型。 | 714| EAP_AKA_PRIME | 7 | AKA Prime类型。 | 715| EAP_UNAUTH_TLS | 8 | UNAUTH TLS类型。 | 716 717 718## Phase2Method<sup>10+</sup> 719 720表示第二阶段认证方式的枚举。 721 722**系统能力:** SystemCapability.Communication.WiFi.STA 723 724| 名称 | 值 | 说明 | 725| -------- | -------- | -------- | 726| PHASE2_NONE | 0 | 不指定。 | 727| PHASE2_PAP | 1 | PAP类型。 | 728| PHASE2_MSCHAP | 2 | MSCHAP类型。 | 729| PHASE2_MSCHAPV2 | 3 | MSCHAPV2类型。 | 730| PHASE2_GTC | 4 | GTC类型。 | 731| PHASE2_SIM | 5 | SIM类型。 | 732| PHASE2_AKA | 6 | AKA类型。 | 733| PHASE2_AKA_PRIME | 7 | AKA Prime类型。 | 734 735 736## WifiProxyConfig <sup>10+</sup> 737 738Wifi 代理配置。 739 740**系统接口:** 此接口为系统接口。 741 742**系统能力:** SystemCapability.Communication.WiFi.STA 743 744| **名称** | **类型** | **可读** | **可写** | **说明** | 745| -------- | -------- | -------- | -------- | -------- | 746| proxyMethod | ProxyMethod | 是 | 否 | 代理方法 | 747| pacWebAddress | string | 是 | 否 | 自动配置代理的PAC web 地址。 | 748| serverHostName | string | 是 | 否 | 手动配置代理的服务器主机名。 | 749| serverPort | string | 是 | 否 | 手动配置代理的服务器端口。 | 750| exclusionObjects | string | 是 | 否 | 手动配置代理的排除对象,对象用“,”分隔。| 751 752## ProxyMethod<sup>10+</sup> 753 754表示WiFi代理方法的枚举。 755 756**系统接口:** 此接口为系统接口。 757 758**系统能力:** SystemCapability.Communication.WiFi.STA 759 760| 名称 | 值 | 说明 | 761| -------- | -------- | -------- | 762| METHOD_NONE | 0 | 不使用代理。 | 763| METHOD_AUTO | 1 | 使用自动配置的代理。 | 764| METHOD_MANUAL | 2 | 使用手动配置的代理。 | 765 766## wifiManager.addDeviceConfig<sup>9+</sup> 767 768addDeviceConfig(config: WifiDeviceConfig, callback: AsyncCallback<number>): void 769 770添加网络配置,使用callback异步回调。 771 772**系统接口:** 此接口为系统接口。 773 774**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.SET_WIFI_CONFIG 775 776**系统能力:** SystemCapability.Communication.WiFi.STA 777 778**参数:** 779 780| **参数名** | **类型** | **必填** | **说明** | 781| -------- | -------- | -------- | -------- | 782| config | [WifiDeviceConfig](#wifideviceconfig) | 是 | WLAN配置信息。如果bssidType未指定值,则bssidType默认为随机设备地址类型。 | 783| callback | AsyncCallback<number> | 是 | 回调函数。当操作成功时,err为0,data为添加的网络配置ID,如果data值为-1,表示添加失败。当error为非0,表示处理出现错误。 | 784 785**错误码:** 786 787以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 788 789| **错误码ID** | **错误信息** | 790 | -------- | -------- | 791| 2501000 | Operation failed.| 792 793**示例:** 794 795```ts 796 import wifiManager from '@ohos.wifiManager'; 797 798 try { 799 let config:wifiManager.WifiDeviceConfig = { 800 ssid : "****", 801 preSharedKey : "****", 802 securityType : 0 803 } 804 wifiManager.addDeviceConfig(config,(error,result) => { 805 console.info("result:" + JSON.stringify(result)); 806 }); 807 }catch(error){ 808 console.error("failed:" + JSON.stringify(error)); 809 } 810``` 811 812## wifiManager.addCandidateConfig<sup>9+</sup> 813 814addCandidateConfig(config: WifiDeviceConfig): Promise<number> 815 816添加候选网络配置,使用Promise异步回调。 817 818**需要权限:** ohos.permission.SET_WIFI_INFO 819 820**系统能力:** SystemCapability.Communication.WiFi.STA 821 822**参数:** 823 824| **参数名** | **类型** | **必填** | **说明** | 825| -------- | -------- | -------- | -------- | 826| config | [WifiDeviceConfig](#wifideviceconfig) | 是 | WLAN配置信息。如果bssidType未指定值,则bssidType默认为随机设备地址类型。 | 827 828**返回值:** 829 830 | **类型** | **说明** | 831 | -------- | -------- | 832 | Promise<number> | Promise对象。表示网络配置ID。 | 833 834**错误码:** 835 836以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 837 838| **错误码ID** | **错误信息** | 839 | -------- | -------- | 840| 2501000 | Operation failed.| 841 842**示例:** 843`````ts 844 import wifiManager from '@ohos.wifiManager'; 845 846 try { 847 let config:wifiManager.WifiDeviceConfig = { 848 ssid : "****", 849 preSharedKey : "****", 850 securityType : 0 851 } 852 wifiManager.addCandidateConfig(config).then(result => { 853 console.info("result:" + JSON.stringify(result)); 854 }); 855 }catch(error){ 856 console.error("failed:" + JSON.stringify(error)); 857 } 858````` 859 860## wifiManager.addCandidateConfig<sup>9+</sup> 861 862addCandidateConfig(config: WifiDeviceConfig, callback: AsyncCallback<number>): void 863 864添加候选网络配置,使用callback异步回调。 865 866**需要权限:** ohos.permission.SET_WIFI_INFO 867 868**系统能力:** SystemCapability.Communication.WiFi.STA 869 870**参数:** 871 872| **参数名** | **类型** | **必填** | **说明** | 873| -------- | -------- | -------- | -------- | 874| config | [WifiDeviceConfig](#wifideviceconfig) | 是 | WLAN配置信息。如果bssidType未指定值,则bssidType默认为随机设备地址类型。 | 875| callback | AsyncCallback<number> | 是 | 回调函数。当操作成功时,err为0,data为添加的网络配置ID,如果data值为-1,表示添加失败。如果操作出现错误,err为非0值。 | 876 877**错误码:** 878 879以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 880 881| **错误码ID** | **错误信息** | 882 | -------- | -------- | 883| 2501000 | Operation failed.| 884 885**示例:** 886`````ts 887 import wifiManager from '@ohos.wifiManager'; 888 889 try { 890 let config:wifiManager.WifiDeviceConfig = { 891 ssid : "****", 892 preSharedKey : "****", 893 securityType : 0 894 } 895 wifiManager.addCandidateConfig(config,(error,result) => { 896 console.info("result:" + JSON.stringify(result)); 897 }); 898 }catch(error){ 899 console.error("failed:" + JSON.stringify(error)); 900 } 901````` 902 903## wifiManager.removeCandidateConfig<sup>9+</sup> 904 905removeCandidateConfig(networkId: number): Promise<void> 906 907移除候选网络配置,使用Promise异步回调。 908 909**需要权限:** ohos.permission.SET_WIFI_INFO 910 911**系统能力:** SystemCapability.Communication.WiFi.STA 912 913**参数:** 914 915 | **参数名** | **类型** | **必填** | **说明** | 916 | -------- | -------- | -------- | -------- | 917 | networkId | number | 是 | 网络配置ID。 | 918 919**返回值:** 920 921 | **类型** | **说明** | 922 | -------- | -------- | 923 | Promise<void> | Promise对象。 | 924 925**错误码:** 926 927以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 928 929| **错误码ID** | **错误信息** | 930 | -------- | -------- | 931| 2501000 | Operation failed.| 932 933**示例:** 934 935```ts 936 import wifiManager from '@ohos.wifiManager'; 937 938 try { 939 let networkId = 0; 940 wifiManager.removeCandidateConfig(networkId).then(result => { 941 console.info("result:" + JSON.stringify(result)); 942 }); 943 }catch(error){ 944 console.error("failed:" + JSON.stringify(error)); 945 } 946``` 947 948## wifiManager.removeCandidateConfig<sup>9+</sup> 949 950removeCandidateConfig(networkId: number, callback: AsyncCallback<void>): void 951 952移除候选网络配置,使用callback异步回调。 953 954**需要权限:** ohos.permission.SET_WIFI_INFO 955 956**系统能力:** SystemCapability.Communication.WiFi.STA 957 958**参数:** 959 960 | **参数名** | **类型** | **必填** | **说明** | 961 | -------- | -------- | -------- | -------- | 962 | networkId | number | 是 | 网络配置ID。 | 963 | callback | AsyncCallback<void> | 是 | 回调函数。当操作成功时,err为0。如果error为非0,表示处理出现错误。 | 964 965**错误码:** 966 967以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 968 969| **错误码ID** | **错误信息** | 970 | -------- | -------- | 971| 2501000 | Operation failed.| 972 973**示例:** 974```ts 975 import wifiManager from '@ohos.wifiManager'; 976 977 try { 978 let networkId = 0; 979 wifiManager.removeCandidateConfig(networkId,(error,result) => { 980 console.info("result:" + JSON.stringify(result)); 981 }); 982 }catch(error){ 983 console.error("failed:" + JSON.stringify(error)); 984 } 985``` 986 987## wifiManager.getCandidateConfigs<sup>9+</sup> 988 989getCandidateConfigs(): Array<[WifiDeviceConfig](#wifideviceconfig)> 990 991获取候选网络配置。 992 993**需要权限:** 994 995API 9:ohos.permission.GET_WIFI_INFO、ohos.permission.LOCATION 和 ohos.permission.APPROXIMATELY_LOCATION 996 997API 10起:ohos.permission.GET_WIFI_INFO 998 999**系统能力:** SystemCapability.Communication.WiFi.STA 1000 1001**返回值:** 1002 1003 | **类型** | **说明** | 1004 | -------- | -------- | 1005 | Array<[WifiDeviceConfig](#wifideviceconfig)> | 候选网络配置数组。 | 1006 1007**错误码:** 1008 1009以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 1010 1011| **错误码ID** | **错误信息** | 1012 | -------- | -------- | 1013| 2501000 | Operation failed.| 1014 1015**示例:** 1016 1017`````ts 1018 import wifiManager from '@ohos.wifiManager'; 1019 1020 try { 1021 let configs = wifiManager.getCandidateConfigs(); 1022 console.info("configs:" + JSON.stringify(configs)); 1023 let len = configs.length; 1024 console.log("result len: " + len); 1025 if(len > 0){ 1026 for (let i = 0; i < len; ++i) { 1027 console.info("ssid: " + configs[i].ssid); 1028 console.info("bssid: " + configs[i].bssid); 1029 } 1030 } 1031 }catch(error){ 1032 console.error("failed:" + JSON.stringify(error)); 1033 } 1034 1035````` 1036 1037## wifiManager.connectToCandidateConfig<sup>9+</sup> 1038 1039connectToCandidateConfig(networkId: number): void 1040 1041应用使用该接口连接到自己添加的候选网络(如果当前已经连接到热点,需要先断开连接)。 1042 1043**需要权限:** ohos.permission.SET_WIFI_INFO 1044 1045**系统能力:** SystemCapability.Communication.WiFi.STA 1046 1047**参数:** 1048 1049 | **参数名** | **类型** | **必填** | **说明** | 1050 | -------- | -------- | -------- | -------- | 1051 | networkId | number | 是 | 候选网络配置的ID。 | 1052 1053**错误码:** 1054 1055以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 1056 1057| **错误码ID** | **错误信息** | 1058 | -------- | -------- | 1059| 2501000 | Operation failed.| 1060| 2501001 | Wifi is closed.| 1061 1062**示例:** 1063```ts 1064 import wifiManager from '@ohos.wifiManager'; 1065 1066 try { 1067 let networkId = 0; 1068 let ret = wifiManager.connectToCandidateConfig(networkId); 1069 console.info("result:" + ret); 1070 }catch(error){ 1071 console.error("failed:" + JSON.stringify(error)); 1072 } 1073 1074``` 1075 1076## wifiManager.connectToNetwork<sup>9+</sup> 1077 1078connectToNetwork(networkId: number): void 1079 1080连接到指定网络(如果当前已经连接到热点,请先使用disconnet()接口断开连接)。 1081 1082**系统接口:** 此接口为系统接口。 1083 1084**需要权限:** ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 1085 1086**系统能力:** SystemCapability.Communication.WiFi.STA 1087 1088**参数:** 1089 1090 | **参数名** | **类型** | **必填** | **说明** | 1091 | -------- | -------- | -------- | -------- | 1092 | networkId | number | 是 | 待连接的网络配置ID。 | 1093 1094**错误码:** 1095 1096以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 1097 1098| **错误码ID** | **错误信息** | 1099 | -------- | -------- | 1100| 2501000 | Operation failed.| 1101| 2501001 | Wifi is closed.| 1102 1103**示例:** 1104 1105```ts 1106 import wifiManager from '@ohos.wifiManager'; 1107 1108 try { 1109 let networkId = 0; 1110 wifiManager.connectToNetwork(networkId); 1111 }catch(error){ 1112 console.error("failed:" + JSON.stringify(error)); 1113 } 1114``` 1115 1116## wifiManager.connectToDevice<sup>9+</sup> 1117 1118connectToDevice(config: WifiDeviceConfig): void 1119 1120连接到指定网络(如果当前已经连接到热点,请先使用disconnet()接口断开连接)。 1121 1122**系统接口:** 此接口为系统接口。 1123 1124**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.SET_WIFI_CONFIG 和 ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 1125 1126**系统能力:** 1127 SystemCapability.Communication.WiFi.STA 1128 1129**参数:** 1130 1131| **参数名** | **类型** | **必填** | **说明** | 1132| -------- | -------- | -------- | -------- | 1133| config | [WifiDeviceConfig](#wifideviceconfig) | 是 | WLAN配置信息。如果bssidType未指定值,则bssidType默认为随机设备地址类型。 | 1134 1135**错误码:** 1136 1137以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 1138 1139| **错误码ID** | **错误信息** | 1140 | -------- | -------- | 1141| 2501000 | Operation failed.| 1142| 2501001 | Wifi is closed.| 1143 1144**示例:** 1145```ts 1146 import wifiManager from '@ohos.wifiManager'; 1147 1148 try { 1149 let config:wifiManager.WifiDeviceConfig = { 1150 ssid : "****", 1151 preSharedKey : "****", 1152 securityType : 3 1153 } 1154 wifiManager.connectToDevice(config); 1155 1156 }catch(error){ 1157 console.error("failed:" + JSON.stringify(error)); 1158 } 1159``` 1160 1161## wifiManager.disconnect<sup>9+</sup> 1162 1163disconnect(): void 1164 1165断开连接的网络。 1166 1167**系统接口:** 此接口为系统接口。 1168 1169**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 1170 1171**系统能力:** 1172 SystemCapability.Communication.WiFi.STA 1173 1174**错误码:** 1175 1176以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 1177 1178| **错误码ID** | **错误信息** | 1179 | -------- | -------- | 1180| 2501000 | Operation failed.| 1181 1182**示例:** 1183```ts 1184 import wifiManager from '@ohos.wifiManager'; 1185 1186 try { 1187 wifiManager.disconnect(); 1188 }catch(error){ 1189 console.error("failed:" + JSON.stringify(error)); 1190 } 1191``` 1192 1193## wifiManager.getSignalLevel<sup>9+</sup> 1194 1195getSignalLevel(rssi: number, band: number): number 1196 1197查询WLAN信号强度。 1198 1199**需要权限:** ohos.permission.GET_WIFI_INFO 1200 1201**系统能力:** SystemCapability.Communication.WiFi.STA 1202 1203**参数:** 1204 1205 | **参数名** | **类型** | **必填** | **说明** | 1206 | -------- | -------- | -------- | -------- | 1207 | rssi | number | 是 | 热点的信号强度(dBm)。 | 1208 | band | number | 是 | WLAN接入点的频段。 | 1209 1210**返回值:** 1211 1212 | **类型** | **说明** | 1213 | -------- | -------- | 1214 | number | 信号强度,取值范围为[0, 4]。 | 1215 1216**错误码:** 1217 1218以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 1219 1220| **错误码ID** | **错误信息** | 1221 | -------- | -------- | 1222| 2501000 | Operation failed.| 1223 1224**示例:** 1225```ts 1226 import wifiManager from '@ohos.wifiManager'; 1227 1228 try { 1229 let rssi = 0; 1230 let band = 0; 1231 let level = wifiManager.getSignalLevel(rssi,band); 1232 console.info("level:" + JSON.stringify(level)); 1233 }catch(error){ 1234 console.error("failed:" + JSON.stringify(error)); 1235 } 1236 1237``` 1238 1239## wifiManager.getLinkedInfo<sup>9+</sup> 1240 1241getLinkedInfo(): Promise<WifiLinkedInfo> 1242 1243获取WLAN连接信息,使用Promise异步回调。 1244 1245**需要权限:** ohos.permission.GET_WIFI_INFO 。 1246 1247 获取 macAddress 还需申请ohos.permission.GET_WIFI_LOCAL_MAC权限,无该权限时,macAddress 返回空字符串。 1248 1249**系统能力:** SystemCapability.Communication.WiFi.STA 1250 1251**返回值:** 1252 1253 | 类型 | 说明 | 1254 | -------- | -------- | 1255 | Promise<[WifiLinkedInfo](#wifilinkedinfo)> | Promise对象。表示WLAN连接信息。 | 1256 1257**错误码:** 1258 1259以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 1260 1261| **错误码ID** | **错误信息** | 1262 | -------- | -------- | 1263| 2501000 | Operation failed.| 1264| 2501001 | Wifi is closed.| 1265 1266## wifiManager.getLinkedInfo<sup>9+</sup> 1267 1268getLinkedInfo(callback: AsyncCallback<WifiLinkedInfo>): void 1269 1270获取WLAN连接信息,使用callback异步回调。 1271 1272**需要权限:** ohos.permission.GET_WIFI_INFO 。 1273 1274获取 macAddress 还需申请ohos.permission.GET_WIFI_LOCAL_MAC权限,无该权限时,macAddress 返回空字符串。 1275 1276**系统能力:** SystemCapability.Communication.WiFi.STA 1277 1278**参数:** 1279 1280 | 参数名 | 类型 | 必填 | 说明 | 1281 | -------- | -------- | -------- | -------- | 1282 | callback | AsyncCallback<[WifiLinkedInfo](#wifilinkedinfo)> | 是 | 回调函数。当获取成功时,err为0,data表示WLAN连接信息。如果error为非0,表示处理出现错误。 | 1283 1284**错误码:** 1285 1286以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 1287 1288| **错误码ID** | **错误信息** | 1289 | -------- | -------- | 1290| 2501000 | Operation failed.| 1291| 2501001 | Wifi is closed.| 1292 1293**示例:** 1294```ts 1295 import wifiManager from '@ohos.wifiManager'; 1296 1297 wifiManager.getLinkedInfo((err, data) => { 1298 if (err) { 1299 console.error("get linked info error"); 1300 return; 1301 } 1302 console.info("get wifi linked info: " + JSON.stringify(data)); 1303 }); 1304 1305 wifiManager.getLinkedInfo().then(data => { 1306 console.info("get wifi linked info: " + JSON.stringify(data)); 1307 }).catch((error:number) => { 1308 console.info("get linked info error"); 1309 }); 1310``` 1311 1312 1313## WifiLinkedInfo<sup>9+</sup> 1314 1315提供WLAN连接的相关信息。 1316 1317**系统能力:** SystemCapability.Communication.WiFi.STA 1318 1319| 名称 | 类型 | 可读 | 可写 | 说明 | 1320| -------- | -------- | -------- | -------- | -------- | 1321| ssid | string | 是 | 否 | 热点的SSID,编码格式为UTF-8。 | 1322| bssid | string | 是 | 否 | 热点的BSSID。 | 1323| networkId | number | 是 | 否 | 网络配置ID。 <br /> **系统接口:** 此接口为系统接口。 | 1324| rssi | number | 是 | 否 | 热点的信号强度(dBm)。 | 1325| band | number | 是 | 否 | WLAN接入点的频段。 | 1326| linkSpeed | number | 是 | 否 | WLAN接入点的上行速度。 | 1327| rxLinkSpeed<sup>10+</sup> | number | 是 | 否 | WLAN接入点的下行速度。 | 1328| maxSupportedTxLinkSpeed<sup>10+</sup> | number | 是 | 否 | 当前支持的最大上行速率。 | 1329| maxSupportedRxLinkSpeed<sup>10+</sup> | number | 是 | 否 | 当前支持的最大下行速率。 | 1330| frequency | number | 是 | 否 | WLAN接入点的频率。 | 1331| isHidden | boolean | 是 | 否 | WLAN接入点是否是隐藏网络。 | 1332| isRestricted | boolean | 是 | 否 | WLAN接入点是否限制数据量。 | 1333| chload | number | 是 | 否 | 连接负载,值越大表示负载约高。 <br /> **系统接口:** 此接口为系统接口。 | 1334| snr | number | 是 | 否 | 信噪比。 <br /> **系统接口:** 此接口为系统接口。 | 1335| macType | number | 是 | 否 | MAC地址类型。0 - 随机MAC地址,1 - 设备MAC地址。 | 1336| macAddress | string | 是 | 否 | 设备的MAC地址。 | 1337| ipAddress | number | 是 | 否 | WLAN连接的IP地址。 | 1338| suppState | [SuppState](#suppstate) | 是 | 否 | 请求状态。 <br /> **系统接口:** 此接口为系统接口。 | 1339| connState | [ConnState](#connstate) | 是 | 否 | WLAN连接状态。 | 1340| channelWidth<sup>10+</sup> | [WifiChannelWidth](#wifichannelwidth) | 是 | 否 | 当前连接热点的信道带宽。 | 1341| wifiStandard<sup>10+</sup> | [WifiStandard](#wifistandard) | 是 | 否 | 当前连接热点的WiFi标准。 | 1342 1343## ConnState<sup>9+</sup> 1344 1345表示WLAN连接状态的枚举。 1346 1347**系统能力:** SystemCapability.Communication.WiFi.STA 1348 1349| 名称 | 值 | 说明 | 1350| -------- | -------- | -------- | 1351| SCANNING | 0 | 设备正在搜索可用的AP。 | 1352| CONNECTING | 1 | 正在建立WLAN连接。 | 1353| AUTHENTICATING | 2 | WLAN连接正在认证中。 | 1354| OBTAINING_IPADDR | 3 | 正在获取WLAN连接的IP地址。 | 1355| CONNECTED | 4 | WLAN连接已建立。 | 1356| DISCONNECTING | 5 | WLAN连接正在断开。 | 1357| DISCONNECTED | 6 | WLAN连接已断开。 | 1358| UNKNOWN | 7 | WLAN连接建立失败。 | 1359 1360 1361## SuppState<sup>9+</sup> 1362 1363表示请求状态的枚举。 1364 1365**系统接口:** 此接口为系统接口。 1366 1367**系统能力:** SystemCapability.Communication.WiFi.STA 1368 1369| 名称 | 值 | 说明 | 1370| -------- | -------- | -------- | 1371| DISCONNECTED | 0 | 已断开。 | 1372| INTERFACE_DISABLED | 1 | 接口禁用。 | 1373| INACTIVE | 2 | 未激活。 | 1374| SCANNING | 3 | 扫描中。 | 1375| AUTHENTICATING | 4 | 认证中。 | 1376| ASSOCIATING | 5 | 关联中。 | 1377| ASSOCIATED | 6 | 已关联。 | 1378| FOUR_WAY_HANDSHAKE | 7 | 四次握手。 | 1379| GROUP_HANDSHAKE | 8 | 组握手。 | 1380| COMPLETED | 9 | 所有认证已完成。 | 1381| UNINITIALIZED | 10 | 连接建立失败。 | 1382| INVALID | 11 | 无效值。 | 1383 1384## wifiManager.isConnected<sup>9+</sup> 1385 1386isConnected(): boolean 1387 1388查询WLAN是否已连接。 1389 1390**需要权限:** ohos.permission.GET_WIFI_INFO 1391 1392**系统能力:** SystemCapability.Communication.WiFi.STA 1393 1394**返回值:** 1395 1396 | **类型** | **说明** | 1397 | -------- | -------- | 1398 | boolean | true:已连接, false:未连接。 | 1399 1400**错误码:** 1401 1402以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 1403 1404| **错误码ID** | **错误信息** | 1405 | -------- | -------- | 1406| 2501000 | Operation failed.| 1407 1408**示例:** 1409```ts 1410 import wifiManager from '@ohos.wifiManager'; 1411 1412 try { 1413 let ret = wifiManager.isConnected(); 1414 console.info("isConnected:" + ret); 1415 }catch(error){ 1416 console.error("failed:" + JSON.stringify(error)); 1417 } 1418 1419``` 1420 1421## wifiManager.getSupportedFeatures<sup>9+</sup> 1422 1423getSupportedFeatures(): number 1424 1425查询设备支持的特性。 1426 1427**系统接口:** 此接口为系统接口。 1428 1429**需要权限:** ohos.permission.GET_WIFI_INFO 1430 1431**系统能力:** SystemCapability.Communication.WiFi.Core 1432 1433**返回值:** 1434 1435 | **类型** | **说明** | 1436 | -------- | -------- | 1437 | number | 支持的特性值。 | 1438 1439**特性ID值枚举:** 1440 1441| 枚举值 | 说明 | 1442| -------- | -------- | 1443| 0x0001 | 基础结构模式特性。 | 1444| 0x0002 | 5 GHz带宽特性。 | 1445| 0x0004 | GAS/ANQP特性。 | 1446| 0x0008 | Wifi-Direct特性。 | 1447| 0x0010 | Soft AP特性。 | 1448| 0x0040 | Wi-Fi AWare组网特性。 | 1449| 0x8000 | AP STA共存特性。 | 1450| 0x8000000 | WPA3-Personal SAE特性。 | 1451| 0x10000000 | WPA3-Enterprise Suite-B | 1452| 0x20000000 | 增强开放特性。 | 1453 1454**错误码:** 1455 1456以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 1457 1458| **错误码ID** | **错误信息** | 1459 | -------- | -------- | 1460| 2401000 | Operation failed.| 1461 1462**示例:** 1463```ts 1464 import wifiManager from '@ohos.wifiManager'; 1465 1466 try { 1467 let ret = wifiManager.getSupportedFeatures(); 1468 console.info("supportedFeatures:" + ret); 1469 }catch(error){ 1470 console.error("failed:" + JSON.stringify(error)); 1471 } 1472 1473``` 1474 1475## wifiManager.isFeatureSupported<sup>9+</sup> 1476 1477isFeatureSupported(featureId: number): boolean 1478 1479判断设备是否支持相关WLAN特性。 1480 1481**需要权限:** ohos.permission.GET_WIFI_INFO 1482 1483**系统能力:** SystemCapability.Communication.WiFi.Core 1484 1485**参数:** 1486 1487 1488 | **参数名** | **类型** | 必填 | **说明** | 1489 | -------- | -------- | -------- | -------- | 1490 | featureId | number | 是 | 特性ID值。 | 1491 1492**返回值:** 1493 1494 | **类型** | **说明** | 1495 | -------- | -------- | 1496 | boolean | true:支持, false:不支持。 | 1497 1498**错误码:** 1499 1500以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 1501 1502| **错误码ID** | **错误信息** | 1503 | -------- | -------- | 1504| 2401000 | Operation failed.| 1505 1506**示例:** 1507```ts 1508 import wifiManager from '@ohos.wifiManager'; 1509 1510 try { 1511 let featureId = 0; 1512 let ret = wifiManager.isFeatureSupported(featureId); 1513 console.info("isFeatureSupported:" + ret); 1514 }catch(error){ 1515 console.error("failed:" + JSON.stringify(error)); 1516 } 1517 1518``` 1519 1520## wifiManager.getDeviceMacAddress<sup>9+</sup> 1521 1522getDeviceMacAddress(): string[] 1523 1524获取设备的MAC地址。 1525 1526**系统接口:** 此接口为系统接口。 1527 1528**需要权限:** ohos.permission.GET_WIFI_LOCAL_MAC 和 ohos.permission.GET_WIFI_INFO,仅系统应用可用。 1529 1530**系统能力:** SystemCapability.Communication.WiFi.STA 1531 1532**返回值:** 1533 1534 | **类型** | **说明** | 1535 | -------- | -------- | 1536 | string[] | MAC地址。 | 1537 1538**错误码:** 1539 1540以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 1541 1542| **错误码ID** | **错误信息** | 1543 | -------- | -------- | 1544| 2501000 | Operation failed.| 1545| 2501001 | wifi is closed.| 1546 1547**示例:** 1548```ts 1549 import wifiManager from '@ohos.wifiManager'; 1550 1551 try { 1552 let ret = wifiManager.getDeviceMacAddress(); 1553 console.info("deviceMacAddress:" + JSON.stringify(ret)); 1554 }catch(error){ 1555 console.error("failed:" + JSON.stringify(error)); 1556 } 1557 1558``` 1559 1560## wifiManager.getIpInfo<sup>9+</sup> 1561 1562getIpInfo(): IpInfo 1563 1564获取IP信息。 1565 1566**需要权限:** ohos.permission.GET_WIFI_INFO 1567 1568**系统能力:** SystemCapability.Communication.WiFi.STA 1569 1570**返回值:** 1571 1572 | **类型** | **说明** | 1573 | -------- | -------- | 1574 | [IpInfo](#ipinfo9) | IP信息。 | 1575 1576**错误码:** 1577 1578以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 1579 1580| **错误码ID** | **错误信息** | 1581 | -------- | -------- | 1582| 2501000 | Operation failed.| 1583 1584**示例:** 1585```ts 1586 import wifiManager from '@ohos.wifiManager'; 1587 1588 try { 1589 let info = wifiManager.getIpInfo(); 1590 console.info("info:" + JSON.stringify(info)); 1591 }catch(error){ 1592 console.error("failed:" + JSON.stringify(error)); 1593 } 1594``` 1595 1596## IpInfo<sup>9+</sup> 1597 1598IP信息。 1599 1600**系统能力:** SystemCapability.Communication.WiFi.STA 1601 1602| **名称** | **类型** | **可读** | **可写** | **说明** | 1603| -------- | -------- | -------- | -------- | -------- | 1604| ipAddress | number | 是 | 否 | IP地址。 | 1605| gateway | number | 是 | 否 | 网关。 | 1606| netmask | number | 是 | 否 | 掩码。 | 1607| primaryDns | number | 是 | 否 | 主DNS服务器IP地址。 | 1608| secondDns | number | 是 | 否 | 备DNS服务器IP地址。 | 1609| serverIp | number | 是 | 否 | DHCP服务端IP地址。 | 1610| leaseDuration | number | 是 | 否 | IP地址租用时长。 | 1611 1612 1613## wifiManager.getIpv6Info<sup>10+</sup> 1614 1615getIpv6Info(): Ipv6Info 1616 1617获取IP信息。 1618 1619**需要权限:** ohos.permission.GET_WIFI_INFO 1620 1621**系统能力:** SystemCapability.Communication.WiFi.STA 1622 1623**返回值:** 1624 1625| **类型** | **说明** | 1626| -------- | -------- | 1627| Ipv6Info | Ipv6信息。 | 1628 1629**错误码:** 1630 1631以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 1632 1633| **错误码ID** | **错误信息** | 1634 | -------- | -------- | 1635| 2501000 | Operation failed.| 1636 1637**示例:** 1638```ts 1639 import wifiManager from '@ohos.wifiManager'; 1640 1641 try { 1642 let info = wifiManager.getIpv6Info(); 1643 console.info("info:" + JSON.stringify(info)); 1644 }catch(error){ 1645 console.error("failed:" + JSON.stringify(error)); 1646 } 1647``` 1648## Ipv6Info <sup>10+</sup> 1649 1650Ipv6信息。 1651 1652**系统能力:** SystemCapability.Communication.WiFi.STA 1653 1654| **名称** | **类型** | **可读** | **可写** | **说明** | 1655| -------- | -------- | -------- | -------- | -------- | 1656| linkIpv6Address | string | 是 | 否 | 链路Ipv6地址。 | 1657| globalIpv6Address | string | 是 | 否 | 全局Ipv6地址。 | 1658| randomGlobalIpv6Address | number | 是 | 否 | 随机全局Ipv6地址。 | 1659| gateway | string | 是 | 否 | 网关。 | 1660| netmask | string | 是 | 否 | 网络掩码。 | 1661| primaryDNS | string | 是 | 否 | 主DNS服务器Ipv6地址。 | 1662| secondDNS | string | 是 | 否 | 备DNS服务器Ipv6地址。 | 1663 1664 1665## wifiManager.getCountryCode<sup>9+</sup> 1666 1667getCountryCode(): string 1668 1669获取国家码信息。 1670 1671**需要权限:** ohos.permission.GET_WIFI_INFO 1672 1673**系统能力:** SystemCapability.Communication.WiFi.Core 1674 1675**返回值:** 1676 1677 | **类型** | **说明** | 1678 | -------- | -------- | 1679 | string | 国家码。 | 1680 1681**错误码:** 1682 1683以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 1684 1685| **错误码ID** | **错误信息** | 1686 | -------- | -------- | 1687| 2401000 | Operation failed.| 1688 1689**示例:** 1690```ts 1691 import wifiManager from '@ohos.wifiManager'; 1692 1693 try { 1694 let code = wifiManager.getCountryCode(); 1695 console.info("code:" + code); 1696 }catch(error){ 1697 console.error("failed:" + JSON.stringify(error)); 1698 } 1699``` 1700 1701## wifiManager.reassociate<sup>9+</sup> 1702 1703reassociate(): void 1704 1705重新关联网络。 1706 1707**系统接口:** 此接口为系统接口。 1708 1709**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 1710 1711**系统能力:** SystemCapability.Communication.WiFi.STA 1712 1713**错误码:** 1714 1715以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 1716 1717| **错误码ID** | **错误信息** | 1718 | -------- | -------- | 1719| 2501000 | Operation failed.| 1720| 2501001 | Wifi is closed.| 1721 1722**示例:** 1723```ts 1724 import wifiManager from '@ohos.wifiManager'; 1725 1726 try { 1727 wifiManager.reassociate(); 1728 }catch(error){ 1729 console.error("failed:" + JSON.stringify(error)); 1730 } 1731``` 1732 1733## wifiManager.reconnect<sup>9+</sup> 1734 1735reconnect(): void 1736 1737重新连接网络。 1738 1739**系统接口:** 此接口为系统接口。 1740 1741**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 1742 1743**系统能力:** SystemCapability.Communication.WiFi.STA 1744 1745**错误码:** 1746 1747以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 1748 1749| **错误码ID** | **错误信息** | 1750 | -------- | -------- | 1751| 2501000 | Operation failed.| 1752| 2501001 | Wifi is closed.| 1753 1754**示例:** 1755```ts 1756 import wifiManager from '@ohos.wifiManager'; 1757 1758 try { 1759 wifiManager.reconnect(); 1760 }catch(error){ 1761 console.error("failed:" + JSON.stringify(error)); 1762 } 1763``` 1764 1765## wifiManager.getDeviceConfigs<sup>9+</sup> 1766 1767getDeviceConfigs(): Array<[WifiDeviceConfig](#wifideviceconfig)> 1768 1769获取网络配置。 1770 1771**系统接口:** 此接口为系统接口。 1772 1773**需要权限:** 1774 1775API 9:ohos.permission.GET_WIFI_INFO、ohos.permission.LOCATION、ohos.permission.APPROXIMATELY_LOCATION 和 ohos.permission.GET_WIFI_CONFIG 1776 1777API 10起:ohos.permission.GET_WIFI_INFO 和 ohos.permission.GET_WIFI_CONFIG 1778 1779**系统能力:** SystemCapability.Communication.WiFi.STA 1780 1781**返回值:** 1782 1783 | **类型** | **说明** | 1784 | -------- | -------- | 1785 | Array<[WifiDeviceConfig](#wifideviceconfig)> | 网络配置信息的数组。 | 1786 1787**错误码:** 1788 1789以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 1790 1791| **错误码ID** | **错误信息** | 1792 | -------- | -------- | 1793| 2501000 | Operation failed.| 1794 1795**示例:** 1796```ts 1797 import wifiManager from '@ohos.wifiManager'; 1798 1799 try { 1800 let configs = wifiManager.getDeviceConfigs(); 1801 console.info("configs:" + JSON.stringify(configs)); 1802 }catch(error){ 1803 console.error("failed:" + JSON.stringify(error)); 1804 } 1805``` 1806 1807## wifiManager.updateNetwork<sup>9+</sup> 1808 1809updateNetwork(config: WifiDeviceConfig): number 1810 1811更新网络配置。 1812 1813**系统接口:** 此接口为系统接口。 1814 1815**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.SET_WIFI_CONFIG 1816 1817**系统能力:** SystemCapability.Communication.WiFi.STA 1818 1819**参数:** 1820 1821 | **参数名** | **类型** | **必填** | **说明** | 1822 | -------- | -------- | -------- | -------- | 1823 | config | [WifiDeviceConfig](#wifideviceconfig) | 是 | WLAN配置信息。 | 1824 1825**返回值:** 1826 1827 | **类型** | **说明** | 1828 | -------- | -------- | 1829 | number | 返回更新的网络配置ID,如果值为-1表示更新失败。 | 1830 1831**错误码:** 1832 1833以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 1834 1835| **错误码ID** | **错误信息** | 1836 | -------- | -------- | 1837| 2501000 | Operation failed.| 1838 1839**示例:** 1840```ts 1841 import wifiManager from '@ohos.wifiManager'; 1842 1843 try { 1844 let config:wifiManager.WifiDeviceConfig = { 1845 ssid : "****", 1846 preSharedKey : "****", 1847 securityType : 3 1848 } 1849 let ret = wifiManager.updateNetwork(config); 1850 console.info("ret:" + ret); 1851 }catch(error){ 1852 console.error("failed:" + JSON.stringify(error)); 1853 } 1854``` 1855 1856## wifiManager.disableNetwork<sup>9+</sup> 1857 1858disableNetwork(netId: number): void 1859 1860去使能网络配置。 1861 1862**系统接口:** 此接口为系统接口。 1863 1864**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 1865 1866**系统能力:** SystemCapability.Communication.WiFi.STA 1867 1868**参数:** 1869 1870 | **参数名** | **类型** | **必填** | **说明** | 1871 | -------- | -------- | -------- | -------- | 1872 | netId | number | 是 | 网络配置ID。 | 1873 1874**错误码:** 1875 1876以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 1877 1878| **错误码ID** | **错误信息** | 1879 | -------- | -------- | 1880| 2501000 | Operation failed.| 1881 1882**示例:** 1883```ts 1884 import wifiManager from '@ohos.wifiManager'; 1885 1886 try { 1887 let netId = 0; 1888 wifiManager.disableNetwork(netId); 1889 }catch(error){ 1890 console.error("failed:" + JSON.stringify(error)); 1891 } 1892``` 1893 1894## wifiManager.removeAllNetwork<sup>9+</sup> 1895 1896removeAllNetwork(): void 1897 1898移除所有网络配置。 1899 1900**系统接口:** 此接口为系统接口。 1901 1902**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 1903 1904**系统能力:** SystemCapability.Communication.WiFi.STA 1905 1906**错误码:** 1907 1908以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 1909 1910| **错误码ID** | **错误信息** | 1911 | -------- | -------- | 1912| 2501000 | Operation failed.| 1913 1914**示例:** 1915```ts 1916 import wifiManager from '@ohos.wifiManager'; 1917 1918 try { 1919 wifiManager.removeAllNetwork(); 1920 }catch(error){ 1921 console.error("failed:" + JSON.stringify(error)); 1922 } 1923``` 1924 1925## wifiManager.removeDevice<sup>9+</sup> 1926 1927removeDevice(id: number): void 1928 1929移除指定的网络配置。 1930 1931**系统接口:** 此接口为系统接口。 1932 1933**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 1934 1935**系统能力:** SystemCapability.Communication.WiFi.STA 1936 1937**参数:** 1938 1939 | **参数名** | **类型** | **必填** | **说明** | 1940 | -------- | -------- | -------- | -------- | 1941 | id | number | 是 | 网络配置ID。 | 1942 1943**错误码:** 1944 1945以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 1946 1947| **错误码ID** | **错误信息** | 1948 | -------- | -------- | 1949| 2501000 | Operation failed.| 1950 1951**示例:** 1952```ts 1953 import wifiManager from '@ohos.wifiManager'; 1954 1955 try { 1956 let id = 0; 1957 wifiManager.removeDevice(id); 1958 }catch(error){ 1959 console.error("failed:" + JSON.stringify(error)); 1960 } 1961``` 1962 1963## wifiManager.isBandTypeSupported<sup>10+</sup> 1964 1965isBandTypeSupported(bandType: WifiBandType): boolean 1966 1967判断当前频段是否支持。 1968 1969**需要权限:** ohos.permission.GET_WIFI_INFO。 1970 1971**系统能力:** SystemCapability.Communication.WiFi.STA 1972 1973**参数:** 1974 1975 | **参数名** | **类型** | **必填** | **说明** | 1976 | -------- | -------- | -------- | -------- | 1977 | bandType | WifiBandType | 是 | Wifi 频段类型。 | 1978 1979**返回值:** 1980 1981 | **类型** | **说明** | 1982 | -------- | -------- | 1983 | boolean | true:支持, false:不支持。 | 1984 1985**错误码:** 1986 1987以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 1988 1989| **错误码ID** | **错误信息** | 1990 | -------- | -------- | 1991| 2501000 | Operation failed.| 1992 1993**示例:** 1994```ts 1995 import wifiManager from '@ohos.wifiManager'; 1996 1997 try { 1998 let type = 0; 1999 let isBandTypeSupported = wifiManager.isBandTypeSupported(type); 2000 console.info("isBandTypeSupported:" + isBandTypeSupported); 2001 }catch(error){ 2002 console.error("failed:" + JSON.stringify(error)); 2003 } 2004``` 2005 2006## wifiManager.get5GChannelList<sup>10+</sup> 2007 2008get5GChannelList(): Array<number> 2009 2010获取当前设备支持的5G信道列表。 2011 2012**系统接口:** 此接口为系统接口。 2013 2014**需要权限:** ohos.permission.GET_WIFI_INFO 和 ohos.permission.GET_WIFI_CONFIG 2015 2016**系统能力:** SystemCapability.Communication.WiFi.STA 2017 2018**返回值:** 2019 2020 | **类型** | **说明** | 2021 | -------- | -------- | 2022 | Array<number> | 设备支持的5G信道列表。 | 2023 2024**错误码:** 2025 2026以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 2027 2028| **错误码ID** | **错误信息** | 2029 | -------- | -------- | 2030| 2501000 | Operation failed.| 2031 2032**示例:** 2033```ts 2034 import wifiManager from '@ohos.wifiManager'; 2035 2036 try { 2037 let channelList = wifiManager.get5GChannelList(); 2038 console.info("channelList:" + JSON.stringify(channelList)); 2039 }catch(error){ 2040 console.error("failed:" + JSON.stringify(error)); 2041 } 2042``` 2043## wifiManager.getDisconnectedReason<sup>10+</sup> 2044 2045getDisconnectedReason(): DisconnectedReason 2046 2047获取最近一次断连原因。 2048 2049**系统接口:** 此接口为系统接口。 2050 2051**需要权限:** ohos.permission.GET_WIFI_INFO 和 ohos.permission.GET_WIFI_CONFIG 2052 2053**系统能力:** SystemCapability.Communication.WiFi.STA 2054 2055**错误码:** 2056 2057以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 2058 2059| **错误码ID** | **错误信息** | 2060 | -------- | -------- | 2061| 2501000 | Operation failed.| 2062 2063**返回值:** 2064 2065| **类型** | **说明** | 2066| -------- | -------- | 2067| DisconnectedReason | 最近断开的原因 | 2068 2069**示例:** 2070```ts 2071 import wifiManager from '@ohos.wifiManager'; 2072 2073 try { 2074 let disconnectedReason = wifiManager.getDisconnectedReason(); 2075 console.info("disconnectedReason:" + disconnectedReason); 2076 }catch(error){ 2077 console.error("failed:" + JSON.stringify(error)); 2078 } 2079``` 2080 2081## DisconnectedReason <sup>10+</sup> 2082 2083表示wifi断开原因的枚举。 2084 2085**系统接口:** 此接口为系统接口。 2086 2087**系统能力:** SystemCapability.Communication.WiFi.STA 2088 2089| 名称 | 值 | 说明 | 2090| -------- | -------- | -------- | 2091| DISC_REASON_DEFAULT | 0 | 默认原因。 | 2092| DISC_REASON_WRONG_PWD | 1 | 密码错误。 | 2093| DISC_REASON_CONNECTION_FULL | 2 | 路由器的连接数已达到最大数量限制。 | 2094 2095## wifiManager.enableHotspot<sup>9+</sup> 2096 2097enableHotspot(): void 2098 2099使能热点,异步接口,是否打开成功需要注册并监听hotspotStateChange的回调。 2100 2101**系统接口:** 此接口为系统接口。 2102 2103**需要权限:** ohos.permission.MANAGE_WIFI_HOTSPOT,仅系统应用可用。 2104 2105**系统能力:** SystemCapability.Communication.WiFi.AP.Core 2106 2107**错误码:** 2108 2109以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 2110 2111| **错误码ID** | **错误信息** | 2112 | -------- | -------- | 2113| 2601000 | Operation failed.| 2114 2115**示例:** 2116```ts 2117 import wifiManager from '@ohos.wifiManager'; 2118 2119 try { 2120 wifiManager.enableHotspot(); 2121 }catch(error){ 2122 console.error("failed:" + JSON.stringify(error)); 2123 } 2124``` 2125 2126## wifiManager.disableHotspot<sup>9+</sup> 2127 2128disableHotspot(): void 2129 2130去使能热点 ,异步接口,是否关闭成功需要注册并监听hotspotStateChange的回调。 2131 2132**系统接口:** 此接口为系统接口。 2133 2134**需要权限:** ohos.permission.MANAGE_WIFI_HOTSPOT,仅系统应用可用。 2135 2136**系统能力:** SystemCapability.Communication.WiFi.AP.Core 2137 2138**错误码:** 2139 2140以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 2141 2142| **错误码ID** | **错误信息** | 2143 | -------- | -------- | 2144| 2601000 | Operation failed.| 2145 2146**示例:** 2147```ts 2148 import wifiManager from '@ohos.wifiManager'; 2149 2150 try { 2151 wifiManager.disableHotspot(); 2152 }catch(error){ 2153 console.error("failed:" + JSON.stringify(error)); 2154 } 2155``` 2156 2157## wifiManager.isHotspotDualBandSupported<sup>9+</sup> 2158 2159isHotspotDualBandSupported(): boolean 2160 2161热点是否支持双频。 2162 2163**系统接口:** 此接口为系统接口。 2164 2165**需要权限:** ohos.permission.GET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_HOTSPOT,仅系统应用可用。 2166 2167**系统能力:** SystemCapability.Communication.WiFi.AP.Core 2168 2169**返回值:** 2170 2171 | **类型** | **说明** | 2172 | -------- | -------- | 2173 | boolean | true:支持, false:不支持.| 2174 2175**错误码:** 2176 2177以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 2178 2179| **错误码ID** | **错误信息** | 2180 | -------- | -------- | 2181| 2601000 | Operation failed.| 2182 2183**示例:** 2184```ts 2185 import wifiManager from '@ohos.wifiManager'; 2186 2187 try { 2188 let ret = wifiManager.isHotspotDualBandSupported(); 2189 console.info("result:" + ret); 2190 }catch(error){ 2191 console.error("failed:" + JSON.stringify(error)); 2192 } 2193``` 2194 2195## wifiManager.isHotspotActive<sup>9+</sup> 2196 2197isHotspotActive(): boolean 2198 2199热点是否已使能。 2200 2201**系统接口:** 此接口为系统接口。 2202 2203**需要权限:** ohos.permission.GET_WIFI_INFO 2204 2205**系统能力:** SystemCapability.Communication.WiFi.AP.Core 2206 2207**返回值:** 2208 2209 | **类型** | **说明** | 2210 | -------- | -------- | 2211 | boolean | true:已使能, false:未使能.| 2212 2213**错误码:** 2214 2215以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 2216 2217| **错误码ID** | **错误信息** | 2218 | -------- | -------- | 2219| 2601000 | Operation failed.| 2220 2221**示例:** 2222```ts 2223 import wifiManager from '@ohos.wifiManager'; 2224 2225 try { 2226 let ret = wifiManager.isHotspotActive(); 2227 console.info("result:" + ret); 2228 }catch(error){ 2229 console.error("failed:" + JSON.stringify(error)); 2230 } 2231``` 2232 2233## wifiManager.setHotspotConfig<sup>9+</sup> 2234 2235setHotspotConfig(config: HotspotConfig): void 2236 2237设置热点配置信息。 2238 2239**系统接口:** 此接口为系统接口。 2240 2241**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.GET_WIFI_CONFIG 2242 2243**系统能力:** SystemCapability.Communication.WiFi.AP.Core 2244 2245**参数:** 2246 2247 | **参数名** | **类型** | **必填** | **说明** | 2248 | -------- | -------- | -------- | -------- | 2249 | config | [HotspotConfig](#hotspotconfig9) | 是 | 热点配置信息。 | 2250 2251**错误码:** 2252 2253以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 2254 2255| **错误码ID** | **错误信息** | 2256 | -------- | -------- | 2257| 2601000 | Operation failed.| 2258 2259**示例:** 2260```ts 2261 import wifiManager from '@ohos.wifiManager'; 2262 2263 try { 2264 let config:wifiManager.HotspotConfig = { 2265 ssid: "****", 2266 securityType: 3, 2267 band: 0, 2268 channel: 0, 2269 preSharedKey: "****", 2270 maxConn: 0 2271 } 2272 let ret = wifiManager.setHotspotConfig(config); 2273 console.info("result:" + ret); 2274 }catch(error){ 2275 console.error("failed:" + JSON.stringify(error)); 2276 } 2277``` 2278 2279## HotspotConfig<sup>9+</sup> 2280 2281热点配置信息。 2282 2283**系统接口:** 此接口为系统接口。 2284 2285**系统能力:** SystemCapability.Communication.WiFi.AP.Core 2286 2287| **名称** | **类型** | **可读** | **可写** | **说明** | 2288| -------- | -------- | -------- | -------- | -------- | 2289| ssid | string | 是 | 是 | 热点的SSID,编码格式为UTF-8。 | 2290| securityType | [WifiSecurityType](#wifisecuritytype) | 是 | 是 | 加密类型。 | 2291| band | number | 是 | 是 | 热点的带宽。1: 2.4G, 2: 5G, 3: 双模频段 | 2292| channel<sup>10+</sup> | number | 是 | 是 | 热点的信道(2.4G:1~14,5G:7~196,双模频段:暂不支持)。 | 2293| preSharedKey | string | 是 | 是 | 热点的密钥。 | 2294| maxConn | number | 是 | 是 | 最大设备连接数。 | 2295 2296## wifiManager.getHotspotConfig<sup>9+</sup> 2297 2298getHotspotConfig(): HotspotConfig 2299 2300获取热点配置信息。 2301 2302**系统接口:** 此接口为系统接口。 2303 2304**需要权限:** ohos.permission.GET_WIFI_INFO 和 ohos.permission.GET_WIFI_CONFIG 2305 2306**系统能力:** SystemCapability.Communication.WiFi.AP.Core 2307 2308**返回值:** 2309 2310 | **类型** | **说明** | 2311 | -------- | -------- | 2312 | [HotspotConfig](#hotspotconfig9) | 热点的配置信息。 | 2313 2314**错误码:** 2315 2316以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 2317 2318| **错误码ID** | **错误信息** | 2319 | -------- | -------- | 2320| 2601000 | Operation failed.| 2321 2322**示例:** 2323```ts 2324 import wifiManager from '@ohos.wifiManager'; 2325 2326 try { 2327 let config = wifiManager.getHotspotConfig(); 2328 console.info("result:" + JSON.stringify(config)); 2329 }catch(error){ 2330 console.error("failed:" + JSON.stringify(error)); 2331 } 2332``` 2333 2334## wifiManager.getStations<sup>9+</sup> 2335 2336getStations(): Array<[StationInfo](#stationinfo9)> 2337 2338获取连接的设备。 2339 2340**系统接口:** 此接口为系统接口。 2341 2342**需要权限:** 2343 2344API 9:ohos.permission.GET_WIFI_INFO、ohos.permission.LOCATION、ohos.permission.APPROXIMATELY_LOCATION 和 ohos.permission.MANAGE_WIFI_HOTSPOT,仅系统应用可用。 2345 2346API 10起:ohos.permission.GET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_HOTSPOT,仅系统应用可用。 2347 2348**系统能力:** SystemCapability.Communication.WiFi.AP.Core 2349 2350**返回值:** 2351 2352| **类型** | **说明** | 2353| -------- | -------- | 2354| Array<[StationInfo](#stationinfo9)> | 连接的设备数组。如果应用申请了ohos.permission.GET_WIFI_PEERS_MAC权限,则返回结果中的macAddress为真实设备地址,否则为随机设备地址。 | 2355 2356**错误码:** 2357 2358以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 2359 2360| **错误码ID** | **错误信息** | 2361 | -------- | -------- | 2362| 2601000 | Operation failed.| 2363 2364**示例:** 2365```ts 2366 import wifiManager from '@ohos.wifiManager'; 2367 2368 try { 2369 let stations = wifiManager.getStations(); 2370 console.info("result:" + JSON.stringify(stations)); 2371 }catch(error){ 2372 console.error("failed:" + JSON.stringify(error)); 2373 } 2374``` 2375 2376## StationInfo<sup>9+</sup> 2377 2378接入的设备信息。 2379 2380**系统接口:** 此接口为系统接口。 2381 2382**系统能力:** SystemCapability.Communication.WiFi.AP.Core 2383 2384| **名称** | **类型** | **可读** | **可写** | **说明** | 2385| -------- | -------- | -------- | -------- | -------- | 2386| name | string | 是 | 否 | 设备名称。 | 2387| macAddress | string | 是 | 否 | MAC地址。 | 2388| macAddressType<sup>10+</sup> | DeviceAddressType | 是 | 否 | MAC地址类型。 | 2389| ipAddress | string | 是 | 否 | IP地址。 | 2390 2391 2392## wifiManager.getP2pLinkedInfo<sup>9+</sup> 2393 2394getP2pLinkedInfo(): Promise<WifiP2pLinkedInfo> 2395 2396获取P2P连接信息,使用Promise异步回调。 2397 2398**需要权限:** ohos.permission.GET_WIFI_INFO 2399 2400**系统能力:** SystemCapability.Communication.WiFi.P2P 2401 2402**返回值:** 2403 2404 | 类型 | 说明 | 2405 | -------- | -------- | 2406 | Promise<[WifiP2pLinkedInfo](#wifip2plinkedinfo9)> | Promise对象。表示P2P连接信息。 | 2407 2408**错误码:** 2409 2410以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 2411 2412| **错误码ID** | **错误信息** | 2413 | -------- | -------- | 2414| 2801000 | Operation failed.| 2415 2416**示例:** 2417```ts 2418 import wifiManager from '@ohos.wifiManager'; 2419 2420 wifiManager.getP2pLinkedInfo((err, data) => { 2421 if (err) { 2422 console.error("get p2p linked info error"); 2423 return; 2424 } 2425 console.info("get wifi p2p linked info: " + JSON.stringify(data)); 2426 }); 2427 2428 wifiManager.getP2pLinkedInfo().then(data => { 2429 console.info("get wifi p2p linked info: " + JSON.stringify(data)); 2430 }); 2431``` 2432 2433 2434## WifiP2pLinkedInfo<sup>9+</sup> 2435 2436提供WLAN连接的相关信息。 2437 2438**系统能力:** SystemCapability.Communication.WiFi.P2P 2439 2440| 名称 | 类型 | 可读 | 可写 | 说明 | 2441| -------- | -------- | -------- | -------- | -------- | 2442| connectState | [P2pConnectState](#p2pconnectstate9) | 是 | 否 | P2P连接状态。 | 2443| isGroupOwner | boolean | 是 | 否 | 是否是群主。 | 2444| groupOwnerAddr | string | 是 | 否 | 群组MAC地址。 2445 2446 2447## P2pConnectState<sup>9+</sup> 2448 2449表示P2P连接状态的枚举。 2450 2451**系统能力:** SystemCapability.Communication.WiFi.P2P 2452 2453| 名称 | 值 | 说明 | 2454| -------- | -------- | -------- | 2455| DISCONNECTED | 0 | 断开状态。 | 2456| CONNECTED | 1 | 连接状态。 | 2457 2458 2459## wifiManager.getP2pLinkedInfo<sup>9+</sup> 2460 2461getP2pLinkedInfo(callback: AsyncCallback<WifiP2pLinkedInfo>): void 2462 2463获取P2P连接信息,使用callback异步回调。 2464 2465**需要权限:** ohos.permission.GET_WIFI_INFO 2466 2467**系统能力:** SystemCapability.Communication.WiFi.P2P 2468 2469**参数:** 2470 2471 | 参数名 | 类型 | 必填 | 说明 | 2472 | -------- | -------- | -------- | -------- | 2473 | callback | AsyncCallback<[WifiP2pLinkedInfo](#wifip2plinkedinfo9)> | 是 | 回调函数。当操作成功时,err为0,data表示P2P连接信息。如果error为非0,表示处理出现错误。 | 2474 2475**错误码:** 2476 2477以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 2478 2479| **错误码ID** | **错误信息** | 2480 | -------- | -------- | 2481| 2801000 | Operation failed.| 2482 2483## wifiManager.getCurrentGroup<sup>9+</sup> 2484 2485getCurrentGroup(): Promise<WifiP2pGroupInfo> 2486 2487获取P2P当前组信息,使用Promise异步回调。 2488 2489**需要权限:** 2490 2491API 9:ohos.permission.GET_WIFI_INFO、ohos.permission.LOCATION 和 ohos.permission.APPROXIMATELY_LOCATION 2492 2493API 10起:ohos.permission.GET_WIFI_INFO 2494 2495**系统能力:** SystemCapability.Communication.WiFi.P2P 2496 2497**返回值:** 2498 2499| 类型 | 说明 | 2500| -------- | -------- | 2501| Promise<[WifiP2pGroupInfo](#wifip2pgroupinfo9)> | Promise对象。表示当前组信息。如果应用申请了ohos.permission.GET_WIFI_PEERS_MAC权限,则返回结果中的deviceAddress为真实设备地址,否则为随机设备地址。 | 2502 2503**错误码:** 2504 2505以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 2506 2507| **错误码ID** | **错误信息** | 2508 | -------- | -------- | 2509| 2801000 | Operation failed.| 2510 2511## wifiManager.getCurrentGroup<sup>9+</sup> 2512 2513getCurrentGroup(callback: AsyncCallback<WifiP2pGroupInfo>): void 2514 2515获取P2P当前组信息,使用callback异步回调。 2516 2517**需要权限:** 2518 2519API 9:ohos.permission.GET_WIFI_INFO、ohos.permission.LOCATION 和 ohos.permission.APPROXIMATELY_LOCATION 2520 2521API 10起:ohos.permission.GET_WIFI_INFO 2522 2523**系统能力:** SystemCapability.Communication.WiFi.P2P 2524 2525**参数:** 2526 2527| 参数名 | 类型 | 必填 | 说明 | 2528| -------- | -------- | -------- | -------- | 2529| callback | AsyncCallback<[WifiP2pGroupInfo](#wifip2pgroupinfo9)> | 是 | 回调函数。当操作成功时,err为0,data表示当前组信息。如果error为非0,表示处理出现错误。如果应用申请了ohos.permission.GET_WIFI_PEERS_MAC权限,则返回结果中的deviceAddress为真实设备地址,否则为随机设备地址。 | 2530 2531**错误码:** 2532 2533以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 2534 2535| **错误码ID** | **错误信息** | 2536 | -------- | -------- | 2537| 2801000 | Operation failed.| 2538 2539**示例:** 2540```ts 2541 import wifiManager from '@ohos.wifiManager'; 2542 2543 wifiManager.getCurrentGroup((err, data) => { 2544 if (err) { 2545 console.error("get current P2P group error"); 2546 return; 2547 } 2548 console.info("get current P2P group: " + JSON.stringify(data)); 2549 }); 2550 2551 wifiManager.getCurrentGroup().then(data => { 2552 console.info("get current P2P group: " + JSON.stringify(data)); 2553 }); 2554``` 2555 2556## wifiManager.getP2pPeerDevices<sup>9+</sup> 2557 2558getP2pPeerDevices(): Promise<WifiP2pDevice[]> 2559 2560获取P2P对端设备列表信息,使用Promise异步回调。 2561 2562**需要权限:** 2563 2564API 9:ohos.permission.GET_WIFI_INFO、ohos.permission.LOCATION 和 ohos.permission.APPROXIMATELY_LOCATION 2565 2566API 10起:ohos.permission.GET_WIFI_INFO 2567 2568**系统能力:** SystemCapability.Communication.WiFi.P2P 2569 2570**返回值:** 2571 2572| 类型 | 说明 | 2573| -------- | -------- | 2574| Promise<[WifiP2pDevice[]](#wifip2pdevice9)> | Promise对象。表示对端设备列表信息。如果应用申请了ohos.permission.GET_WIFI_PEERS_MAC权限,则返回结果中的deviceAddress为真实设备地址,否则为随机设备地址。 | 2575 2576**错误码:** 2577 2578以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 2579 2580| **错误码ID** | **错误信息** | 2581 | -------- | -------- | 2582| 2801000 | Operation failed.| 2583 2584## wifiManager.getP2pPeerDevices<sup>9+</sup> 2585 2586getP2pPeerDevices(callback: AsyncCallback<WifiP2pDevice[]>): void 2587 2588获取P2P对端设备列表信息,使用callback异步回调。 2589 2590**需要权限:** 2591 2592API 9:ohos.permission.GET_WIFI_INFO、ohos.permission.LOCATION 和 ohos.permission.APPROXIMATELY_LOCATION 2593 2594API 10起:ohos.permission.GET_WIFI_INFO 2595 2596**系统能力:** SystemCapability.Communication.WiFi.P2P 2597 2598**参数:** 2599 2600| 参数名 | 类型 | 必填 | 说明 | 2601| -------- | -------- | -------- | -------- | 2602| callback | AsyncCallback<[WifiP2pDevice[]](#wifip2pdevice9)> | 是 | 回调函数。当操作成功时,err为0,data表示对端设备列表信息。如果error为非0,表示处理出现错误。如果应用申请了ohos.permission.GET_WIFI_PEERS_MAC权限,则返回结果中的deviceAddress为真实设备地址,否则为随机设备地址。 | 2603 2604**错误码:** 2605 2606以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 2607 2608| **错误码ID** | **错误信息** | 2609 | -------- | -------- | 2610| 2801000 | Operation failed.| 2611 2612**示例:** 2613```ts 2614 import wifiManager from '@ohos.wifiManager'; 2615 2616 wifiManager.getP2pPeerDevices((err, data) => { 2617 if (err) { 2618 console.error("get P2P peer devices error"); 2619 return; 2620 } 2621 console.info("get P2P peer devices: " + JSON.stringify(data)); 2622 }); 2623 2624 wifiManager.getP2pPeerDevices().then(data => { 2625 console.info("get P2P peer devices: " + JSON.stringify(data)); 2626 }); 2627``` 2628 2629## WifiP2pDevice<sup>9+</sup> 2630 2631表示P2P设备信息。 2632 2633**系统能力:** SystemCapability.Communication.WiFi.P2P 2634 2635| 名称 | 类型 | 可读 | 可写 | 说明 | 2636| -------- | -------- | -------- | -------- | -------- | 2637| deviceName | string | 是 | 否 | 设备名称。 | 2638| deviceAddress | string | 是 | 否 | 设备MAC地址。 | 2639| deviceAddressType<sup>10+</sup> | DeviceAddressType | 是 | 否 | 设备MAC地址类型。 | 2640| primaryDeviceType | string | 是 | 否 | 主设备类型。 | 2641| deviceStatus | [P2pDeviceStatus](#p2pdevicestatus9) | 是 | 否 | 设备状态。 | 2642| groupCapabilities | number | 是 | 否 | 群组能力。 | 2643 2644 2645## P2pDeviceStatus<sup>9+</sup> 2646 2647表示设备状态的枚举。 2648 2649**系统能力:** SystemCapability.Communication.WiFi.P2P 2650 2651| 名称 | 值 | 说明 | 2652| -------- | -------- | -------- | 2653| CONNECTED | 0 | 连接状态。 | 2654| INVITED | 1 | 邀请状态。 | 2655| FAILED | 2 | 失败状态。 | 2656| AVAILABLE | 3 | 可用状态。 | 2657| UNAVAILABLE | 4 | 不可用状态。 | 2658 2659 2660## wifiManager.getP2pLocalDevice<sup>9+</sup> 2661 2662getP2pLocalDevice(): Promise<WifiP2pDevice> 2663 2664获取P2P本端设备信息,使用Promise异步回调。 2665 2666**需要权限:** ohos.permission.GET_WIFI_INFO 和 ohos.permission.GET_WIFI_CONFIG 2667 2668**系统能力:** SystemCapability.Communication.WiFi.P2P 2669 2670**返回值:** 2671 2672 | 类型 | 说明 | 2673 | -------- | -------- | 2674 | Promise<[WifiP2pDevice](#wifip2pdevice9)> | Promise对象。表示本端设备信息。 | 2675 2676**错误码:** 2677 2678以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 2679 2680| **错误码ID** | **错误信息** | 2681 | -------- | -------- | 2682| 2801000 | Operation failed.| 2683 2684## wifiManager.getP2pLocalDevice<sup>9+</sup> 2685 2686getP2pLocalDevice(callback: AsyncCallback<WifiP2pDevice>): void 2687 2688获取P2P本端设备信息,使用callback异步回调。 2689 2690**需要权限:** ohos.permission.GET_WIFI_INFO 和 ohos.permission.GET_WIFI_CONFIG 2691 2692**系统能力:** SystemCapability.Communication.WiFi.P2P 2693 2694**参数:** 2695 2696 | 参数名 | 类型 | 必填 | 说明 | 2697 | -------- | -------- | -------- | -------- | 2698 | callback | AsyncCallback<[WifiP2pDevice](#wifip2pdevice9)> | 是 | 回调函数。当操作成功时,err为0,data表示本端设备信息。如果error为非0,表示处理出现错误。 | 2699 2700**错误码:** 2701 2702| **错误码ID** | **错误信息** | 2703 | -------- | -------- | 2704| 2801000 | Operation failed.| 2705 2706**示例:** 2707```ts 2708 import wifiManager from '@ohos.wifiManager'; 2709 2710 wifiManager.getP2pLocalDevice((err, data) => { 2711 if (err) { 2712 console.error("get P2P local device error"); 2713 return; 2714 } 2715 console.info("get P2P local device: " + JSON.stringify(data)); 2716 }); 2717 2718 wifiManager.getP2pLocalDevice().then(data => { 2719 console.info("get P2P local device: " + JSON.stringify(data)); 2720 }); 2721``` 2722 2723## wifiManager.createGroup<sup>9+</sup> 2724 2725createGroup(config: WifiP2PConfig): void 2726 2727创建群组。 2728 2729**需要权限:** ohos.permission.GET_WIFI_INFO 2730 2731**系统能力:** SystemCapability.Communication.WiFi.P2P 2732 2733**参数:** 2734 2735| **参数名** | **类型** | 必填 | **说明** | 2736| -------- | -------- | -------- | -------- | 2737| config | [WifiP2PConfig](#wifip2pconfig9) | 是 | 群组配置信息。如果DeviceAddressType未指定值,则DeviceAddressType默认为随机设备地址类型。 | 2738 2739**错误码:** 2740 2741以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 2742 2743| **错误码ID** | **错误信息** | 2744 | -------- | -------- | 2745| 2801000 | Operation failed.| 2746 2747**示例:** 2748```ts 2749 import wifiManager from '@ohos.wifiManager'; 2750 2751 try { 2752 let config:wifiManager.WifiP2PConfig = { 2753 deviceAddress: "****", 2754 netId: 0, 2755 passphrase: "*****", 2756 groupName: "****", 2757 goBand: 0 2758 } 2759 wifiManager.createGroup(config); 2760 2761 }catch(error){ 2762 console.error("failed:" + JSON.stringify(error)); 2763 } 2764``` 2765 2766## WifiP2PConfig<sup>9+</sup> 2767 2768表示P2P配置信息。 2769 2770**系统能力:** SystemCapability.Communication.WiFi.P2P 2771 2772| 名称 | 类型 | 可读 | 可写 | 说明 | 2773| -------- | -------- | -------- | -------- | -------- | 2774| deviceAddress | string | 是 | 否 | 设备地址。 | 2775| deviceAddressType<sup>10+</sup>| DeviceAddressType | 是 | 否 | 设备地址类型。 | 2776| netId | number | 是 | 否 | 网络ID。创建群组时-1表示创建临时组,-2表示创建永久组。 | 2777| passphrase | string | 是 | 否 | 群组密钥。 | 2778| groupName | string | 是 | 否 | 群组名称。 | 2779| goBand | [GroupOwnerBand](#groupownerband9) | 是 | 否 | 群组带宽。 | 2780 2781 2782## GroupOwnerBand<sup>9+</sup> 2783 2784表示群组带宽的枚举。 2785 2786**系统能力:** SystemCapability.Communication.WiFi.P2P 2787 2788| 名称 | 值 | 说明 | 2789| -------- | -------- | -------- | 2790| GO_BAND_AUTO | 0 | 自动模式。 | 2791| GO_BAND_2GHZ | 1 | 2GHZ。 | 2792| GO_BAND_5GHZ | 2 | 5GHZ。 | 2793 2794 2795## wifiManager.removeGroup<sup>9+</sup> 2796 2797removeGroup(): void 2798 2799移除群组。 2800 2801**需要权限:** ohos.permission.GET_WIFI_INFO 2802 2803**系统能力:** SystemCapability.Communication.WiFi.P2P 2804 2805**错误码:** 2806 2807以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 2808 2809| **错误码ID** | **错误信息** | 2810 | -------- | -------- | 2811| 2801000 | Operation failed.| 2812 2813**示例:** 2814```ts 2815 import wifiManager from '@ohos.wifiManager'; 2816 2817 try { 2818 wifiManager.removeGroup(); 2819 }catch(error){ 2820 console.error("failed:" + JSON.stringify(error)); 2821 } 2822``` 2823 2824## wifiManager.p2pConnect<sup>9+</sup> 2825 2826p2pConnect(config: WifiP2PConfig): void 2827 2828执行P2P连接。 2829 2830**需要权限:** 2831 2832API 9:ohos.permission.GET_WIFI_INFO、ohos.permission.LOCATION 和 ohos.permission.APPROXIMATELY_LOCATION 2833 2834API 10起:ohos.permission.GET_WIFI_INFO 2835 2836**系统能力:** SystemCapability.Communication.WiFi.P2P 2837 2838**参数:** 2839 2840| **参数名** | **类型** | 必填 | **说明** | 2841| -------- | -------- | -------- | -------- | 2842| config | [WifiP2PConfig](#wifip2pconfig9) | 是 | 连接配置信息。如果DeviceAddressType未指定值,则DeviceAddressType默认为随机设备地址类型。 | 2843 2844**错误码:** 2845 2846以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 2847 2848| **错误码ID** | **错误信息** | 2849 | -------- | -------- | 2850| 2801000 | Operation failed.| 2851 2852**示例:** 2853```ts 2854 import wifiManager from '@ohos.wifiManager'; 2855 2856 let recvP2pConnectionChangeFunc = (result:wifiManager.WifiP2pLinkedInfo) => { 2857 console.info("p2p connection change receive event: " + JSON.stringify(result)); 2858 wifiManager.getP2pLinkedInfo((err, data) => { 2859 if (err) { 2860 console.error('failed to get getP2pLinkedInfo: ' + JSON.stringify(err)); 2861 return; 2862 } 2863 console.info("get getP2pLinkedInfo: " + JSON.stringify(data)); 2864 }); 2865 } 2866 wifiManager.on("p2pConnectionChange", recvP2pConnectionChangeFunc); 2867 2868 let recvP2pDeviceChangeFunc = (result:wifiManager.WifiP2pDevice) => { 2869 console.info("p2p device change receive event: " + JSON.stringify(result)); 2870 } 2871 wifiManager.on("p2pDeviceChange", recvP2pDeviceChangeFunc); 2872 2873 let recvP2pPeerDeviceChangeFunc = (result:wifiManager.WifiP2pDevice[]) => { 2874 console.info("p2p peer device change receive event: " + JSON.stringify(result)); 2875 wifiManager.getP2pPeerDevices((err, data) => { 2876 if (err) { 2877 console.error('failed to get peer devices: ' + JSON.stringify(err)); 2878 return; 2879 } 2880 console.info("get peer devices: " + JSON.stringify(data)); 2881 let len = data.length; 2882 for (let i = 0; i < len; ++i) { 2883 if (data[i].deviceName === "my_test_device") { 2884 console.info("p2p connect to test device: " + data[i].deviceAddress); 2885 let config:wifiManager.WifiP2PConfig = { 2886 deviceAddress:data[i].deviceAddress, 2887 netId:-2, 2888 passphrase:"", 2889 groupName:"", 2890 goBand:0, 2891 } 2892 wifiManager.p2pConnect(config); 2893 } 2894 } 2895 }); 2896 } 2897 wifiManager.on("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc); 2898 2899 let recvP2pPersistentGroupChangeFunc = () => { 2900 console.info("p2p persistent group change receive event"); 2901 2902 wifiManager.getCurrentGroup((err, data) => { 2903 if (err) { 2904 console.error('failed to get current group: ' + JSON.stringify(err)); 2905 return; 2906 } 2907 console.info("get current group: " + JSON.stringify(data)); 2908 }); 2909 } 2910 wifiManager.on("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc); 2911 2912 setTimeout(() => {wifiManager.off("p2pConnectionChange", recvP2pConnectionChangeFunc);}, 125 * 1000); 2913 setTimeout(() => {wifiManager.off("p2pDeviceChange", recvP2pDeviceChangeFunc);}, 125 * 1000); 2914 setTimeout(() => {wifiManager.off("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc);}, 125 * 1000); 2915 setTimeout(() => {wifiManager.off("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc);}, 125 * 1000); 2916 console.info("start discover devices -> " + wifiManager.startDiscoverDevices()); 2917``` 2918 2919## wifiManager.p2pCancelConnect<sup>9+</sup> 2920 2921p2pCancelConnect(): void 2922 2923取消P2P连接。 2924 2925**需要权限:** ohos.permission.GET_WIFI_INFO 2926 2927**系统能力:** SystemCapability.Communication.WiFi.P2P 2928 2929**错误码:** 2930 2931以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 2932 2933| **错误码ID** | **错误信息** | 2934 | -------- | -------- | 2935| 2801000 | Operation failed.| 2936 2937**示例:** 2938```ts 2939 import wifiManager from '@ohos.wifiManager'; 2940 2941 try { 2942 wifiManager.p2pCancelConnect(); 2943 }catch(error){ 2944 console.error("failed:" + JSON.stringify(error)); 2945 } 2946``` 2947 2948## wifiManager.startDiscoverDevices<sup>9+</sup> 2949 2950startDiscoverDevices(): void 2951 2952开始发现设备。 2953 2954**需要权限:** 2955 2956API 9:ohos.permission.GET_WIFI_INFO 和 ohos.permission.LOCATION 和 ohos.permission.APPROXIMATELY_LOCATION 2957 2958API 10起:ohos.permission.GET_WIFI_INFO 2959 2960**系统能力:** SystemCapability.Communication.WiFi.P2P 2961 2962**错误码:** 2963 2964以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 2965 2966| **错误码ID** | **错误信息** | 2967 | -------- | -------- | 2968| 2801000 | Operation failed.| 2969 2970**示例:** 2971```ts 2972 import wifiManager from '@ohos.wifiManager'; 2973 2974 try { 2975 wifiManager.startDiscoverDevices(); 2976 }catch(error){ 2977 console.error("failed:" + JSON.stringify(error)); 2978 } 2979``` 2980 2981## wifiManager.stopDiscoverDevices<sup>9+</sup> 2982 2983stopDiscoverDevices(): void 2984 2985停止发现设备。 2986 2987**需要权限:** ohos.permission.GET_WIFI_INFO 2988 2989**系统能力:** SystemCapability.Communication.WiFi.P2P 2990 2991**错误码:** 2992 2993以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 2994 2995| **错误码ID** | **错误信息** | 2996 | -------- | -------- | 2997| 2801000 | Operation failed.| 2998 2999**示例:** 3000```ts 3001 import wifiManager from '@ohos.wifiManager'; 3002 3003 try { 3004 wifiManager.stopDiscoverDevices(); 3005 }catch(error){ 3006 console.error("failed:" + JSON.stringify(error)); 3007 } 3008``` 3009 3010## wifiManager.deletePersistentGroup<sup>9+</sup> 3011 3012deletePersistentGroup(netId: number): void 3013 3014删除永久组。 3015 3016**系统接口:** 此接口为系统接口。 3017 3018**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION 3019 3020**系统能力:** SystemCapability.Communication.WiFi.P2P 3021 3022**参数:** 3023 3024 3025 | **参数名** | **类型** | 必填 | **说明** | 3026 | -------- | -------- | -------- | -------- | 3027 | netId | number | 是 | 组的ID。 | 3028 3029**错误码:** 3030 3031以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 3032 3033| **错误码ID** | **错误信息** | 3034 | -------- | -------- | 3035| 2801000 | Operation failed.| 3036 3037**示例:** 3038```ts 3039 import wifiManager from '@ohos.wifiManager'; 3040 3041 try { 3042 let netId = 0; 3043 wifiManager.deletePersistentGroup(netId); 3044 }catch(error){ 3045 console.error("failed:" + JSON.stringify(error)); 3046 } 3047``` 3048 3049## wifiManager.getP2pGroups<sup>9+</sup> 3050 3051getP2pGroups(): Promise<Array<WifiP2pGroupInfo>> 3052 3053获取创建的所有P2P群组信息,使用Promise异步回调。 3054 3055**系统接口:** 此接口为系统接口。 3056 3057**需要权限:** 3058 3059API 9:ohos.permission.GET_WIFI_INFO、ohos.permission.LOCATION 和 ohos.permission.APPROXIMATELY_LOCATION 3060 3061API 10起:ohos.permission.GET_WIFI_INFO 3062 3063**系统能力:** SystemCapability.Communication.WiFi.P2P 3064 3065**返回值:** 3066 3067| 类型 | 说明 | 3068| -------- | -------- | 3069| Promise< Array<[WifiP2pGroupInfo](#wifip2pgroupinfo9)> > | Promise对象。表示所有群组信息。如果应用申请了ohos.permission.GET_WIFI_PEERS_MAC权限,则返回结果中的deviceAddress为真实设备地址,否则为随机设备地址。 | 3070 3071**错误码:** 3072 3073以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 3074 3075| **错误码ID** | **错误信息** | 3076 | -------- | -------- | 3077| 2801000 | Operation failed.| 3078 3079**示例:** 3080```ts 3081 import wifiManager from '@ohos.wifiManager'; 3082 3083 wifiManager.getP2pGroups((err, data) => { 3084 if (err) { 3085 console.error("get P2P groups error"); 3086 return; 3087 } 3088 console.info("get P2P groups: " + JSON.stringify(data)); 3089 }); 3090 3091 wifiManager.getP2pGroups().then(data => { 3092 console.info("get P2P groups: " + JSON.stringify(data)); 3093 }); 3094 3095``` 3096 3097## WifiP2pGroupInfo<sup>9+</sup> 3098 3099表示P2P群组相关信息。 3100 3101**系统能力:** SystemCapability.Communication.WiFi.P2P 3102 3103| 名称 | 类型 | 可读 | 可写 | 说明 | 3104| -------- | -------- | -------- | -------- | -------- | 3105| isP2pGo | boolean | 是 | 否 | 是否是群主。 | 3106| ownerInfo | [WifiP2pDevice](#wifip2pdevice9) | 是 | 否 | 群组的设备信息。 | 3107| passphrase | string | 是 | 否 | 群组密钥。 | 3108| interface | string | 是 | 否 | 接口名称。 | 3109| groupName | string | 是 | 否 | 群组名称。 | 3110| networkId | number | 是 | 否 | 网络ID。 | 3111| frequency | number | 是 | 否 | 群组的频率。 | 3112| clientDevices | [WifiP2pDevice[]](#wifip2pdevice9) | 是 | 否 | 接入的设备列表信息。 | 3113| goIpAddress | string | 是 | 否 | 群组IP地址。 | 3114 3115 3116## wifiManager.getP2pGroups<sup>9+</sup> 3117 3118getP2pGroups(callback: AsyncCallback<Array<WifiP2pGroupInfo>>): void 3119 3120获取创建的所有P2P群组信息,使用callback方式作为异步方法。 3121 3122**系统接口:** 此接口为系统接口。 3123 3124**需要权限:** 3125 3126API 9:ohos.permission.GET_WIFI_INFO、ohos.permission.LOCATION 和 ohos.permission.APPROXIMATELY_LOCATION 3127 3128API 10起:ohos.permission.GET_WIFI_INFO 3129 3130**系统能力:** SystemCapability.Communication.WiFi.P2P 3131 3132**参数:** 3133 3134| 参数名 | 类型 | 必填 | 说明 | 3135| -------- | -------- | -------- | -------- | 3136| callback | AsyncCallback< Array<[WifiP2pGroupInfo](#wifip2pgroupinfo9)>> | 是 | 回调函数。当操作成功时,err为0,data表示所有群组信息。如果error为非0,表示处理出现错误。如果应用申请了ohos.permission.GET_WIFI_PEERS_MAC权限,则返回结果中的deviceAddress为真实设备地址,否则为随机设备地址。 | 3137 3138**错误码:** 3139 3140以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 3141 3142| **错误码ID** | **错误信息** | 3143 | -------- | -------- | 3144| 2801000 | Operation failed.| 3145 3146## wifiManager.setDeviceName<sup>9+</sup> 3147 3148setDeviceName(devName: string): void 3149 3150设置设备名称。 3151 3152**系统接口:** 此接口为系统接口。 3153 3154**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 3155 3156**系统能力:** SystemCapability.Communication.WiFi.P2P 3157 3158**参数:** 3159 3160 | **参数名** | **类型** | **必填** | **说明** | 3161 | -------- | -------- | -------- | -------- | 3162 | devName | string | 是 | 设备名称。 | 3163 3164**错误码:** 3165 3166以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 3167 3168| **错误码ID** | **错误信息** | 3169 | -------- | -------- | 3170| 2801000 | Operation failed.| 3171 3172**示例:** 3173```ts 3174 import wifiManager from '@ohos.wifiManager'; 3175 3176 try { 3177 let name = "****"; 3178 wifiManager.setDeviceName(name); 3179 }catch(error){ 3180 console.error("failed:" + JSON.stringify(error)); 3181 } 3182``` 3183 3184## wifiManager.on('wifiStateChange')<sup>9+</sup> 3185 3186on(type: "wifiStateChange", callback: Callback<number>): void 3187 3188注册WLAN状态改变事件。 3189 3190**需要权限:** ohos.permission.GET_WIFI_INFO 3191 3192**系统能力:** SystemCapability.Communication.WiFi.STA 3193 3194**参数:** 3195 3196 | **参数名** | **类型** | **必填** | **说明** | 3197 | -------- | -------- | -------- | -------- | 3198 | type | string | 是 | 固定填"wifiStateChange"字符串。 | 3199 | callback | Callback<number> | 是 | 状态改变回调函数。 | 3200 3201**错误码:** 3202 3203以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 3204 3205| **错误码ID** | **错误信息** | 3206 | -------- | -------- | 3207| 2501000 | Operation failed.| 3208 3209**状态改变事件的枚举:** 3210 3211| **枚举值** | **说明** | 3212| -------- | -------- | 3213| 0 | 未激活。 | 3214| 1 | 已激活。 | 3215| 2 | 激活中。 | 3216| 3 | 去激活中。 | 3217 3218 3219## wifiManager.off('wifiStateChange')<sup>9+</sup> 3220 3221off(type: "wifiStateChange", callback?: Callback<number>): void 3222 3223取消注册WLAN状态改变事件。 3224 3225**需要权限:** ohos.permission.GET_WIFI_INFO 3226 3227**系统能力:** SystemCapability.Communication.WiFi.STA 3228 3229**参数:** 3230 3231 | **参数名** | **类型** | **必填** | **说明** | 3232 | -------- | -------- | -------- | -------- | 3233 | type | string | 是 | 固定填"wifiStateChange"字符串。 | 3234 | callback | Callback<number> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 3235 3236**错误码:** 3237 3238以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 3239 3240| **错误码ID** | **错误信息** | 3241 | -------- | -------- | 3242| 2501000 | Operation failed.| 3243 3244**示例:** 3245```ts 3246 import wifiManager from '@ohos.wifiManager'; 3247 3248 let recvPowerNotifyFunc = (result:number) => { 3249 console.info("Receive power state change event: " + result); 3250 } 3251 3252 // Register event 3253 wifiManager.on("wifiStateChange", recvPowerNotifyFunc); 3254 3255 // Unregister event 3256 wifiManager.off("wifiStateChange", recvPowerNotifyFunc); 3257``` 3258 3259 3260## wifiManager.on('wifiConnectionChange')<sup>9+</sup> 3261 3262on(type: "wifiConnectionChange", callback: Callback<number>): void 3263 3264注册WLAN连接状态改变事件。 3265 3266**需要权限:** ohos.permission.GET_WIFI_INFO 3267 3268**系统能力:** SystemCapability.Communication.WiFi.STA 3269 3270**参数:** 3271 3272 | **参数名** | **类型** | **必填** | **说明** | 3273 | -------- | -------- | -------- | -------- | 3274 | type | string | 是 | 固定填"wifiConnectionChange"字符串。 | 3275 | callback | Callback<number> | 是 | 状态改变回调函数。 | 3276 3277**连接状态改变事件的枚举:** 3278 3279| **枚举值** | **说明** | 3280| -------- | -------- | 3281| 0 | 已断开。 | 3282| 1 | 已连接。 | 3283 3284**错误码:** 3285 3286以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 3287 3288| **错误码ID** | **错误信息** | 3289 | -------- | -------- | 3290| 2501000 | Operation failed.| 3291 3292## wifiManager.off('wifiConnectionChange')<sup>9+</sup> 3293 3294off(type: "wifiConnectionChange", callback?: Callback<number>): void 3295 3296取消注册WLAN连接状态改变事件。 3297 3298**需要权限:** ohos.permission.GET_WIFI_INFO 3299 3300**系统能力:** SystemCapability.Communication.WiFi.STA 3301 3302**参数:** 3303 3304 | **参数名** | **类型** | **必填** | **说明** | 3305 | -------- | -------- | -------- | -------- | 3306 | type | string | 是 | 固定填"wifiConnectionChange"字符串。 | 3307 | callback | Callback<number> | 否 | 连接状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 3308 3309**错误码:** 3310 3311以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 3312 3313| **错误码ID** | **错误信息** | 3314 | -------- | -------- | 3315| 2501000 | Operation failed.| 3316 3317**示例:** 3318```ts 3319 import wifiManager from '@ohos.wifiManager'; 3320 3321 let recvWifiConnectionChangeFunc = (result:number) => { 3322 console.info("Receive wifi connection change event: " + result); 3323 } 3324 3325 // Register event 3326 wifiManager.on("wifiConnectionChange", recvWifiConnectionChangeFunc); 3327 3328 // Unregister event 3329 wifiManager.off("wifiConnectionChange", recvWifiConnectionChangeFunc); 3330``` 3331 3332## wifiManager.on('wifiScanStateChange')<sup>9+</sup> 3333 3334on(type: "wifiScanStateChange", callback: Callback<number>): void 3335 3336注册扫描状态改变事件。 3337 3338**需要权限:** ohos.permission.GET_WIFI_INFO 3339 3340**系统能力:** SystemCapability.Communication.WiFi.STA 3341 3342**参数:** 3343 3344 | **参数名** | **类型** | **必填** | **说明** | 3345 | -------- | -------- | -------- | -------- | 3346 | type | string | 是 | 固定填"wifiScanStateChange"字符串。 | 3347 | callback | Callback<number> | 是 | 状态改变回调函数。 | 3348 3349**扫描状态改变事件的枚举:** 3350 3351| **枚举值** | **说明** | 3352| -------- | -------- | 3353| 0 | 扫描失败。 | 3354| 1 | 扫描成功。 | 3355 3356**错误码:** 3357 3358以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 3359 3360| **错误码ID** | **错误信息** | 3361 | -------- | -------- | 3362| 2501000 | Operation failed.| 3363 3364## wifiManager.off('wifiScanStateChange')<sup>9+</sup> 3365 3366off(type: "wifiScanStateChange", callback?: Callback<number>): void 3367 3368取消注册扫描状态改变事件。 3369 3370**需要权限:** ohos.permission.GET_WIFI_INFO 3371 3372**系统能力:** SystemCapability.Communication.WiFi.STA 3373 3374**参数:** 3375 3376| **参数名** | **类型** | **必填** | **说明** | 3377| -------- | -------- | -------- | -------- | 3378| type | string | 是 | 固定填"wifiScanStateChange"字符串。 | 3379| callback | Callback<number> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 3380 3381**错误码:** 3382 3383以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 3384 3385| **错误码ID** | **错误信息** | 3386 | -------- | -------- | 3387| 2501000 | Operation failed.| 3388 3389**示例:** 3390```ts 3391 import wifiManager from '@ohos.wifiManager'; 3392 3393 let recvWifiScanStateChangeFunc = (result:number) => { 3394 console.info("Receive Wifi scan state change event: " + result); 3395 } 3396 3397 // Register event 3398 wifiManager.on("wifiScanStateChange", recvWifiScanStateChangeFunc); 3399 3400 // Unregister event 3401 wifiManager.off("wifiScanStateChange", recvWifiScanStateChangeFunc); 3402``` 3403 3404## wifiManager.on('wifiRssiChange')<sup>9+</sup> 3405 3406on(type: "wifiRssiChange", callback: Callback<number>): void 3407 3408注册RSSI状态改变事件。 3409 3410**需要权限:** ohos.permission.GET_WIFI_INFO 3411 3412**系统能力:** SystemCapability.Communication.WiFi.STA 3413 3414**参数:** 3415 3416 | **参数名** | **类型** | **必填** | **说明** | 3417 | -------- | -------- | -------- | -------- | 3418 | type | string | 是 | 固定填"wifiRssiChange"字符串。 | 3419 | callback | Callback<number> | 是 | 状态改变回调函数,返回以dBm为单位的RSSI值。 | 3420 3421**错误码:** 3422 3423以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 3424 3425| **错误码ID** | **错误信息** | 3426 | -------- | -------- | 3427| 2501000 | Operation failed.| 3428 3429## wifiManager.off('wifiRssiChange')<sup>9+</sup> 3430 3431off(type: "wifiRssiChange", callback?: Callback<number>): void 3432 3433取消注册RSSI状态改变事件。 3434 3435**需要权限:** ohos.permission.GET_WIFI_INFO 3436 3437**系统能力:** SystemCapability.Communication.WiFi.STA 3438 3439**参数:** 3440 3441| **参数名** | **类型** | **必填** | **说明** | 3442| -------- | -------- | -------- | -------- | 3443| type | string | 是 | 固定填"wifiRssiChange"字符串。 | 3444| callback | Callback<number> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 3445 3446**错误码:** 3447 3448以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 3449 3450| **错误码ID** | **错误信息** | 3451 | -------- | -------- | 3452| 2501000 | Operation failed.| 3453 3454**示例:** 3455```ts 3456 import wifiManager from '@ohos.wifiManager'; 3457 3458 let recvWifiRssiChangeFunc = (result:number) => { 3459 console.info("Receive wifi rssi change event: " + result); 3460 } 3461 3462 // Register event 3463 wifiManager.on("wifiRssiChange", recvWifiRssiChangeFunc); 3464 3465 // Unregister event 3466 wifiManager.off("wifiRssiChange", recvWifiRssiChangeFunc); 3467``` 3468 ## wifiManager.on('streamChange')<sup>9+</sup> 3469 3470on(type: "streamChange", callback: Callback<number>): void 3471 3472注册WIFI流变更事件,当前版本不支持,抛出通用错误码801。 3473 3474**系统接口:** 此接口为系统接口。 3475 3476**需要权限:** ohos.permission.MANAGE_WIFI_CONNECTION 3477 3478**系统能力:** SystemCapability.Communication.WiFi.STA 3479 3480**参数:** 3481 3482| **参数名** | **类型** | **必填** | **说明** | 3483| -------- | -------- | -------- | -------- | 3484| type | string | 是 | 固定填"streamChange"字符串。 | 3485| callback | Callback<number> | 是 | 状态改变回调函数,返回0:无,1:向下,2:向上,3:双向。 | 3486 3487**错误码:** 3488 3489以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 3490 3491| **错误码ID** | **错误信息** | 3492 | -------- | -------- | 3493| 2501000 | Operation failed.| 3494 3495## wifiManager.off('streamChange')<sup>9+</sup> 3496 3497off(type: "streamChange", callback?: Callback<number>): void 3498 3499取消注册WIFI流变更事件,当前版本不支持,抛出通用错误码801。 3500 3501**系统接口:** 此接口为系统接口。 3502 3503**需要权限:** ohos.permission.MANAGE_WIFI_CONNECTION 3504 3505**系统能力:** SystemCapability.Communication.WiFi.STA 3506 3507**参数:** 3508 3509| **参数名** | **类型** | **必填** | **说明** | 3510| -------- | -------- | -------- | -------- | 3511| type | string | 是 | 固定填"streamChange"字符串。 | 3512| callback | Callback<number> | 否 | 状态改变回调函数,返回0:无,1:向下,2:向上,3:双向。 | 3513 3514**错误码:** 3515 3516以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 3517 3518| **错误码ID** | **错误信息** | 3519 | -------- | -------- | 3520| 2501000 | Operation failed.| 3521 3522**示例:** 3523```ts 3524import wifi from '@ohos.wifi'; 3525 3526let recvStreamChangeFunc = (result:number) => { 3527 console.info("Receive stream change event: " + result); 3528} 3529 3530// Register event 3531wifi.on("streamChange", recvStreamChangeFunc); 3532 3533// Unregister event 3534wifi.off("streamChange", recvStreamChangeFunc); 3535 3536``` 3537## wifiManager.on('deviceConfigChange')<sup>9+</sup> 3538 3539on(type: "deviceConfigChange", callback: Callback<number>): void 3540 3541注册WIFI设备配置更改事件。 3542 3543**系统接口:** 此接口为系统接口。 3544 3545**需要权限:** ohos.permission.GET_WIFI_INFO 3546 3547**系统能力:** SystemCapability.Communication.WiFi.STA 3548 3549**参数:** 3550 3551| **参数名** | **类型** | **必填** | **说明** | 3552| -------- | -------- | -------- | -------- | 3553| type | string | 是 | 固定填"streamChange"字符串。 | 3554| callback | Callback<number> | 是 | 状态改变回调函数,返回0: 添加配置, 1: 更改配置, 2: 删除配置. | 3555 3556**错误码:** 3557 3558以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 3559 3560| **错误码ID** | **错误信息** | 3561 | -------- | -------- | 3562| 2501000 | Operation failed.| 3563 3564## wifiManager.off('deviceConfigChange')<sup>9+</sup> 3565 3566off(type: "deviceConfigChange", callback?: Callback<number>): void 3567 3568取消注册WIFI设备配置更改事件。 3569 3570**系统接口:** 此接口为系统接口。 3571 3572**需要权限:** ohos.permission.GET_WIFI_INFO 3573 3574**系统能力:** SystemCapability.Communication.WiFi.STA 3575 3576**参数:** 3577 3578| **参数名** | **类型** | **必填** | **说明** | 3579| -------- | -------- | -------- | -------- | 3580| type | string | 是 | 固定填"streamChange"字符串。 | 3581| callback | Callback<number> | 否 | 状态改变回调函数,返回0: 添加配置, 1: 更改配置, 2: 删除配置.| 3582 3583**错误码:** 3584 3585以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 3586 3587| **错误码ID** | **错误信息** | 3588 | -------- | -------- | 3589| 2501000 | Operation failed.| 3590 3591**示例:** 3592```ts 3593import wifi from '@ohos.wifiManager'; 3594 3595let recvDeviceConfigChangeFunc = (result:number) => { 3596 console.info("Receive device config change event: " + result); 3597} 3598 3599// Register event 3600wifi.on("deviceConfigChange", recvDeviceConfigChangeFunc); 3601 3602// Unregister event 3603wifi.off("deviceConfigChange", recvDeviceConfigChangeFunc); 3604 3605``` 3606 3607## wifiManager.on('hotspotStateChange')<sup>9+</sup> 3608 3609on(type: "hotspotStateChange", callback: Callback<number>): void 3610 3611注册热点状态改变事件。 3612 3613**需要权限:** ohos.permission.GET_WIFI_INFO 3614 3615**系统能力:** SystemCapability.Communication.WiFi.AP.Core 3616 3617**参数:** 3618 3619| **参数名** | **类型** | **必填** | **说明** | 3620| -------- | -------- | -------- | -------- | 3621| type | string | 是 | 固定填"hotspotStateChange"字符串。 | 3622| callback | Callback<number> | 是 | 状态改变回调函数。 | 3623 3624**热点状态改变事件的枚举:** 3625 3626| **枚举值** | **说明** | 3627| -------- | -------- | 3628| 0 | 未激活。 | 3629| 1 | 已激活。 | 3630| 2 | 激活中。 | 3631| 3 | 去激活中。 | 3632 3633**错误码:** 3634 3635以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 3636 3637| **错误码ID** | **错误信息** | 3638 | -------- | -------- | 3639| 2601000 | Operation failed.| 3640 3641## wifiManager.off('hotspotStateChange')<sup>9+</sup> 3642 3643off(type: "hotspotStateChange", callback?: Callback<number>): void 3644 3645取消注册热点状态改变事件。 3646 3647**需要权限:** ohos.permission.GET_WIFI_INFO 3648 3649**系统能力:** SystemCapability.Communication.WiFi.AP.Core 3650 3651**参数:** 3652 3653| **参数名** | **类型** | **必填** | **说明** | 3654| -------- | -------- | -------- | -------- | 3655| type | string | 是 | 固定填"hotspotStateChange"字符串。 | 3656| callback | Callback<number> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 3657 3658**错误码:** 3659 3660以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 3661 3662| **错误码ID** | **错误信息** | 3663 | -------- | -------- | 3664| 2601000 | Operation failed.| 3665 3666**示例:** 3667```ts 3668 import wifiManager from '@ohos.wifiManager'; 3669 3670 let recvHotspotStateChangeFunc = (result:number) => { 3671 console.info("Receive hotspot state change event: " + result); 3672 } 3673 3674 // Register event 3675 wifiManager.on("hotspotStateChange", recvHotspotStateChangeFunc); 3676 3677 // Unregister event 3678 wifiManager.off("hotspotStateChange", recvHotspotStateChangeFunc); 3679``` 3680 3681## wifiManager.on('hotspotStaJoin')<sup>9+</sup> 3682 3683on(type: "hotspotStaJoin", callback: Callback<StationInfo>): void 3684 3685注册wifi热点sta加入事件。 3686 3687**系统接口:** 此接口为系统接口。 3688 3689**需要权限:** ohos.permission.MANAGE_WIFI_HOTSPOT 3690 3691**系统能力:** SystemCapability.Communication.WiFi.AP.Core 3692 3693**参数:** 3694 3695| **参数名** | **类型** | **必填** | **说明** | 3696| -------- | -------- | -------- | -------- | 3697| type | string | 是 | 固定填"hotspotStaJoin"字符串。 | 3698| callback | Callback<StationInfo> | 否 | 状态改变回调函数。 | 3699 3700**错误码:** 3701 3702以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 3703 3704| **错误码ID** | **错误信息** | 3705 | -------- | -------- | 3706| 2601000 | Operation failed.| 3707 3708## wifiManager.off('hotspotStaJoin')<sup>9+</sup> 3709 3710off(type: "hotspotStaJoin", callback?: Callback<StationInfo>): void 3711 3712取消注册wifi热点sta加入事件。 3713 3714**系统接口:** 此接口为系统接口。 3715 3716**需要权限:** ohos.permission.MANAGE_WIFI_HOTSPOT 3717 3718**系统能力:** SystemCapability.Communication.WiFi.AP.Core 3719 3720**参数:** 3721 3722| **参数名** | **类型** | **必填** | **说明** | 3723| -------- | -------- | -------- | -------- | 3724| type | string | 是 | 固定填"hotspotStaJoin"字符串。 | 3725| callback | Callback<StationInfo> | 否 | 状态改变回调函数。 | 3726 3727**错误码:** 3728 3729以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 3730 3731| **错误码ID** | **错误信息** | 3732 | -------- | -------- | 3733| 2601000 | Operation failed.| 3734 3735**示例:** 3736```ts 3737import wifiManager from '@ohos.wifiManager'; 3738 3739let recvHotspotStaJoinFunc = (result:wifiManager.StationInfo) => { 3740 console.info("Receive hotspot sta join event: " + result); 3741} 3742 3743// Register event 3744wifiManager.on("hotspotStaJoin", recvHotspotStaJoinFunc); 3745 3746// Unregister event 3747wifiManager.off("hotspotStaJoin", recvHotspotStaJoinFunc); 3748 3749``` 3750 3751## wifiManager.on('hotspotStaLeave')<sup>9+</sup> 3752 3753on(type: "hotspotStaLeave", callback: Callback<StationInfo>): void 3754 3755注册wifi热点sta离开事件。 3756 3757**系统接口:** 此接口为系统接口。 3758 3759**需要权限:** ohos.permission.MANAGE_WIFI_HOTSPOT 3760 3761**系统能力:** SystemCapability.Communication.WiFi.AP.Core 3762 3763**参数:** 3764 3765 | **参数名** | **类型** | **必填** | **说明** | 3766 | -------- | -------- | -------- | -------- | 3767 | type | string | 是 | 固定填"hotspotStaLeave"字符串。 | 3768 | callback | Callback<StationInf]> | 否 | 状态改变回调函数。 | 3769 3770**错误码:** 3771 3772以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 3773 3774| **错误码ID** | **错误信息** | 3775 | -------- | -------- | 3776| 2601000 | Operation failed.| 3777 3778## wifiManager.off('hotspotStaLeave')<sup>7+</sup> 3779 3780off(type: "hotspotStaLeave", callback?: Callback<StationInfo>): void 3781 3782取消注册wifi热点sta离开事件。 3783 3784**系统接口:** 此接口为系统接口。 3785 3786**需要权限:** ohos.permission.MANAGE_WIFI_HOTSPOT 3787 3788**系统能力:** SystemCapability.Communication.WiFi.AP.Core 3789 3790**参数:** 3791 3792| **参数名** | **类型** | **必填** | **说明** | 3793| -------- | -------- | -------- | -------- | 3794| type | string | 是 | 固定填"hotspotStaLeave"字符串。 | 3795| callback | Callback<StationInf]> | 否 | 状态改变回调函数。 | 3796 3797**错误码:** 3798 3799以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 3800 3801| **错误码ID** | **错误信息** | 3802 | -------- | -------- | 3803| 2601000 | Operation failed.| 3804 3805**示例:** 3806```ts 3807import wifiManager from '@ohos.wifiManager'; 3808 3809let recvHotspotStaLeaveFunc = (result:wifiManager.StationInfo) => { 3810 console.info("Receive hotspot sta leave event: " + result); 3811} 3812 3813// Register event 3814wifiManager.on("hotspotStaLeave", recvHotspotStaLeaveFunc); 3815 3816// Unregister event 3817wifiManager.off("hotspotStaLeave", recvHotspotStaLeaveFunc); 3818 3819``` 3820 3821## wifiManager.on('p2pStateChange')<sup>9+</sup> 3822 3823on(type: "p2pStateChange", callback: Callback<number>): void 3824 3825注册P2P开关状态改变事件。 3826 3827**需要权限:** ohos.permission.GET_WIFI_INFO 3828 3829**系统能力:** SystemCapability.Communication.WiFi.P2P 3830 3831**参数:** 3832 3833| **参数名** | **类型** | **必填** | **说明** | 3834| -------- | -------- | -------- | -------- | 3835| type | string | 是 | 固定填"p2pStateChange"字符串。 | 3836| callback | Callback<number> | 是 | 状态改变回调函数。 | 3837 3838** P2P状态改变事件的枚举:** 3839 3840| **枚举值** | **说明** | 3841| -------- | -------- | 3842| 1 | 空闲。 | 3843| 2 | 打开中。 | 3844| 3 | 已打开。 | 3845| 4 | 关闭中。 | 3846| 5 | 已关闭。 | 3847 3848**错误码:** 3849 3850以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 3851 3852| **错误码ID** | **错误信息** | 3853 | -------- | -------- | 3854| 2801000 | Operation failed.| 3855 3856## wifiManager.off('p2pStateChange')<sup>9+</sup> 3857 3858off(type: "p2pStateChange", callback?: Callback<number>): void 3859 3860取消注册P2P开关状态改变事件。 3861 3862**需要权限:** ohos.permission.GET_WIFI_INFO 3863 3864**系统能力:** SystemCapability.Communication.WiFi.P2P 3865 3866**参数:** 3867 3868 | **参数名** | **类型** | **必填** | **说明** | 3869 | -------- | -------- | -------- | -------- | 3870 | type | string | 是 | 固定填"p2pStateChange"字符串。 | 3871 | callback | Callback<number> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 3872 3873**错误码:** 3874 3875以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 3876 3877| **错误码ID** | **错误信息** | 3878 | -------- | -------- | 3879| 2801000 | Operation failed.| 3880 3881**示例:** 3882```ts 3883 import wifiManager from '@ohos.wifiManager'; 3884 3885 let recvP2pStateChangeFunc = (result:number) => { 3886 console.info("Receive p2p state change event: " + result); 3887 } 3888 3889 // Register event 3890 wifiManager.on("p2pStateChange", recvP2pStateChangeFunc); 3891 3892 // Unregister event 3893 wifiManager.off("p2pStateChange", recvP2pStateChangeFunc); 3894``` 3895 3896## wifiManager.on('p2pConnectionChange')<sup>9+</sup> 3897 3898on(type: "p2pConnectionChange", callback: Callback<WifiP2pLinkedInfo>): void 3899 3900注册P2P连接状态改变事件。 3901 3902**需要权限:** ohos.permission.GET_WIFI_INFO 3903 3904**系统能力:** SystemCapability.Communication.WiFi.P2P 3905 3906**参数:** 3907 3908 | **参数名** | **类型** | **必填** | **说明** | 3909 | -------- | -------- | -------- | -------- | 3910 | type | string | 是 | 固定填"p2pConnectionChange"字符串。 | 3911 | callback | Callback<[WifiP2pLinkedInfo](#wifip2plinkedinfo9)> | 是 | 状态改变回调函数。 | 3912 3913**错误码:** 3914 3915以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 3916 3917| **错误码ID** | **错误信息** | 3918 | -------- | -------- | 3919| 2801000 | Operation failed.| 3920 3921## wifiManager.off('p2pConnectionChange')<sup>9+</sup> 3922 3923off(type: "p2pConnectionChange", callback?: Callback<WifiP2pLinkedInfo>): void 3924 3925取消注册P2P连接状态改变事件。 3926 3927**需要权限:** ohos.permission.GET_WIFI_INFO 3928 3929**系统能力:** SystemCapability.Communication.WiFi.P2P 3930 3931**参数:** 3932 3933 | **参数名** | **类型** | **必填** | **说明** | 3934 | -------- | -------- | -------- | -------- | 3935 | type | string | 是 | 固定填"p2pConnectionChange"字符串。 | 3936 | callback | Callback<[WifiP2pLinkedInfo](#wifip2plinkedinfo9)> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 3937 3938**错误码:** 3939 3940以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 3941 3942| **错误码ID** | **错误信息** | 3943 | -------- | -------- | 3944| 2801000 | Operation failed.| 3945 3946**示例:** 3947```ts 3948 import wifiManager from '@ohos.wifiManager'; 3949 3950 let recvP2pConnectionChangeFunc = (result:wifiManager.WifiP2pLinkedInfo) => { 3951 console.info("Receive p2p connection change event: " + result); 3952 } 3953 3954 // Register event 3955 wifiManager.on("p2pConnectionChange", recvP2pConnectionChangeFunc); 3956 3957 // Unregister event 3958 wifiManager.off("p2pConnectionChange", recvP2pConnectionChangeFunc); 3959``` 3960 3961## wifiManager.on('p2pDeviceChange')<sup>9+</sup> 3962 3963on(type: "p2pDeviceChange", callback: Callback<WifiP2pDevice>): void 3964 3965注册P2P设备状态改变事件。 3966 3967**需要权限:** 3968 3969API 9:ohos.permission.GET_WIFI_INFO、ohos.permission.LOCATION 和 ohos.permission.APPROXIMATELY_LOCATION 3970 3971API 10起:ohos.permission.GET_WIFI_INFO 3972 3973**系统能力:** SystemCapability.Communication.WiFi.P2P 3974 3975**参数:** 3976 3977 | **参数名** | **类型** | **必填** | **说明** | 3978 | -------- | -------- | -------- | -------- | 3979 | type | string | 是 | 固定填"p2pDeviceChange"字符串。 | 3980 | callback | Callback<[WifiP2pDevice](#wifip2pdevice9)> | 是 | 状态改变回调函数。 | 3981 3982**错误码:** 3983 3984以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 3985 3986| **错误码ID** | **错误信息** | 3987 | -------- | -------- | 3988| 2801000 | Operation failed.| 3989 3990## wifiManager.off('p2pDeviceChange')<sup>9+</sup> 3991 3992off(type: "p2pDeviceChange", callback?: Callback<WifiP2pDevice>): void 3993 3994取消注册P2P设备状态改变事件。 3995 3996**需要权限:** 3997 3998API 9:ohos.permission.LOCATION 和 ohos.permission.APPROXIMATELY_LOCATION 3999 4000API 10起:无 4001 4002**系统能力:** SystemCapability.Communication.WiFi.P2P 4003 4004**参数:** 4005 4006 | **参数名** | **类型** | **必填** | **说明** | 4007 | -------- | -------- | -------- | -------- | 4008 | type | string | 是 | 固定填"p2pDeviceChange"字符串。 | 4009 | callback | Callback<[WifiP2pDevice](#wifip2pdevice9)> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 4010 4011**错误码:** 4012 4013以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 4014 4015| **错误码ID** | **错误信息** | 4016 | -------- | -------- | 4017| 2801000 | Operation failed.| 4018 4019**示例:** 4020```ts 4021 import wifiManager from '@ohos.wifiManager'; 4022 4023 let recvP2pDeviceChangeFunc = (result:wifiManager.WifiP2pDevice) => { 4024 console.info("Receive p2p device change event: " + result); 4025 } 4026 4027 // Register event 4028 wifiManager.on("p2pDeviceChange", recvP2pDeviceChangeFunc); 4029 4030 // Unregister event 4031 wifiManager.off("p2pDeviceChange", recvP2pDeviceChangeFunc); 4032``` 4033 4034## wifiManager.on('p2pPeerDeviceChange')<sup>9+</sup> 4035 4036on(type: "p2pPeerDeviceChange", callback: Callback<WifiP2pDevice[]>): void 4037 4038注册P2P对端设备状态改变事件。 4039 4040**需要权限:** 4041 4042API 9:ohos.permission.GET_WIFI_INFO、ohos.permission.LOCATION 和 ohos.permission.APPROXIMATELY_LOCATION 4043 4044API 10起:ohos.permission.GET_WIFI_INFO 4045 4046**系统能力:** SystemCapability.Communication.WiFi.P2P 4047 4048**参数:** 4049 4050| **参数名** | **类型** | **必填** | **说明** | 4051| -------- | -------- | -------- | -------- | 4052| type | string | 是 | 固定填"p2pPeerDeviceChange"字符串。 | 4053| callback | Callback<[WifiP2pDevice[]](#wifip2pdevice9)> | 是 | 状态改变回调函数。如果应用申请了ohos.permission.GET_WIFI_PEERS_MAC权限,则返回结果中的deviceAddress为真实设备地址,否则为随机设备地址。 | 4054 4055**错误码:** 4056 4057以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 4058 4059| **错误码ID** | **错误信息** | 4060 | -------- | -------- | 4061| 2801000 | Operation failed.| 4062 4063## wifiManager.off('p2pPeerDeviceChange')<sup>9+</sup> 4064 4065off(type: "p2pPeerDeviceChange", callback?: Callback<WifiP2pDevice[]>): void 4066 4067取消注册P2P对端设备状态改变事件。 4068 4069**需要权限:** 4070 4071API 9:ohos.permission.LOCATION 和 ohos.permission.APPROXIMATELY_LOCATION 4072 4073API 10起:无 4074 4075**系统能力:** SystemCapability.Communication.WiFi.P2P 4076 4077**参数:** 4078 4079| **参数名** | **类型** | **必填** | **说明** | 4080| -------- | -------- | -------- | -------- | 4081| type | string | 是 | 固定填"p2pPeerDeviceChange"字符串。 | 4082| callback | Callback<[WifiP2pDevice[]](#wifip2pdevice9)> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。如果应用申请了ohos.permission.GET_WIFI_PEERS_MAC权限,则返回结果中的deviceAddress为真实设备地址,否则为随机设备地址。 | 4083 4084**错误码:** 4085 4086以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 4087 4088| **错误码ID** | **错误信息** | 4089 | -------- | -------- | 4090| 2801000 | Operation failed.| 4091 4092**示例:** 4093```ts 4094 import wifiManager from '@ohos.wifiManager'; 4095 4096 let recvP2pPeerDeviceChangeFunc = (result:wifiManager.WifiP2pDevice[]) => { 4097 console.info("Receive p2p peer device change event: " + result); 4098 } 4099 4100 // Register event 4101 wifiManager.on("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc); 4102 4103 // Unregister event 4104 wifiManager.off("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc); 4105``` 4106 4107## wifiManager.on('p2pPersistentGroupChange')<sup>9+</sup> 4108 4109on(type: "p2pPersistentGroupChange", callback: Callback<void>): void 4110 4111注册P2P永久组状态改变事件。 4112 4113**需要权限:** ohos.permission.GET_WIFI_INFO 4114 4115**系统能力:** SystemCapability.Communication.WiFi.P2P 4116 4117**参数:** 4118 4119 | **参数名** | **类型** | **必填** | **说明** | 4120 | -------- | -------- | -------- | -------- | 4121 | type | string | 是 | 固定填"p2pPersistentGroupChange"字符串。 | 4122 | callback | Callback<void> | 是 | 状态改变回调函数。 | 4123 4124**错误码:** 4125 4126以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 4127 4128| **错误码ID** | **错误信息** | 4129 | -------- | -------- | 4130| 2801000 | Operation failed.| 4131 4132## wifiManager.off('p2pPersistentGroupChange')<sup>9+</sup> 4133 4134off(type: "p2pPersistentGroupChange", callback?: Callback<void>): void 4135 4136取消注册P2P永久组状态改变事件。 4137 4138**需要权限:** ohos.permission.GET_WIFI_INFO 4139 4140**系统能力:** SystemCapability.Communication.WiFi.P2P 4141 4142**参数:** 4143 4144| **参数名** | **类型** | **必填** | **说明** | 4145| -------- | -------- | -------- | -------- | 4146| type | string | 是 | 固定填"p2pPersistentGroupChange"字符串。 | 4147| callback | Callback<void> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 4148 4149**错误码:** 4150 4151以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 4152 4153| **错误码ID** | **错误信息** | 4154 | -------- | -------- | 4155| 2801000 | Operation failed.| 4156 4157**示例:** 4158```ts 4159 import wifiManager from '@ohos.wifiManager'; 4160 4161 let recvP2pPersistentGroupChangeFunc = (result:void) => { 4162 console.info("Receive p2p persistent group change event: " + result); 4163 } 4164 4165 // Register event 4166 wifiManager.on("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc); 4167 4168 // Unregister event 4169 wifiManager.off("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc); 4170``` 4171 4172## wifiManager.on('p2pDiscoveryChange')<sup>9+</sup> 4173 4174on(type: "p2pDiscoveryChange", callback: Callback<number>): void 4175 4176注册发现设备状态改变事件。 4177 4178**需要权限:** ohos.permission.GET_WIFI_INFO 4179 4180**系统能力:** SystemCapability.Communication.WiFi.P2P 4181 4182**参数:** 4183 4184 | **参数名** | **类型** | **必填** | **说明** | 4185 | -------- | -------- | -------- | -------- | 4186 | type | string | 是 | 固定填"p2pDiscoveryChange"字符串。 | 4187 | callback | Callback<number> | 是 | 状态改变回调函数。 | 4188 4189**发现设备状态改变事件的枚举:** 4190 4191| **枚举值** | **说明** | 4192| -------- | -------- | 4193| 0 | 初始状态。 | 4194| 1 | 发现成功。 | 4195 4196**错误码:** 4197 4198以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 4199 4200| **错误码ID** | **错误信息** | 4201 | -------- | -------- | 4202| 2801000 | Operation failed.| 4203 4204## wifiManager.off('p2pDiscoveryChange')<sup>9+</sup> 4205 4206off(type: "p2pDiscoveryChange", callback?: Callback<number>): void 4207 4208取消注册发现设备状态改变事件。 4209 4210**需要权限:** ohos.permission.GET_WIFI_INFO 4211 4212**系统能力:** SystemCapability.Communication.WiFi.P2P 4213 4214**参数:** 4215 4216 | **参数名** | **类型** | **必填** | **说明** | 4217 | -------- | -------- | -------- | -------- | 4218 | type | string | 是 | 固定填"p2pDiscoveryChange"字符串。 | 4219 | callback | Callback<number> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 4220 4221**错误码:** 4222 4223以下错误码的详细介绍请参见[WIFI错误码](../errorcodes/errorcode-wifi.md)。 4224 4225| **错误码ID** | **错误信息** | 4226 | -------- | -------- | 4227| 2801000 | Operation failed.| 4228 4229**示例:** 4230```ts 4231 import wifiManager from '@ohos.wifiManager'; 4232 4233 let recvP2pDiscoveryChangeFunc = (result:number) => { 4234 console.info("Receive p2p discovery change event: " + result); 4235 } 4236 4237 // Register event 4238 wifiManager.on("p2pDiscoveryChange", recvP2pDiscoveryChangeFunc); 4239 4240 // Unregister event 4241 wifiManager.off("p2pDiscoveryChange", recvP2pDiscoveryChangeFunc); 4242```