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> The APIs of this module are no longer maintained since API version 9. You are advised to use [@ohos.wifiManager (WLAN)](js-apis-wifiManager.md). 9 10 11## Modules to Import 12 13```ts 14import wifi from '@ohos.wifi'; 15``` 16 17 18## wifi.isWifiActive 19 20isWifiActive(): boolean 21 22Checks whether WLAN is enabled. 23 24**Required permissions**: ohos.permission.GET_WIFI_INFO 25 26**System capability**: SystemCapability.Communication.WiFi.STA 27 28**Return value** 29 30 | **Type**| **Description**| 31 | -------- | -------- | 32 | boolean | Returns **true** if WLAN is enabled; returns **false** otherwise.| 33 34**Example** 35 36```ts 37import wifi from '@ohos.wifi'; 38 39try { 40 let isWifiActive = wifi.isWifiActive(); 41 console.info("isWifiActive:" + isWifiActive); 42}catch(error){ 43 console.error("failed:" + JSON.stringify(error)); 44} 45``` 46 47## wifi.scan 48 49scan(): boolean 50 51Starts a scan for WLAN. 52 53**Required permissions**: **ohos.permission.SET_WIFI_INFO** and **ohos.permission.LOCATION** 54 55**System capability**: SystemCapability.Communication.WiFi.STA 56 57**Return value** 58 59 | **Type**| **Description**| 60 | -------- | -------- | 61 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 62 63**Example** 64 65```ts 66import wifi from '@ohos.wifi'; 67 68try { 69 wifi.scan(); 70}catch(error){ 71 console.error("failed:" + JSON.stringify(error)); 72} 73``` 74 75## wifi.getScanInfos 76 77getScanInfos(): Promise<Array<WifiScanInfo>> 78 79Obtains the scan result. This API uses a promise to return the result. 80 81**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION or ohos.permission.GET_WIFI_PEERS_MAC ( 82available only for system applications) 83 84**System capability**: SystemCapability.Communication.WiFi.STA 85 86**Return value** 87 88 | **Type**| **Description**| 89 | -------- | -------- | 90 | Promise< Array<[WifiScanInfo](#wifiscaninfo)> > | Promise used to return the detected hotspots.| 91 92 93## wifi.getScanInfos 94 95getScanInfos(callback: AsyncCallback<Array<WifiScanInfo>>): void 96 97Obtains the scan result. This API uses an asynchronous callback to return the result. 98 99**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION or ohos.permission.GET_WIFI_PEERS_MAC ( 100available only for system applications) 101 102**System capability**: SystemCapability.Communication.WiFi.STA 103 104**Parameters** 105 106 | **Name**| **Type**| **Mandatory**| **Description**| 107 | -------- | -------- | -------- | -------- | 108 | callback | AsyncCallback< Array<[WifiScanInfo](#wifiscaninfo)>> | Yes| Callback used 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.| 109 110**Example** 111 112```ts 113import wifi from '@ohos.wifi'; 114 115wifi.getScanInfos((err, result) => { 116 if (err) { 117 console.error("get scan info error"); 118 return; 119 } 120 121 let len = result.length; 122 console.log("wifi received scan info: " + len); 123 for (let i = 0; i < len; ++i) { 124 console.info("ssid: " + result[i].ssid); 125 console.info("bssid: " + result[i].bssid); 126 console.info("capabilities: " + result[i].capabilities); 127 console.info("securityType: " + result[i].securityType); 128 console.info("rssi: " + result[i].rssi); 129 console.info("band: " + result[i].band); 130 console.info("frequency: " + result[i].frequency); 131 console.info("channelWidth: " + result[i].channelWidth); 132 console.info("timestamp: " + result[i].timestamp); 133 } 134}); 135 136wifi.getScanInfos().then(result => { 137 let len = result.length; 138 console.log("wifi received scan info: " + len); 139 for (let i = 0; i < len; ++i) { 140 console.info("ssid: " + result[i].ssid); 141 console.info("bssid: " + result[i].bssid); 142 console.info("capabilities: " + result[i].capabilities); 143 console.info("securityType: " + result[i].securityType); 144 console.info("rssi: " + result[i].rssi); 145 console.info("band: " + result[i].band); 146 console.info("frequency: " + result[i].frequency); 147 console.info("channelWidth: " + result[i].channelWidth); 148 console.info("timestamp: " + result[i].timestamp); 149 } 150}); 151``` 152 153 154## WifiScanInfo 155 156Represents WLAN hotspot information. 157 158**System capability**: SystemCapability.Communication.WiFi.STA 159 160 161| **Name**| **Type**| **Readable**| **Writable**| **Description**| 162| -------- | -------- | -------- | -------- | -------- | 163| ssid | string | Yes| No| Service set identifier (SSID) of the hotspot, in UTF-8 format. The maximum length is 32 bytes.| 164| bssid | string | Yes| No| Basic service set identifier (BSSID) of the hotspot, for example, **00:11:22:33:44:55**.| 165| capabilities | string | Yes| No| Hotspot capabilities.| 166| securityType | [WifiSecurityType](#wifisecuritytype) | Yes| No| WLAN security type.| 167| rssi | number | Yes| No| Received signal strength indicator (RSSI) of the hotspot, in dBm.| 168| band | number | Yes| No| Frequency band of the WLAN access point (AP).| 169| frequency | number | Yes| No| Frequency of the WLAN AP.| 170| channelWidth | number | Yes| No| Channel width of the WLAN AP.| 171| timestamp | number | Yes| No| Timestamp.| 172 173 174## WifiSecurityType 175 176Enumerates the WLAN security types. 177 178**System capability**: SystemCapability.Communication.WiFi.Core 179 180 181| **Name**| **Value**| **Description**| 182| -------- | -------- | -------- | 183| WIFI_SEC_TYPE_INVALID | 0 | Invalid security type.| 184| WIFI_SEC_TYPE_OPEN | 1 | Open security type.| 185| WIFI_SEC_TYPE_WEP | 2 | Wired Equivalent Privacy (WEP).| 186| WIFI_SEC_TYPE_PSK | 3 | Pre-shared key (PSK).| 187| WIFI_SEC_TYPE_SAE | 4 | Simultaneous Authentication of Equals (SAE).| 188 189 190 191## WifiDeviceConfig 192 193Represents the WLAN configuration. 194 195**System capability**: SystemCapability.Communication.WiFi.STA 196 197 198| **Name**| **Type**| **Readable**| **Writable**| **Description**| 199| -------- | -------- | -------- | -------- | -------- | 200| ssid | string | Yes| No| Service set identifier (SSID) of the hotspot, in UTF-8 format. The maximum length is 32 bytes.| 201| bssid | string | Yes| No| Hotspot BSSID, for example, **00:11:22:33:44:55**.| 202| preSharedKey | string | Yes| No| PSK of the hotspot. The maximum length is 64 bytes.| 203| isHiddenSsid | boolean | Yes| No| Whether the network is hidden.| 204| securityType | [WifiSecurityType](#wifisecuritytype) | Yes| No| Security type.| 205 206 207 208## wifi.addUntrustedConfig<sup>7+</sup> 209 210addUntrustedConfig(config: WifiDeviceConfig): Promise<boolean> 211 212Adds the configuration of an untrusted network. This API uses a promise to return the result. 213 214**Required permissions**: ohos.permission.SET_WIFI_INFO 215 216**System capability**: SystemCapability.Communication.WiFi.STA 217 218**Parameters** 219 220 | **Name**| **Type**| **Mandatory**| **Description**| 221 | -------- | -------- | -------- | -------- | 222 | config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration to add.| 223 224**Return value** 225 226 | **Type**| **Description**| 227 | -------- | -------- | 228 | Promise<boolean> | Promise used to return the result. The value **true** means the operation is successful; the value **false** means the opposite.| 229 230**Example** 231```ts 232import wifi from '@ohos.wifi'; 233 234try { 235 let config:wifi.WifiDeviceConfig = { 236 ssid : "****", 237 bssid: "****", 238 preSharedKey: "****", 239 isHiddenSsid: false, 240 securityType: 0, 241 creatorUid: 0, 242 disableReason: 0, 243 netId: 0, 244 randomMacType: 0, 245 randomMacAddr: "****", 246 ipType: 0, 247 staticIp: { 248 ipAddress: 0, 249 gateway: 0, 250 dnsServers: [], 251 domains: [] 252 } 253 } 254 wifi.addUntrustedConfig(config).then(result => { 255 console.info("result:" + JSON.stringify(result)); 256 }); 257}catch(error){ 258 console.error("failed:" + JSON.stringify(error)); 259} 260``` 261 262## wifi.addUntrustedConfig<sup>7+</sup> 263 264addUntrustedConfig(config: WifiDeviceConfig, callback: AsyncCallback<boolean>): void 265 266Adds the configuration of an untrusted network. This API uses an asynchronous callback to return the result. 267 268**Required permissions**: ohos.permission.SET_WIFI_INFO 269 270**System capability**: SystemCapability.Communication.WiFi.STA 271 272**Parameters** 273 274 | **Name**| **Type**| **Mandatory**| **Description**| 275 | -------- | -------- | -------- | -------- | 276 | config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration to add.| 277 | callback | AsyncCallback<boolean> | Yes| Callback used 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.| 278 279**Example** 280```ts 281import wifi from '@ohos.wifi'; 282 283try { 284 let config:wifi.WifiDeviceConfig = { 285 ssid : "****", 286 bssid: "****", 287 preSharedKey: "****", 288 isHiddenSsid: false, 289 securityType: 0, 290 creatorUid: 0, 291 disableReason: 0, 292 netId: 0, 293 randomMacType: 0, 294 randomMacAddr: "****", 295 ipType: 0, 296 staticIp: { 297 ipAddress: 0, 298 gateway: 0, 299 dnsServers: [], 300 domains: [] 301 } 302 } 303 wifi.addUntrustedConfig(config,(error,result) => { 304 console.info("result:" + JSON.stringify(result)); 305 }); 306}catch(error){ 307 console.error("failed:" + JSON.stringify(error)); 308} 309``` 310 311## wifi.removeUntrustedConfig<sup>7+</sup> 312 313removeUntrustedConfig(config: WifiDeviceConfig): Promise<boolean> 314 315Removes the configuration of an untrusted network. This API uses a promise to return the result. 316 317**Required permissions**: ohos.permission.SET_WIFI_INFO 318 319**System capability**: SystemCapability.Communication.WiFi.STA 320 321**Parameters** 322 323 | **Name**| **Type**| **Mandatory**| **Description**| 324 | -------- | -------- | -------- | -------- | 325 | config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration to add.| 326 327**Return value** 328 329 | **Type**| **Description**| 330 | -------- | -------- | 331 | Promise<boolean> | Promise used to return the result. The value **true** means the operation is successful; the value **false** means the opposite.| 332 333**Example** 334 335```ts 336import wifi from '@ohos.wifi'; 337 338try { 339 let config:wifi.WifiDeviceConfig = { 340 ssid : "****", 341 bssid: "****", 342 preSharedKey: "****", 343 isHiddenSsid: false, 344 securityType: 0, 345 creatorUid: 0, 346 disableReason: 0, 347 netId: 0, 348 randomMacType: 0, 349 randomMacAddr: "****", 350 ipType: 0, 351 staticIp: { 352 ipAddress: 0, 353 gateway: 0, 354 dnsServers: [], 355 domains: [] 356 } 357 } 358 wifi.removeUntrustedConfig(config).then(result => { 359 console.info("result:" + JSON.stringify(result)); 360 }); 361}catch(error){ 362 console.error("failed:" + JSON.stringify(error)); 363} 364``` 365 366 367## wifi.removeUntrustedConfig<sup>7+</sup> 368 369removeUntrustedConfig(config: WifiDeviceConfig, callback: AsyncCallback<boolean>): void 370 371Removes the configuration of an untrusted network. This API uses an asynchronous callback to return the result. 372 373**Required permissions**: ohos.permission.SET_WIFI_INFO 374 375**System capability**: SystemCapability.Communication.WiFi.STA 376 377**Parameters** 378 379 | **Name**| **Type**| **Mandatory**| **Description**| 380 | -------- | -------- | -------- | -------- | 381 | config | [WifiDeviceConfig](#wifideviceconfig) | Yes| WLAN configuration to add.| 382 | callback | AsyncCallback<boolean> | Yes| Callback used 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.| 383 384**Example** 385```ts 386import wifi from '@ohos.wifi'; 387 388try { 389 let config:wifi.WifiDeviceConfig = { 390 ssid : "****", 391 bssid: "****", 392 preSharedKey: "****", 393 isHiddenSsid: false, 394 securityType: 0, 395 creatorUid: 0, 396 disableReason: 0, 397 netId: 0, 398 randomMacType: 0, 399 randomMacAddr: "****", 400 ipType: 0, 401 staticIp: { 402 ipAddress: 0, 403 gateway: 0, 404 dnsServers: [], 405 domains: [] 406 } 407 } 408 wifi.removeUntrustedConfig(config,(error,result) => { 409 console.info("result:" + JSON.stringify(result)); 410 }); 411}catch(error){ 412 console.error("failed:" + JSON.stringify(error)); 413} 414``` 415 416 417## wifi.getSignalLevel 418 419getSignalLevel(rssi: number, band: number): number 420 421Obtains the WLAN signal level. 422 423**Required permissions**: ohos.permission.GET_WIFI_INFO 424 425**System capability**: SystemCapability.Communication.WiFi.STA 426 427**Parameters** 428 429 | **Name**| **Type**| **Mandatory**| **Description**| 430 | -------- | -------- | -------- | -------- | 431 | rssi | number | Yes| RSSI of the hotspot, in dBm.| 432 | band | number | Yes| Frequency band of the WLAN AP.| 433 434**Return value** 435 436 | **Type**| **Description**| 437 | -------- | -------- | 438 | number | Signal level obtained. The value range is [0, 4].| 439 440**Example** 441```ts 442import wifi from '@ohos.wifi'; 443 444try { 445 let rssi = 0; 446 let band = 0; 447 let level = wifi.getSignalLevel(rssi,band); 448 console.info("level:" + JSON.stringify(level)); 449}catch(error){ 450 console.error("failed:" + JSON.stringify(error)); 451} 452 453``` 454 455## wifi.getLinkedInfo 456 457getLinkedInfo(): Promise<WifiLinkedInfo> 458 459Obtains WLAN connection information. This API uses a promise to return the result. 460 461**Required permissions**: ohos.permission.GET_WIFI_INFO 462 463**System capability**: SystemCapability.Communication.WiFi.STA 464 465**Return value** 466 467 | Type| Description| 468 | -------- | -------- | 469 | Promise<[WifiLinkedInfo](#wifilinkedinfo)> | Promise used to return the WLAN connection information.| 470 471 472## wifi.getLinkedInfo 473 474getLinkedInfo(callback: AsyncCallback<WifiLinkedInfo>): void 475 476Obtains WLAN connection information. This API uses an asynchronous callback to return the result. 477 478**Required permissions**: ohos.permission.GET_WIFI_INFO 479 480**System capability**: SystemCapability.Communication.WiFi.STA 481 482**Parameters** 483 484 | Name| Type| Mandatory| Description| 485 | -------- | -------- | -------- | -------- | 486 | callback | AsyncCallback<[WifiLinkedInfo](#wifilinkedinfo)> | Yes| Callback used 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.| 487 488**Example** 489```ts 490import wifi from '@ohos.wifi'; 491 492wifi.getLinkedInfo((err, data:wifi.WifiLinkedInfo) => { 493 if (err) { 494 console.error("get linked info error"); 495 return; 496 } 497 console.info("get wifi linked info: " + JSON.stringify(data)); 498}); 499 500wifi.getLinkedInfo().then(data => { 501 console.info("get wifi linked info: " + JSON.stringify(data)); 502}).catch((error:number) => { 503 console.info("get linked info error"); 504}); 505``` 506 507 508## WifiLinkedInfo 509 510Represents the WLAN connection information. 511 512**System capability**: SystemCapability.Communication.WiFi.STA 513 514| Name| Type| Readable| Writable| Description| 515| -------- | -------- | -------- | -------- | -------- | 516| ssid | string | Yes| No| Service set identifier (SSID) of the hotspot, in UTF-8 format. The maximum length is 32 bytes.| 517| bssid | string | Yes| No| Hotspot BSSID, for example, **00:11:22:33:44:55**.| 518| rssi | number | Yes| No| Received signal strength indicator (RSSI) of the hotspot, in dBm.| 519| band | number | Yes| No| Frequency band of the WLAN AP.| 520| linkSpeed | number | Yes| No| Speed of the WLAN AP.| 521| frequency | number | Yes| No| Frequency of the WLAN AP.| 522| isHidden | boolean | Yes| No| Whether to hide the WLAN AP.| 523| isRestricted | boolean | Yes| No| Whether to restrict data volume at the WLAN AP.| 524| macAddress | string | Yes| No| MAC address of the device.| 525| ipAddress | number | Yes| No| IP address of the device that sets up the WLAN connection.| 526| connState | [ConnState](#connstate) | Yes| No| WLAN connection state.| 527 528 529## ConnState 530 531Enumerates the WLAN connection states. 532 533**System capability**: SystemCapability.Communication.WiFi.STA 534 535| Name| Value| Description| 536| -------- | -------- | -------- | 537| SCANNING | 0 | The device is scanning for available APs.| 538| CONNECTING | 1 | A WLAN connection is being established.| 539| AUTHENTICATING | 2 | An authentication is being performed for a WLAN connection.| 540| OBTAINING_IPADDR | 3 | The IP address of the WLAN connection is being acquired.| 541| CONNECTED | 4 | A WLAN connection is established.| 542| DISCONNECTING | 5 | The WLAN connection is being disconnected.| 543| DISCONNECTED | 6 | The WLAN connection is disconnected.| 544| UNKNOWN | 7 | Failed to set up the WLAN connection.| 545 546 547## wifi.isConnected<sup>7+</sup> 548 549isConnected(): boolean 550 551Checks whether the WLAN is connected. 552 553**Required permissions**: ohos.permission.GET_WIFI_INFO 554 555**System capability**: SystemCapability.Communication.WiFi.STA 556 557**Return value** 558 559 | **Type**| **Description**| 560 | -------- | -------- | 561 | boolean | Returns **true** if the WLAN is connected; returns **false** otherwise.| 562 563 564 565## wifi.isFeatureSupported<sup>7+</sup> 566 567isFeatureSupported(featureId: number): boolean 568 569Checks whether the device supports the specified WLAN feature. 570 571**Required permissions**: ohos.permission.GET_WIFI_INFO 572 573**System capability**: SystemCapability.Communication.WiFi.Core 574 575**Parameters** 576 577 578 | **Name**| **Type**| Mandatory| **Description**| 579 | -------- | -------- | -------- | -------- | 580 | featureId | number | Yes| Feature ID.| 581 582**Return value** 583 584 | **Type**| **Description**| 585 | -------- | -------- | 586 | boolean | Returns **true** if the feature is supported; returns **false** otherwise.| 587 588**Example** 589```ts 590import wifi from '@ohos.wifi'; 591 592try { 593 let featureId = 0; 594 let ret = wifi.isFeatureSupported(featureId); 595 console.info("isFeatureSupported:" + ret); 596}catch(error){ 597 console.error("failed:" + JSON.stringify(error)); 598} 599 600``` 601 602 603## wifi.getIpInfo<sup>7+</sup> 604 605getIpInfo(): IpInfo 606 607Obtains IP information. 608 609**Required permissions**: ohos.permission.GET_WIFI_INFO 610 611**System capability**: SystemCapability.Communication.WiFi.STA 612 613**Return value** 614 615 | **Type**| **Description**| 616 | -------- | -------- | 617 | [IpInfo](#ipinfo7) | IP information obtained.| 618 619**Example** 620```ts 621import wifi from '@ohos.wifi'; 622 623try { 624 let info = wifi.getIpInfo(); 625 console.info("info:" + JSON.stringify(info)); 626}catch(error){ 627 console.error("failed:" + JSON.stringify(error)); 628} 629``` 630 631## IpInfo<sup>7+</sup> 632 633Represents IP information. 634 635**System capability**: SystemCapability.Communication.WiFi.AP.Core 636 637| **Name**| **Type**| **Readable**| **Writable**| **Description**| 638| -------- | -------- | -------- | -------- | -------- | 639| ipAddress | number | Yes| No| IP address.| 640| gateway | number | Yes| No| Gateway.| 641| netmask | number | Yes| No| Subnet mask.| 642| primaryDns | number | Yes| No| IP address of the preferred DNS server.| 643| secondDns | number | Yes| No| IP address of the alternate DNS server.| 644| serverIp | number | Yes| No| IP address of the DHCP server.| 645| leaseDuration | number | Yes| No| Lease duration of the IP address.| 646 647 648## wifi.getCountryCode<sup>7+</sup> 649 650getCountryCode(): string 651 652Obtains the country code. 653 654**Required permissions**: ohos.permission.GET_WIFI_INFO 655 656**System capability**: SystemCapability.Communication.WiFi.Core 657 658**Return value** 659 660 | **Type**| **Description**| 661 | -------- | -------- | 662 | string | Country code obtained.| 663 664**Example** 665```ts 666import wifi from '@ohos.wifi'; 667 668try { 669 let code = wifi.getCountryCode(); 670 console.info("code:" + code); 671}catch(error){ 672 console.error("failed:" + JSON.stringify(error)); 673} 674``` 675 676 677## wifi.getP2pLinkedInfo<sup>8+</sup> 678 679getP2pLinkedInfo(): Promise<WifiP2pLinkedInfo> 680 681Obtains P2P link information. This API uses a promise to return the result. 682 683**Required permissions**: ohos.permission.GET_WIFI_INFO 684 685**System capability**: SystemCapability.Communication.WiFi.P2P 686 687**Return value** 688 689 | Type| Description| 690 | -------- | -------- | 691 | Promise<[WifiP2pLinkedInfo](#wifip2plinkedinfo8)> | Promise used to return the P2P link information.| 692 693 694 695## WifiP2pLinkedInfo<sup>8+</sup> 696 697Represents the P2P link information obtained. 698 699**System capability**: SystemCapability.Communication.WiFi.P2P 700 701| Name| Type| Readable| Writable| Description| 702| -------- | -------- | -------- | -------- | -------- | 703| connectState | [P2pConnectState](#p2pconnectstate8) | Yes| No| P2P connection state.| 704| isGroupOwner | boolean | Yes| No| Whether the device is the group owner.| 705| groupOwnerAddr | string | Yes| No| MAC address of the group. 706 707 708## P2pConnectState<sup>8+</sup> 709 710Enumerates the P2P connection states. 711 712**System capability**: SystemCapability.Communication.WiFi.P2P 713 714| Name| Value| Description| 715| -------- | -------- | -------- | 716| DISCONNECTED | 0 | Disconnected.| 717| CONNECTED | 1 | Connected.| 718 719 720## wifi.getP2pLinkedInfo<sup>8+</sup> 721 722getP2pLinkedInfo(callback: AsyncCallback<WifiP2pLinkedInfo>): void 723 724Obtains P2P link information. This API uses an asynchronous callback to return the result. 725 726**Required permissions**: ohos.permission.GET_WIFI_INFO 727 728**System capability**: SystemCapability.Communication.WiFi.P2P 729 730**Parameters** 731 732 | Name| Type| Mandatory| Description| 733 | -------- | -------- | -------- | -------- | 734 | callback | AsyncCallback<[WifiP2pLinkedInfo](#wifip2plinkedinfo8)> | Yes| Callback used 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.| 735 736**Example** 737```ts 738import wifi from '@ohos.wifi'; 739 740wifi.getP2pLinkedInfo((err, data:wifi.WifiP2pLinkedInfo) => { 741 if (err) { 742 console.error("get p2p linked info error"); 743 return; 744 } 745 console.info("get wifi p2p linked info: " + JSON.stringify(data)); 746}); 747 748wifi.getP2pLinkedInfo().then(data => { 749 console.info("get wifi p2p linked info: " + JSON.stringify(data)); 750}); 751``` 752 753## wifi.getCurrentGroup<sup>8+</sup> 754 755getCurrentGroup(): Promise<WifiP2pGroupInfo> 756 757Obtains the current P2P group information. This API uses a promise to return the result. 758 759**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION 760 761**System capability**: SystemCapability.Communication.WiFi.P2P 762 763**Return value** 764 765 | Type| Description| 766 | -------- | -------- | 767 | Promise<[WifiP2pGroupInfo](#wifip2pgroupinfo8)> | Promise used to return the P2P group information obtained.| 768 769 770## wifi.getCurrentGroup<sup>8+</sup> 771 772getCurrentGroup(callback: AsyncCallback<WifiP2pGroupInfo>): void 773 774Obtains the current P2P group information. This API uses an asynchronous callback to return the result. 775 776**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION 777 778**System capability**: SystemCapability.Communication.WiFi.P2P 779 780**Parameters** 781 782 | Name| Type| Mandatory| Description| 783 | -------- | -------- | -------- | -------- | 784 | callback | AsyncCallback<[WifiP2pGroupInfo](#wifip2pgroupinfo8)> | Yes| Callback used 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.| 785 786**Example** 787```ts 788import wifi from '@ohos.wifi'; 789 790wifi.getCurrentGroup((err, data:wifi.WifiP2pGroupInfo) => { 791 if (err) { 792 console.error("get current P2P group error"); 793 return; 794 } 795 console.info("get current P2P group: " + JSON.stringify(data)); 796}); 797 798wifi.getCurrentGroup().then(data => { 799 console.info("get current P2P group: " + JSON.stringify(data)); 800}); 801``` 802 803## wifi.getP2pPeerDevices<sup>8+</sup> 804 805getP2pPeerDevices(): Promise<WifiP2pDevice[]> 806 807Obtains the peer device list in the P2P connection. This API uses a promise to return the result. 808 809**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION 810 811**System capability**: SystemCapability.Communication.WiFi.P2P 812 813**Return value** 814 815 | Type| Description| 816 | -------- | -------- | 817 | Promise<[WifiP2pDevice[]](#wifip2pdevice8)> | Promise used to return the peer device list.| 818 819 820## wifi.getP2pPeerDevices<sup>8+</sup> 821 822getP2pPeerDevices(callback: AsyncCallback<WifiP2pDevice[]>): void 823 824Obtains the peer device list in the P2P connection. This API uses an asynchronous callback to return the result. 825 826**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION 827 828**System capability**: SystemCapability.Communication.WiFi.P2P 829 830**Parameters** 831 832 | Name| Type| Mandatory| Description| 833 | -------- | -------- | -------- | -------- | 834 | callback | AsyncCallback<[WifiP2pDevice[]](#wifip2pdevice8)> | Yes| Callback used 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.| 835 836**Example** 837```ts 838import wifi from '@ohos.wifi'; 839 840wifi.getP2pPeerDevices((err, data:wifi.WifiP2pDevice) => { 841 if (err) { 842 console.error("get P2P peer devices error"); 843 return; 844 } 845 console.info("get P2P peer devices: " + JSON.stringify(data)); 846}); 847 848wifi.getP2pPeerDevices().then(data => { 849 console.info("get P2P peer devices: " + JSON.stringify(data)); 850}); 851``` 852 853## WifiP2pDevice<sup>8+</sup> 854 855Represents the P2P device information. 856 857**System capability**: SystemCapability.Communication.WiFi.P2P 858 859| Name| Type| Readable| Writable| Description| 860| -------- | -------- | -------- | -------- | -------- | 861| deviceName | string | Yes| No| Device name.| 862| deviceAddress | string | Yes| No| MAC address of the device.| 863| primaryDeviceType | string | Yes| No| Type of the primary device.| 864| deviceStatus | [P2pDeviceStatus](#p2pdevicestatus8) | Yes| No| Device status.| 865| groupCapabilitys | number | Yes| No| Group capabilities.| 866 867 868## P2pDeviceStatus<sup>8+</sup> 869 870Enumerates the P2P device states. 871 872**System capability**: SystemCapability.Communication.WiFi.P2P 873 874| Name| Value| Description| 875| -------- | -------- | -------- | 876| CONNECTED | 0 | Connected.| 877| INVITED | 1 | Invited.| 878| FAILED | 2 | Failed.| 879| AVAILABLE | 3 | Available.| 880| UNAVAILABLE | 4 | Unavailable.| 881 882 883## wifi.createGroup<sup>8+</sup> 884 885createGroup(config: WifiP2PConfig): boolean 886 887Creates a P2P group. 888 889**Required permissions**: ohos.permission.GET_WIFI_INFO 890 891**System capability**: SystemCapability.Communication.WiFi.P2P 892 893**Parameters** 894 895 | **Name**| **Type**| Mandatory| **Description**| 896 | -------- | -------- | -------- | -------- | 897 | config | [WifiP2PConfig](#wifip2pconfig8) | Yes| Group configuration.| 898 899**Return value** 900 901 | Type| Description| 902 | -------- | -------- | 903 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 904 905**Example** 906```ts 907import wifi from '@ohos.wifi'; 908 909try { 910 let config:wifi.WifiP2PConfig = { 911 deviceAddress: "****", 912 netId: 0, 913 passphrase: "*****", 914 groupName: "****", 915 goBand: 0 916 } 917 wifi.createGroup(config); 918 919}catch(error){ 920 console.error("failed:" + JSON.stringify(error)); 921} 922``` 923 924## WifiP2PConfig<sup>8+</sup> 925 926Represents P2P group configuration. 927 928**System capability**: SystemCapability.Communication.WiFi.P2P 929 930| Name| Type| Readable| Writable| Description| 931| -------- | -------- | -------- | -------- | -------- | 932| deviceAddress | string | Yes| No| Device address.| 933| netId | number | Yes| No| Network ID. The value **-1** indicates a temporary group, and **-2** indicates a persistent group.| 934| passphrase | string | Yes| No| Passphrase of the group.| 935| groupName | string | Yes| No| Group name.| 936| goBand | [GroupOwnerBand](#groupownerband8) | Yes| No| Frequency band of the group.| 937 938 939## GroupOwnerBand<sup>8+</sup> 940 941Enumerates the P2P group frequency bands. 942 943**System capability**: SystemCapability.Communication.WiFi.P2P 944 945| Name| Value| Description| 946| -------- | -------- | -------- | 947| GO_BAND_AUTO | 0 | Auto.| 948| GO_BAND_2GHZ | 1 | 2 GHz.| 949| GO_BAND_5GHZ | 2 | 5 GHz.| 950 951 952## wifi.removeGroup<sup>8+</sup> 953 954removeGroup(): boolean 955 956Removes this P2P group. 957 958**Required permissions**: ohos.permission.GET_WIFI_INFO 959 960**System capability**: SystemCapability.Communication.WiFi.P2P 961 962**Return value** 963 964 | Type| Description| 965 | -------- | -------- | 966 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 967 968**Example** 969```ts 970import wifi from '@ohos.wifi'; 971 972try { 973 wifi.removeGroup(); 974}catch(error){ 975 console.error("failed:" + JSON.stringify(error)); 976} 977``` 978 979## wifi.p2pConnect<sup>8+</sup> 980 981p2pConnect(config: WifiP2PConfig): boolean 982 983Sets up a P2P connection. 984 985**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION 986 987**System capability**: SystemCapability.Communication.WiFi.P2P 988 989**Parameters** 990 991 992 | **Name**| **Type**| Mandatory| **Description**| 993 | -------- | -------- | -------- | -------- | 994 | config | [WifiP2PConfig](#wifip2pconfig8) | Yes| P2P group configuration.| 995 996**Return value** 997 998 | Type| Description| 999 | -------- | -------- | 1000 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1001 1002 1003**Example** 1004```ts 1005import wifi from '@ohos.wifi'; 1006 1007let recvP2pConnectionChangeFunc = (result:wifi.WifiP2pLinkedInfo) => { 1008 console.info("p2p connection change receive event: " + JSON.stringify(result)); 1009 wifi.getP2pLinkedInfo((err, data:wifi.WifiP2pLinkedInfo) => { 1010 if (err) { 1011 console.error('failed to get getP2pLinkedInfo: ' + JSON.stringify(err)); 1012 return; 1013 } 1014 console.info("get getP2pLinkedInfo: " + JSON.stringify(data)); 1015 }); 1016} 1017wifi.on("p2pConnectionChange", recvP2pConnectionChangeFunc); 1018 1019let recvP2pDeviceChangeFunc = (result:wifi.WifiP2pDevice) => { 1020 console.info("p2p device change receive event: " + JSON.stringify(result)); 1021} 1022wifi.on("p2pDeviceChange", recvP2pDeviceChangeFunc); 1023 1024let recvP2pPeerDeviceChangeFunc = (result:wifi.WifiP2pDevice[]) => { 1025 console.info("p2p peer device change receive event: " + JSON.stringify(result)); 1026 wifi.getP2pPeerDevices((err, data:wifi.WifiP2pDevice) => { 1027 if (err) { 1028 console.error('failed to get peer devices: ' + JSON.stringify(err)); 1029 return; 1030 } 1031 console.info("get peer devices: " + JSON.stringify(data)); 1032 let len = data.length; 1033 for (let i = 0; i < len; ++i) { 1034 if (data[i].deviceName === "my_test_device") { 1035 console.info("p2p connect to test device: " + data[i].deviceAddress); 1036 let config:wifi.WifiP2PConfig = { 1037 deviceAddress:data[i].deviceAddress, 1038 netId:-2, 1039 passphrase:"", 1040 groupName:"", 1041 goBand:0, 1042 } 1043 wifi.p2pConnect(config); 1044 } 1045 } 1046 }); 1047} 1048wifi.on("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc); 1049 1050let recvP2pPersistentGroupChangeFunc = () => { 1051 console.info("p2p persistent group change receive event"); 1052 1053 wifi.getCurrentGroup((err, data:wifi.WifiP2pGroupInfo) => { 1054 if (err) { 1055 console.error('failed to get current group: ' + JSON.stringify(err)); 1056 return; 1057 } 1058 console.info("get current group: " + JSON.stringify(data)); 1059 }); 1060} 1061wifi.on("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc); 1062 1063setTimeout(() => {wifi.off("p2pConnectionChange", recvP2pConnectionChangeFunc);}, 125 * 1000); 1064setTimeout(() => {wifi.off("p2pDeviceChange", recvP2pDeviceChangeFunc);}, 125 * 1000); 1065setTimeout(() => {wifi.off("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc);}, 125 * 1000); 1066setTimeout(() => {wifi.off("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc);}, 125 * 1000); 1067console.info("start discover devices -> " + wifi.startDiscoverDevices()); 1068``` 1069 1070## wifi.p2pCancelConnect<sup>8+</sup> 1071 1072p2pCancelConnect(): boolean 1073 1074Cancels this P2P connection. 1075 1076**Required permissions**: ohos.permission.GET_WIFI_INFO 1077 1078**System capability**: SystemCapability.Communication.WiFi.P2P 1079 1080**Return value** 1081 1082 | Type| Description| 1083 | -------- | -------- | 1084 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1085 1086**Example** 1087```ts 1088import wifi from '@ohos.wifi'; 1089 1090try { 1091 wifi.p2pCancelConnect(); 1092}catch(error){ 1093 console.error("failed:" + JSON.stringify(error)); 1094} 1095``` 1096 1097## wifi.startDiscoverDevices<sup>8+</sup> 1098 1099startDiscoverDevices(): boolean 1100 1101Starts to discover devices. 1102 1103**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION 1104 1105**System capability**: SystemCapability.Communication.WiFi.P2P 1106 1107**Return value** 1108 1109 | Type| Description| 1110 | -------- | -------- | 1111 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1112 1113**Example** 1114```ts 1115import wifi from '@ohos.wifi'; 1116 1117try { 1118 wifi.startDiscoverDevices(); 1119}catch(error){ 1120 console.error("failed:" + JSON.stringify(error)); 1121} 1122``` 1123 1124## wifi.stopDiscoverDevices<sup>8+</sup> 1125 1126stopDiscoverDevices(): boolean 1127 1128Stops discovering devices. 1129 1130**Required permissions**: ohos.permission.GET_WIFI_INFO 1131 1132**System capability**: SystemCapability.Communication.WiFi.P2P 1133 1134**Return value** 1135 1136 | Type| Description| 1137 | -------- | -------- | 1138 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1139 1140**Example** 1141```ts 1142import wifi from '@ohos.wifi'; 1143 1144try { 1145 wifi.stopDiscoverDevices(); 1146}catch(error){ 1147 console.error("failed:" + JSON.stringify(error)); 1148} 1149``` 1150 1151## WifiP2pGroupInfo<sup>8+</sup> 1152 1153Represents the P2P group information. 1154 1155**System capability**: SystemCapability.Communication.WiFi.P2P 1156 1157| Name| Type| Readable| Writable| Description| 1158| -------- | -------- | -------- | -------- | -------- | 1159| isP2pGo | boolean | Yes| No| Whether the device is the group owner.| 1160| ownerInfo | [WifiP2pDevice](#wifip2pdevice8) | Yes| No| Device information of the group.| 1161| passphrase | string | Yes| No| Passphrase of the group.| 1162| interface | string | Yes| No| Interface name.| 1163| groupName | string | Yes| No| Group name.| 1164| networkId | number | Yes| No| Network ID.| 1165| frequency | number | Yes| No| Frequency of the group.| 1166| clientDevices | [WifiP2pDevice[]](#wifip2pdevice8) | Yes| No| List of connected devices.| 1167| goIpAddress | string | Yes| No| IP address of the group.| 1168 1169 1170 1171## wifi.on('wifiStateChange')<sup>7+</sup> 1172 1173on(type: 'wifiStateChange', callback: Callback<number>): void 1174 1175Subscribes to WLAN state changes. 1176 1177**Required permissions**: ohos.permission.GET_WIFI_INFO 1178 1179**System capability**: SystemCapability.Communication.WiFi.STA 1180 1181**Parameters** 1182 1183 | **Name**| **Type**| **Mandatory**| **Description**| 1184 | -------- | -------- | -------- | -------- | 1185 | type | string | Yes| Event type, which has a fixed value of **wifiStateChange**.| 1186 | callback | Callback<number> | Yes| Callback used to return the WLAN state.| 1187 1188**WLAN states** 1189 1190| **Value**| **Description**| 1191| -------- | -------- | 1192| 0 | Deactivated| 1193| 1 | Activated| 1194| 2 | Activating| 1195| 3 | Deactivating| 1196 1197 1198## wifi.off('wifiStateChange')<sup>7+</sup> 1199 1200off(type: 'wifiStateChange', callback?: Callback<number>): void 1201 1202Unsubscribes from WLAN state changes. 1203 1204**Required permissions**: ohos.permission.GET_WIFI_INFO 1205 1206**System capability**: SystemCapability.Communication.WiFi.STA 1207 1208**Parameters** 1209 1210 | **Name**| **Type**| **Mandatory**| **Description**| 1211 | -------- | -------- | -------- | -------- | 1212 | type | string | Yes| Event type, which has a fixed value of **wifiStateChange**.| 1213 | callback | Callback<number> | No| Callback to unregister. If this parameter is not specified, all callbacks for the specified event will be unregistered.| 1214 1215**Example** 1216```ts 1217import wifi from '@ohos.wifi'; 1218 1219let recvPowerNotifyFunc = (result:number) => { 1220 console.info("Receive power state change event: " + result); 1221} 1222 1223// Register an event. 1224wifi.on("wifiStateChange", recvPowerNotifyFunc); 1225 1226// Unregister an event. 1227wifi.off("wifiStateChange", recvPowerNotifyFunc); 1228``` 1229 1230 1231## wifi.on('wifiConnectionChange')<sup>7+</sup> 1232 1233on(type: 'wifiConnectionChange', callback: Callback<number>): void 1234 1235Subscribes to WLAN connection state changes. 1236 1237**Required permissions**: ohos.permission.GET_WIFI_INFO 1238 1239**System capability**: SystemCapability.Communication.WiFi.STA 1240 1241**Parameters** 1242 1243 | **Name**| **Type**| **Mandatory**| **Description**| 1244 | -------- | -------- | -------- | -------- | 1245 | type | string | Yes| Event type, which has a fixed value of **wifiConnectionChange**.| 1246 | callback | Callback<number> | Yes| Callback used to return the WLAN connection state.| 1247 1248**WLAN connection states** 1249 1250| **Value**| **Description**| 1251| -------- | -------- | 1252| 0 | Disconnected.| 1253| 1 | Connected.| 1254 1255 1256## wifi.off('wifiConnectionChange')<sup>7+</sup> 1257 1258off(type: 'wifiConnectionChange', callback?: Callback<number>): void 1259 1260Unsubscribes from WLAN connection state changes. 1261 1262**Required permissions**: ohos.permission.GET_WIFI_INFO 1263 1264**System capability**: SystemCapability.Communication.WiFi.STA 1265 1266**Parameters** 1267 1268 | **Name**| **Type**| **Mandatory**| **Description**| 1269 | -------- | -------- | -------- | -------- | 1270 | type | string | Yes| Event type, which has a fixed value of **wifiConnectionChange**.| 1271 | callback | Callback<number> | No| Callback to unregister. If this parameter is not specified, all callbacks for the specified event will be unregistered.| 1272 1273**Example** 1274```ts 1275import wifi from '@ohos.wifi'; 1276 1277let recvWifiConnectionChangeFunc = (result:number) => { 1278 console.info("Receive wifi connection change event: " + result); 1279} 1280 1281// Register an event. 1282wifi.on("wifiConnectionChange", recvWifiConnectionChangeFunc); 1283 1284// Unregister an event. 1285wifi.off("wifiConnectionChange", recvWifiConnectionChangeFunc); 1286``` 1287 1288## wifi.on('wifiScanStateChange')<sup>7+</sup> 1289 1290on(type: 'wifiScanStateChange', callback: Callback<number>): void 1291 1292Subscribes to WLAN scan state changes. 1293 1294**Required permissions**: ohos.permission.GET_WIFI_INFO 1295 1296**System capability**: SystemCapability.Communication.WiFi.STA 1297 1298**Parameters** 1299 1300 | **Name**| **Type**| **Mandatory**| **Description**| 1301 | -------- | -------- | -------- | -------- | 1302 | type | string | Yes| Event type, which has a fixed value of **wifiScanStateChange**.| 1303 | callback | Callback<number> | Yes| Callback used to return the WLAN scan state.| 1304 1305**WLAN scan states** 1306 1307| **Value**| **Description**| 1308| -------- | -------- | 1309| 0 | Scan failed.| 1310| 1 | Scan successful.| 1311 1312 1313## wifi.off('wifiScanStateChange')<sup>7+</sup> 1314 1315off(type: 'wifiScanStateChange', callback?: Callback<number>): void 1316 1317Unsubscribes from WLAN scan state changes. 1318 1319**Required permissions**: ohos.permission.GET_WIFI_INFO 1320 1321**System capability**: SystemCapability.Communication.WiFi.STA 1322 1323**Parameters** 1324 1325| **Name**| **Type**| **Mandatory**| **Description**| 1326| -------- | -------- | -------- | -------- | 1327| type | string | Yes| Event type, which has a fixed value of **wifiScanStateChange**.| 1328| callback | Callback<number> | No| Callback to unregister. If this parameter is not specified, all callbacks for the specified event will be unregistered.| 1329 1330**Example** 1331```ts 1332import wifi from '@ohos.wifi'; 1333 1334let recvWifiScanStateChangeFunc = (result:number) => { 1335 console.info("Receive Wifi scan state change event: " + result); 1336} 1337 1338// Register an event. 1339wifi.on("wifiScanStateChange", recvWifiScanStateChangeFunc); 1340 1341// Unregister an event. 1342wifi.off("wifiScanStateChange", recvWifiScanStateChangeFunc); 1343``` 1344 1345## wifi.on('wifiRssiChange')<sup>7+</sup> 1346 1347on(type: 'wifiRssiChange', callback: Callback<number>): void 1348 1349Subscribes to RSSI changes. 1350 1351**Required permissions**: ohos.permission.GET_WIFI_INFO 1352 1353**System capability**: SystemCapability.Communication.WiFi.STA 1354 1355**Parameters** 1356 1357 | **Name**| **Type**| **Mandatory**| **Description**| 1358 | -------- | -------- | -------- | -------- | 1359 | type | string | Yes| Event type, which has a fixed value of **wifiRssiChange**.| 1360 | callback | Callback<number> | Yes| Callback used to return the RSSI, in dBm.| 1361 1362 1363## wifi.off('wifiRssiChange')<sup>7+</sup> 1364 1365off(type: 'wifiRssiChange', callback?: Callback<number>): void 1366 1367Unsubscribes from RSSI changes. 1368 1369**Required permissions**: ohos.permission.GET_WIFI_INFO 1370 1371**System capability**: SystemCapability.Communication.WiFi.STA 1372 1373**Parameters** 1374 1375 | **Name**| **Type**| **Mandatory**| **Description**| 1376 | -------- | -------- | -------- | -------- | 1377 | type | string | Yes| Event type, which has a fixed value of **wifiRssiChange**.| 1378 | callback | Callback<number> | No| Callback to unregister. If this parameter is not specified, all callbacks for the specified event will be unregistered.| 1379 1380**Example** 1381```ts 1382import wifi from '@ohos.wifi'; 1383 1384let recvWifiRssiChangeFunc = (result:number) => { 1385 console.info("Receive wifi rssi change event: " + result); 1386} 1387 1388// Register an event. 1389wifi.on("wifiRssiChange", recvWifiRssiChangeFunc); 1390 1391// Unregister an event. 1392wifi.off("wifiRssiChange", recvWifiRssiChangeFunc); 1393 1394``` 1395 1396 1397## wifi.on('hotspotStateChange')<sup>7+</sup> 1398 1399on(type: 'hotspotStateChange', callback: Callback<number>): void 1400 1401Subscribes to hotspot state changes. 1402 1403**Required permissions**: ohos.permission.GET_WIFI_INFO 1404 1405**System capability**: SystemCapability.Communication.WiFi.AP.Core 1406 1407**Parameters** 1408 1409 | **Name**| **Type**| **Mandatory**| **Description**| 1410 | -------- | -------- | -------- | -------- | 1411 | type | string | Yes| Event type, which has a fixed value of **hotspotStateChange**.| 1412 | callback | Callback<number> | Yes| Callback used to return the hotspot state.| 1413 1414**Hotspot states** 1415 1416| **Value**| **Description**| 1417| -------- | -------- | 1418| 0 | Deactivated| 1419| 1 | Activated| 1420| 2 | Activating| 1421| 3 | Deactivating| 1422 1423**Example** 1424```ts 1425import wifi from '@ohos.wifi'; 1426 1427let recvHotspotStateChangeFunc = (result:number) => { 1428 console.info("Receive hotspot state change event: " + result); 1429} 1430 1431// Register an event. 1432wifi.on("hotspotStateChange", recvHotspotStateChangeFunc); 1433 1434// Unregister an event. 1435wifi.off("hotspotStateChange", recvHotspotStateChangeFunc); 1436``` 1437 1438## wifi.off('hotspotStateChange')<sup>7+</sup> 1439 1440off(type: 'hotspotStateChange', callback?: Callback<number>): void 1441 1442Unsubscribes from hotspot state changes. 1443 1444**Required permissions**: ohos.permission.GET_WIFI_INFO 1445 1446**System capability**: SystemCapability.Communication.WiFi.AP.Core 1447 1448**Parameters** 1449 1450 | **Name**| **Type**| **Mandatory**| **Description**| 1451 | -------- | -------- | -------- | -------- | 1452 | type | string | Yes| Event type, which has a fixed value of **hotspotStateChange**.| 1453 | callback | Callback<number> | No| Callback to unregister. If this parameter is not specified, all callbacks for the specified event will be unregistered.| 1454 1455 1456 1457## wifi.on('p2pStateChange')<sup>8+</sup> 1458 1459on(type: 'p2pStateChange', callback: Callback<number>): void 1460 1461Subscribes to P2P state changes. 1462 1463**Required permissions**: ohos.permission.GET_WIFI_INFO 1464 1465**System capability**: SystemCapability.Communication.WiFi.P2P 1466 1467**Parameters** 1468 1469 | **Name**| **Type**| **Mandatory**| **Description**| 1470 | -------- | -------- | -------- | -------- | 1471 | type | string | Yes| Event type, which has a fixed value of **p2pStateChange**.| 1472 | callback | Callback<number> | Yes| Callback used to return the P2P state.| 1473 1474**P2P states** 1475 1476| **Value**| **Description**| 1477| -------- | -------- | 1478| 1 | Available| 1479| 2 | Opening| 1480| 3 | Opened| 1481| 4 | Closing| 1482| 5 | Closed| 1483 1484## wifi.off('p2pStateChange')<sup>8+</sup> 1485 1486off(type: 'p2pStateChange', callback?: Callback<number>): void 1487 1488Unsubscribes from P2P state changes. 1489 1490**Required permissions**: ohos.permission.GET_WIFI_INFO 1491 1492**System capability**: SystemCapability.Communication.WiFi.P2P 1493 1494**Parameters** 1495 1496 | **Name**| **Type**| **Mandatory**| **Description**| 1497 | -------- | -------- | -------- | -------- | 1498 | type | string | Yes| Event type, which has a fixed value of **p2pStateChange**.| 1499 | callback | Callback<number> | No| Callback to unregister. If this parameter is not specified, all callbacks for the specified event will be unregistered.| 1500 1501**Example** 1502```ts 1503import wifi from '@ohos.wifi'; 1504 1505let recvP2pStateChangeFunc = (result:number) => { 1506 console.info("Receive p2p state change event: " + result); 1507} 1508 1509// Register an event. 1510wifi.on("p2pStateChange", recvP2pStateChangeFunc); 1511 1512// Unregister an event. 1513wifi.off("p2pStateChange", recvP2pStateChangeFunc); 1514``` 1515 1516## wifi.on('p2pConnectionChange')<sup>8+</sup> 1517 1518on(type: 'p2pConnectionChange', callback: Callback<WifiP2pLinkedInfo>): void 1519 1520Subscribes to P2P connection state changes. 1521 1522**Required permissions**: ohos.permission.GET_WIFI_INFO 1523 1524**System capability**: SystemCapability.Communication.WiFi.P2P 1525 1526**Parameters** 1527 1528 | **Name**| **Type**| **Mandatory**| **Description**| 1529 | -------- | -------- | -------- | -------- | 1530 | type | string | Yes| Event type, which has a fixed value of **p2pConnectionChange**.| 1531 | callback | Callback<[WifiP2pLinkedInfo](#wifip2plinkedinfo8)> | Yes| Callback used to return the P2P connection state.| 1532 1533 1534## wifi.off('p2pConnectionChange')<sup>8+</sup> 1535 1536off(type: 'p2pConnectionChange', callback?: Callback<WifiP2pLinkedInfo>): void 1537 1538Unsubscribes from P2P connection state changes. 1539 1540**Required permissions**: ohos.permission.GET_WIFI_INFO 1541 1542**System capability**: SystemCapability.Communication.WiFi.P2P 1543 1544**Parameters** 1545 1546 | **Name**| **Type**| **Mandatory**| **Description**| 1547 | -------- | -------- | -------- | -------- | 1548 | type | string | Yes| Event type, which has a fixed value of **p2pConnectionChange**.| 1549 | callback | Callback<[WifiP2pLinkedInfo](#wifip2plinkedinfo8)> | No| Callback to unregister. If this parameter is not specified, all callbacks for the specified event will be unregistered.| 1550 1551**Example** 1552```ts 1553import wifi from '@ohos.wifi'; 1554 1555let recvP2pConnectionChangeFunc = (result:wifi.WifiP2pLinkedInfo) => { 1556 console.info("Receive p2p connection change event: " + result); 1557} 1558 1559// Register an event. 1560wifi.on("p2pConnectionChange", recvP2pConnectionChangeFunc); 1561 1562// Unregister an event. 1563wifi.off("p2pConnectionChange", recvP2pConnectionChangeFunc); 1564``` 1565 1566## wifi.on('p2pDeviceChange')<sup>8+</sup> 1567 1568on(type: 'p2pDeviceChange', callback: Callback<WifiP2pDevice>): void 1569 1570Subscribes to P2P device state changes. 1571 1572**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION 1573 1574**System capability**: SystemCapability.Communication.WiFi.P2P 1575 1576**Parameters** 1577 1578 | **Name**| **Type**| **Mandatory**| **Description**| 1579 | -------- | -------- | -------- | -------- | 1580 | type | string | Yes| Event type, which has a fixed value of **p2pDeviceChange**.| 1581 | callback | Callback<[WifiP2pDevice](#wifip2pdevice8)> | Yes| Callback used to return the P2P device state.| 1582 1583 1584## wifi.off('p2pDeviceChange')<sup>8+</sup> 1585 1586off(type: 'p2pDeviceChange', callback?: Callback<WifiP2pDevice>): void 1587 1588Unsubscribes from P2P device state changes. 1589 1590**Required permissions**: ohos.permission.LOCATION 1591 1592**System capability**: SystemCapability.Communication.WiFi.P2P 1593 1594**Parameters** 1595 1596 | **Name**| **Type**| **Mandatory**| **Description**| 1597 | -------- | -------- | -------- | -------- | 1598 | type | string | Yes| Event type, which has a fixed value of **p2pDeviceChange**.| 1599 | callback | Callback<[WifiP2pDevice](#wifip2pdevice8)> | No| Callback to unregister. If this parameter is not specified, all callbacks for the specified event will be unregistered.| 1600 1601**Example** 1602```ts 1603import wifi from '@ohos.wifi'; 1604 1605let recvP2pDeviceChangeFunc = (result:wifi.WifiP2pDevice) => { 1606 console.info("Receive p2p device change event: " + result); 1607} 1608 1609// Register an event. 1610wifi.on("p2pDeviceChange", recvP2pDeviceChangeFunc); 1611 1612// Unregister an event. 1613wifi.off("p2pDeviceChange", recvP2pDeviceChangeFunc); 1614``` 1615 1616## wifi.on('p2pPeerDeviceChange')<sup>8+</sup> 1617 1618on(type: 'p2pPeerDeviceChange', callback: Callback<WifiP2pDevice[]>): void 1619 1620Subscribes to P2P peer device state changes. 1621 1622**Required permissions**: ohos.permission.GET_WIFI_INFO and ohos.permission.LOCATION 1623 1624**System capability**: SystemCapability.Communication.WiFi.P2P 1625 1626**Parameters** 1627 1628 | **Name**| **Type**| **Mandatory**| **Description**| 1629 | -------- | -------- | -------- | -------- | 1630 | type | string | Yes| Event type, which has a fixed value of **p2pPeerDeviceChange**.| 1631 | callback | Callback<[WifiP2pDevice[]](#wifip2pdevice8)> | Yes| Callback used to return the P2P peer device state.| 1632 1633 1634## wifi.off('p2pPeerDeviceChange')<sup>8+</sup> 1635 1636off(type: 'p2pPeerDeviceChange', callback?: Callback<WifiP2pDevice[]>): void 1637 1638Unsubscribes from P2P peer device state changes. 1639 1640**Required permissions**: ohos.permission.LOCATION 1641 1642**System capability**: SystemCapability.Communication.WiFi.P2P 1643 1644**Parameters** 1645 1646 | **Name**| **Type**| **Mandatory**| **Description**| 1647 | -------- | -------- | -------- | -------- | 1648 | type | string | Yes| Event type, which has a fixed value of **p2pPeerDeviceChange**.| 1649 | callback | Callback<[WifiP2pDevice[]](#wifip2pdevice8)> | No| Callback to unregister. If this parameter is not specified, all callbacks for the specified event will be unregistered.| 1650 1651**Example** 1652```ts 1653import wifi from '@ohos.wifi'; 1654 1655let recvP2pPeerDeviceChangeFunc = (result:wifi.WifiP2pDevice[]) => { 1656 console.info("Receive p2p peer device change event: " + result); 1657} 1658 1659// Register an event. 1660wifi.on("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc); 1661 1662// Unregister an event. 1663wifi.off("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc); 1664``` 1665 1666## wifi.on('p2pPersistentGroupChange')<sup>8+</sup> 1667 1668on(type: 'p2pPersistentGroupChange', callback: Callback<void>): void 1669 1670Subscribes to P2P persistent group state changes. 1671 1672**Required permissions**: ohos.permission.GET_WIFI_INFO 1673 1674**System capability**: SystemCapability.Communication.WiFi.P2P 1675 1676**Parameters** 1677 1678 | **Name**| **Type**| **Mandatory**| **Description**| 1679 | -------- | -------- | -------- | -------- | 1680 | type | string | Yes| Event type, which has a fixed value of **p2pPersistentGroupChange**.| 1681 | callback | Callback<void> | Yes| Callback used to return the P2P persistent group state.| 1682 1683 1684## wifi.off('p2pPersistentGroupChange')<sup>8+</sup> 1685 1686off(type: 'p2pPersistentGroupChange', callback?: Callback<void>): void 1687 1688Unsubscribes from P2P persistent group state changes. 1689 1690**Required permissions**: ohos.permission.GET_WIFI_INFO 1691 1692**System capability**: SystemCapability.Communication.WiFi.P2P 1693 1694**Parameters** 1695 1696 | **Name**| **Type**| **Mandatory**| **Description**| 1697 | -------- | -------- | -------- | -------- | 1698 | type | string | Yes| Event type, which has a fixed value of **p2pPersistentGroupChange**.| 1699 | callback | Callback<void> | No| Callback to unregister. If this parameter is not specified, all callbacks for the specified event will be unregistered.| 1700 1701**Example** 1702```ts 1703import wifi from '@ohos.wifi'; 1704 1705let recvP2pPersistentGroupChangeFunc = (result:void) => { 1706 console.info("Receive p2p persistent group change event: " + result); 1707} 1708 1709// Register an event. 1710wifi.on("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc); 1711 1712// Unregister an event. 1713wifi.off("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc); 1714 1715``` 1716 1717## wifi.on('p2pDiscoveryChange')<sup>8+</sup> 1718 1719on(type: 'p2pDiscoveryChange', callback: Callback<number>): void 1720 1721Subscribes to P2P device discovery state changes. 1722 1723**Required permissions**: ohos.permission.GET_WIFI_INFO 1724 1725**System capability**: SystemCapability.Communication.WiFi.P2P 1726 1727**Parameters** 1728 1729 | **Name**| **Type**| **Mandatory**| **Description**| 1730 | -------- | -------- | -------- | -------- | 1731 | type | string | Yes| Event type, which has a fixed value of **p2pDiscoveryChange**.| 1732 | callback | Callback<number> | Yes| Callback used to return the P2P device discovery state.| 1733 1734**P2P discovered device states** 1735 1736| **Value**| **Description**| 1737| -------- | -------- | 1738| 0 | Initial state.| 1739| 1 | Discovered.| 1740 1741 1742## wifi.off('p2pDiscoveryChange')<sup>8+</sup> 1743 1744off(type: 'p2pDiscoveryChange', callback?: Callback<number>): void 1745 1746Unsubscribes from P2P device discovery state changes. 1747 1748**Required permissions**: ohos.permission.GET_WIFI_INFO 1749 1750**System capability**: SystemCapability.Communication.WiFi.P2P 1751 1752**Parameters** 1753 1754 | **Name**| **Type**| **Mandatory**| **Description**| 1755 | -------- | -------- | -------- | -------- | 1756 | type | string | Yes| Event type, which has a fixed value of **p2pDiscoveryChange**.| 1757 | callback | Callback<number> | No| Callback to unregister. If this parameter is not specified, all callbacks for the specified event will be unregistered.| 1758 1759**Example** 1760```ts 1761import wifi from '@ohos.wifi'; 1762 1763let recvP2pDiscoveryChangeFunc = (result:number) => { 1764 console.info("Receive p2p discovery change event: " + result); 1765} 1766 1767// Register an event. 1768wifi.on("p2pDiscoveryChange", recvP2pDiscoveryChangeFunc); 1769 1770// Unregister an event. 1771wifi.off("p2pDiscoveryChange", recvP2pDiscoveryChangeFunc); 1772``` 1773