1# @ohos.wifi (WLAN) 2 3The **WLAN** module provides basic wireless local area network (WLAN) functions, peer-to-peer (P2P) functions, and WLAN message notification services. It allows applications to communicate with other devices over WLAN. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8 9 10## Modules to Import 11 12```ts 13import wifi from '@ohos.wifi'; 14``` 15 16## wifi.enableWifi 17 18enableWifi(): boolean 19 20Enables WLAN. 21 22**System API**: This is a system API. 23 24**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications) 25 26**System capability**: SystemCapability.Communication.WiFi.STA 27 28**Return value** 29 30 | **Type**| **Description**| 31 | -------- | -------- | 32 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 33 34**Example** 35 36```ts 37import wifi from '@ohos.wifi'; 38 39try { 40 wifi.enableWifi(); 41}catch(error){ 42 console.error("failed:" + JSON.stringify(error)); 43} 44``` 45 46## wifi.disableWifi 47 48disableWifi(): boolean 49 50Disables WLAN. 51 52**System API**: This is a system API. 53 54**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications) 55 56**System capability**: SystemCapability.Communication.WiFi.STA 57 58**Return value** 59 60 | **Type**| **Description**| 61 | -------- | -------- | 62 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 63 64**Example** 65 66```ts 67import wifi from '@ohos.wifi'; 68 69try { 70 wifi.disableWifi(); 71}catch(error){ 72 console.error("failed:" + JSON.stringify(error)); 73} 74 75``` 76 77## wifi.isWifiActive 78 79isWifiActive(): boolean 80 81Checks whether WLAN is enabled. 82 83**Required permissions**: ohos.permission.GET_WIFI_INFO 84 85**System capability**: SystemCapability.Communication.WiFi.STA 86 87**Return value** 88 89 | **Type**| **Description**| 90 | -------- | -------- | 91 | boolean | Returns **true** if WLAN is enabled; returns **false** otherwise.| 92 93**Example** 94 95```ts 96import wifi from '@ohos.wifi'; 97 98try { 99 let isWifiActive = wifi.isWifiActive(); 100 console.info("isWifiActive:" + isWifiActive); 101}catch(error){ 102 console.error("failed:" + JSON.stringify(error)); 103} 104``` 105 106## wifi.scan 107 108scan(): boolean 109 110Starts a scan for WLAN. 111 112**Required permissions**: **ohos.permission.SET_WIFI_INFO** and **ohos.permission.LOCATION** 113 114**System capability**: SystemCapability.Communication.WiFi.STA 115 116**Return value** 117 118 | **Type**| **Description**| 119 | -------- | -------- | 120 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 121 122**Example** 123 124```ts 125import wifi from '@ohos.wifi'; 126 127try { 128 wifi.scan(); 129}catch(error){ 130 console.error("failed:" + JSON.stringify(error)); 131} 132``` 133 134## wifi.getScanInfos 135 136getScanInfos(): Promise<Array<WifiScanInfo>> 137 138Obtains the scan result. This API uses a promise to return the result. 139 140**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.GET_WIFI_PEERS_MAC (or ohos.permission.LOCATION) 141 142**System capability**: SystemCapability.Communication.WiFi.STA 143 144**Return value** 145 146 | **Type**| **Description**| 147 | -------- | -------- | 148| Promise< Array<[WifiScanInfo](#wifiscaninfo)> > | Promise used to return the detected hotspots.| 149 150 151## wifi.getScanInfos 152 153getScanInfos(callback: AsyncCallback<Array<WifiScanInfo>>): void 154 155Obtains the scan result. This API uses an asynchronous callback to return the result. 156 157**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.GET_WIFI_PEERS_MAC (or ohos.permission.LOCATION) 158 159**System capability**: SystemCapability.Communication.WiFi.STA 160 161**Parameters** 162 163 | **Name**| **Type**| **Mandatory**| **Description**| 164 | -------- | -------- | -------- | -------- | 165 | callback | AsyncCallback< Array<[WifiScanInfo](#wifiscaninfo)>> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is the detected hotspots. Otherwise, **err** is a non-zero value and **data** is empty.| 166 167**Example** 168 169```ts 170import wifi from '@ohos.wifi'; 171 172wifi.getScanInfos((err, result) => { 173 if (err) { 174 console.error("get scan info error"); 175 return; 176 } 177 178 let len = result.length; 179 console.log("wifi received scan info: " + len); 180 for (let i = 0; i < len; ++i) { 181 console.info("ssid: " + result[i].ssid); 182 console.info("bssid: " + result[i].bssid); 183 console.info("capabilities: " + result[i].capabilities); 184 console.info("securityType: " + result[i].securityType); 185 console.info("rssi: " + result[i].rssi); 186 console.info("band: " + result[i].band); 187 console.info("frequency: " + result[i].frequency); 188 console.info("channelWidth: " + result[i].channelWidth); 189 console.info("timestamp: " + result[i].timestamp); 190 } 191}); 192 193wifi.getScanInfos().then(result => { 194 let len = result.length; 195 console.log("wifi received scan info: " + len); 196 for (let i = 0; i < len; ++i) { 197 console.info("ssid: " + result[i].ssid); 198 console.info("bssid: " + result[i].bssid); 199 console.info("capabilities: " + result[i].capabilities); 200 console.info("securityType: " + result[i].securityType); 201 console.info("rssi: " + result[i].rssi); 202 console.info("band: " + result[i].band); 203 console.info("frequency: " + result[i].frequency); 204 console.info("channelWidth: " + result[i].channelWidth); 205 console.info("timestamp: " + result[i].timestamp); 206 } 207}); 208``` 209 210 211## WifiScanInfo 212 213Represents WLAN hotspot information. 214 215**System capability**: SystemCapability.Communication.WiFi.STA 216 217 218| **Name**| **Type**| **Readable**| **Writable**| **Description**| 219| -------- | -------- | -------- | -------- | -------- | 220| ssid | string | Yes| No| Service set identifier (SSID) of the hotspot, in UTF-8 format.| 221| bssid | string | Yes| No| Basic service set identifier (BSSID) of the hotspot.| 222| capabilities | string | Yes| No| Hotspot capabilities.| 223| securityType | [WifiSecurityType](#wifisecuritytype) | Yes| No| WLAN security type.| 224| rssi | number | Yes| No| Received signal strength indicator (RSSI) of the hotspot, in dBm.| 225| band | number | Yes| No| Frequency band of the WLAN access point (AP).| 226| frequency | number | Yes| No| Frequency of the WLAN AP.| 227| channelWidth | number | Yes| No| Channel width of the WLAN AP.| 228| timestamp | number | Yes| No| Timestamp.| 229 230 231## WifiSecurityType 232 233Enumerates the WLAN security types. 234 235**System capability**: SystemCapability.Communication.WiFi.Core 236 237 238| **Name**| **Value**| **Description**| 239| -------- | -------- | -------- | 240| WIFI_SEC_TYPE_INVALID | 0 | Invalid security type.| 241| WIFI_SEC_TYPE_OPEN | 1 | Open security type.| 242| WIFI_SEC_TYPE_WEP | 2 | Wired Equivalent Privacy (WEP).| 243| WIFI_SEC_TYPE_PSK | 3 | Pre-shared key (PSK).| 244| WIFI_SEC_TYPE_SAE | 4 | Simultaneous Authentication of Equals (SAE).| 245 246 247## wifi.addDeviceConfig 248 249addDeviceConfig(config: WifiDeviceConfig): Promise<number> 250 251Adds network configuration. This API uses a promise to return the result. 252 253**System API**: This is a system API. 254 255**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.SET_WIFI_CONFIG 256 257**System capability**: SystemCapability.Communication.WiFi.STA 258 259**Parameters** 260 261 | **Name**| **Type**| **Mandatory**| **Description**| 262 | -------- | -------- | -------- | -------- | 263 | config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration to add.| 264 265**Return value** 266 267 | **Type**| **Description**| 268 | -------- | -------- | 269| Promise<number> | Promise used to return the WLAN configuration ID. If **-1** is returned, the network configuration fails to be added.| 270 271 **Example** 272 273```ts 274import wifi from '@ohos.wifi'; 275 276try { 277 let config:wifi.WifiDeviceConfig = { 278 ssid : "****", 279 preSharedKey : "****", 280 securityType : 0 281 } 282 wifi.addDeviceConfig(config).then(result => { 283 console.info("result:" + JSON.stringify(result)); 284 }); 285}catch(error){ 286 console.error("failed:" + JSON.stringify(error)); 287} 288``` 289 290## WifiDeviceConfig 291 292Represents the WLAN configuration. 293 294**System capability**: SystemCapability.Communication.WiFi.STA 295 296 297| **Name**| **Type**| **Readable**| **Writable**| **Description**| 298| -------- | -------- | -------- | -------- | -------- | 299| ssid | string | Yes| No| SSID of the hotspot, in UTF-8 format.| 300| bssid | string | Yes| No| BSSID of the hotspot.| 301| preSharedKey | string | Yes| No| PSK of the hotspot.| 302| isHiddenSsid | boolean | Yes| No| Whether the network is hidden.| 303| securityType | [WifiSecurityType](#wifisecuritytype) | Yes| No| Security type.| 304| creatorUid | number | Yes| No| ID of the creator.<br>**System API**: This is a system API.| 305| disableReason | number | Yes| No| Reason for disabling WLAN.<br>**System API**: This is a system API.| 306| netId | number | Yes| No| Network ID.<br>**System API**: This is a system API.| 307| randomMacType | number | Yes| No| Random MAC type.<br>**System API**: This is a system API.| 308| randomMacAddr | string | Yes| No| Random MAC address.<br>**System API**: This is a system API.| 309| ipType | [IpType](#iptype7) | Yes| No| IP address type.<br>**System API**: This is a system API.| 310| staticIp | [IpConfig](#ipconfig7) | Yes| No| Static IP address configuration.<br>**System API**: This is a system API.| 311 312 313## IpType<sup>7+</sup> 314 315Enumerates the IP address types. 316 317**System API**: This is a system API. 318 319**System capability**: SystemCapability.Communication.WiFi.STA 320 321 322| Name| Value| Description| 323| -------- | -------- | -------- | 324| STATIC | 0 | Static IP address.| 325| DHCP | 1 | IP address allocated by DHCP.| 326| UNKNOWN | 2 | Not specified.| 327 328 329## IpConfig<sup>7+</sup> 330 331Represents IP configuration information. 332 333**System API**: This is a system API. 334 335**System capability**: SystemCapability.Communication.WiFi.STA 336 337| **Name**| **Type**| **Readable**| **Writable**| **Description**| 338| -------- | -------- | -------- | -------- | -------- | 339| ipAddress | number | Yes| No| IP address.| 340| gateway | number | Yes| No| Gateway.| 341| dnsServers | number[] | Yes| No| Domain name server (DNS) information.| 342| domains | Array<string> | Yes| No| Domain information.| 343 344 345## wifi.addDeviceConfig 346 347addDeviceConfig(config: WifiDeviceConfig, callback: AsyncCallback<number>): void 348 349Adds network configuration. This API uses an asynchronous callback to return the result. 350 351**System API**: This is a system API. 352 353**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.SET_WIFI_CONFIG 354 355**System capability**: SystemCapability.Communication.WiFi.STA 356 357**Parameters** 358 359 | **Name**| **Type**| **Mandatory**| **Description**| 360 | -------- | -------- | -------- | -------- | 361 | config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration to add.| 362 | callback | AsyncCallback<number> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is the network configuration ID. If **data** is **-1**, the operation has failed. If **err** is not **0**, an error has occurred.| 363 364**Example** 365 366```ts 367import wifi from '@ohos.wifi'; 368 369try { 370 let config:wifi.WifiDeviceConfig = { 371 ssid : "****", 372 preSharedKey : "****", 373 securityType : 0 374 } 375 wifi.addDeviceConfig(config,(error,result) => { 376 console.info("result:" + JSON.stringify(result)); 377 }); 378}catch(error){ 379 console.error("failed:" + JSON.stringify(error)); 380} 381``` 382## wifi.addUntrustedConfig<sup>7+</sup> 383 384addUntrustedConfig(config: WifiDeviceConfig): Promise<boolean> 385 386Adds the configuration of an untrusted network. This API uses a promise to return the result. 387 388**Required permissions**: ohos.permission.SET_WIFI_INFO 389 390**System capability**: SystemCapability.Communication.WiFi.STA 391 392**Parameters** 393 394 | **Name**| **Type**| **Mandatory**| **Description**| 395 | -------- | -------- | -------- | -------- | 396 | config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration to add.| 397 398**Return value** 399 400 | **Type**| **Description**| 401 | -------- | -------- | 402| Promise<boolean> | Promise used to return the result. If the operation is successful, **true** is returned; otherwise, **false** is returned.| 403 404**Example** 405```ts 406import wifi from '@ohos.wifi'; 407 408try { 409 let config:wifi.WifiDeviceConfig = { 410 ssid : "****", 411 preSharedKey : "****", 412 securityType : 0 413 } 414 wifi.addUntrustedConfig(config).then(result => { 415 console.info("result:" + JSON.stringify(result)); 416 }); 417}catch(error){ 418 console.error("failed:" + JSON.stringify(error)); 419} 420``` 421 422## wifi.addUntrustedConfig<sup>7+</sup> 423 424addUntrustedConfig(config: WifiDeviceConfig, callback: AsyncCallback<boolean>): void 425 426Adds the configuration of an untrusted network. This API uses an asynchronous callback to return the result. 427 428**Required permissions**: ohos.permission.SET_WIFI_INFO 429 430**System capability**: SystemCapability.Communication.WiFi.STA 431 432**Parameters** 433 434 | **Name**| **Type**| **Mandatory**| **Description**| 435 | -------- | -------- | -------- | -------- | 436 | config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration to add.| 437 | callback | AsyncCallback<boolean> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is **true**. If the operation fails, **data** is **false**. If **err** is not **0**, an error has occurred.| 438 439**Example** 440```ts 441import wifi from '@ohos.wifi'; 442 443try { 444 let config:wifi.WifiDeviceConfig = { 445 ssid : "****", 446 preSharedKey : "****", 447 securityType : 0 448 } 449 wifi.addUntrustedConfig(config,(error,result) => { 450 console.info("result:" + JSON.stringify(result)); 451 }); 452}catch(error){ 453 console.error("failed:" + JSON.stringify(error)); 454} 455``` 456 457## wifi.removeUntrustedConfig<sup>7+</sup> 458 459removeUntrustedConfig(config: WifiDeviceConfig): Promise<boolean> 460 461Removes the configuration of an untrusted network. This API uses a promise to return the result. 462 463**Required permissions**: ohos.permission.SET_WIFI_INFO 464 465**System capability**: SystemCapability.Communication.WiFi.STA 466 467**Parameters** 468 469 | **Name**| **Type**| **Mandatory**| **Description**| 470 | -------- | -------- | -------- | -------- | 471| config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration to remove.| 472 473**Return value** 474 475 | **Type**| **Description**| 476 | -------- | -------- | 477| Promise<boolean> | Promise used to return the result. If the operation is successful, **true** is returned; otherwise, **false** is returned.| 478 479**Example** 480 481```ts 482import wifi from '@ohos.wifi'; 483 484try { 485 let networkId = 0; 486 wifi.removeUntrustedConfig(networkId).then(result => { 487 console.info("result:" + JSON.stringify(result)); 488 }); 489}catch(error){ 490 console.error("failed:" + JSON.stringify(error)); 491} 492``` 493 494 495## wifi.removeUntrustedConfig<sup>7+</sup> 496 497removeUntrustedConfig(config: WifiDeviceConfig, callback: AsyncCallback<boolean>): void 498 499Removes the configuration of an untrusted network. This API uses an asynchronous callback to return the result. 500 501**Required permissions**: ohos.permission.SET_WIFI_INFO 502 503**System capability**: SystemCapability.Communication.WiFi.STA 504 505**Parameters** 506 507 | **Name**| **Type**| **Mandatory**| **Description**| 508 | -------- | -------- | -------- | -------- | 509| config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration to remove.| 510 | callback | AsyncCallback<boolean> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is **true**. If the operation fails, **data** is **false**. If **err** is not **0**, an error has occurred.| 511 512**Example** 513```ts 514import wifi from '@ohos.wifi'; 515 516try { 517 let networkId = 0; 518 wifi.removeUntrustedConfig(networkId,(error,result) => { 519 console.info("result:" + JSON.stringify(result)); 520 }); 521}catch(error){ 522 console.error("failed:" + JSON.stringify(error)); 523} 524``` 525 526## wifi.connectToNetwork 527 528connectToNetwork(networkId: number): boolean 529 530Connects to the specified network. 531 532**System API**: This is a system API. 533 534**Required permissions**: ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications) 535 536**System capability**: SystemCapability.Communication.WiFi.STA 537 538**Parameters** 539 540 | **Name**| **Type**| **Mandatory**| **Description**| 541 | -------- | -------- | -------- | -------- | 542 | networkId | number | Yes| Network configuration ID.| 543 544**Return value** 545 546 | **Type**| **Description**| 547 | -------- | -------- | 548 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 549 550**Example** 551 552```ts 553import wifi from '@ohos.wifi'; 554 555try { 556 let networkId = 0; 557 wifi.connectToNetwork(networkId); 558}catch(error){ 559 console.error("failed:" + JSON.stringify(error)); 560} 561``` 562 563## wifi.connectToDevice 564 565connectToDevice(config: WifiDeviceConfig): boolean 566 567Connects to the specified network. 568 569**System API**: This is a system API. 570 571**Required permissions**: ohos.permission.SET_WIFI_INFO, ohos.permission.SET_WIFI_CONFIG, and ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications) 572 573**System capability**: 574 SystemCapability.Communication.WiFi.STA 575 576**Parameters** 577 578 | **Name**| **Type**| **Mandatory**| **Description**| 579 | -------- | -------- | -------- | -------- | 580| config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration.| 581 582**Return value** 583 584 | **Type**| **Description**| 585 | -------- | -------- | 586 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 587 588**Example** 589```ts 590import wifi from '@ohos.wifi'; 591 592try { 593 let config:wifi.WifiDeviceConfig = { 594 ssid : "****", 595 preSharedKey : "****", 596 securityType : 3 597 } 598 wifi.connectToDevice(config); 599 600}catch(error){ 601 console.error("failed:" + JSON.stringify(error)); 602} 603``` 604 605## wifi.disconnect 606 607disconnect(): boolean 608 609Disconnects the network. 610 611**System API**: This is a system API. 612 613**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications) 614 615**System capability**: 616 SystemCapability.Communication.WiFi.STA 617 618**Return value** 619 620 | **Type**| **Description**| 621 | -------- | -------- | 622 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 623 624**Example** 625```ts 626import wifi from '@ohos.wifi'; 627 628try { 629 wifi.disconnect(); 630}catch(error){ 631 console.error("failed:" + JSON.stringify(error)); 632} 633``` 634 635## wifi.getSignalLevel 636 637getSignalLevel(rssi: number, band: number): number 638 639Obtains the WLAN signal level. 640 641**Required permissions**: ohos.permission.GET_WIFI_INFO 642 643**System capability**: SystemCapability.Communication.WiFi.STA 644 645**Parameters** 646 647 | **Name**| **Type**| **Mandatory**| **Description**| 648 | -------- | -------- | -------- | -------- | 649 | rssi | number | Yes| RSSI of the hotspot, in dBm.| 650 | band | number | Yes| Frequency band of the WLAN AP.| 651 652**Return value** 653 654 | **Type**| **Description**| 655 | -------- | -------- | 656 | number | Signal level obtained. The value range is [0, 4].| 657 658**Example** 659```ts 660import wifi from '@ohos.wifi'; 661 662try { 663 let rssi = 0; 664 let band = 0; 665 let level = wifi.getSignalLevel(rssi,band); 666 console.info("level:" + JSON.stringify(level)); 667}catch(error){ 668 console.error("failed:" + JSON.stringify(error)); 669} 670 671``` 672 673## wifi.getLinkedInfo 674 675getLinkedInfo(): Promise<WifiLinkedInfo> 676 677Obtains WLAN connection information. This API uses a promise to return the result. 678 679**Required permissions**: ohos.permission.GET_WIFI_INFO 680 681**System capability**: SystemCapability.Communication.WiFi.STA 682 683**Return value** 684 685 | Type| Description| 686 | -------- | -------- | 687 | Promise<[WifiLinkedInfo](#wifilinkedinfo)> | Promise used to return the WLAN connection information.| 688 689 690## wifi.getLinkedInfo 691 692getLinkedInfo(callback: AsyncCallback<WifiLinkedInfo>): void 693 694Obtains WLAN connection information. This API uses an asynchronous callback to return the result. 695 696**Required permissions**: ohos.permission.GET_WIFI_INFO 697 698**System capability**: SystemCapability.Communication.WiFi.STA 699 700**Parameters** 701 702 | Name| Type| Mandatory| Description| 703 | -------- | -------- | -------- | -------- | 704 | callback | AsyncCallback<[WifiLinkedInfo](#wifilinkedinfo)> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is the WLAN connection information obtained. If **err** is not **0**, an error has occurred.| 705 706**Example** 707```ts 708import wifi from '@ohos.wifi'; 709 710wifi.getLinkedInfo((err, data) => { 711 if (err) { 712 console.error("get linked info error"); 713 return; 714 } 715 console.info("get wifi linked info: " + JSON.stringify(data)); 716}); 717 718wifi.getLinkedInfo().then(data => { 719 console.info("get wifi linked info: " + JSON.stringify(data)); 720}).catch((error:number) => { 721 console.info("get linked info error"); 722}); 723``` 724 725 726## WifiLinkedInfo 727 728Represents the WLAN connection information. 729 730**System capability**: SystemCapability.Communication.WiFi.STA 731 732| Name| Type| Readable| Writable| Description| 733| -------- | -------- | -------- | -------- | -------- | 734| ssid | string | Yes| No| SSID of the hotspot, in UTF-8 format.| 735| bssid | string | Yes| No| BSSID of the hotspot.| 736| networkId | number | Yes| No| Network configuration ID.<br>**System API**: This is a system API.| 737| rssi | number | Yes| No| RSSI of the hotspot, in dBm.| 738| band | number | Yes| No| Frequency band of the WLAN AP.| 739| linkSpeed | number | Yes| No| Speed of the WLAN AP.| 740| frequency | number | Yes| No| Frequency of the WLAN AP.| 741| isHidden | boolean | Yes| No| Whether to hide the WLAN AP.| 742| isRestricted | boolean | Yes| No| Whether to restrict data volume at the WLAN AP.| 743| chload | number | Yes| No| Channel load. A larger value indicates a higher load.<br>**System API**: This is a system API.| 744| snr | number | Yes| No| Signal-to-noise ratio (SNR).<br>**System API**: This is a system API.| 745| macAddress | string | Yes| No| MAC address of the device.| 746| ipAddress | number | Yes| No| IP address of the device that sets up the WLAN connection.| 747| suppState | [SuppState](#suppstate) | Yes| No| Supplicant state.<br>**System API**: This is a system API.| 748| connState | [ConnState](#connstate) | Yes| No| WLAN connection state.| 749 750 751## ConnState 752 753Enumerates the WLAN connection states. 754 755**System capability**: SystemCapability.Communication.WiFi.STA 756 757| Name| Value| Description| 758| -------- | -------- | -------- | 759| SCANNING | 0 | The device is scanning for available APs.| 760| CONNECTING | 1 | A WLAN connection is being established.| 761| AUTHENTICATING | 2 | An authentication is being performed for a WLAN connection.| 762| OBTAINING_IPADDR | 3 | The IP address of the WLAN connection is being acquired.| 763| CONNECTED | 4 | A WLAN connection is established.| 764| DISCONNECTING | 5 | The WLAN connection is being disconnected.| 765| DISCONNECTED | 6 | The WLAN connection is disconnected.| 766| UNKNOWN | 7 | Failed to set up the WLAN connection.| 767 768 769## SuppState 770 771Enumerates the supplicant states. 772 773**System API**: This is a system API. 774 775**System capability**: SystemCapability.Communication.WiFi.STA 776 777| Name| Value| Description| 778| -------- | -------- | -------- | 779| DISCONNECTED | 0 | The supplicant is disconnected from the AP.| 780| INTERFACE_DISABLED | 1 | The network interface is disabled.| 781| INACTIVE | 2 | The supplicant is inactive.| 782| SCANNING | 3 | The supplicant is scanning for a WLAN connection.| 783| AUTHENTICATING | 4 | The supplicant is being authenticated.| 784| ASSOCIATING | 5 | The supplicant is being associated with an AP.| 785| ASSOCIATED | 6 | The supplicant is associated with an AP.| 786| FOUR_WAY_HANDSHAKE | 7 | A four-way handshake is being performed for the supplicant.| 787| GROUP_HANDSHAKE | 8 | A group handshake is being performed for the supplicant.| 788| COMPLETED | 9 | The authentication is complete.| 789| UNINITIALIZED | 10 | The supplicant failed to set up the connection.| 790| INVALID | 11 | Invalid value.| 791 792 793## wifi.isConnected<sup>7+</sup> 794 795isConnected(): boolean 796 797Checks whether the WLAN is connected. 798 799**Required permissions**: ohos.permission.GET_WIFI_INFO 800 801**System capability**: SystemCapability.Communication.WiFi.STA 802 803**Return value** 804 805 | **Type**| **Description**| 806 | -------- | -------- | 807 | boolean | Returns **true** if the WLAN is connected; returns **false** otherwise.| 808 809 810## wifi.getSupportedFeatures<sup>7+</sup> 811 812getSupportedFeatures(): number 813 814Obtains the features supported by this device. 815 816**System API**: This is a system API. 817 818**Required permissions**: ohos.permission.GET_WIFI_INFO 819 820**System capability**: SystemCapability.Communication.WiFi.Core 821 822**Return value** 823 824 | **Type**| **Description**| 825 | -------- | -------- | 826 | number | Feature value. | 827 828**Feature IDs** 829 830| Value| Description| 831| -------- | -------- | 832| 0x0001 | WLAN infrastructure mode| 833| 0x0002 | 5 GHz feature| 834| 0x0004 | Generic Advertisement Service (GAS)/Access Network Query Protocol (ANQP) feature| 835| 0x0008 | Wi-Fi Direct| 836| 0x0010 | SoftAP| 837| 0x0040 | Wi-Fi AWare| 838| 0x8000 | WLAN AP/STA concurrency| 839| 0x8000000 | WPA3 Personal (WPA-3 SAE)| 840| 0x10000000 | WPA3-Enterprise Suite B | 841| 0x20000000 | Enhanced open feature| 842 843 844## wifi.isFeatureSupported<sup>7+</sup> 845 846isFeatureSupported(featureId: number): boolean 847 848Checks whether the device supports the specified WLAN feature. 849 850**Required permissions**: ohos.permission.GET_WIFI_INFO 851 852**System capability**: SystemCapability.Communication.WiFi.Core 853 854**Parameters** 855 856 857 | **Name**| **Type**| Mandatory| **Description**| 858 | -------- | -------- | -------- | -------- | 859 | featureId | number | Yes| Feature ID.| 860 861**Return value** 862 863 | **Type**| **Description**| 864 | -------- | -------- | 865 | boolean | Returns **true** if the feature is supported; returns **false** otherwise.| 866 867**Example** 868```ts 869import wifi from '@ohos.wifi'; 870 871try { 872 let featureId = 0; 873 let ret = wifi.isFeatureSupported(featureId); 874 console.info("isFeatureSupported:" + ret); 875}catch(error){ 876 console.error("failed:" + JSON.stringify(error)); 877} 878 879``` 880 881## wifi.getDeviceMacAddress<sup>7+</sup> 882 883getDeviceMacAddress(): string[] 884 885Obtains the device MAC address. 886 887**System API**: This is a system API. 888 889**Required permissions**: ohos.permission.GET_WIFI_LOCAL_MAC and ohos.permission.GET_WIFI_INFO (available only to system applications) 890 891**System capability**: SystemCapability.Communication.WiFi.STA 892 893**Return value** 894 895 | **Type**| **Description**| 896 | -------- | -------- | 897 | string[] | MAC address obtained.| 898 899**Example** 900```ts 901import wifi from '@ohos.wifi'; 902 903try { 904 let ret = wifi.getDeviceMacAddress(); 905 console.info("deviceMacAddress:" + JSON.stringify(ret)); 906}catch(error){ 907 console.error("failed:" + JSON.stringify(error)); 908} 909 910``` 911 912## wifi.getIpInfo<sup>7+</sup> 913 914getIpInfo(): IpInfo 915 916Obtains IP information. 917 918**Required permissions**: ohos.permission.GET_WIFI_INFO 919 920**System capability**: SystemCapability.Communication.WiFi.STA 921 922**Return value** 923 924 | **Type**| **Description**| 925 | -------- | -------- | 926 | [IpInfo](#ipinfo7) | IP information obtained.| 927 928**Example** 929```ts 930import wifi from '@ohos.wifi'; 931 932try { 933 let info = wifi.getIpInfo(); 934 console.info("info:" + JSON.stringify(info)); 935}catch(error){ 936 console.error("failed:" + JSON.stringify(error)); 937} 938``` 939 940## IpInfo<sup>7+</sup> 941 942Represents IP information. 943 944**System capability**: SystemCapability.Communication.WiFi.STA 945 946| **Name**| **Type**| **Readable**| **Writable**| **Description**| 947| -------- | -------- | -------- | -------- | -------- | 948| ipAddress | number | Yes| No| IP address.| 949| gateway | number | Yes| No| Gateway.| 950| netmask | number | Yes| No| Subnet mask.| 951| primaryDns | number | Yes| No| IP address of the preferred DNS server.| 952| secondDns | number | Yes| No| IP address of the alternate DNS server.| 953| serverIp | number | Yes| No| IP address of the DHCP server.| 954| leaseDuration | number | Yes| No| Lease duration of the IP address.| 955 956 957## wifi.getCountryCode<sup>7+</sup> 958 959getCountryCode(): string 960 961Obtains the country code. 962 963**Required permissions**: ohos.permission.GET_WIFI_INFO 964 965**System capability**: SystemCapability.Communication.WiFi.Core 966 967**Return value** 968 969 | **Type**| **Description**| 970 | -------- | -------- | 971 | string | Country code obtained.| 972 973**Example** 974```ts 975import wifi from '@ohos.wifi'; 976 977try { 978 let code = wifi.getCountryCode(); 979 console.info("code:" + code); 980}catch(error){ 981 console.error("failed:" + JSON.stringify(error)); 982} 983``` 984 985## wifi.reassociate<sup>7+</sup> 986 987reassociate(): boolean 988 989Re-associates with the network. 990 991**System API**: This is a system API. 992 993**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications) 994 995**System capability**: SystemCapability.Communication.WiFi.STA 996 997**Return value** 998 999 | **Type**| **Description**| 1000 | -------- | -------- | 1001 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1002 1003**Example** 1004```ts 1005import wifi from '@ohos.wifi'; 1006 1007try { 1008 wifi.reassociate(); 1009}catch(error){ 1010 console.error("failed:" + JSON.stringify(error)); 1011} 1012``` 1013 1014## wifi.reconnect<sup>7+</sup> 1015 1016reconnect(): boolean 1017 1018Reconnects to the network. 1019 1020**System API**: This is a system API. 1021 1022**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications) 1023 1024**System capability**: SystemCapability.Communication.WiFi.STA 1025 1026**Return value** 1027 1028 | **Type**| **Description**| 1029 | -------- | -------- | 1030 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1031 1032**Example** 1033```ts 1034import wifi from '@ohos.wifi'; 1035 1036try { 1037 wifi.reconnect(); 1038}catch(error){ 1039 console.error("failed:" + JSON.stringify(error)); 1040} 1041``` 1042 1043## wifi.getDeviceConfigs<sup>7+</sup> 1044 1045getDeviceConfigs(): Array<[WifiDeviceConfig](#wifideviceconfig)> 1046 1047Obtains network configuration. 1048 1049**System API**: This is a system API. 1050 1051**Required permissions**: ohos.permission.GET_WIFI_INFO, ohos.permission.LOCATION, and ohos.permission.GET_WIFI_CONFIG 1052 1053**System capability**: SystemCapability.Communication.WiFi.STA 1054 1055**Return value** 1056 1057 | **Type**| **Description**| 1058 | -------- | -------- | 1059 | Array<[WifiDeviceConfig](#wifideviceconfig)> | Array of network configuration obtained.| 1060 1061**Example** 1062```ts 1063import wifi from '@ohos.wifi'; 1064 1065try { 1066 let configs = wifi.getDeviceConfigs(); 1067 console.info("configs:" + JSON.stringify(configs)); 1068}catch(error){ 1069 console.error("failed:" + JSON.stringify(error)); 1070} 1071``` 1072 1073## wifi.updateNetwork<sup>7+</sup> 1074 1075updateNetwork(config: WifiDeviceConfig): number 1076 1077Updates network configuration. 1078 1079**System API**: This is a system API. 1080 1081**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.SET_WIFI_CONFIG 1082 1083**System capability**: SystemCapability.Communication.WiFi.STA 1084 1085**Parameters** 1086 1087 | **Name**| **Type**| **Mandatory**| **Description**| 1088 | -------- | -------- | -------- | -------- | 1089| config | [WifiDeviceConfig](#wifideviceconfig) | Yes| New WLAN configuration.| 1090 1091**Return value** 1092 1093 | **Type**| **Description**| 1094 | -------- | -------- | 1095 | number | ID of the updated network configuration. The value **-1** indicates that the operation has failed.| 1096 1097**Example** 1098```ts 1099import wifi from '@ohos.wifi'; 1100 1101try { 1102 let config:wifi.WifiDeviceConfig = { 1103 ssid : "****", 1104 preSharedKey : "****", 1105 securityType : 3 1106 } 1107 let ret = wifi.updateNetwork(config); 1108 console.error("ret:" + ret); 1109}catch(error){ 1110 console.error("failed:" + JSON.stringify(error)); 1111} 1112``` 1113 1114## wifi.disableNetwork<sup>7+</sup> 1115 1116disableNetwork(netId: number): boolean 1117 1118Disables network configuration. 1119 1120**System API**: This is a system API. 1121 1122**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications) 1123 1124**System capability**: SystemCapability.Communication.WiFi.STA 1125 1126**Parameters** 1127 1128 | **Name**| **Type**| **Mandatory**| **Description**| 1129 | -------- | -------- | -------- | -------- | 1130 | netId | number | Yes| ID of the network configuration to disable.| 1131 1132**Return value** 1133 1134 | **Type**| **Description**| 1135 | -------- | -------- | 1136 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1137 1138**Example** 1139```ts 1140import wifi from '@ohos.wifi'; 1141 1142try { 1143 let netId = 0; 1144 wifi.disableNetwork(netId); 1145}catch(error){ 1146 console.error("failed:" + JSON.stringify(error)); 1147} 1148``` 1149 1150## wifi.removeAllNetwork<sup>7+</sup> 1151 1152removeAllNetwork(): boolean 1153 1154Removes the configuration of all networks. 1155 1156**System API**: This is a system API. 1157 1158**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications) 1159 1160**System capability**: SystemCapability.Communication.WiFi.STA 1161 1162**Return value** 1163 1164 | **Type**| **Description**| 1165 | -------- | -------- | 1166 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1167 1168**Example** 1169```ts 1170import wifi from '@ohos.wifi'; 1171 1172try { 1173 wifi.removeAllNetwork(); 1174}catch(error){ 1175 console.error("failed:" + JSON.stringify(error)); 1176} 1177``` 1178 1179## wifi.removeDevice<sup>7+</sup> 1180 1181removeDevice(id: number): boolean 1182 1183Removes the specified network configuration. 1184 1185**System API**: This is a system API. 1186 1187**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications) 1188 1189**System capability**: SystemCapability.Communication.WiFi.STA 1190 1191**Parameters** 1192 1193 | **Name**| **Type**| **Mandatory**| **Description**| 1194 | -------- | -------- | -------- | -------- | 1195| id | number | Yes| ID of the network configuration to remove.| 1196 1197**Return value** 1198 1199 | **Type**| **Description**| 1200 | -------- | -------- | 1201 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1202 1203**Example** 1204```ts 1205import wifi from '@ohos.wifi'; 1206 1207try { 1208 let id = 0; 1209 wifi.removeDevice(id); 1210}catch(error){ 1211 console.error("failed:" + JSON.stringify(error)); 1212} 1213``` 1214 1215## wifi.enableHotspot<sup>7+</sup> 1216 1217enableHotspot(): boolean 1218 1219Enables this hotspot. 1220 1221**System API**: This is a system API. 1222 1223**Required permissions**: ohos.permission.MANAGE_WIFI_HOTSPOT (available only to system applications) 1224 1225**System capability**: SystemCapability.Communication.WiFi.AP.Core 1226 1227**Return value** 1228 1229 | **Type**| **Description**| 1230 | -------- | -------- | 1231 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1232 1233**Example** 1234```ts 1235import wifi from '@ohos.wifi'; 1236 1237try { 1238 wifi.enableHotspot(); 1239}catch(error){ 1240 console.error("failed:" + JSON.stringify(error)); 1241} 1242``` 1243 1244## wifi.disableHotspot<sup>7+</sup> 1245 1246disableHotspot(): boolean 1247 1248Disables this hotspot. 1249 1250**System API**: This is a system API. 1251 1252**Required permissions**: ohos.permission.MANAGE_WIFI_HOTSPOT (available only to system applications) 1253 1254**System capability**: SystemCapability.Communication.WiFi.AP.Core 1255 1256**Return value** 1257 1258 | **Type**| **Description**| 1259 | -------- | -------- | 1260 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1261 1262**Example** 1263```ts 1264import wifi from '@ohos.wifi'; 1265 1266try { 1267 wifi.disableHotspot(); 1268}catch(error){ 1269 console.error("failed:" + JSON.stringify(error)); 1270} 1271``` 1272 1273## wifi.isHotspotDualBandSupported<sup>7+</sup> 1274 1275isHotspotDualBandSupported(): boolean 1276 1277Checks whether the hotspot supports dual band. 1278 1279**System API**: This is a system API. 1280 1281**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.MANAGE_WIFI_HOTSPOT (available only to system applications) 1282 1283**System capability**: SystemCapability.Communication.WiFi.AP.Core 1284 1285**Return value** 1286 1287 | **Type**| **Description**| 1288 | -------- | -------- | 1289 | boolean | Returns **true** if the feature is supported; returns **false** otherwise.| 1290 1291**Example** 1292```ts 1293import wifi from '@ohos.wifi'; 1294 1295try { 1296 let ret = wifi.isHotspotDualBandSupported(); 1297 console.info("result:" + ret); 1298}catch(error){ 1299 console.error("failed:" + JSON.stringify(error)); 1300} 1301``` 1302 1303## wifi.isHotspotActive<sup>7+</sup> 1304 1305isHotspotActive(): boolean 1306 1307Checks whether this hotspot is active. 1308 1309**System API**: This is a system API. 1310 1311**Required permissions**: ohos.permission.GET_WIFI_INFO 1312 1313**System capability**: SystemCapability.Communication.WiFi.AP.Core 1314 1315**Return value** 1316 1317 | **Type**| **Description**| 1318 | -------- | -------- | 1319| boolean | Returns **true** if the hotspot is active; returns **false** otherwise.| 1320 1321**Example** 1322```ts 1323import wifi from '@ohos.wifi'; 1324 1325try { 1326 let ret = wifi.isHotspotActive(); 1327 console.info("result:" + ret); 1328}catch(error){ 1329 console.error("failed:" + JSON.stringify(error)); 1330} 1331``` 1332 1333## wifi.setHotspotConfig<sup>7+</sup> 1334 1335setHotspotConfig(config: HotspotConfig): boolean 1336 1337Sets hotspot configuration. 1338 1339**System API**: This is a system API. 1340 1341**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.GET_WIFI_CONFIG 1342 1343**System capability**: SystemCapability.Communication.WiFi.AP.Core 1344 1345**Parameters** 1346 1347 | **Name**| **Type**| **Mandatory**| **Description**| 1348 | -------- | -------- | -------- | -------- | 1349 | config | [HotspotConfig](#hotspotconfig7) | Yes| Hotspot configuration to set.| 1350 1351**Return value** 1352 1353 | **Type**| **Description**| 1354 | -------- | -------- | 1355 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1356 1357**Example** 1358```ts 1359import wifi from '@ohos.wifi'; 1360 1361try { 1362 let config:wifi.HotspotConfig = { 1363 ssid: "****", 1364 securityType: 3, 1365 band: 0, 1366 channel: 0, 1367 preSharedKey: "****", 1368 maxConn: 0 1369 } 1370 let ret = wifi.setHotspotConfig(config); 1371 console.info("result:" + ret); 1372}catch(error){ 1373 console.error("failed:" + JSON.stringify(error)); 1374} 1375``` 1376 1377## HotspotConfig<sup>7+</sup> 1378 1379Represents the hotspot configuration. 1380 1381**System API**: This is a system API. 1382 1383**System capability**: SystemCapability.Communication.WiFi.AP.Core 1384 1385| **Name**| **Type**| **Readable**| **Writable**| **Description**| 1386| -------- | -------- | -------- | -------- | -------- | 1387| ssid | string | Yes| No| SSID of the hotspot, in UTF-8 format.| 1388| securityType | [WifiSecurityType](#wifisecuritytype) | Yes| No| Security type.| 1389| band | number | Yes| No| Hotspot band. The value **1** stands for 2.4 GHz, the value **2** for 5 GHz, and the value **3** for dual band.| 1390| preSharedKey | string | Yes| No| PSK of the hotspot.| 1391| maxConn | number | Yes| No| Maximum number of connections allowed.| 1392 1393 1394## wifi.getHotspotConfig<sup>7+</sup> 1395 1396getHotspotConfig(): HotspotConfig 1397 1398obtains hotspot configuration. 1399 1400**System API**: This is a system API. 1401 1402**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.GET_WIFI_CONFIG 1403 1404**System capability**: SystemCapability.Communication.WiFi.AP.Core 1405 1406**Return value** 1407 1408 | **Type**| **Description**| 1409 | -------- | -------- | 1410 | [HotspotConfig](#hotspotconfig7) | Hotspot configuration obtained.| 1411 1412**Example** 1413```ts 1414import wifi from '@ohos.wifi'; 1415 1416try { 1417 let config = wifi.getHotspotConfig(); 1418 console.info("result:" + JSON.stringify(config)); 1419}catch(error){ 1420 console.error("failed:" + JSON.stringify(error)); 1421} 1422``` 1423 1424## wifi.getStations<sup>7+</sup> 1425 1426getStations(): Array<[StationInfo](#stationinfo7)> 1427 1428Obtains information about the connected stations. 1429 1430**System API**: This is a system API. 1431 1432**Required permissions**: ohos.permission.GET_WIFI_INFO, ohos.permission.LOCATION, and ohos.permission.MANAGE_WIFI_HOTSPOT (available only to system applications) 1433 1434**System capability**: SystemCapability.Communication.WiFi.AP.Core 1435 1436**Return value** 1437 1438 | **Type**| **Description**| 1439 | -------- | -------- | 1440 | Array<[StationInfo](#stationinfo7)> | Connected stations obtained.| 1441 1442**Example** 1443```ts 1444import wifi from '@ohos.wifi'; 1445 1446try { 1447 let stations = wifi.getStations(); 1448 console.info("result:" + JSON.stringify(stations)); 1449}catch(error){ 1450 console.error("failed:" + JSON.stringify(error)); 1451} 1452``` 1453 1454## StationInfo<sup>7+</sup> 1455 1456Represents the station information. 1457 1458**System API**: This is a system API. 1459 1460**System capability**: SystemCapability.Communication.WiFi.AP.Core 1461 1462| **Name**| **Type**| **Readable**| **Writable**| **Description**| 1463| -------- | -------- | -------- | -------- | -------- | 1464| name | string | Yes| No| Device name.| 1465| macAddress | string | Yes| No| MAC address.| 1466| ipAddress | string | Yes| No| IP address.| 1467 1468 1469## wifi.getP2pLinkedInfo<sup>8+</sup> 1470 1471getP2pLinkedInfo(): Promise<WifiP2pLinkedInfo> 1472 1473Obtains P2P link information. This API uses a promise to return the result. 1474 1475**Required permissions**: ohos.permission.GET_WIFI_INFO 1476 1477**System capability**: SystemCapability.Communication.WiFi.P2P 1478 1479**Return value** 1480 1481 | Type| Description| 1482 | -------- | -------- | 1483| Promise<[WifiP2pLinkedInfo](#wifip2plinkedinfo8)> | Promise used to return the P2P link information obtained.| 1484 1485 1486 1487## WifiP2pLinkedInfo<sup>8+</sup> 1488 1489Represents the P2P link information. 1490 1491**System capability**: SystemCapability.Communication.WiFi.P2P 1492 1493| Name| Type| Readable| Writable| Description| 1494| -------- | -------- | -------- | -------- | -------- | 1495| connectState | [P2pConnectState](#p2pconnectstate8) | Yes| No| P2P connection state.| 1496| isGroupOwner | boolean | Yes| No| Whether the device is the group owner.| 1497| groupOwnerAddr | string | Yes| No| MAC address of the group. 1498 1499 1500## P2pConnectState<sup>8+</sup> 1501 1502Enumerates the P2P connection states. 1503 1504**System capability**: SystemCapability.Communication.WiFi.P2P 1505 1506| Name| Value| Description| 1507| -------- | -------- | -------- | 1508| DISCONNECTED | 0 | Disconnected.| 1509| CONNECTED | 1 | Connected.| 1510 1511 1512## wifi.getP2pLinkedInfo<sup>8+</sup> 1513 1514getP2pLinkedInfo(callback: AsyncCallback<WifiP2pLinkedInfo>): void 1515 1516Obtains P2P link information. This API uses an asynchronous callback to return the result. 1517 1518**Required permissions**: ohos.permission.GET_WIFI_INFO 1519 1520**System capability**: SystemCapability.Communication.WiFi.P2P 1521 1522**Parameters** 1523 1524 | Name| Type| Mandatory| Description| 1525 | -------- | -------- | -------- | -------- | 1526 | callback | AsyncCallback<[WifiP2pLinkedInfo](#wifip2plinkedinfo8)> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is the P2P link information. If **err** is not **0**, an error has occurred.| 1527 1528**Example** 1529```ts 1530import wifi from '@ohos.wifi'; 1531 1532wifi.getP2pLinkedInfo((err, data) => { 1533 if (err) { 1534 console.error("get p2p linked info error"); 1535 return; 1536 } 1537 console.info("get wifi p2p linked info: " + JSON.stringify(data)); 1538}); 1539 1540wifi.getP2pLinkedInfo().then(data => { 1541 console.info("get wifi p2p linked info: " + JSON.stringify(data)); 1542}); 1543``` 1544 1545## wifi.getCurrentGroup<sup>8+</sup> 1546 1547getCurrentGroup(): Promise<WifiP2pGroupInfo> 1548 1549Obtains the current P2P group information. This API uses a promise to return the result. 1550 1551**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION 1552 1553**System capability**: SystemCapability.Communication.WiFi.P2P 1554 1555**Return value** 1556 1557 | Type| Description| 1558 | -------- | -------- | 1559 | Promise<[WifiP2pGroupInfo](#wifip2pgroupinfo8)> | Promise used to return the P2P group information obtained.| 1560 1561 1562## wifi.getCurrentGroup<sup>8+</sup> 1563 1564getCurrentGroup(callback: AsyncCallback<WifiP2pGroupInfo>): void 1565 1566Obtains the current P2P group information. This API uses an asynchronous callback to return the result. 1567 1568**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION 1569 1570**System capability**: SystemCapability.Communication.WiFi.P2P 1571 1572**Parameters** 1573 1574 | Name| Type| Mandatory| Description| 1575 | -------- | -------- | -------- | -------- | 1576 | callback | AsyncCallback<[WifiP2pGroupInfo](#wifip2pgroupinfo8)> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is the group information obtained. If **err** is not **0**, an error has occurred.| 1577 1578**Example** 1579```ts 1580import wifi from '@ohos.wifi'; 1581 1582wifi.getCurrentGroup((err, data) => { 1583 if (err) { 1584 console.error("get current P2P group error"); 1585 return; 1586 } 1587 console.info("get current P2P group: " + JSON.stringify(data)); 1588}); 1589 1590wifi.getCurrentGroup().then(data => { 1591 console.info("get current P2P group: " + JSON.stringify(data)); 1592}); 1593``` 1594 1595## wifi.getP2pPeerDevices<sup>8+</sup> 1596 1597getP2pPeerDevices(): Promise<WifiP2pDevice[]> 1598 1599Obtains the peer device list in the P2P connection. This API uses a promise to return the result. 1600 1601**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION 1602 1603**System capability**: SystemCapability.Communication.WiFi.P2P 1604 1605**Return value** 1606 1607 | Type| Description| 1608 | -------- | -------- | 1609 | Promise<[WifiP2pDevice[]](#wifip2pdevice8)> | Promise used to return the peer device list.| 1610 1611 1612## wifi.getP2pPeerDevices<sup>8+</sup> 1613 1614getP2pPeerDevices(callback: AsyncCallback<WifiP2pDevice[]>): void 1615 1616Obtains the peer device list in the P2P connection. This API uses an asynchronous callback to return the result. 1617 1618**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION 1619 1620**System capability**: SystemCapability.Communication.WiFi.P2P 1621 1622**Parameters** 1623 1624 | Name| Type| Mandatory| Description| 1625 | -------- | -------- | -------- | -------- | 1626 | callback | AsyncCallback<[WifiP2pDevice[]](#wifip2pdevice8)> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **0** and **data** is the peer device list obtained. If **err** is not **0**, an error has occurred.| 1627 1628**Example** 1629```ts 1630import wifi from '@ohos.wifi'; 1631 1632wifi.getP2pPeerDevices((err, data) => { 1633 if (err) { 1634 console.error("get P2P peer devices error"); 1635 return; 1636 } 1637 console.info("get P2P peer devices: " + JSON.stringify(data)); 1638}); 1639 1640wifi.getP2pPeerDevices().then(data => { 1641 console.info("get P2P peer devices: " + JSON.stringify(data)); 1642}); 1643``` 1644 1645## WifiP2pDevice<sup>8+</sup> 1646 1647Represents the P2P device information. 1648 1649**System capability**: SystemCapability.Communication.WiFi.P2P 1650 1651| Name| Type| Readable| Writable| Description| 1652| -------- | -------- | -------- | -------- | -------- | 1653| deviceName | string | Yes| No| Device name.| 1654| deviceAddress | string | Yes| No| MAC address of the device.| 1655| primaryDeviceType | string | Yes| No| Type of the primary device.| 1656| deviceStatus | [P2pDeviceStatus](#p2pdevicestatus8) | Yes| No| Device status.| 1657| groupCapabilitys | number | Yes| No| Group capabilities.| 1658 1659 1660## P2pDeviceStatus<sup>8+</sup> 1661 1662Enumerates the P2P device states. 1663 1664**System capability**: SystemCapability.Communication.WiFi.P2P 1665 1666| Name| Value| Description| 1667| -------- | -------- | -------- | 1668| CONNECTED | 0 | Connected.| 1669| INVITED | 1 | Invited.| 1670| FAILED | 2 | Failed.| 1671| AVAILABLE | 3 | Available.| 1672| UNAVAILABLE | 4 | Unavailable.| 1673 1674 1675## wifi.createGroup<sup>8+</sup> 1676 1677createGroup(config: WifiP2PConfig): boolean 1678 1679Creates a P2P group. 1680 1681**Required permissions**: ohos.permission.GET_WIFI_INFO 1682 1683**System capability**: SystemCapability.Communication.WiFi.P2P 1684 1685**Parameters** 1686 1687 | **Name**| **Type**| Mandatory| **Description**| 1688 | -------- | -------- | -------- | -------- | 1689 | config | [WifiP2PConfig](#wifip2pconfig8) | Yes| Group configuration.| 1690 1691**Return value** 1692 1693 | Type| Description| 1694 | -------- | -------- | 1695 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1696 1697**Example** 1698```ts 1699import wifi from '@ohos.wifi'; 1700 1701try { 1702 let config:wifi.WifiP2PConfig = { 1703 deviceAddress: "****", 1704 netId: 0, 1705 passphrase: "*****", 1706 groupName: "****", 1707 goBand: 0 1708 } 1709 wifi.createGroup(config); 1710 1711}catch(error){ 1712 console.error("failed:" + JSON.stringify(error)); 1713} 1714``` 1715 1716## WifiP2PConfig<sup>8+</sup> 1717 1718Represents P2P group configuration. 1719 1720**System capability**: SystemCapability.Communication.WiFi.P2P 1721 1722| Name| Type| Readable| Writable| Description| 1723| -------- | -------- | -------- | -------- | -------- | 1724| deviceAddress | string | Yes| No| Device address.| 1725| netId | number | Yes| No| Network ID. The value **-1** indicates a temporary group, and **-2** indicates a persistent group.| 1726| passphrase | string | Yes| No| Passphrase of the group.| 1727| groupName | string | Yes| No| Name of the group.| 1728| goBand | [GroupOwnerBand](#groupownerband8) | Yes| No| Frequency band of the group.| 1729 1730 1731## GroupOwnerBand<sup>8+</sup> 1732 1733Enumerates the P2P group frequency bands. 1734 1735**System capability**: SystemCapability.Communication.WiFi.P2P 1736 1737| Name| Value| Description| 1738| -------- | -------- | -------- | 1739| GO_BAND_AUTO | 0 | Auto.| 1740| GO_BAND_2GHZ | 1 | 2 GHz.| 1741| GO_BAND_5GHZ | 2 | 5 GHz.| 1742 1743 1744## wifi.removeGroup<sup>8+</sup> 1745 1746removeGroup(): boolean 1747 1748Removes this P2P group. 1749 1750**Required permissions**: ohos.permission.GET_WIFI_INFO 1751 1752**System capability**: SystemCapability.Communication.WiFi.P2P 1753 1754**Return value** 1755 1756 | Type| Description| 1757 | -------- | -------- | 1758 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1759 1760**Example** 1761```ts 1762import wifi from '@ohos.wifi'; 1763 1764try { 1765 wifi.removeGroup(); 1766}catch(error){ 1767 console.error("failed:" + JSON.stringify(error)); 1768} 1769``` 1770 1771## wifi.p2pConnect<sup>8+</sup> 1772 1773p2pConnect(config: WifiP2PConfig): boolean 1774 1775Sets up a P2P connection. 1776 1777**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION 1778 1779**System capability**: SystemCapability.Communication.WiFi.P2P 1780 1781**Parameters** 1782 1783 1784 | **Name**| **Type**| Mandatory| **Description**| 1785 | -------- | -------- | -------- | -------- | 1786 | config | [WifiP2PConfig](#wifip2pconfig8) | Yes| P2P group configuration.| 1787 1788**Return value** 1789 1790 | Type| Description| 1791 | -------- | -------- | 1792 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1793 1794 1795**Example** 1796```ts 1797import wifi from '@ohos.wifi'; 1798 1799let recvP2pConnectionChangeFunc = (result:wifi.WifiP2pLinkedInfo) => { 1800 console.info("p2p connection change receive event: " + JSON.stringify(result)); 1801 wifi.getP2pLinkedInfo((err, data) => { 1802 if (err) { 1803 console.error('failed to get getP2pLinkedInfo: ' + JSON.stringify(err)); 1804 return; 1805 } 1806 console.info("get getP2pLinkedInfo: " + JSON.stringify(data)); 1807 }); 1808} 1809wifi.on("p2pConnectionChange", recvP2pConnectionChangeFunc); 1810 1811let recvP2pDeviceChangeFunc = (result:wifi.WifiP2pLinkedInfo) => { 1812 console.info("p2p device change receive event: " + JSON.stringify(result)); 1813} 1814wifi.on("p2pDeviceChange", recvP2pDeviceChangeFunc); 1815 1816let recvP2pPeerDeviceChangeFunc = (result:wifi.WifiP2pLinkedInfo) => { 1817 console.info("p2p peer device change receive event: " + JSON.stringify(result)); 1818 wifi.getP2pPeerDevices((err, data) => { 1819 if (err) { 1820 console.error('failed to get peer devices: ' + JSON.stringify(err)); 1821 return; 1822 } 1823 console.info("get peer devices: " + JSON.stringify(data)); 1824 let len = data.length; 1825 for (let i = 0; i < len; ++i) { 1826 if (data[i].deviceName === "my_test_device") { 1827 console.info("p2p connect to test device: " + data[i].deviceAddress); 1828 let config:wifi.WifiP2PConfig = { 1829 deviceAddress:data[i].deviceAddress, 1830 netId:-2, 1831 passphrase:"", 1832 groupName:"", 1833 goBand:0, 1834 } 1835 wifi.p2pConnect(config); 1836 } 1837 } 1838 }); 1839} 1840wifi.on("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc); 1841 1842let recvP2pPersistentGroupChangeFunc = () => { 1843 console.info("p2p persistent group change receive event"); 1844 1845 wifi.getCurrentGroup((err, data) => { 1846 if (err) { 1847 console.error('failed to get current group: ' + JSON.stringify(err)); 1848 return; 1849 } 1850 console.info("get current group: " + JSON.stringify(data)); 1851 }); 1852} 1853wifi.on("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc); 1854 1855setTimeout(() => {wifi.off("p2pConnectionChange", recvP2pConnectionChangeFunc);}, 125 * 1000); 1856setTimeout(() => {wifi.off("p2pDeviceChange", recvP2pDeviceChangeFunc);}, 125 * 1000); 1857setTimeout(() => {wifi.off("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc);}, 125 * 1000); 1858setTimeout(() => {wifi.off("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc);}, 125 * 1000); 1859console.info("start discover devices -> " + wifi.startDiscoverDevices()); 1860``` 1861 1862## wifi.p2pCancelConnect<sup>8+</sup> 1863 1864p2pCancelConnect(): boolean 1865 1866Cancels this P2P connection. 1867 1868**Required permissions**: ohos.permission.GET_WIFI_INFO 1869 1870**System capability**: SystemCapability.Communication.WiFi.P2P 1871 1872**Return value** 1873 1874 | Type| Description| 1875 | -------- | -------- | 1876 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1877 1878**Example** 1879```ts 1880import wifi from '@ohos.wifi'; 1881 1882try { 1883 wifi.p2pCancelConnect(); 1884}catch(error){ 1885 console.error("failed:" + JSON.stringify(error)); 1886} 1887``` 1888 1889## wifi.startDiscoverDevices<sup>8+</sup> 1890 1891startDiscoverDevices(): boolean 1892 1893Starts to discover devices. 1894 1895**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION 1896 1897**System capability**: SystemCapability.Communication.WiFi.P2P 1898 1899**Return value** 1900 1901 | Type| Description| 1902 | -------- | -------- | 1903 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1904 1905**Example** 1906```ts 1907import wifi from '@ohos.wifi'; 1908 1909try { 1910 wifi.startDiscoverDevices(); 1911}catch(error){ 1912 console.error("failed:" + JSON.stringify(error)); 1913} 1914``` 1915 1916## wifi.stopDiscoverDevices<sup>8+</sup> 1917 1918stopDiscoverDevices(): boolean 1919 1920Stops discovering devices. 1921 1922**Required permissions**: ohos.permission.GET_WIFI_INFO 1923 1924**System capability**: SystemCapability.Communication.WiFi.P2P 1925 1926**Return value** 1927 1928 | Type| Description| 1929 | -------- | -------- | 1930 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1931 1932**Example** 1933```ts 1934import wifi from '@ohos.wifi'; 1935 1936try { 1937 wifi.stopDiscoverDevices(); 1938}catch(error){ 1939 console.error("failed:" + JSON.stringify(error)); 1940} 1941``` 1942 1943## wifi.deletePersistentGroup<sup>8+</sup> 1944 1945deletePersistentGroup(netId: number): boolean 1946 1947Deletes a persistent group. 1948 1949**System API**: This is a system API. 1950 1951**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION 1952 1953**System capability**: SystemCapability.Communication.WiFi.P2P 1954 1955**Parameters** 1956 1957 1958 | **Name**| **Type**| Mandatory| **Description**| 1959 | -------- | -------- | -------- | -------- | 1960 | netId | number | Yes| ID of the group to delete.| 1961 1962**Return value** 1963 1964 | Type| Description| 1965 | -------- | -------- | 1966 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1967 1968**Example** 1969```ts 1970import wifi from '@ohos.wifi'; 1971 1972try { 1973 let netId = 0; 1974 wifi.deletePersistentGroup(netId); 1975}catch(error){ 1976 console.error("failed:" + JSON.stringify(error)); 1977} 1978``` 1979 1980## WifiP2pGroupInfo<sup>8+</sup> 1981 1982Represents the P2P group information. 1983 1984**System capability**: SystemCapability.Communication.WiFi.P2P 1985 1986| Name| Type| Readable| Writable| Description| 1987| -------- | -------- | -------- | -------- | -------- | 1988| isP2pGo | boolean | Yes| No| Whether the device is the group owner.| 1989| ownerInfo | [WifiP2pDevice](#wifip2pdevice8) | Yes| No| Device information of the group.| 1990| passphrase | string | Yes| No| Passphrase of the group.| 1991| interface | string | Yes| No| Interface name.| 1992| groupName | string | Yes| No| Group name.| 1993| networkId | number | Yes| No| Network ID.| 1994| frequency | number | Yes| No| Frequency of the group.| 1995| clientDevices | [WifiP2pDevice[]](#wifip2pdevice8) | Yes| No| List of connected devices.| 1996| goIpAddress | string | Yes| No| IP address of the group.| 1997 1998 1999## wifi.setDeviceName<sup>8+</sup> 2000 2001setDeviceName(devName: string): boolean 2002 2003Sets the device name. 2004 2005**System API**: This is a system API. 2006 2007**Required permissions**: ohos.permission.SET_WIFI_INFO and ohos.permission.MANAGE_WIFI_CONNECTION (available only to system applications) 2008 2009**System capability**: SystemCapability.Communication.WiFi.P2P 2010 2011**Parameters** 2012 2013 | **Name**| **Type**| **Mandatory**| **Description**| 2014 | -------- | -------- | -------- | -------- | 2015 | devName | string | Yes| Device name to set.| 2016 2017**Return value** 2018 2019 | **Type**| **Description**| 2020 | -------- | -------- | 2021 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 2022 2023**Example** 2024```ts 2025import wifi from '@ohos.wifi'; 2026 2027try { 2028 let name = "****"; 2029 wifi.setDeviceName(name); 2030}catch(error){ 2031 console.error("failed:" + JSON.stringify(error)); 2032} 2033``` 2034 2035## wifi.on('wifiStateChange')<sup>7+</sup> 2036 2037on(type: "wifiStateChange", callback: Callback<number>): void 2038 2039Subscribes to WLAN state changes. 2040 2041**Required permissions**: ohos.permission.GET_WIFI_INFO 2042 2043**System capability**: SystemCapability.Communication.WiFi.STA 2044 2045**Parameters** 2046 2047 | **Name**| **Type**| **Mandatory**| **Description**| 2048 | -------- | -------- | -------- | -------- | 2049 | type | string | Yes| Event type, which has a fixed value of **wifiStateChange**.| 2050 | callback | Callback<number> | Yes| Callback invoked to return the WLAN state.| 2051 2052**WLAN states** 2053 2054| **Value**| **Description**| 2055| -------- | -------- | 2056| 0 | Deactivated| 2057| 1 | Activated| 2058| 2 | Activating| 2059| 3 | Deactivating| 2060 2061 2062## wifi.off('wifiStateChange')<sup>7+</sup> 2063 2064off(type: "wifiStateChange", callback?: Callback<number>): void 2065 2066Unsubscribes from WLAN state changes. 2067 2068**Required permissions**: ohos.permission.GET_WIFI_INFO 2069 2070**System capability**: SystemCapability.Communication.WiFi.STA 2071 2072**Parameters** 2073 2074 | **Name**| **Type**| **Mandatory**| **Description**| 2075 | -------- | -------- | -------- | -------- | 2076 | type | string | Yes| Event type, which has a fixed value of **wifiStateChange**.| 2077| callback | Callback<number> | No| Callback for the WLAN state change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| 2078 2079**Example** 2080```ts 2081import wifi from '@ohos.wifi'; 2082 2083let recvPowerNotifyFunc = (result:number) => { 2084 console.info("Receive power state change event: " + result); 2085} 2086 2087// Register an event. 2088wifi.on("wifiStateChange", recvPowerNotifyFunc); 2089 2090// Unregister an event. 2091wifi.off("wifiStateChange", recvPowerNotifyFunc); 2092``` 2093 2094 2095## wifi.on('wifiConnectionChange')<sup>7+</sup> 2096 2097on(type: "wifiConnectionChange", callback: Callback<number>): void 2098 2099Subscribes to WLAN connection state changes. 2100 2101**Required permissions**: ohos.permission.GET_WIFI_INFO 2102 2103**System capability**: SystemCapability.Communication.WiFi.STA 2104 2105**Parameters** 2106 2107 | **Name**| **Type**| **Mandatory**| **Description**| 2108 | -------- | -------- | -------- | -------- | 2109 | type | string | Yes| Event type, which has a fixed value of **wifiConnectionChange**.| 2110 | callback | Callback<number> | Yes| Callback invoked to return the WLAN connection state.| 2111 2112**WLAN connection states** 2113 2114| **Value**| **Description**| 2115| -------- | -------- | 2116| 0 | Disconnected.| 2117| 1 | Connected.| 2118 2119 2120## wifi.off('wifiConnectionChange')<sup>7+</sup> 2121 2122off(type: "wifiConnectionChange", callback?: Callback<number>): void 2123 2124Unsubscribes from WLAN connection state changes. 2125 2126**Required permissions**: ohos.permission.GET_WIFI_INFO 2127 2128**System capability**: SystemCapability.Communication.WiFi.STA 2129 2130**Parameters** 2131 2132 | **Name**| **Type**| **Mandatory**| **Description**| 2133 | -------- | -------- | -------- | -------- | 2134 | type | string | Yes| Event type, which has a fixed value of **wifiConnectionChange**.| 2135| callback | Callback<number> | No| Callback for the WLAN connection state change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| 2136 2137**Example** 2138```ts 2139import wifi from '@ohos.wifi'; 2140 2141let recvWifiConnectionChangeFunc = (result:number) => { 2142 console.info("Receive wifi connection change event: " + result); 2143} 2144 2145// Register an event. 2146wifi.on("wifiConnectionChange", recvWifiConnectionChangeFunc); 2147 2148// Unregister an event. 2149wifi.off("wifiConnectionChange", recvWifiConnectionChangeFunc); 2150``` 2151 2152## wifi.on('wifiScanStateChange')<sup>7+</sup> 2153 2154on(type: "wifiScanStateChange", callback: Callback<number>): void 2155 2156Subscribes to WLAN scan state changes. 2157 2158**Required permissions**: ohos.permission.GET_WIFI_INFO 2159 2160**System capability**: SystemCapability.Communication.WiFi.STA 2161 2162**Parameters** 2163 2164 | **Name**| **Type**| **Mandatory**| **Description**| 2165 | -------- | -------- | -------- | -------- | 2166 | type | string | Yes| Event type, which has a fixed value of **wifiScanStateChange**.| 2167 | callback | Callback<number> | Yes| Callback invoked to return the WLAN scan state.| 2168 2169**WLAN scan states** 2170 2171| **Value**| **Description**| 2172| -------- | -------- | 2173| 0 | Scan failed.| 2174| 1 | Scan successful.| 2175 2176 2177## wifi.off('wifiScanStateChange')<sup>7+</sup> 2178 2179off(type: "wifiScanStateChange", callback?: Callback<number>): void 2180 2181Unsubscribes from WLAN scan state changes. 2182 2183**Required permissions**: ohos.permission.GET_WIFI_INFO 2184 2185**System capability**: SystemCapability.Communication.WiFi.STA 2186 2187**Parameters** 2188 2189| **Name**| **Type**| **Mandatory**| **Description**| 2190| -------- | -------- | -------- | -------- | 2191| type | string | Yes| Event type, which has a fixed value of **wifiScanStateChange**.| 2192| callback | Callback<number> | No| Callback for the WLAN scan state change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| 2193 2194**Example** 2195```ts 2196import wifi from '@ohos.wifi'; 2197 2198let recvWifiScanStateChangeFunc = (result:number) => { 2199 console.info("Receive Wifi scan state change event: " + result); 2200} 2201 2202// Register an event. 2203wifi.on("wifiScanStateChange", recvWifiScanStateChangeFunc); 2204 2205// Unregister an event. 2206wifi.off("wifiScanStateChange", recvWifiScanStateChangeFunc); 2207``` 2208 2209## wifi.on('wifiRssiChange')<sup>7+</sup> 2210 2211on(type: "wifiRssiChange", callback: Callback<number>): void 2212 2213Subscribes to RSSI changes. 2214 2215**Required permissions**: ohos.permission.GET_WIFI_INFO 2216 2217**System capability**: SystemCapability.Communication.WiFi.STA 2218 2219**Parameters** 2220 2221 | **Name**| **Type**| **Mandatory**| **Description**| 2222 | -------- | -------- | -------- | -------- | 2223 | type | string | Yes| Event type, which has a fixed value of **wifiRssiChange**.| 2224 | callback | Callback<number> | Yes| Callback invoked to return the RSSI, in dBm.| 2225 2226 2227## wifi.off('wifiRssiChange')<sup>7+</sup> 2228 2229off(type: "wifiRssiChange", callback?: Callback<number>): void 2230 2231Unsubscribes from RSSI changes. 2232 2233**Required permissions**: ohos.permission.GET_WIFI_INFO 2234 2235**System capability**: SystemCapability.Communication.WiFi.STA 2236 2237**Parameters** 2238 2239 | **Name**| **Type**| **Mandatory**| **Description**| 2240 | -------- | -------- | -------- | -------- | 2241 | type | string | Yes| Event type, which has a fixed value of **wifiRssiChange**.| 2242| callback | Callback<number> | No| Callback for the RSSI change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| 2243 2244**Example** 2245```ts 2246import wifi from '@ohos.wifi'; 2247 2248let recvWifiRssiChangeFunc = (result:number) => { 2249 console.info("Receive wifi rssi change event: " + result); 2250} 2251 2252// Register an event. 2253wifi.on("wifiRssiChange", recvWifiRssiChangeFunc); 2254 2255// Unregister an event. 2256wifi.off("wifiRssiChange", recvWifiRssiChangeFunc); 2257 2258``` 2259## wifi.on('streamChange')<sup>7+</sup> 2260 2261on(type: "streamChange", callback: Callback<number>): void 2262 2263Subscribes to Wi-Fi stream changes. 2264 2265**System API**: This is a system API. 2266 2267**Required permissions**: ohos.permission.MANAGE_WIFI_CONNECTION 2268 2269**System capability**: SystemCapability.Communication.WiFi.STA 2270 2271**Parameters** 2272 2273 | **Name**| **Type**| **Mandatory**| **Description**| 2274 | -------- | -------- | -------- | -------- | 2275 | type | string | Yes| Event type, which has a fixed value of **streamChange**.| 2276| callback | Callback<number> | Yes| Callback invoked to return the Wi-Fi stream change, which can be any of the following values:<br>- **0**: No stream.<br>- **1**: Downward.<br>- **2**: Upward.<br>- **3**: Bidirectional. | 2277 2278## wifi.off('streamChange')<sup>7+</sup> 2279 2280off(type: "streamChange", callback?: Callback<number>): void 2281 2282Unsubscribes from Wi-Fi stream changes. 2283 2284**System API**: This is a system API. 2285 2286**Required permissions**: ohos.permission.MANAGE_WIFI_CONNECTION 2287 2288**System capability**: SystemCapability.Communication.WiFi.STA 2289 2290**Parameters** 2291 2292| **Name**| **Type**| **Mandatory**| **Description**| 2293| -------- | -------- | -------- | -------- | 2294| type | string | Yes| Event type, which has a fixed value of **streamChange**.| 2295| callback | Callback<number> | No| Callback for the Wi-Fi stream change, which can be any of the following values:<br>- **0**: No stream.<br>- **1**: Downward.<br>- **2**: Upward.<br>- **3**: Bidirectional.| 2296 2297**Example** 2298```ts 2299import wifi from '@ohos.wifi'; 2300 2301let recvStreamChangeFunc = (result:number) => { 2302 console.info("Receive stream change event: " + result); 2303} 2304 2305// Register an event. 2306wifi.on("streamChange", recvStreamChangeFunc); 2307 2308// Unregister an event. 2309wifi.off("streamChange", recvStreamChangeFunc); 2310 2311``` 2312 2313## wifi.on('hotspotStateChange')<sup>7+</sup> 2314 2315on(type: "hotspotStateChange", callback: Callback<number>): void 2316 2317Subscribes to hotspot state changes. 2318 2319**Required permissions**: ohos.permission.GET_WIFI_INFO 2320 2321**System capability**: SystemCapability.Communication.WiFi.AP.Core 2322 2323**Parameters** 2324 2325 | **Name**| **Type**| **Mandatory**| **Description**| 2326 | -------- | -------- | -------- | -------- | 2327 | type | string | Yes| Event type, which has a fixed value of **hotspotStateChange**.| 2328 | callback | Callback<number> | Yes| Callback invoked to return the hotspot state.| 2329 2330**Hotspot states** 2331 2332| **Value**| **Description**| 2333| -------- | -------- | 2334| 0 | Deactivated| 2335| 1 | Activated| 2336| 2 | Activating| 2337| 3 | Deactivating| 2338 2339**Example** 2340```ts 2341import wifi from '@ohos.wifi'; 2342 2343let recvHotspotStateChangeFunc = (result:number) => { 2344 console.info("Receive hotspot state change event: " + result); 2345} 2346 2347// Register an event. 2348wifi.on("hotspotStateChange", recvHotspotStateChangeFunc); 2349 2350// Unregister an event. 2351wifi.off("hotspotStateChange", recvHotspotStateChangeFunc); 2352``` 2353 2354## wifi.off('hotspotStateChange')<sup>7+</sup> 2355 2356off(type: "hotspotStateChange", callback?: Callback<number>): void 2357 2358Unsubscribes from hotspot state changes. 2359 2360**Required permissions**: ohos.permission.GET_WIFI_INFO 2361 2362**System capability**: SystemCapability.Communication.WiFi.AP.Core 2363 2364**Parameters** 2365 2366 | **Name**| **Type**| **Mandatory**| **Description**| 2367 | -------- | -------- | -------- | -------- | 2368 | type | string | Yes| Event type, which has a fixed value of **hotspotStateChange**.| 2369 | callback | Callback<number> | No| Callback for the hotspot state change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| 2370 2371## wifi.on('hotspotStaJoin')<sup>7+</sup> 2372 2373on(type: "hotspotStaJoin", callback: Callback<StationInfo>): void 2374 2375Subscribes to the connection of an STA to a Wi-Fi hotspot. 2376 2377**Required permissions**: ohos.permission.MANAGE_WIFI_HOTSPOT 2378 2379**System API**: This is a system API. 2380 2381**System capability**: SystemCapability.Communication.WiFi.AP.Core 2382 2383**Parameters** 2384 2385 | **Name**| **Type**| **Mandatory**| **Description**| 2386 | -------- | -------- | -------- | -------- | 2387 | type | string | Yes| Event type, which has a fixed value of **hotspotStaJoin**.| 2388 | callback | Callback<StationInfo> | Yes| Callback invoked when an STA is connected to a Wi-Fi hotspot.| 2389 2390## wifi.off('hotspotStaJoin')<sup>7+</sup> 2391 2392off(type: "hotspotStaJoin", callback?: Callback<StationInfo>): void 2393 2394Unsubscribes from the connection of an STA to a Wi-Fi hotspot. 2395 2396**Required permissions**: ohos.permission.MANAGE_WIFI_HOTSPOT 2397 2398**System API**: This is a system API. 2399 2400**System capability**: SystemCapability.Communication.WiFi.AP.Core 2401 2402**Parameters** 2403 2404 | **Name**| **Type**| **Mandatory**| **Description**| 2405 | -------- | -------- | -------- | -------- | 2406 | type | string | Yes| Event type, which has a fixed value of **hotspotStaJoin**.| 2407 | callback | Callback<StationInfo> | No| Callback for the connection of an STA to a Wi-Fi hotspot.| 2408 2409 **Example** 2410```ts 2411import wifi from '@ohos.wifi'; 2412 2413let recvHotspotStaJoinFunc = (result:wifi.StationInfo) => { 2414 console.info("Receive hotspot sta join event: " + result); 2415} 2416 2417// Register an event. 2418wifi.on("hotspotStaJoin", recvHotspotStaJoinFunc); 2419 2420// Unregister an event. 2421wifi.off("hotspotStaJoin", recvHotspotStaJoinFunc); 2422 2423``` 2424 2425## wifi.on('hotspotStaLeave')<sup>7+</sup> 2426 2427on(type: "hotspotStaLeave", callback: Callback<StationInfo>): void 2428 2429Subscribes to the disconnection of an STA from a Wi-Fi hotspot. 2430 2431**Required permissions**: ohos.permission.MANAGE_WIFI_HOTSPOT 2432 2433**System API**: This is a system API. 2434 2435**System capability**: SystemCapability.Communication.WiFi.AP.Core 2436 2437**Parameters** 2438 2439 | **Name**| **Type**| **Mandatory**| **Description**| 2440 | -------- | -------- | -------- | -------- | 2441 | type | string | Yes| Event type, which has a fixed value of **hotspotStaLeave**.| 2442 | callback | Callback<StationInf]> | Yes| Callback invoked when an STA is disconnected from a Wi-Fi hotspot.| 2443 2444## wifi.off('hotspotStaLeave')<sup>9+</sup> 2445 2446off(type: "hotspotStaLeave", callback?: Callback<StationInfo>): void 2447 2448Unsubscribes from the disconnection of an STA from a Wi-Fi hotspot. 2449 2450**Required permissions**: ohos.permission.MANAGE_WIFI_HOTSPOT 2451 2452**System API**: This is a system API. 2453 2454**System capability**: SystemCapability.Communication.WiFi.AP.Core 2455 2456**Parameters** 2457 2458 | **Name**| **Type**| **Mandatory**| **Description**| 2459 | -------- | -------- | -------- | -------- | 2460 | type | string | Yes| Event type, which has a fixed value of **hotspotStaLeave**.| 2461 | callback | Callback<StationInf]> | No| Callback for the disconnection of an STA from a Wi-Fi hotspot.| 2462 2463 **Example** 2464```ts 2465import wifi from '@ohos.wifi'; 2466 2467let recvHotspotStaLeaveFunc = (result:wifi.StationInfo) => { 2468 console.info("Receive hotspot sta leave event: " + result); 2469} 2470 2471// Register an event. 2472wifi.on("hotspotStaLeave", recvHotspotStaLeaveFunc); 2473 2474// Unregister an event. 2475wifi.off("hotspotStaLeave", recvHotspotStaLeaveFunc); 2476 2477``` 2478 2479## wifi.on('p2pStateChange')<sup>8+</sup> 2480 2481on(type: "p2pStateChange", callback: Callback<number>): void 2482 2483Subscribes to P2P state changes. 2484 2485**Required permissions**: ohos.permission.GET_WIFI_INFO 2486 2487**System capability**: SystemCapability.Communication.WiFi.P2P 2488 2489**Parameters** 2490 2491 | **Name**| **Type**| **Mandatory**| **Description**| 2492 | -------- | -------- | -------- | -------- | 2493 | type | string | Yes| Event type, which has a fixed value of **p2pStateChange**.| 2494 | callback | Callback<number> | Yes| Callback invoked to return the P2P state.| 2495 2496**P2P states** 2497 2498| **Value**| **Description**| 2499| -------- | -------- | 2500| 1 | Available| 2501| 2 | Opening| 2502| 3 | Opened| 2503| 4 | Closing| 2504| 5 | Closed| 2505 2506## wifi.off('p2pStateChange')<sup>8+</sup> 2507 2508off(type: "p2pStateChange", callback?: Callback<number>): void 2509 2510Unsubscribes from P2P state changes. 2511 2512**Required permissions**: ohos.permission.GET_WIFI_INFO 2513 2514**System capability**: SystemCapability.Communication.WiFi.P2P 2515 2516**Parameters** 2517 2518 | **Name**| **Type**| **Mandatory**| **Description**| 2519 | -------- | -------- | -------- | -------- | 2520 | type | string | Yes| Event type, which has a fixed value of **p2pStateChange**.| 2521 | callback | Callback<number> | No| Callback for the P2P state change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| 2522 2523**Example** 2524```ts 2525import wifi from '@ohos.wifi'; 2526 2527let recvP2pStateChangeFunc = (result:number) => { 2528 console.info("Receive p2p state change event: " + result); 2529} 2530 2531// Register an event. 2532wifi.on("p2pStateChange", recvP2pStateChangeFunc); 2533 2534// Unregister an event. 2535wifi.off("p2pStateChange", recvP2pStateChangeFunc); 2536``` 2537 2538## wifi.on('p2pConnectionChange')<sup>8+</sup> 2539 2540on(type: "p2pConnectionChange", callback: Callback<WifiP2pLinkedInfo>): void 2541 2542Subscribes to P2P connection state changes. 2543 2544**Required permissions**: ohos.permission.GET_WIFI_INFO 2545 2546**System capability**: SystemCapability.Communication.WiFi.P2P 2547 2548**Parameters** 2549 2550 | **Name**| **Type**| **Mandatory**| **Description**| 2551 | -------- | -------- | -------- | -------- | 2552 | type | string | Yes| Event type, which has a fixed value of **p2pConnectionChange**.| 2553 | callback | Callback<[WifiP2pLinkedInfo](#wifip2plinkedinfo8)> | Yes| Callback invoked to return the P2P connection state.| 2554 2555 2556## wifi.off('p2pConnectionChange')<sup>8+</sup> 2557 2558off(type: "p2pConnectionChange", callback?: Callback<WifiP2pLinkedInfo>): void 2559 2560Unsubscribes from P2P connection state changes. 2561 2562**Required permissions**: ohos.permission.GET_WIFI_INFO 2563 2564**System capability**: SystemCapability.Communication.WiFi.P2P 2565 2566**Parameters** 2567 2568 | **Name**| **Type**| **Mandatory**| **Description**| 2569 | -------- | -------- | -------- | -------- | 2570 | type | string | Yes| Event type, which has a fixed value of **p2pConnectionChange**.| 2571 | callback | Callback<[WifiP2pLinkedInfo](#wifip2plinkedinfo8)> | No| Callback for the P2P connection state change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| 2572 2573**Example** 2574```ts 2575import wifi from '@ohos.wifi'; 2576 2577let recvP2pConnectionChangeFunc = (result:wifi.WifiP2pLinkedInfo) => { 2578 console.info("Receive p2p connection change event: " + result); 2579} 2580 2581// Register an event. 2582wifi.on("p2pConnectionChange", recvP2pConnectionChangeFunc); 2583 2584// Unregister an event. 2585wifi.off("p2pConnectionChange", recvP2pConnectionChangeFunc); 2586``` 2587 2588## wifi.on('p2pDeviceChange')<sup>8+</sup> 2589 2590on(type: "p2pDeviceChange", callback: Callback<WifiP2pDevice>): void 2591 2592Subscribes to P2P device state changes. 2593 2594**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION 2595 2596**System capability**: SystemCapability.Communication.WiFi.P2P 2597 2598**Parameters** 2599 2600 | **Name**| **Type**| **Mandatory**| **Description**| 2601 | -------- | -------- | -------- | -------- | 2602 | type | string | Yes| Event type, which has a fixed value of **p2pDeviceChange**.| 2603 | callback | Callback<[WifiP2pDevice](#wifip2pdevice8)> | Yes| Callback invoked to return the P2P device state.| 2604 2605 2606## wifi.off('p2pDeviceChange')<sup>8+</sup> 2607 2608off(type: "p2pDeviceChange", callback?: Callback<WifiP2pDevice>): void 2609 2610Unsubscribes from P2P device state changes. 2611 2612**Required permissions**: ohos.permission.LOCATION 2613 2614**System capability**: SystemCapability.Communication.WiFi.P2P 2615 2616**Parameters** 2617 2618 | **Name**| **Type**| **Mandatory**| **Description**| 2619 | -------- | -------- | -------- | -------- | 2620 | type | string | Yes| Event type, which has a fixed value of **p2pDeviceChange**.| 2621 | callback | Callback<[WifiP2pDevice](#wifip2pdevice8)> | No| Callback for the P2P device state change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| 2622 2623**Example** 2624```ts 2625import wifi from '@ohos.wifi'; 2626 2627let recvP2pDeviceChangeFunc = (result:wifi.WifiP2pDevice) => { 2628 console.info("Receive p2p device change event: " + result); 2629} 2630 2631// Register an event. 2632wifi.on("p2pDeviceChange", recvP2pDeviceChangeFunc); 2633 2634// Unregister an event. 2635wifi.off("p2pDeviceChange", recvP2pDeviceChangeFunc); 2636``` 2637 2638## wifi.on('p2pPeerDeviceChange')<sup>8+</sup> 2639 2640on(type: "p2pPeerDeviceChange", callback: Callback<WifiP2pDevice[]>): void 2641 2642Subscribes to P2P peer device state changes. 2643 2644**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION 2645 2646**System capability**: SystemCapability.Communication.WiFi.P2P 2647 2648**Parameters** 2649 2650 | **Name**| **Type**| **Mandatory**| **Description**| 2651 | -------- | -------- | -------- | -------- | 2652 | type | string | Yes| Event type, which has a fixed value of **p2pPeerDeviceChange**.| 2653 | callback | Callback<[WifiP2pDevice[]](#wifip2pdevice8)> | Yes| Callback invoked to return the P2P peer device state.| 2654 2655 2656## wifi.off('p2pPeerDeviceChange')<sup>8+</sup> 2657 2658off(type: "p2pPeerDeviceChange", callback?: Callback<WifiP2pDevice[]>): void 2659 2660Unsubscribes from P2P peer device state changes. 2661 2662**Required permissions**: ohos.permission.LOCATION 2663 2664**System capability**: SystemCapability.Communication.WiFi.P2P 2665 2666**Parameters** 2667 2668 | **Name**| **Type**| **Mandatory**| **Description**| 2669 | -------- | -------- | -------- | -------- | 2670 | type | string | Yes| Event type, which has a fixed value of **p2pPeerDeviceChange**.| 2671 | callback | Callback<[WifiP2pDevice[]](#wifip2pdevice8)> | No| Callback for the peer device state change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| 2672 2673**Example** 2674```ts 2675import wifi from '@ohos.wifi'; 2676 2677let recvP2pPeerDeviceChangeFunc = (result:wifi.WifiP2pDevice[]) => { 2678 console.info("Receive p2p peer device change event: " + result); 2679} 2680 2681// Register an event. 2682wifi.on("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc); 2683 2684// Unregister an event. 2685wifi.off("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc); 2686``` 2687 2688## wifi.on('p2pPersistentGroupChange')<sup>8+</sup> 2689 2690on(type: "p2pPersistentGroupChange", callback: Callback<void>): void 2691 2692Subscribes to P2P persistent group state changes. 2693 2694**Required permissions**: ohos.permission.GET_WIFI_INFO 2695 2696**System capability**: SystemCapability.Communication.WiFi.P2P 2697 2698**Parameters** 2699 2700 | **Name**| **Type**| **Mandatory**| **Description**| 2701 | -------- | -------- | -------- | -------- | 2702 | type | string | Yes| Event type, which has a fixed value of **p2pPersistentGroupChange**.| 2703 | callback | Callback<void> | Yes| Callback invoked to return the P2P persistent group state.| 2704 2705 2706## wifi.off('p2pPersistentGroupChange')<sup>8+</sup> 2707 2708off(type: "p2pPersistentGroupChange", callback?: Callback<void>): void 2709 2710Unsubscribes from P2P persistent group state changes. 2711 2712**Required permissions**: ohos.permission.GET_WIFI_INFO 2713 2714**System capability**: SystemCapability.Communication.WiFi.P2P 2715 2716**Parameters** 2717 2718 | **Name**| **Type**| **Mandatory**| **Description**| 2719 | -------- | -------- | -------- | -------- | 2720 | type | string | Yes| Event type, which has a fixed value of **p2pPersistentGroupChange**.| 2721 | callback | Callback<void> | No| Callback for the P2P persistent group state change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| 2722 2723**Example** 2724```ts 2725import wifi from '@ohos.wifi'; 2726 2727let recvP2pPersistentGroupChangeFunc = (result:void) => { 2728 console.info("Receive p2p persistent group change event: " + result); 2729} 2730 2731// Register an event. 2732wifi.on("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc); 2733 2734// Unregister an event. 2735wifi.off("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc); 2736 2737``` 2738 2739## wifi.on('p2pDiscoveryChange')<sup>8+</sup> 2740 2741on(type: "p2pDiscoveryChange", callback: Callback<number>): void 2742 2743Subscribes to P2P device discovery state changes. 2744 2745**Required permissions**: ohos.permission.GET_WIFI_INFO 2746 2747**System capability**: SystemCapability.Communication.WiFi.P2P 2748 2749**Parameters** 2750 2751 | **Name**| **Type**| **Mandatory**| **Description**| 2752 | -------- | -------- | -------- | -------- | 2753 | type | string | Yes| Event type, which has a fixed value of **p2pDiscoveryChange**.| 2754 | callback | Callback<number> | Yes| Callback invoked to return the P2P device discovery state.| 2755 2756**P2P discovered device states** 2757 2758| **Value**| **Description**| 2759| -------- | -------- | 2760| 0 | Initial state.| 2761| 1 | Discovered.| 2762 2763 2764## wifi.off('p2pDiscoveryChange')<sup>8+</sup> 2765 2766off(type: "p2pDiscoveryChange", callback?: Callback<number>): void 2767 2768Unsubscribes from P2P device discovery state changes. 2769 2770**Required permissions**: ohos.permission.GET_WIFI_INFO 2771 2772**System capability**: SystemCapability.Communication.WiFi.P2P 2773 2774**Parameters** 2775 2776 | **Name**| **Type**| **Mandatory**| **Description**| 2777 | -------- | -------- | -------- | -------- | 2778 | type | string | Yes| Event type, which has a fixed value of **p2pDiscoveryChange**.| 2779 | callback | Callback<number> | No| Callback for the P2P device discovery state change. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.| 2780 2781**Example** 2782```ts 2783import wifi from '@ohos.wifi'; 2784 2785let recvP2pDiscoveryChangeFunc = (result:number) => { 2786 console.info("Receive p2p discovery change event: " + result); 2787} 2788 2789// Register an event. 2790wifi.on("p2pDiscoveryChange", recvP2pDiscoveryChangeFunc); 2791 2792// Unregister an event. 2793wifi.off("p2pDiscoveryChange", recvP2pDiscoveryChangeFunc); 2794``` 2795