1# @ohos.wifi (WLAN) 2 3该模块主要提供WLAN基础功能、P2P(peer-to-peer)功能和WLAN消息通知的相应服务,让应用可以通过WLAN和其他设备互联互通。 4 5> **说明:** 6> 7> 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 从API Version 9 开始,该接口不再维护,推荐使用[`@ohos.wifiManager (WLAN)`](js-apis-wifiManager.md)等相关接口。 9 10 11## 导入模块 12 13```ts 14import wifi from '@ohos.wifi'; 15``` 16 17 18## wifi.isWifiActive 19 20isWifiActive(): boolean 21 22查询WLAN是否已使能。 23 24**需要权限:** ohos.permission.GET_WIFI_INFO 25 26**系统能力:** SystemCapability.Communication.WiFi.STA 27 28**返回值:** 29 30 | **类型** | **说明** | 31 | -------- | -------- | 32 | boolean | true:已使能, false:未使能。 | 33 34**示例:** 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 51启动WLAN扫描。 52 53**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.LOCATION 54 55**系统能力:** SystemCapability.Communication.WiFi.STA 56 57**返回值:** 58 59 | **类型** | **说明** | 60 | -------- | -------- | 61 | boolean | true:扫描操作执行成功, false:扫描操作执行失败。 | 62 63**示例:** 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 79获取扫描结果,使用Promise异步回调。 80 81**需要权限:** ohos.permission.GET_WIFI_INFO 和 (ohos.permission.GET_WIFI_PEERS_MAC 或 ohos.permission.LOCATION) 82 83**系统能力:** SystemCapability.Communication.WiFi.STA 84 85**返回值:** 86 87 | **类型** | **说明** | 88 | -------- | -------- | 89 | Promise< Array<[WifiScanInfo](#wifiscaninfo)> > | Promise对象。返回扫描到的热点列表。 | 90 91 92## wifi.getScanInfos 93 94getScanInfos(callback: AsyncCallback<Array<WifiScanInfo>>): void 95 96获取扫描结果,使用callback异步回调。 97 98**需要权限:** ohos.permission.GET_WIFI_INFO 和 (ohos.permission.GET_WIFI_PEERS_MAC 或 ohos.permission.LOCATION) 99 100**系统能力:** SystemCapability.Communication.WiFi.STA 101 102**参数:** 103 104 | **参数名** | **类型** | **必填** | **说明** | 105 | -------- | -------- | -------- | -------- | 106 | callback | AsyncCallback< Array<[WifiScanInfo](#wifiscaninfo)>> | 是 | 回调函数。当成功时,err为0,data为扫描到的热点;否则err为非0值,data为空。 | 107 108**示例:** 109 110```ts 111import wifi from '@ohos.wifi'; 112 113wifi.getScanInfos((err, result) => { 114 if (err) { 115 console.error("get scan info error"); 116 return; 117 } 118 119 let len = result.length; 120 console.log("wifi received scan info: " + len); 121 for (let i = 0; i < len; ++i) { 122 console.info("ssid: " + result[i].ssid); 123 console.info("bssid: " + result[i].bssid); 124 console.info("capabilities: " + result[i].capabilities); 125 console.info("securityType: " + result[i].securityType); 126 console.info("rssi: " + result[i].rssi); 127 console.info("band: " + result[i].band); 128 console.info("frequency: " + result[i].frequency); 129 console.info("channelWidth: " + result[i].channelWidth); 130 console.info("timestamp: " + result[i].timestamp); 131 } 132}); 133 134wifi.getScanInfos().then(result => { 135 let len = result.length; 136 console.log("wifi received scan info: " + len); 137 for (let i = 0; i < len; ++i) { 138 console.info("ssid: " + result[i].ssid); 139 console.info("bssid: " + result[i].bssid); 140 console.info("capabilities: " + result[i].capabilities); 141 console.info("securityType: " + result[i].securityType); 142 console.info("rssi: " + result[i].rssi); 143 console.info("band: " + result[i].band); 144 console.info("frequency: " + result[i].frequency); 145 console.info("channelWidth: " + result[i].channelWidth); 146 console.info("timestamp: " + result[i].timestamp); 147 } 148}); 149``` 150 151 152## WifiScanInfo 153 154WLAN热点信息。 155 156**系统能力:** SystemCapability.Communication.WiFi.STA 157 158 159| **名称** | **类型** | **可读** | **可写** | **说明** | 160| -------- | -------- | -------- | -------- | -------- | 161| ssid | string | 是 | 否 | 热点的SSID,最大长度为32字节,编码格式为UTF-8。 | 162| bssid | string | 是 | 否 | 热点的BSSID,例如:00:11:22:33:44:55。 | 163| capabilities | string | 是 | 否 | 热点能力。 | 164| securityType | [WifiSecurityType](#wifisecuritytype) | 是 | 否 | WLAN加密类型。 | 165| rssi | number | 是 | 否 | 热点的信号强度(dBm)。 | 166| band | number | 是 | 否 | WLAN接入点的频段。 | 167| frequency | number | 是 | 否 | WLAN接入点的频率。 | 168| channelWidth | number | 是 | 否 | WLAN接入点的带宽。 | 169| timestamp | number | 是 | 否 | 时间戳。 | 170 171 172## WifiSecurityType 173 174表示加密类型的枚举。 175 176**系统能力:** SystemCapability.Communication.WiFi.Core 177 178 179| **名称** | **值** | **说明** | 180| -------- | -------- | -------- | 181| WIFI_SEC_TYPE_INVALID | 0 | 无效加密类型。 | 182| WIFI_SEC_TYPE_OPEN | 1 | 开放加密类型。 | 183| WIFI_SEC_TYPE_WEP | 2 | Wired Equivalent Privacy (WEP)加密类型。 | 184| WIFI_SEC_TYPE_PSK | 3 | Pre-shared key (PSK)加密类型。 | 185| WIFI_SEC_TYPE_SAE | 4 | Simultaneous Authentication of Equals (SAE)加密类型。 | 186 187 188 189## WifiDeviceConfig 190 191WLAN配置信息。 192 193**系统能力:** SystemCapability.Communication.WiFi.STA 194 195 196| **名称** | **类型** | **可读** | **可写** | **说明** | 197| -------- | -------- | -------- | -------- | -------- | 198| ssid | string | 是 | 否 | 热点的SSID,最大长度为32字节,编码格式为UTF-8。 | 199| bssid | string | 是 | 否 | 热点的BSSID,例如:00:11:22:33:44:55。 | 200| preSharedKey | string | 是 | 否 | 热点的密钥,最大长度为64字节。 | 201| isHiddenSsid | boolean | 是 | 否 | 是否是隐藏网络。 | 202| securityType | [WifiSecurityType](#wifisecuritytype) | 是 | 否 | 加密类型。 | 203 204 205 206## wifi.addUntrustedConfig<sup>7+</sup> 207 208addUntrustedConfig(config: WifiDeviceConfig): Promise<boolean> 209 210添加不可信网络配置,使用Promise异步回调。 211 212**需要权限:** ohos.permission.SET_WIFI_INFO 213 214**系统能力:** SystemCapability.Communication.WiFi.STA 215 216**参数:** 217 218 | **参数名** | **类型** | **必填** | **说明** | 219 | -------- | -------- | -------- | -------- | 220 | config | [WifiDeviceConfig](#wifideviceconfig) | 是 | WLAN配置信息。 | 221 222**返回值:** 223 224 | **类型** | **说明** | 225 | -------- | -------- | 226 | Promise<boolean> | Promise对象。表示操作结果,true: 成功, false: 失败。 | 227 228**示例:** 229```ts 230import wifi from '@ohos.wifi'; 231 232try { 233 let config:wifi.WifiDeviceConfig = { 234 ssid : "****", 235 bssid: "****", 236 preSharedKey: "****", 237 isHiddenSsid: false, 238 securityType: 0, 239 creatorUid: 0, 240 disableReason: 0, 241 netId: 0, 242 randomMacType: 0, 243 randomMacAddr: "****", 244 ipType: 0, 245 staticIp: { 246 ipAddress: 0, 247 gateway: 0, 248 dnsServers: [], 249 domains: [] 250 } 251 } 252 wifi.addUntrustedConfig(config).then(result => { 253 console.info("result:" + JSON.stringify(result)); 254 }); 255}catch(error){ 256 console.error("failed:" + JSON.stringify(error)); 257} 258``` 259 260## wifi.addUntrustedConfig<sup>7+</sup> 261 262addUntrustedConfig(config: WifiDeviceConfig, callback: AsyncCallback<boolean>): void 263 264添加不可信网络配置,使用callback异步回调。 265 266**需要权限:** ohos.permission.SET_WIFI_INFO 267 268**系统能力:** SystemCapability.Communication.WiFi.STA 269 270**参数:** 271 272 | **参数名** | **类型** | **必填** | **说明** | 273 | -------- | -------- | -------- | -------- | 274 | config | [WifiDeviceConfig](#wifideviceconfig) | 是 | WLAN配置信息。 | 275 | callback | AsyncCallback<boolean> | 是 | 回调函数。当操作成功时,err为0,data表示操作结果,true: 成功, false: 失败。如果error为非0,表示处理出现错误。 | 276 277**示例:** 278```ts 279import wifi from '@ohos.wifi'; 280 281try { 282 let config:wifi.WifiDeviceConfig = { 283 ssid : "****", 284 bssid: "****", 285 preSharedKey: "****", 286 isHiddenSsid: false, 287 securityType: 0, 288 creatorUid: 0, 289 disableReason: 0, 290 netId: 0, 291 randomMacType: 0, 292 randomMacAddr: "****", 293 ipType: 0, 294 staticIp: { 295 ipAddress: 0, 296 gateway: 0, 297 dnsServers: [], 298 domains: [] 299 } 300 } 301 wifi.addUntrustedConfig(config,(error,result) => { 302 console.info("result:" + JSON.stringify(result)); 303 }); 304}catch(error){ 305 console.error("failed:" + JSON.stringify(error)); 306} 307``` 308 309## wifi.removeUntrustedConfig<sup>7+</sup> 310 311removeUntrustedConfig(config: WifiDeviceConfig): Promise<boolean> 312 313移除不可信网络配置,使用Promise异步回调。 314 315**需要权限:** ohos.permission.SET_WIFI_INFO 316 317**系统能力:** SystemCapability.Communication.WiFi.STA 318 319**参数:** 320 321 | **参数名** | **类型** | **必填** | **说明** | 322 | -------- | -------- | -------- | -------- | 323 | config | [WifiDeviceConfig](#wifideviceconfig) | 是 | WLAN配置信息。 | 324 325**返回值:** 326 327 | **类型** | **说明** | 328 | -------- | -------- | 329 | Promise<boolean> | Promise对象。表示操作结果,true: 成功, false: 失败。 | 330 331**示例:** 332 333```ts 334import wifi from '@ohos.wifi'; 335 336try { 337 let config:wifi.WifiDeviceConfig = { 338 ssid : "****", 339 bssid: "****", 340 preSharedKey: "****", 341 isHiddenSsid: false, 342 securityType: 0, 343 creatorUid: 0, 344 disableReason: 0, 345 netId: 0, 346 randomMacType: 0, 347 randomMacAddr: "****", 348 ipType: 0, 349 staticIp: { 350 ipAddress: 0, 351 gateway: 0, 352 dnsServers: [], 353 domains: [] 354 } 355 } 356 wifi.removeUntrustedConfig(config).then(result => { 357 console.info("result:" + JSON.stringify(result)); 358 }); 359}catch(error){ 360 console.error("failed:" + JSON.stringify(error)); 361} 362``` 363 364 365## wifi.removeUntrustedConfig<sup>7+</sup> 366 367removeUntrustedConfig(config: WifiDeviceConfig, callback: AsyncCallback<boolean>): void 368 369移除不可信网络配置,使用callback异步回调。 370 371**需要权限:** ohos.permission.SET_WIFI_INFO 372 373**系统能力:** SystemCapability.Communication.WiFi.STA 374 375**参数:** 376 377 | **参数名** | **类型** | **必填** | **说明** | 378 | -------- | -------- | -------- | -------- | 379 | config | [WifiDeviceConfig](#wifideviceconfig) | 是 | WLAN配置信息。 | 380 | callback | AsyncCallback<boolean> | 是 | 回调函数。当操作成功时,err为0,data表示操作结果,true: 成功, false: 失败。如果error为非0,表示处理出现错误。 | 381 382**示例:** 383```ts 384import wifi from '@ohos.wifi'; 385 386try { 387 let config:wifi.WifiDeviceConfig = { 388 ssid : "****", 389 bssid: "****", 390 preSharedKey: "****", 391 isHiddenSsid: false, 392 securityType: 0, 393 creatorUid: 0, 394 disableReason: 0, 395 netId: 0, 396 randomMacType: 0, 397 randomMacAddr: "****", 398 ipType: 0, 399 staticIp: { 400 ipAddress: 0, 401 gateway: 0, 402 dnsServers: [], 403 domains: [] 404 } 405 } 406 wifi.removeUntrustedConfig(config,(error,result) => { 407 console.info("result:" + JSON.stringify(result)); 408 }); 409}catch(error){ 410 console.error("failed:" + JSON.stringify(error)); 411} 412``` 413 414 415## wifi.getSignalLevel 416 417getSignalLevel(rssi: number, band: number): number 418 419查询WLAN信号强度。 420 421**需要权限:** ohos.permission.GET_WIFI_INFO 422 423**系统能力:** SystemCapability.Communication.WiFi.STA 424 425**参数:** 426 427 | **参数名** | **类型** | **必填** | **说明** | 428 | -------- | -------- | -------- | -------- | 429 | rssi | number | 是 | 热点的信号强度(dBm)。 | 430 | band | number | 是 | WLAN接入点的频段。 | 431 432**返回值:** 433 434 | **类型** | **说明** | 435 | -------- | -------- | 436 | number | 信号强度,取值范围为[0, 4]。 | 437 438**示例:** 439```ts 440import wifi from '@ohos.wifi'; 441 442try { 443 let rssi = 0; 444 let band = 0; 445 let level = wifi.getSignalLevel(rssi,band); 446 console.info("level:" + JSON.stringify(level)); 447}catch(error){ 448 console.error("failed:" + JSON.stringify(error)); 449} 450 451``` 452 453## wifi.getLinkedInfo 454 455getLinkedInfo(): Promise<WifiLinkedInfo> 456 457获取WLAN连接信息,使用Promise异步回调。 458 459**需要权限:** ohos.permission.GET_WIFI_INFO 460 461**系统能力:** SystemCapability.Communication.WiFi.STA 462 463**返回值:** 464 465 | 类型 | 说明 | 466 | -------- | -------- | 467 | Promise<[WifiLinkedInfo](#wifilinkedinfo)> | Promise对象。表示WLAN连接信息。 | 468 469 470## wifi.getLinkedInfo 471 472getLinkedInfo(callback: AsyncCallback<WifiLinkedInfo>): void 473 474获取WLAN连接信息,使用callback异步回调。 475 476**需要权限:** ohos.permission.GET_WIFI_INFO 477 478**系统能力:** SystemCapability.Communication.WiFi.STA 479 480**参数:** 481 482 | 参数名 | 类型 | 必填 | 说明 | 483 | -------- | -------- | -------- | -------- | 484 | callback | AsyncCallback<[WifiLinkedInfo](#wifilinkedinfo)> | 是 | 回调函数。当获取成功时,err为0,data表示WLAN连接信息。如果error为非0,表示处理出现错误。 | 485 486**示例:** 487```ts 488import wifi from '@ohos.wifi'; 489 490wifi.getLinkedInfo((err, data) => { 491 if (err) { 492 console.error("get linked info error"); 493 return; 494 } 495 console.info("get wifi linked info: " + JSON.stringify(data)); 496}); 497 498wifi.getLinkedInfo().then(data => { 499 console.info("get wifi linked info: " + JSON.stringify(data)); 500}).catch((error:number) => { 501 console.info("get linked info error"); 502}); 503``` 504 505 506## WifiLinkedInfo 507 508提供WLAN连接的相关信息。 509 510**系统能力:** SystemCapability.Communication.WiFi.STA 511 512| 名称 | 类型 | 可读 | 可写 | 说明 | 513| -------- | -------- | -------- | -------- | -------- | 514| ssid | string | 是 | 否 | 热点的SSID,最大长度为32字节,编码格式为UTF-8。 | 515| bssid | string | 是 | 否 | 热点的BSSID,例如:00:11:22:33:44:55。 | 516| rssi | number | 是 | 否 | 热点的信号强度(dBm)。 | 517| band | number | 是 | 否 | WLAN接入点的频段。 | 518| linkSpeed | number | 是 | 否 | WLAN接入点的速度。 | 519| frequency | number | 是 | 否 | WLAN接入点的频率。 | 520| isHidden | boolean | 是 | 否 | WLAN接入点是否是隐藏网络。 | 521| isRestricted | boolean | 是 | 否 | WLAN接入点是否限制数据量。 | 522| macAddress | string | 是 | 否 | 设备的MAC地址。 | 523| ipAddress | number | 是 | 否 | WLAN连接的IP地址。 | 524| connState | [ConnState](#connstate) | 是 | 否 | WLAN连接状态。 | 525 526 527## ConnState 528 529表示WLAN连接状态的枚举。 530 531**系统能力:** SystemCapability.Communication.WiFi.STA 532 533| 名称 | 值 | 说明 | 534| -------- | -------- | -------- | 535| SCANNING | 0 | 设备正在搜索可用的AP。 | 536| CONNECTING | 1 | 正在建立WLAN连接。 | 537| AUTHENTICATING | 2 | WLAN连接正在认证中。 | 538| OBTAINING_IPADDR | 3 | 正在获取WLAN连接的IP地址。 | 539| CONNECTED | 4 | WLAN连接已建立。 | 540| DISCONNECTING | 5 | WLAN连接正在断开。 | 541| DISCONNECTED | 6 | WLAN连接已断开。 | 542| UNKNOWN | 7 | WLAN连接建立失败。 | 543 544 545## wifi.isConnected<sup>7+</sup> 546 547isConnected(): boolean 548 549查询WLAN是否已连接。 550 551**需要权限:** ohos.permission.GET_WIFI_INFO 552 553**系统能力:** SystemCapability.Communication.WiFi.STA 554 555**返回值:** 556 557 | **类型** | **说明** | 558 | -------- | -------- | 559 | boolean | true:已连接, false:未连接。 | 560 561 562 563## wifi.isFeatureSupported<sup>7+</sup> 564 565isFeatureSupported(featureId: number): boolean 566 567判断设备是否支持相关WLAN特性。 568 569**需要权限:** ohos.permission.GET_WIFI_INFO 570 571**系统能力:** SystemCapability.Communication.WiFi.Core 572 573**参数:** 574 575 576 | **参数名** | **类型** | 必填 | **说明** | 577 | -------- | -------- | -------- | -------- | 578 | featureId | number | 是 | 特性ID值。 | 579 580**返回值:** 581 582 | **类型** | **说明** | 583 | -------- | -------- | 584 | boolean | true:支持, false:不支持。 | 585 586**示例:** 587```ts 588import wifi from '@ohos.wifi'; 589 590try { 591 let featureId = 0; 592 let ret = wifi.isFeatureSupported(featureId); 593 console.info("isFeatureSupported:" + ret); 594}catch(error){ 595 console.error("failed:" + JSON.stringify(error)); 596} 597 598``` 599 600 601## wifi.getIpInfo<sup>7+</sup> 602 603getIpInfo(): IpInfo 604 605获取IP信息。 606 607**需要权限:** ohos.permission.GET_WIFI_INFO 608 609**系统能力:** SystemCapability.Communication.WiFi.STA 610 611**返回值:** 612 613 | **类型** | **说明** | 614 | -------- | -------- | 615 | [IpInfo](#ipinfo7) | IP信息。 | 616 617**示例:** 618```ts 619import wifi from '@ohos.wifi'; 620 621try { 622 let info = wifi.getIpInfo(); 623 console.info("info:" + JSON.stringify(info)); 624}catch(error){ 625 console.error("failed:" + JSON.stringify(error)); 626} 627``` 628 629## IpInfo<sup>7+</sup> 630 631IP信息。 632 633**系统能力:** SystemCapability.Communication.WiFi.AP.Core 634 635| **名称** | **类型** | **可读** | **可写** | **说明** | 636| -------- | -------- | -------- | -------- | -------- | 637| ipAddress | number | 是 | 否 | IP地址。 | 638| gateway | number | 是 | 否 | 网关。 | 639| netmask | number | 是 | 否 | 掩码。 | 640| primaryDns | number | 是 | 否 | 主DNS服务器IP地址。 | 641| secondDns | number | 是 | 否 | 备DNS服务器IP地址。 | 642| serverIp | number | 是 | 否 | DHCP服务端IP地址。 | 643| leaseDuration | number | 是 | 否 | IP地址租用时长。 | 644 645 646## wifi.getCountryCode<sup>7+</sup> 647 648getCountryCode(): string 649 650获取国家码信息。 651 652**需要权限:** ohos.permission.GET_WIFI_INFO 653 654**系统能力:** SystemCapability.Communication.WiFi.Core 655 656**返回值:** 657 658 | **类型** | **说明** | 659 | -------- | -------- | 660 | string | 国家码。 | 661 662**示例:** 663```ts 664import wifi from '@ohos.wifi'; 665 666try { 667 let code = wifi.getCountryCode(); 668 console.info("code:" + code); 669}catch(error){ 670 console.error("failed:" + JSON.stringify(error)); 671} 672``` 673 674 675## wifi.getP2pLinkedInfo<sup>8+</sup> 676 677getP2pLinkedInfo(): Promise<WifiP2pLinkedInfo> 678 679获取P2P连接信息,使用Promise异步回调。 680 681**需要权限:** ohos.permission.GET_WIFI_INFO 682 683**系统能力:** SystemCapability.Communication.WiFi.P2P 684 685**返回值:** 686 687 | 类型 | 说明 | 688 | -------- | -------- | 689 | Promise<[WifiP2pLinkedInfo](#wifip2plinkedinfo8)> | Promise对象。表示P2P连接信息。 | 690 691 692 693## WifiP2pLinkedInfo<sup>8+</sup> 694 695提供WLAN连接的相关信息。 696 697**系统能力:** SystemCapability.Communication.WiFi.P2P 698 699| 名称 | 类型 | 可读 | 可写 | 说明 | 700| -------- | -------- | -------- | -------- | -------- | 701| connectState | [P2pConnectState](#p2pconnectstate8) | 是 | 否 | P2P连接状态。 | 702| isGroupOwner | boolean | 是 | 否 | 是否是群主。 | 703| groupOwnerAddr | string | 是 | 否 | 群组MAC地址。 704 705 706## P2pConnectState<sup>8+</sup> 707 708表示P2P连接状态的枚举。 709 710**系统能力:** SystemCapability.Communication.WiFi.P2P 711 712| 名称 | 值 | 说明 | 713| -------- | -------- | -------- | 714| DISCONNECTED | 0 | 断开状态。 | 715| CONNECTED | 1 | 连接状态。 | 716 717 718## wifi.getP2pLinkedInfo<sup>8+</sup> 719 720getP2pLinkedInfo(callback: AsyncCallback<WifiP2pLinkedInfo>): void 721 722获取P2P连接信息,使用callback异步回调。 723 724**需要权限:** ohos.permission.GET_WIFI_INFO 725 726**系统能力:** SystemCapability.Communication.WiFi.P2P 727 728**参数:** 729 730 | 参数名 | 类型 | 必填 | 说明 | 731 | -------- | -------- | -------- | -------- | 732 | callback | AsyncCallback<[WifiP2pLinkedInfo](#wifip2plinkedinfo8)> | 是 | 回调函数。当操作成功时,err为0,data表示P2P连接信息。如果error为非0,表示处理出现错误。 | 733 734**示例:** 735```ts 736import wifi from '@ohos.wifi'; 737 738wifi.getP2pLinkedInfo((err, data) => { 739 if (err) { 740 console.error("get p2p linked info error"); 741 return; 742 } 743 console.info("get wifi p2p linked info: " + JSON.stringify(data)); 744}); 745 746wifi.getP2pLinkedInfo().then(data => { 747 console.info("get wifi p2p linked info: " + JSON.stringify(data)); 748}); 749``` 750 751## wifi.getCurrentGroup<sup>8+</sup> 752 753getCurrentGroup(): Promise<WifiP2pGroupInfo> 754 755获取P2P当前组信息,使用Promise异步回调。 756 757**需要权限:** ohos.permission.GET_WIFI_INFO 和 ohos.permission.LOCATION 758 759**系统能力:** SystemCapability.Communication.WiFi.P2P 760 761**返回值:** 762 763 | 类型 | 说明 | 764 | -------- | -------- | 765 | Promise<[WifiP2pGroupInfo](#wifip2pgroupinfo8)> | Promise对象。表示当前组信息。 | 766 767 768## wifi.getCurrentGroup<sup>8+</sup> 769 770getCurrentGroup(callback: AsyncCallback<WifiP2pGroupInfo>): void 771 772获取P2P当前组信息,使用callback异步回调。 773 774**需要权限:** ohos.permission.GET_WIFI_INFO 和 ohos.permission.LOCATION 775 776**系统能力:** SystemCapability.Communication.WiFi.P2P 777 778**参数:** 779 780 | 参数名 | 类型 | 必填 | 说明 | 781 | -------- | -------- | -------- | -------- | 782 | callback | AsyncCallback<[WifiP2pGroupInfo](#wifip2pgroupinfo8)> | 是 | 回调函数。当操作成功时,err为0,data表示当前组信息。如果error为非0,表示处理出现错误。 | 783 784**示例:** 785```ts 786import wifi from '@ohos.wifi'; 787 788wifi.getCurrentGroup((err, data) => { 789 if (err) { 790 console.error("get current P2P group error"); 791 return; 792 } 793 console.info("get current P2P group: " + JSON.stringify(data)); 794}); 795 796wifi.getCurrentGroup().then(data => { 797 console.info("get current P2P group: " + JSON.stringify(data)); 798}); 799``` 800 801## wifi.getP2pPeerDevices<sup>8+</sup> 802 803getP2pPeerDevices(): Promise<WifiP2pDevice[]> 804 805获取P2P对端设备列表信息,使用Promise异步回调。 806 807**需要权限:** ohos.permission.GET_WIFI_INFO 和 ohos.permission.LOCATION 808 809**系统能力:** SystemCapability.Communication.WiFi.P2P 810 811**返回值:** 812 813 | 类型 | 说明 | 814 | -------- | -------- | 815 | Promise<[WifiP2pDevice[]](#wifip2pdevice8)> | Promise对象。表示对端设备列表信息。 | 816 817 818## wifi.getP2pPeerDevices<sup>8+</sup> 819 820getP2pPeerDevices(callback: AsyncCallback<WifiP2pDevice[]>): void 821 822获取P2P对端设备列表信息,使用callback异步回调。 823 824**需要权限:** ohos.permission.GET_WIFI_INFO 和 ohos.permission.LOCATION 825 826**系统能力:** SystemCapability.Communication.WiFi.P2P 827 828**参数:** 829 830 | 参数名 | 类型 | 必填 | 说明 | 831 | -------- | -------- | -------- | -------- | 832 | callback | AsyncCallback<[WifiP2pDevice[]](#wifip2pdevice8)> | 是 | 回调函数。当操作成功时,err为0,data表示对端设备列表信息。如果error为非0,表示处理出现错误。 | 833 834**示例:** 835```ts 836import wifi from '@ohos.wifi'; 837 838wifi.getP2pPeerDevices((err, data) => { 839 if (err) { 840 console.error("get P2P peer devices error"); 841 return; 842 } 843 console.info("get P2P peer devices: " + JSON.stringify(data)); 844}); 845 846wifi.getP2pPeerDevices().then(data => { 847 console.info("get P2P peer devices: " + JSON.stringify(data)); 848}); 849``` 850 851## WifiP2pDevice<sup>8+</sup> 852 853表示P2P设备信息。 854 855**系统能力:** SystemCapability.Communication.WiFi.P2P 856 857| 名称 | 类型 | 可读 | 可写 | 说明 | 858| -------- | -------- | -------- | -------- | -------- | 859| deviceName | string | 是 | 否 | 设备名称。 | 860| deviceAddress | string | 是 | 否 | 设备MAC地址。 | 861| primaryDeviceType | string | 是 | 否 | 主设备类型。 | 862| deviceStatus | [P2pDeviceStatus](#p2pdevicestatus8) | 是 | 否 | 设备状态。 | 863| groupCapabilitys | number | 是 | 否 | 群组能力。 | 864 865 866## P2pDeviceStatus<sup>8+</sup> 867 868表示设备状态的枚举。 869 870**系统能力:** SystemCapability.Communication.WiFi.P2P 871 872| 名称 | 值 | 说明 | 873| -------- | -------- | -------- | 874| CONNECTED | 0 | 连接状态。 | 875| INVITED | 1 | 邀请状态。 | 876| FAILED | 2 | 失败状态。 | 877| AVAILABLE | 3 | 可用状态。 | 878| UNAVAILABLE | 4 | 不可用状态。 | 879 880 881## wifi.createGroup<sup>8+</sup> 882 883createGroup(config: WifiP2PConfig): boolean 884 885创建群组。 886 887**需要权限:** ohos.permission.GET_WIFI_INFO 888 889**系统能力:** SystemCapability.Communication.WiFi.P2P 890 891**参数:** 892 893 | **参数名** | **类型** | 必填 | **说明** | 894 | -------- | -------- | -------- | -------- | 895 | config | [WifiP2PConfig](#wifip2pconfig8) | 是 | 群组配置信息。 | 896 897**返回值:** 898 899 | 类型 | 说明 | 900 | -------- | -------- | 901 | boolean | true:创建群组操作执行成功, false:创建群组操作执行失败。 | 902 903**示例:** 904```ts 905import wifi from '@ohos.wifi'; 906 907try { 908 let config:wifi.WifiP2PConfig = { 909 deviceAddress: "****", 910 netId: 0, 911 passphrase: "*****", 912 groupName: "****", 913 goBand: 0 914 } 915 wifi.createGroup(config); 916 917}catch(error){ 918 console.error("failed:" + JSON.stringify(error)); 919} 920``` 921 922## WifiP2PConfig<sup>8+</sup> 923 924表示P2P配置信息。 925 926**系统能力:** SystemCapability.Communication.WiFi.P2P 927 928| 名称 | 类型 | 可读 | 可写 | 说明 | 929| -------- | -------- | -------- | -------- | -------- | 930| deviceAddress | string | 是 | 否 | 设备地址。 | 931| netId | number | 是 | 否 | 网络ID。创建群组时-1表示创建临时组,-2表示创建永久组。 | 932| passphrase | string | 是 | 否 | 群组密钥。 | 933| groupName | string | 是 | 否 | 群组名称。 | 934| goBand | [GroupOwnerBand](#groupownerband8) | 是 | 否 | 群组带宽。 | 935 936 937## GroupOwnerBand<sup>8+</sup> 938 939表示群组带宽的枚举。 940 941**系统能力:** SystemCapability.Communication.WiFi.P2P 942 943| 名称 | 值 | 说明 | 944| -------- | -------- | -------- | 945| GO_BAND_AUTO | 0 | 自动模式。 | 946| GO_BAND_2GHZ | 1 | 2GHZ。 | 947| GO_BAND_5GHZ | 2 | 5GHZ。 | 948 949 950## wifi.removeGroup<sup>8+</sup> 951 952removeGroup(): boolean 953 954移除群组。 955 956**需要权限:** ohos.permission.GET_WIFI_INFO 957 958**系统能力:** SystemCapability.Communication.WiFi.P2P 959 960**返回值:** 961 962 | 类型 | 说明 | 963 | -------- | -------- | 964 | boolean | true:操作执行成功, false:操作执行失败。 | 965 966**示例:** 967```ts 968import wifi from '@ohos.wifi'; 969 970try { 971 wifi.removeGroup(); 972}catch(error){ 973 console.error("failed:" + JSON.stringify(error)); 974} 975``` 976 977## wifi.p2pConnect<sup>8+</sup> 978 979p2pConnect(config: WifiP2PConfig): boolean 980 981执行P2P连接。 982 983**需要权限:** ohos.permission.GET_WIFI_INFO 和 ohos.permission.LOCATION 984 985**系统能力:** SystemCapability.Communication.WiFi.P2P 986 987**参数:** 988 989 990 | **参数名** | **类型** | 必填 | **说明** | 991 | -------- | -------- | -------- | -------- | 992 | config | [WifiP2PConfig](#wifip2pconfig8) | 是 | 连接配置信息。 | 993 994**返回值:** 995 996 | 类型 | 说明 | 997 | -------- | -------- | 998 | boolean | true:操作执行成功, false:操作执行失败。 | 999 1000 1001**示例:** 1002```ts 1003import wifi from '@ohos.wifi'; 1004 1005let recvP2pConnectionChangeFunc = (result:wifi.WifiP2pLinkedInfo) => { 1006 console.info("p2p connection change receive event: " + JSON.stringify(result)); 1007 wifi.getP2pLinkedInfo((err, data) => { 1008 if (err) { 1009 console.error('failed to get getP2pLinkedInfo: ' + JSON.stringify(err)); 1010 return; 1011 } 1012 console.info("get getP2pLinkedInfo: " + JSON.stringify(data)); 1013 }); 1014} 1015wifi.on("p2pConnectionChange", recvP2pConnectionChangeFunc); 1016 1017let recvP2pDeviceChangeFunc = (result:wifi.WifiP2pDevice) => { 1018 console.info("p2p device change receive event: " + JSON.stringify(result)); 1019} 1020wifi.on("p2pDeviceChange", recvP2pDeviceChangeFunc); 1021 1022let recvP2pPeerDeviceChangeFunc = (result:wifi.WifiP2pDevice[]) => { 1023 console.info("p2p peer device change receive event: " + JSON.stringify(result)); 1024 wifi.getP2pPeerDevices((err, data) => { 1025 if (err) { 1026 console.error('failed to get peer devices: ' + JSON.stringify(err)); 1027 return; 1028 } 1029 console.info("get peer devices: " + JSON.stringify(data)); 1030 let len = data.length; 1031 for (let i = 0; i < len; ++i) { 1032 if (data[i].deviceName === "my_test_device") { 1033 console.info("p2p connect to test device: " + data[i].deviceAddress); 1034 let config:wifi.WifiP2PConfig = { 1035 deviceAddress:data[i].deviceAddress, 1036 netId:-2, 1037 passphrase:"", 1038 groupName:"", 1039 goBand:0, 1040 } 1041 wifi.p2pConnect(config); 1042 } 1043 } 1044 }); 1045} 1046wifi.on("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc); 1047 1048let recvP2pPersistentGroupChangeFunc = () => { 1049 console.info("p2p persistent group change receive event"); 1050 1051 wifi.getCurrentGroup((err, data) => { 1052 if (err) { 1053 console.error('failed to get current group: ' + JSON.stringify(err)); 1054 return; 1055 } 1056 console.info("get current group: " + JSON.stringify(data)); 1057 }); 1058} 1059wifi.on("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc); 1060 1061setTimeout(() => {wifi.off("p2pConnectionChange", recvP2pConnectionChangeFunc);}, 125 * 1000); 1062setTimeout(() => {wifi.off("p2pDeviceChange", recvP2pDeviceChangeFunc);}, 125 * 1000); 1063setTimeout(() => {wifi.off("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc);}, 125 * 1000); 1064setTimeout(() => {wifi.off("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc);}, 125 * 1000); 1065console.info("start discover devices -> " + wifi.startDiscoverDevices()); 1066``` 1067 1068## wifi.p2pCancelConnect<sup>8+</sup> 1069 1070p2pCancelConnect(): boolean 1071 1072取消P2P连接。 1073 1074**需要权限:** ohos.permission.GET_WIFI_INFO 1075 1076**系统能力:** SystemCapability.Communication.WiFi.P2P 1077 1078**返回值:** 1079 1080 | 类型 | 说明 | 1081 | -------- | -------- | 1082 | boolean | true:操作执行成功, false:操作执行失败。 | 1083 1084**示例:** 1085```ts 1086import wifi from '@ohos.wifi'; 1087 1088try { 1089 wifi.p2pCancelConnect(); 1090}catch(error){ 1091 console.error("failed:" + JSON.stringify(error)); 1092} 1093``` 1094 1095## wifi.startDiscoverDevices<sup>8+</sup> 1096 1097startDiscoverDevices(): boolean 1098 1099开始发现设备。 1100 1101**需要权限:** ohos.permission.GET_WIFI_INFO 和 ohos.permission.LOCATION 1102 1103**系统能力:** SystemCapability.Communication.WiFi.P2P 1104 1105**返回值:** 1106 1107 | 类型 | 说明 | 1108 | -------- | -------- | 1109 | boolean | true:操作执行成功, false:操作执行失败。 | 1110 1111**示例:** 1112```ts 1113import wifi from '@ohos.wifi'; 1114 1115try { 1116 wifi.startDiscoverDevices(); 1117}catch(error){ 1118 console.error("failed:" + JSON.stringify(error)); 1119} 1120``` 1121 1122## wifi.stopDiscoverDevices<sup>8+</sup> 1123 1124stopDiscoverDevices(): boolean 1125 1126停止发现设备。 1127 1128**需要权限:** ohos.permission.GET_WIFI_INFO 1129 1130**系统能力:** SystemCapability.Communication.WiFi.P2P 1131 1132**返回值:** 1133 1134 | 类型 | 说明 | 1135 | -------- | -------- | 1136 | boolean | true:操作执行成功,操作执行失败。 | 1137 1138**示例:** 1139```ts 1140import wifi from '@ohos.wifi'; 1141 1142try { 1143 wifi.stopDiscoverDevices(); 1144}catch(error){ 1145 console.error("failed:" + JSON.stringify(error)); 1146} 1147``` 1148 1149 1150 1151## WifiP2pGroupInfo<sup>8+</sup> 1152 1153表示P2P群组相关信息。 1154 1155**系统能力:** SystemCapability.Communication.WiFi.P2P 1156 1157| 名称 | 类型 | 可读 | 可写 | 说明 | 1158| -------- | -------- | -------- | -------- | -------- | 1159| isP2pGo | boolean | 是 | 否 | 是否是群主。 | 1160| ownerInfo | [WifiP2pDevice](#wifip2pdevice8) | 是 | 否 | 群组的设备信息。 | 1161| passphrase | string | 是 | 否 | 群组密钥。 | 1162| interface | string | 是 | 否 | 接口名称。 | 1163| groupName | string | 是 | 否 | 群组名称。 | 1164| networkId | number | 是 | 否 | 网络ID。 | 1165| frequency | number | 是 | 否 | 群组的频率。 | 1166| clientDevices | [WifiP2pDevice[]](#wifip2pdevice8) | 是 | 否 | 接入的设备列表信息。 | 1167| goIpAddress | string | 是 | 否 | 群组IP地址。 | 1168 1169 1170 1171## wifi.on('wifiStateChange')<sup>7+</sup> 1172 1173on(type: "wifiStateChange", callback: Callback<number>): void 1174 1175注册WLAN状态改变事件。 1176 1177**需要权限:** ohos.permission.GET_WIFI_INFO 1178 1179**系统能力:** SystemCapability.Communication.WiFi.STA 1180 1181**参数:** 1182 1183 | **参数名** | **类型** | **必填** | **说明** | 1184 | -------- | -------- | -------- | -------- | 1185 | type | string | 是 | 固定填"wifiStateChange"字符串。 | 1186 | callback | Callback<number> | 是 | 状态改变回调函数。 | 1187 1188**状态改变事件的枚举:** 1189 1190| **枚举值** | **说明** | 1191| -------- | -------- | 1192| 0 | 未激活。 | 1193| 1 | 已激活。 | 1194| 2 | 激活中。 | 1195| 3 | 去激活中。 | 1196 1197 1198## wifi.off('wifiStateChange')<sup>7+</sup> 1199 1200off(type: "wifiStateChange", callback?: Callback<number>): void 1201 1202取消注册WLAN状态改变事件。 1203 1204**需要权限:** ohos.permission.GET_WIFI_INFO 1205 1206**系统能力:** SystemCapability.Communication.WiFi.STA 1207 1208**参数:** 1209 1210 | **参数名** | **类型** | **必填** | **说明** | 1211 | -------- | -------- | -------- | -------- | 1212 | type | string | 是 | 固定填"wifiStateChange"字符串。 | 1213 | callback | Callback<number> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 1214 1215**示例:** 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 event 1224wifi.on("wifiStateChange", recvPowerNotifyFunc); 1225 1226// Unregister event 1227wifi.off("wifiStateChange", recvPowerNotifyFunc); 1228``` 1229 1230 1231## wifi.on('wifiConnectionChange')<sup>7+</sup> 1232 1233on(type: "wifiConnectionChange", callback: Callback<number>): void 1234 1235注册WLAN连接状态改变事件。 1236 1237**需要权限:** ohos.permission.GET_WIFI_INFO 1238 1239**系统能力:** SystemCapability.Communication.WiFi.STA 1240 1241**参数:** 1242 1243 | **参数名** | **类型** | **必填** | **说明** | 1244 | -------- | -------- | -------- | -------- | 1245 | type | string | 是 | 固定填"wifiConnectionChange"字符串。 | 1246 | callback | Callback<number> | 是 | 状态改变回调函数。 | 1247 1248**连接状态改变事件的枚举:** 1249 1250| **枚举值** | **说明** | 1251| -------- | -------- | 1252| 0 | 已断开。 | 1253| 1 | 已连接。 | 1254 1255 1256## wifi.off('wifiConnectionChange')<sup>7+</sup> 1257 1258off(type: "wifiConnectionChange", callback?: Callback<number>): void 1259 1260取消注册WLAN连接状态改变事件。 1261 1262**需要权限:** ohos.permission.GET_WIFI_INFO 1263 1264**系统能力:** SystemCapability.Communication.WiFi.STA 1265 1266**参数:** 1267 1268 | **参数名** | **类型** | **必填** | **说明** | 1269 | -------- | -------- | -------- | -------- | 1270 | type | string | 是 | 固定填"wifiConnectionChange"字符串。 | 1271 | callback | Callback<number> | 否 | 连接状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 1272 1273**示例:** 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 event 1282wifi.on("wifiConnectionChange", recvWifiConnectionChangeFunc); 1283 1284// Unregister event 1285wifi.off("wifiConnectionChange", recvWifiConnectionChangeFunc); 1286``` 1287 1288## wifi.on('wifiScanStateChange')<sup>7+</sup> 1289 1290on(type: "wifiScanStateChange", callback: Callback<number>): void 1291 1292注册扫描状态改变事件。 1293 1294**需要权限:** ohos.permission.GET_WIFI_INFO 1295 1296**系统能力:** SystemCapability.Communication.WiFi.STA 1297 1298**参数:** 1299 1300 | **参数名** | **类型** | **必填** | **说明** | 1301 | -------- | -------- | -------- | -------- | 1302 | type | string | 是 | 固定填"wifiScanStateChange"字符串。 | 1303 | callback | Callback<number> | 是 | 状态改变回调函数。 | 1304 1305**扫描状态改变事件的枚举:** 1306 1307| **枚举值** | **说明** | 1308| -------- | -------- | 1309| 0 | 扫描失败。 | 1310| 1 | 扫描成功。 | 1311 1312 1313## wifi.off('wifiScanStateChange')<sup>7+</sup> 1314 1315off(type: "wifiScanStateChange", callback?: Callback<number>): void 1316 1317取消注册扫描状态改变事件。 1318 1319**需要权限:** ohos.permission.GET_WIFI_INFO 1320 1321**系统能力:** SystemCapability.Communication.WiFi.STA 1322 1323**参数:** 1324 1325| **参数名** | **类型** | **必填** | **说明** | 1326| -------- | -------- | -------- | -------- | 1327| type | string | 是 | 固定填"wifiScanStateChange"字符串。 | 1328| callback | Callback<number> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 1329 1330**示例:** 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 event 1339wifi.on("wifiScanStateChange", recvWifiScanStateChangeFunc); 1340 1341// Unregister event 1342wifi.off("wifiScanStateChange", recvWifiScanStateChangeFunc); 1343``` 1344 1345## wifi.on('wifiRssiChange')<sup>7+</sup> 1346 1347on(type: "wifiRssiChange", callback: Callback<number>): void 1348 1349注册RSSI状态改变事件。 1350 1351**需要权限:** ohos.permission.GET_WIFI_INFO 1352 1353**系统能力:** SystemCapability.Communication.WiFi.STA 1354 1355**参数:** 1356 1357 | **参数名** | **类型** | **必填** | **说明** | 1358 | -------- | -------- | -------- | -------- | 1359 | type | string | 是 | 固定填"wifiRssiChange"字符串。 | 1360 | callback | Callback<number> | 是 | 状态改变回调函数,返回以dBm为单位的RSSI值。 | 1361 1362 1363## wifi.off('wifiRssiChange')<sup>7+</sup> 1364 1365off(type: "wifiRssiChange", callback?: Callback<number>): void 1366 1367取消注册RSSI状态改变事件。 1368 1369**需要权限:** ohos.permission.GET_WIFI_INFO 1370 1371**系统能力:** SystemCapability.Communication.WiFi.STA 1372 1373**参数:** 1374 1375 | **参数名** | **类型** | **必填** | **说明** | 1376 | -------- | -------- | -------- | -------- | 1377 | type | string | 是 | 固定填"wifiRssiChange"字符串。 | 1378 | callback | Callback<number> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 1379 1380**示例:** 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 event 1389wifi.on("wifiRssiChange", recvWifiRssiChangeFunc); 1390 1391// Unregister 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 1401注册热点状态改变事件。 1402 1403**需要权限:** ohos.permission.GET_WIFI_INFO 1404 1405**系统能力:** SystemCapability.Communication.WiFi.AP.Core 1406 1407**参数:** 1408 1409 | **参数名** | **类型** | **必填** | **说明** | 1410 | -------- | -------- | -------- | -------- | 1411 | type | string | 是 | 固定填"hotspotStateChange"字符串。 | 1412 | callback | Callback<number> | 是 | 状态改变回调函数。 | 1413 1414**热点状态改变事件的枚举:** 1415 1416| **枚举值** | **说明** | 1417| -------- | -------- | 1418| 0 | 未激活。 | 1419| 1 | 已激活。 | 1420| 2 | 激活中。 | 1421| 3 | 去激活中。 | 1422 1423**示例:** 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 event 1432wifi.on("hotspotStateChange", recvHotspotStateChangeFunc); 1433 1434// Unregister event 1435wifi.off("hotspotStateChange", recvHotspotStateChangeFunc); 1436``` 1437 1438## wifi.off('hotspotStateChange')<sup>7+</sup> 1439 1440off(type: "hotspotStateChange", callback?: Callback<number>): void 1441 1442取消注册热点状态改变事件。 1443 1444**需要权限:** ohos.permission.GET_WIFI_INFO 1445 1446**系统能力:** SystemCapability.Communication.WiFi.AP.Core 1447 1448**参数:** 1449 1450 | **参数名** | **类型** | **必填** | **说明** | 1451 | -------- | -------- | -------- | -------- | 1452 | type | string | 是 | 固定填"hotspotStateChange"字符串。 | 1453 | callback | Callback<number> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 1454 1455 1456 1457## wifi.on('p2pStateChange')<sup>8+</sup> 1458 1459on(type: "p2pStateChange", callback: Callback<number>): void 1460 1461注册P2P开关状态改变事件。 1462 1463**需要权限:** ohos.permission.GET_WIFI_INFO 1464 1465**系统能力:** SystemCapability.Communication.WiFi.P2P 1466 1467**参数:** 1468 1469 | **参数名** | **类型** | **必填** | **说明** | 1470 | -------- | -------- | -------- | -------- | 1471 | type | string | 是 | 固定填"p2pStateChange"字符串。 | 1472 | callback | Callback<number> | 是 | 状态改变回调函数。 | 1473 1474**P2P状态改变事件的枚举:** 1475 1476| **枚举值** | **说明** | 1477| -------- | -------- | 1478| 1 | 空闲。 | 1479| 2 | 打开中。 | 1480| 3 | 已打开。 | 1481| 4 | 关闭中。 | 1482| 5 | 已关闭。 | 1483 1484## wifi.off('p2pStateChange')<sup>8+</sup> 1485 1486off(type: "p2pStateChange", callback?: Callback<number>): void 1487 1488取消注册P2P开关状态改变事件。 1489 1490**需要权限:** ohos.permission.GET_WIFI_INFO 1491 1492**系统能力:** SystemCapability.Communication.WiFi.P2P 1493 1494**参数:** 1495 1496 | **参数名** | **类型** | **必填** | **说明** | 1497 | -------- | -------- | -------- | -------- | 1498 | type | string | 是 | 固定填"p2pStateChange"字符串。 | 1499 | callback | Callback<number> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 1500 1501**示例:** 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 event 1510wifi.on("p2pStateChange", recvP2pStateChangeFunc); 1511 1512// Unregister event 1513wifi.off("p2pStateChange", recvP2pStateChangeFunc); 1514``` 1515 1516## wifi.on('p2pConnectionChange')<sup>8+</sup> 1517 1518on(type: "p2pConnectionChange", callback: Callback<WifiP2pLinkedInfo>): void 1519 1520注册P2P连接状态改变事件。 1521 1522**需要权限:** ohos.permission.GET_WIFI_INFO 1523 1524**系统能力:** SystemCapability.Communication.WiFi.P2P 1525 1526**参数:** 1527 1528 | **参数名** | **类型** | **必填** | **说明** | 1529 | -------- | -------- | -------- | -------- | 1530 | type | string | 是 | 固定填"p2pConnectionChange"字符串。 | 1531 | callback | Callback<[WifiP2pLinkedInfo](#wifip2plinkedinfo8)> | 是 | 状态改变回调函数。 | 1532 1533 1534## wifi.off('p2pConnectionChange')<sup>8+</sup> 1535 1536off(type: "p2pConnectionChange", callback?: Callback<WifiP2pLinkedInfo>): void 1537 1538取消注册P2P连接状态改变事件。 1539 1540**需要权限:** ohos.permission.GET_WIFI_INFO 1541 1542**系统能力:** SystemCapability.Communication.WiFi.P2P 1543 1544**参数:** 1545 1546 | **参数名** | **类型** | **必填** | **说明** | 1547 | -------- | -------- | -------- | -------- | 1548 | type | string | 是 | 固定填"p2pConnectionChange"字符串。 | 1549 | callback | Callback<[WifiP2pLinkedInfo](#wifip2plinkedinfo8)> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 1550 1551**示例:** 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 event 1560wifi.on("p2pConnectionChange", recvP2pConnectionChangeFunc); 1561 1562// Unregister event 1563wifi.off("p2pConnectionChange", recvP2pConnectionChangeFunc); 1564``` 1565 1566## wifi.on('p2pDeviceChange')<sup>8+</sup> 1567 1568on(type: "p2pDeviceChange", callback: Callback<WifiP2pDevice>): void 1569 1570注册P2P设备状态改变事件。 1571 1572**需要权限:** ohos.permission.GET_WIFI_INFO 和 ohos.permission.LOCATION 1573 1574**系统能力:** SystemCapability.Communication.WiFi.P2P 1575 1576**参数:** 1577 1578 | **参数名** | **类型** | **必填** | **说明** | 1579 | -------- | -------- | -------- | -------- | 1580 | type | string | 是 | 固定填"p2pDeviceChange"字符串。 | 1581 | callback | Callback<[WifiP2pDevice](#wifip2pdevice8)> | 是 | 状态改变回调函数。 | 1582 1583 1584## wifi.off('p2pDeviceChange')<sup>8+</sup> 1585 1586off(type: "p2pDeviceChange", callback?: Callback<WifiP2pDevice>): void 1587 1588取消注册P2P设备状态改变事件。 1589 1590**需要权限:** ohos.permission.LOCATION 1591 1592**系统能力:** SystemCapability.Communication.WiFi.P2P 1593 1594**参数:** 1595 1596 | **参数名** | **类型** | **必填** | **说明** | 1597 | -------- | -------- | -------- | -------- | 1598 | type | string | 是 | 固定填"p2pDeviceChange"字符串。 | 1599 | callback | Callback<[WifiP2pDevice](#wifip2pdevice8)> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 1600 1601**示例:** 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 event 1610wifi.on("p2pDeviceChange", recvP2pDeviceChangeFunc); 1611 1612// Unregister event 1613wifi.off("p2pDeviceChange", recvP2pDeviceChangeFunc); 1614``` 1615 1616## wifi.on('p2pPeerDeviceChange')<sup>8+</sup> 1617 1618on(type: "p2pPeerDeviceChange", callback: Callback<WifiP2pDevice[]>): void 1619 1620注册P2P对端设备状态改变事件。 1621 1622**需要权限:** ohos.permission.GET_WIFI_INFO 和 ohos.permission.LOCATION 1623 1624**系统能力:** SystemCapability.Communication.WiFi.P2P 1625 1626**参数:** 1627 1628 | **参数名** | **类型** | **必填** | **说明** | 1629 | -------- | -------- | -------- | -------- | 1630 | type | string | 是 | 固定填"p2pPeerDeviceChange"字符串。 | 1631 | callback | Callback<[WifiP2pDevice[]](#wifip2pdevice8)> | 是 | 状态改变回调函数。 | 1632 1633 1634## wifi.off('p2pPeerDeviceChange')<sup>8+</sup> 1635 1636off(type: "p2pPeerDeviceChange", callback?: Callback<WifiP2pDevice[]>): void 1637 1638取消注册P2P对端设备状态改变事件。 1639 1640**需要权限:** ohos.permission.LOCATION 1641 1642**系统能力:** SystemCapability.Communication.WiFi.P2P 1643 1644**参数:** 1645 1646 | **参数名** | **类型** | **必填** | **说明** | 1647 | -------- | -------- | -------- | -------- | 1648 | type | string | 是 | 固定填"p2pPeerDeviceChange"字符串。 | 1649 | callback | Callback<[WifiP2pDevice[]](#wifip2pdevice8)> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 1650 1651**示例:** 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 event 1660wifi.on("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc); 1661 1662// Unregister event 1663wifi.off("p2pPeerDeviceChange", recvP2pPeerDeviceChangeFunc); 1664``` 1665 1666## wifi.on('p2pPersistentGroupChange')<sup>8+</sup> 1667 1668on(type: "p2pPersistentGroupChange", callback: Callback<void>): void 1669 1670注册P2P永久组状态改变事件。 1671 1672**需要权限:** ohos.permission.GET_WIFI_INFO 1673 1674**系统能力:** SystemCapability.Communication.WiFi.P2P 1675 1676**参数:** 1677 1678 | **参数名** | **类型** | **必填** | **说明** | 1679 | -------- | -------- | -------- | -------- | 1680 | type | string | 是 | 固定填"p2pPersistentGroupChange"字符串。 | 1681 | callback | Callback<void> | 是 | 状态改变回调函数。 | 1682 1683 1684## wifi.off('p2pPersistentGroupChange')<sup>8+</sup> 1685 1686off(type: "p2pPersistentGroupChange", callback?: Callback<void>): void 1687 1688取消注册P2P永久组状态改变事件。 1689 1690**需要权限:** ohos.permission.GET_WIFI_INFO 1691 1692**系统能力:** SystemCapability.Communication.WiFi.P2P 1693 1694**参数:** 1695 1696 | **参数名** | **类型** | **必填** | **说明** | 1697 | -------- | -------- | -------- | -------- | 1698 | type | string | 是 | 固定填"p2pPersistentGroupChange"字符串。 | 1699 | callback | Callback<void> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 1700 1701**示例:** 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 event 1710wifi.on("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc); 1711 1712// Unregister event 1713wifi.off("p2pPersistentGroupChange", recvP2pPersistentGroupChangeFunc); 1714 1715``` 1716 1717## wifi.on('p2pDiscoveryChange')<sup>8+</sup> 1718 1719on(type: "p2pDiscoveryChange", callback: Callback<number>): void 1720 1721注册发现设备状态改变事件。 1722 1723**需要权限:** ohos.permission.GET_WIFI_INFO 1724 1725**系统能力:** SystemCapability.Communication.WiFi.P2P 1726 1727**参数:** 1728 1729 | **参数名** | **类型** | **必填** | **说明** | 1730 | -------- | -------- | -------- | -------- | 1731 | type | string | 是 | 固定填"p2pDiscoveryChange"字符串。 | 1732 | callback | Callback<number> | 是 | 状态改变回调函数。 | 1733 1734**发现设备状态改变事件的枚举:** 1735 1736| **枚举值** | **说明** | 1737| -------- | -------- | 1738| 0 | 初始状态。 | 1739| 1 | 发现成功。 | 1740 1741 1742## wifi.off('p2pDiscoveryChange')<sup>8+</sup> 1743 1744off(type: "p2pDiscoveryChange", callback?: Callback<number>): void 1745 1746取消注册发现设备状态改变事件。 1747 1748**需要权限:** ohos.permission.GET_WIFI_INFO 1749 1750**系统能力:** SystemCapability.Communication.WiFi.P2P 1751 1752**参数:** 1753 1754 | **参数名** | **类型** | **必填** | **说明** | 1755 | -------- | -------- | -------- | -------- | 1756 | type | string | 是 | 固定填"p2pDiscoveryChange"字符串。 | 1757 | callback | Callback<number> | 否 | 状态改变回调函数。如果callback不填,将取消注册该事件关联的所有回调函数。 | 1758 1759**示例:** 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 event 1768wifi.on("p2pDiscoveryChange", recvP2pDiscoveryChangeFunc); 1769 1770// Unregister event 1771wifi.off("p2pDiscoveryChange", recvP2pDiscoveryChangeFunc); 1772```