1# @ohos.wifi (WLAN)(系统接口) 2 3该模块主要提供WLAN基础功能、P2P(peer-to-peer)功能和WLAN消息通知的相应服务,让应用可以通过WLAN和其他设备互联互通。 4 5> **说明:** 6> 7> 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.wifi (WLAN)](js-apis-wifi.md) 9> 从API Version 9 开始,该接口不再维护,推荐使用[`@ohos.wifiManager (WLAN)`](js-apis-wifiManager-sys.md)等相关接口。 10 11 12## 导入模块 13 14```ts 15import wifi from '@ohos.wifi'; 16``` 17 18## wifi.enableWifi 19 20enableWifi(): boolean 21 22使能WLAN。 23 24**系统接口:** 此接口为系统接口。 25 26**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 27 28**系统能力:** SystemCapability.Communication.WiFi.STA 29 30**返回值:** 31 32 | **类型** | **说明** | 33 | -------- | -------- | 34 | boolean | true:操作成功, false:操作失败。| 35 36**示例:** 37 38```ts 39import wifi from '@ohos.wifi'; 40 41try { 42 wifi.enableWifi(); 43}catch(error){ 44 console.error("failed:" + JSON.stringify(error)); 45} 46``` 47 48## wifi.disableWifi 49 50disableWifi(): boolean 51 52去使能WLAN。 53 54**系统接口:** 此接口为系统接口。 55 56**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 57 58**系统能力:** SystemCapability.Communication.WiFi.STA 59 60**返回值:** 61 62 | **类型** | **说明** | 63 | -------- | -------- | 64 | boolean | true:操作成功, false:操作失败。| 65 66**示例:** 67 68```ts 69import wifi from '@ohos.wifi'; 70 71try { 72 wifi.disableWifi(); 73}catch(error){ 74 console.error("failed:" + JSON.stringify(error)); 75} 76 77``` 78 79 80## wifi.addDeviceConfig 81 82addDeviceConfig(config: WifiDeviceConfig): Promise<number> 83 84添加网络配置,使用Promise异步回调。 85 86**系统接口:** 此接口为系统接口。 87 88**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.SET_WIFI_CONFIG 89 90**系统能力:** SystemCapability.Communication.WiFi.STA 91 92**参数:** 93 94 | **参数名** | **类型** | **必填** | **说明** | 95 | -------- | -------- | -------- | -------- | 96 | config | [WifiDeviceConfig](#wifideviceconfig) | 是 | WLAN配置信息。 | 97 98**返回值:** 99 100 | **类型** | **说明** | 101 | -------- | -------- | 102 | Promise<number> | Promise对象。返回添加的网络配置ID,如果值为-1表示添加失败。 | 103 104 **示例:** 105 106```ts 107import wifi from '@ohos.wifi'; 108 109try { 110 let config:wifi.WifiDeviceConfig = { 111 ssid : "****", 112 bssid: "****", 113 preSharedKey: "****", 114 isHiddenSsid: false, 115 securityType: 0, 116 creatorUid: 0, 117 disableReason: 0, 118 netId: 0, 119 randomMacType: 0, 120 randomMacAddr: "****", 121 ipType: 0, 122 staticIp: { 123 ipAddress: 0, 124 gateway: 0, 125 dnsServers: [], 126 domains: [] 127 } 128 } 129 wifi.addDeviceConfig(config).then(result => { 130 console.info("result:" + JSON.stringify(result)); 131 }); 132}catch(error){ 133 console.error("failed:" + JSON.stringify(error)); 134} 135``` 136 137## WifiDeviceConfig 138 139WLAN配置信息。 140 141**系统能力:** SystemCapability.Communication.WiFi.STA 142 143 144| **名称** | **类型** | **可读** | **可写** | **说明** | 145| -------- | -------- | -------- | -------- | -------- | 146| creatorUid | number | 是 | 否 | 创建用户的ID。 <br /> **系统接口:** 此接口为系统接口。 | 147| disableReason | number | 是 | 否 | 禁用原因。 <br /> **系统接口:** 此接口为系统接口。 | 148| netId | number | 是 | 否 | 分配的网络ID。 <br /> **系统接口:** 此接口为系统接口。 | 149| randomMacType | number | 是 | 否 | 随机MAC类型。 <br /> **系统接口:** 此接口为系统接口。 | 150| randomMacAddr | string | 是 | 否 | 随机MAC地址。 <br /> **系统接口:** 此接口为系统接口。 | 151| ipType | [IpType](#iptype7) | 是 | 否 | IP地址类型。 <br /> **系统接口:** 此接口为系统接口。 | 152| family<sup>20+</sup> | number | 否 | 是 | ip协议版本。 <br /> **系统接口:** 此接口为系统接口。 | 153| staticIp | [IpConfig](#ipconfig7) | 否 | 是 | 静态IPv4配置信息。 <br /> **系统接口:** 此接口为系统接口。 | 154| staticIpv6<sup>20+</sup> | [Ipv6Config](#ipv6config20) | 否 | 是 | 静态IPv6配置信息。 <br /> **系统接口:** 此接口为系统接口。 | 155 156 157## IpType<sup>7+</sup> 158 159表示IP类型的枚举。 160 161**系统接口:** 此接口为系统接口。 162 163**系统能力:** SystemCapability.Communication.WiFi.STA 164 165 166| 名称 | 值 | 说明 | 167| -------- | -------- | -------- | 168| STATIC | 0 | 静态IP。 | 169| DHCP | 1 | 通过DHCP获取。 | 170| UNKNOWN | 2 | 未指定。 | 171 172 173## IpConfig<sup>7+</sup> 174 175IPv4配置信息。 176 177**系统接口:** 此接口为系统接口。 178 179**系统能力:** SystemCapability.Communication.WiFi.STA 180 181| **名称** | **类型** | **只读** | **可选** | **说明** | 182| -------- | -------- | -------- | -------- | -------- | 183| ipAddress | number | 否 | 否 | IPv4地址。 | 184| gateway | number | 否 | 否 | 网关。 | 185| prefixLength | number | 否 | 否 | 前缀长度。 | 186| dnsServers | number[] | 否 | 否 | DNS服务器。 | 187| domains | Array<string> | 否 | 否 | 域信息。 | 188 189 190## Ipv6Config<sup>20+</sup> 191 192IPv6配置信息。 193 194**系统接口:** 此接口为系统接口。 195 196**系统能力:** SystemCapability.Communication.WiFi.STA 197 198| **名称** | **类型** | **只读** | **可选** | **说明** | 199| -------- | -------- | -------- | -------- | -------- | 200| ipAddress | string | 否 | 否 | IPv6地址。 | 201| gateway | string| 否 | 否 | 网关。 | 202| prefixLength | number | 否 | 否 | 前缀长度。 | 203| dnsServers | Array\<string> | 否 | 否 | DNS服务器。 | 204| domains | Array\<string> | 否 | 否 | 域信息。 | 205 206 207 208## wifi.addDeviceConfig 209 210addDeviceConfig(config: WifiDeviceConfig, callback: AsyncCallback<number>): void 211 212添加网络配置,使用callback异步回调。 213 214**系统接口:** 此接口为系统接口。 215 216**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.SET_WIFI_CONFIG 217 218**系统能力:** SystemCapability.Communication.WiFi.STA 219 220**参数:** 221 222 | **参数名** | **类型** | **必填** | **说明** | 223 | -------- | -------- | -------- | -------- | 224 | config | [WifiDeviceConfig](#wifideviceconfig) | 是 | WLAN配置信息。 | 225 | callback | AsyncCallback<number> | 是 | 回调函数。当操作成功时,err为0,data为添加的网络配置ID,如果data值为-1,表示添加失败。当error为非0,表示处理出现错误。 | 226 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.addDeviceConfig(config,(error,result) => { 253 console.info("result:" + JSON.stringify(result)); 254 }); 255}catch(error){ 256 console.error("failed:" + JSON.stringify(error)); 257} 258``` 259 260 261## wifi.connectToNetwork 262 263connectToNetwork(networkId: number): boolean 264 265连接到指定网络。 266 267**系统接口:** 此接口为系统接口。 268 269**需要权限:** ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 270 271**系统能力:** SystemCapability.Communication.WiFi.STA 272 273**参数:** 274 275 | **参数名** | **类型** | **必填** | **说明** | 276 | -------- | -------- | -------- | -------- | 277 | networkId | number | 是 | 待连接的网络配置ID。 | 278 279**返回值:** 280 281 | **类型** | **说明** | 282 | -------- | -------- | 283 | boolean | true:操作成功, false:操作失败。 | 284 285**示例:** 286 287```ts 288import wifi from '@ohos.wifi'; 289 290try { 291 let networkId = 0; 292 wifi.connectToNetwork(networkId); 293}catch(error){ 294 console.error("failed:" + JSON.stringify(error)); 295} 296``` 297 298## wifi.connectToDevice 299 300connectToDevice(config: WifiDeviceConfig): boolean 301 302连接到指定网络。 303 304**系统接口:** 此接口为系统接口。 305 306**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.SET_WIFI_CONFIG 和 ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 307 308**系统能力:** 309 SystemCapability.Communication.WiFi.STA 310 311**参数:** 312 313 | **参数名** | **类型** | **必填** | **说明** | 314 | -------- | -------- | -------- | -------- | 315 | config | [WifiDeviceConfig](#wifideviceconfig) | 是 | WLAN配置信息。 | 316 317**返回值:** 318 319 | **类型** | **说明** | 320 | -------- | -------- | 321 | boolean | true:操作成功, false:操作失败。 | 322 323**示例:** 324```ts 325import wifi from '@ohos.wifi'; 326 327try { 328 let config:wifi.WifiDeviceConfig = { 329 ssid : "****", 330 bssid: "****", 331 preSharedKey: "****", 332 isHiddenSsid: false, 333 securityType: 3, 334 creatorUid: 0, 335 disableReason: 0, 336 netId: 0, 337 randomMacType: 0, 338 randomMacAddr: "****", 339 ipType: 0, 340 staticIp: { 341 ipAddress: 0, 342 gateway: 0, 343 dnsServers: [], 344 domains: [] 345 } 346 } 347 wifi.connectToDevice(config); 348 349}catch(error){ 350 console.error("failed:" + JSON.stringify(error)); 351} 352``` 353 354## wifi.disconnect 355 356disconnect(): boolean 357 358断开连接的网络。 359 360**系统接口:** 此接口为系统接口。 361 362**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 363 364**系统能力:** 365 SystemCapability.Communication.WiFi.STA 366 367**返回值:** 368 369 | **类型** | **说明** | 370 | -------- | -------- | 371 | boolean | true:操作成功, false:操作失败。 | 372 373**示例:** 374```ts 375import wifi from '@ohos.wifi'; 376 377try { 378 wifi.disconnect(); 379}catch(error){ 380 console.error("failed:" + JSON.stringify(error)); 381} 382``` 383 384 385## WifiLinkedInfo 386 387提供WLAN连接的相关信息。 388 389**系统能力:** SystemCapability.Communication.WiFi.STA 390 391| 名称 | 类型 | 可读 | 可写 | 说明 | 392| -------- | -------- | -------- | -------- | -------- | 393| networkId | number | 是 | 否 | 网络配置ID。 <br /> **系统接口:** 此接口为系统接口。 | 394| chload | number | 是 | 否 | 连接负载,值越大表示负载约高。 <br /> **系统接口:** 此接口为系统接口。 | 395| snr | number | 是 | 否 | 信噪比。 <br /> **系统接口:** 此接口为系统接口。 | 396| suppState | [SuppState](#suppstate) | 是 | 否 | 请求状态。 <br /> **系统接口:** 此接口为系统接口。 | 397 398 399## SuppState 400 401表示请求状态的枚举。 402 403**系统接口:** 此接口为系统接口。 404 405**系统能力:** SystemCapability.Communication.WiFi.STA 406 407| 名称 | 值 | 说明 | 408| -------- | -------- | -------- | 409| DISCONNECTED | 0 | 已断开。 | 410| INTERFACE_DISABLED | 1 | 接口禁用。 | 411| INACTIVE | 2 | 未激活。 | 412| SCANNING | 3 | 扫描中。 | 413| AUTHENTICATING | 4 | 认证中。 | 414| ASSOCIATING | 5 | 关联中。 | 415| ASSOCIATED | 6 | 已关联。 | 416| FOUR_WAY_HANDSHAKE | 7 | 四次握手。 | 417| GROUP_HANDSHAKE | 8 | 组握手。 | 418| COMPLETED | 9 | 所有认证已完成。 | 419| UNINITIALIZED | 10 | 连接建立失败。 | 420| INVALID | 11 | 无效值。 | 421 422 423 424## wifi.getSupportedFeatures<sup>7+</sup> 425 426getSupportedFeatures(): number 427 428查询设备支持的特性。 429 430**系统接口:** 此接口为系统接口。 431 432**需要权限:** ohos.permission.GET_WIFI_INFO 433 434**系统能力:** SystemCapability.Communication.WiFi.Core 435 436**返回值:** 437 438 | **类型** | **说明** | 439 | -------- | -------- | 440 | number | 支持的特性值。 | 441 442**特性ID值枚举:** 443 444| 枚举值 | 说明 | 445| -------- | -------- | 446| 0x0001 | 基础结构模式特性。 | 447| 0x0002 | 5 GHz带宽特性。 | 448| 0x0004 | GAS/ANQP特性。 | 449| 0x0008 | Wifi-Direct特性。 | 450| 0x0010 | Soft AP特性。 | 451| 0x0040 | Wi-Fi AWare组网特性。 | 452| 0x8000 | AP STA共存特性。 | 453| 0x8000000 | WPA3-Personal SAE特性。 | 454| 0x10000000 | WPA3-Enterprise Suite-B | 455| 0x20000000 | 增强开放特性。 | 456 457 458 459 460## wifi.getDeviceMacAddress<sup>7+</sup> 461 462getDeviceMacAddress(): string[] 463 464获取设备的MAC地址。 465 466**系统接口:** 此接口为系统接口。 467 468**需要权限:** ohos.permission.GET_WIFI_LOCAL_MAC 和 ohos.permission.GET_WIFI_INFO,仅系统应用可用。 469 470**系统能力:** SystemCapability.Communication.WiFi.STA 471 472**返回值:** 473 474 | **类型** | **说明** | 475 | -------- | -------- | 476 | string[] | MAC地址。 | 477 478**示例:** 479```ts 480import wifi from '@ohos.wifi'; 481 482try { 483 let ret = wifi.getDeviceMacAddress(); 484 console.info("deviceMacAddress:" + JSON.stringify(ret)); 485}catch(error){ 486 console.error("failed:" + JSON.stringify(error)); 487} 488 489``` 490 491 492## wifi.reassociate<sup>7+</sup> 493 494reassociate(): boolean 495 496重新关联网络。 497 498**系统接口:** 此接口为系统接口。 499 500**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 501 502**系统能力:** SystemCapability.Communication.WiFi.STA 503 504**返回值:** 505 506 | **类型** | **说明** | 507 | -------- | -------- | 508 | boolean | true:操作成功, false:操作失败。 | 509 510**示例:** 511```ts 512import wifi from '@ohos.wifi'; 513 514try { 515 wifi.reassociate(); 516}catch(error){ 517 console.error("failed:" + JSON.stringify(error)); 518} 519``` 520 521## wifi.reconnect<sup>7+</sup> 522 523reconnect(): boolean 524 525重新连接网络。 526 527**系统接口:** 此接口为系统接口。 528 529**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 530 531**系统能力:** SystemCapability.Communication.WiFi.STA 532 533**返回值:** 534 535 | **类型** | **说明** | 536 | -------- | -------- | 537 | boolean | true:操作成功, false:操作失败。 | 538 539**示例:** 540```ts 541import wifi from '@ohos.wifi'; 542 543try { 544 wifi.reconnect(); 545}catch(error){ 546 console.error("failed:" + JSON.stringify(error)); 547} 548``` 549 550## wifi.getDeviceConfigs<sup>7+</sup> 551 552getDeviceConfigs(): Array<[WifiDeviceConfig](#wifideviceconfig)> 553 554获取网络配置。 555 556**系统接口:** 此接口为系统接口。 557 558**需要权限:** ohos.permission.GET_WIFI_INFO 和 ohos.permission.LOCATION 和 ohos.permission.GET_WIFI_CONFIG 559 560**系统能力:** SystemCapability.Communication.WiFi.STA 561 562**返回值:** 563 564 | **类型** | **说明** | 565 | -------- | -------- | 566 | Array<[WifiDeviceConfig](#wifideviceconfig)> | 网络配置信息的数组。 | 567 568**示例:** 569```ts 570import wifi from '@ohos.wifi'; 571 572try { 573 let configs = wifi.getDeviceConfigs(); 574 console.info("configs:" + JSON.stringify(configs)); 575}catch(error){ 576 console.error("failed:" + JSON.stringify(error)); 577} 578``` 579 580## wifi.updateNetwork<sup>7+</sup> 581 582updateNetwork(config: WifiDeviceConfig): number 583 584更新网络配置。 585 586**系统接口:** 此接口为系统接口。 587 588**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.SET_WIFI_CONFIG 589 590**系统能力:** SystemCapability.Communication.WiFi.STA 591 592**参数:** 593 594 | **参数名** | **类型** | **必填** | **说明** | 595 | -------- | -------- | -------- | -------- | 596 | config | [WifiDeviceConfig](#wifideviceconfig) | 是 | WLAN配置信息。 | 597 598**返回值:** 599 600 | **类型** | **说明** | 601 | -------- | -------- | 602 | number | 返回更新的网络配置ID,如果值为-1表示更新失败。 | 603 604**示例:** 605```ts 606import wifi from '@ohos.wifi'; 607 608try { 609 let config:wifi.WifiDeviceConfig = { 610 ssid : "****", 611 bssid: "****", 612 preSharedKey: "****", 613 isHiddenSsid: false, 614 securityType: 3, 615 creatorUid: 0, 616 disableReason: 0, 617 netId: 0, 618 randomMacType: 0, 619 randomMacAddr: "****", 620 ipType: 0, 621 staticIp: { 622 ipAddress: 0, 623 gateway: 0, 624 dnsServers: [], 625 domains: [] 626 } 627 } 628 let ret = wifi.updateNetwork(config); 629 console.error("ret:" + ret); 630}catch(error){ 631 console.error("failed:" + JSON.stringify(error)); 632} 633``` 634 635## wifi.disableNetwork<sup>7+</sup> 636 637disableNetwork(netId: number): boolean 638 639去使能网络配置。 640 641**系统接口:** 此接口为系统接口。 642 643**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 644 645**系统能力:** SystemCapability.Communication.WiFi.STA 646 647**参数:** 648 649 | **参数名** | **类型** | **必填** | **说明** | 650 | -------- | -------- | -------- | -------- | 651 | netId | number | 是 | 网络配置ID。 | 652 653**返回值:** 654 655 | **类型** | **说明** | 656 | -------- | -------- | 657 | boolean | true:操作成功, false:操作失败。 | 658 659**示例:** 660```ts 661import wifi from '@ohos.wifi'; 662 663try { 664 let netId = 0; 665 wifi.disableNetwork(netId); 666}catch(error){ 667 console.error("failed:" + JSON.stringify(error)); 668} 669``` 670 671## wifi.removeAllNetwork<sup>7+</sup> 672 673removeAllNetwork(): boolean 674 675移除所有网络配置。 676 677**系统接口:** 此接口为系统接口。 678 679**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 680 681**系统能力:** SystemCapability.Communication.WiFi.STA 682 683**返回值:** 684 685 | **类型** | **说明** | 686 | -------- | -------- | 687 | boolean | true:操作成功, false:操作失败。 | 688 689**示例:** 690```ts 691import wifi from '@ohos.wifi'; 692 693try { 694 wifi.removeAllNetwork(); 695}catch(error){ 696 console.error("failed:" + JSON.stringify(error)); 697} 698``` 699 700## wifi.removeDevice<sup>7+</sup> 701 702removeDevice(id: number): boolean 703 704移除指定的网络配置。 705 706**系统接口:** 此接口为系统接口。 707 708**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 709 710**系统能力:** SystemCapability.Communication.WiFi.STA 711 712**参数:** 713 714 | **参数名** | **类型** | **必填** | **说明** | 715 | -------- | -------- | -------- | -------- | 716 | id | number | 是 | 网络配置ID。 | 717 718**返回值:** 719 720 | **类型** | **说明** | 721 | -------- | -------- | 722 | boolean | true:操作成功, false:操作失败。 | 723 724**示例:** 725```ts 726import wifi from '@ohos.wifi'; 727 728try { 729 let id = 0; 730 wifi.removeDevice(id); 731}catch(error){ 732 console.error("failed:" + JSON.stringify(error)); 733} 734``` 735 736## wifi.enableHotspot<sup>7+</sup> 737 738enableHotspot(): boolean 739 740使能热点。 741 742**系统接口:** 此接口为系统接口。 743 744**需要权限:** ohos.permission.MANAGE_WIFI_HOTSPOT,仅系统应用可用。 745 746**系统能力:** SystemCapability.Communication.WiFi.AP.Core 747 748**返回值:** 749 750 | **类型** | **说明** | 751 | -------- | -------- | 752 | boolean | true:操作成功, false:操作失败。| 753 754**示例:** 755```ts 756import wifi from '@ohos.wifi'; 757 758try { 759 wifi.enableHotspot(); 760}catch(error){ 761 console.error("failed:" + JSON.stringify(error)); 762} 763``` 764 765## wifi.disableHotspot<sup>7+</sup> 766 767disableHotspot(): boolean 768 769去使能热点。 770 771**系统接口:** 此接口为系统接口。 772 773**需要权限:** ohos.permission.MANAGE_WIFI_HOTSPOT,仅系统应用可用。 774 775**系统能力:** SystemCapability.Communication.WiFi.AP.Core 776 777**返回值:** 778 779 | **类型** | **说明** | 780 | -------- | -------- | 781 | boolean | true:操作成功, false:操作失败。| 782 783**示例:** 784```ts 785import wifi from '@ohos.wifi'; 786 787try { 788 wifi.disableHotspot(); 789}catch(error){ 790 console.error("failed:" + JSON.stringify(error)); 791} 792``` 793 794## wifi.isHotspotDualBandSupported<sup>7+</sup> 795 796isHotspotDualBandSupported(): boolean 797 798热点是否支持双频。 799 800**系统接口:** 此接口为系统接口。 801 802**需要权限:** ohos.permission.GET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_HOTSPOT,仅系统应用可用。 803 804**系统能力:** SystemCapability.Communication.WiFi.AP.Core 805 806**返回值:** 807 808 | **类型** | **说明** | 809 | -------- | -------- | 810 | boolean | true:支持, false:不支持。| 811 812**示例:** 813```ts 814import wifi from '@ohos.wifi'; 815 816try { 817 let ret = wifi.isHotspotDualBandSupported(); 818 console.info("result:" + ret); 819}catch(error){ 820 console.error("failed:" + JSON.stringify(error)); 821} 822``` 823 824## wifi.isHotspotActive<sup>7+</sup> 825 826isHotspotActive(): boolean 827 828热点是否已使能。 829 830**系统接口:** 此接口为系统接口。 831 832**需要权限:** ohos.permission.GET_WIFI_INFO 833 834**系统能力:** SystemCapability.Communication.WiFi.AP.Core 835 836**返回值:** 837 838 | **类型** | **说明** | 839 | -------- | -------- | 840 | boolean | true:已使能, false:未使能。| 841 842**示例:** 843```ts 844import wifi from '@ohos.wifi'; 845 846try { 847 let ret = wifi.isHotspotActive(); 848 console.info("result:" + ret); 849}catch(error){ 850 console.error("failed:" + JSON.stringify(error)); 851} 852``` 853 854## wifi.setHotspotConfig<sup>7+</sup> 855 856setHotspotConfig(config: HotspotConfig): boolean 857 858设置热点配置信息。 859 860**系统接口:** 此接口为系统接口。 861 862**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.GET_WIFI_CONFIG 863 864**系统能力:** SystemCapability.Communication.WiFi.AP.Core 865 866**参数:** 867 868 | **参数名** | **类型** | **必填** | **说明** | 869 | -------- | -------- | -------- | -------- | 870 | config | [HotspotConfig](#hotspotconfig7) | 是 | 热点配置信息。 | 871 872**返回值:** 873 874 | **类型** | **说明** | 875 | -------- | -------- | 876 | boolean | true:操作成功, false:操作失败。 | 877 878**示例:** 879```ts 880import wifi from '@ohos.wifi'; 881 882try { 883 let config:wifi.HotspotConfig = { 884 ssid: "****", 885 securityType: 3, 886 band: 0, 887 preSharedKey: "****", 888 maxConn: 0 889 } 890 let ret = wifi.setHotspotConfig(config); 891 console.info("result:" + ret); 892}catch(error){ 893 console.error("failed:" + JSON.stringify(error)); 894} 895``` 896 897## HotspotConfig<sup>7+</sup> 898 899热点配置信息。 900 901**系统接口:** 此接口为系统接口。 902 903**系统能力:** SystemCapability.Communication.WiFi.AP.Core 904 905| **名称** | **类型** | **可读** | **可写** | **说明** | 906| -------- | -------- | -------- | -------- | -------- | 907| ssid | string | 是 | 否 | 热点的SSID,编码格式为UTF-8。 | 908| securityType | [WifiSecurityType](js-apis-wifi.md#wifisecuritytype) | 是 | 否 | 加密类型。 | 909| band | number | 是 | 否 | 热点的带宽。1: 2.4G, 2: 5G, 3: 双模频段 | 910| preSharedKey | string | 是 | 否 | 热点的密钥。 | 911| maxConn | number | 是 | 否 | 最大设备连接数。 | 912 913 914## wifi.getHotspotConfig<sup>7+</sup> 915 916getHotspotConfig(): HotspotConfig 917 918获取热点配置信息。 919 920**系统接口:** 此接口为系统接口。 921 922**需要权限:** ohos.permission.GET_WIFI_INFO 和 ohos.permission.GET_WIFI_CONFIG 923 924**系统能力:** SystemCapability.Communication.WiFi.AP.Core 925 926**返回值:** 927 928 | **类型** | **说明** | 929 | -------- | -------- | 930 | [HotspotConfig](#hotspotconfig7) | 热点的配置信息。 | 931 932**示例:** 933```ts 934import wifi from '@ohos.wifi'; 935 936try { 937 let config = wifi.getHotspotConfig(); 938 console.info("result:" + JSON.stringify(config)); 939}catch(error){ 940 console.error("failed:" + JSON.stringify(error)); 941} 942``` 943 944## wifi.getStations<sup>7+</sup> 945 946getStations(): Array<[StationInfo](#stationinfo7)> 947 948获取连接的设备。 949 950**系统接口:** 此接口为系统接口。 951 952**需要权限:** ohos.permission.GET_WIFI_INFO 和 ohos.permission.LOCATION 和 ohos.permission.MANAGE_WIFI_HOTSPOT,仅系统应用可用。 953 954**系统能力:** SystemCapability.Communication.WiFi.AP.Core 955 956**返回值:** 957 958 | **类型** | **说明** | 959 | -------- | -------- | 960 | Array<[StationInfo](#stationinfo7)> | 连接的设备数组。 | 961 962**示例:** 963```ts 964import wifi from '@ohos.wifi'; 965 966try { 967 let stations = wifi.getStations(); 968 console.info("result:" + JSON.stringify(stations)); 969}catch(error){ 970 console.error("failed:" + JSON.stringify(error)); 971} 972``` 973 974## StationInfo<sup>7+</sup> 975 976接入的设备信息。 977 978**系统接口:** 此接口为系统接口。 979 980**系统能力:** SystemCapability.Communication.WiFi.AP.Core 981 982| **名称** | **类型** | **可读** | **可写** | **说明** | 983| -------- | -------- | -------- | -------- | -------- | 984| name | string | 是 | 否 | 设备名称。 | 985| macAddress | string | 是 | 否 | MAC地址。 | 986| ipAddress | string | 是 | 否 | IP地址。 | 987 988 989## wifi.deletePersistentGroup<sup>8+</sup> 990 991deletePersistentGroup(netId: number): boolean 992 993删除永久组。 994 995**系统接口:** 此接口为系统接口。 996 997**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION 998 999**系统能力:** SystemCapability.Communication.WiFi.P2P 1000 1001**参数:** 1002 1003 1004 | **参数名** | **类型** | 必填 | **说明** | 1005 | -------- | -------- | -------- | -------- | 1006 | netId | number | 是 | 组的ID。 | 1007 1008**返回值:** 1009 1010 | 类型 | 说明 | 1011 | -------- | -------- | 1012 | boolean | true:操作执行成功,操作执行失败。 | 1013 1014**示例:** 1015```ts 1016import wifi from '@ohos.wifi'; 1017 1018try { 1019 let netId = 0; 1020 wifi.deletePersistentGroup(netId); 1021}catch(error){ 1022 console.error("failed:" + JSON.stringify(error)); 1023} 1024``` 1025 1026 1027## wifi.setDeviceName<sup>8+</sup> 1028 1029setDeviceName(devName: string): boolean 1030 1031设置设备名称。 1032 1033**系统接口:** 此接口为系统接口。 1034 1035**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 1036 1037**系统能力:** SystemCapability.Communication.WiFi.P2P 1038 1039**参数:** 1040 1041 | **参数名** | **类型** | **必填** | **说明** | 1042 | -------- | -------- | -------- | -------- | 1043 | devName | string | 是 | 设备名称。 | 1044 1045**返回值:** 1046 1047 | **类型** | **说明** | 1048 | -------- | -------- | 1049 | boolean | true:操作成功, false:操作失败。 | 1050 1051**示例:** 1052```ts 1053import wifi from '@ohos.wifi'; 1054 1055try { 1056 let name = "****"; 1057 wifi.setDeviceName(name); 1058}catch(error){ 1059 console.error("failed:" + JSON.stringify(error)); 1060} 1061``` 1062 1063## wifi.on('streamChange')<sup>7+</sup> 1064 1065on(type: "streamChange", callback: Callback<number>): void 1066 1067注册WIFI流更改事件。 1068 1069**系统接口:** 此接口为系统接口。 1070 1071**需要权限:** ohos.permission.MANAGE_WIFI_CONNECTION 1072 1073**系统能力:** SystemCapability.Communication.WiFi.STA 1074 1075**参数:** 1076 1077 | **参数名** | **类型** | **必填** | **说明** | 1078 | -------- | -------- | -------- | -------- | 1079 | type | string | 是 | 固定填"streamChange"字符串。 | 1080 | callback | Callback<number> | 是 | 状态改变回调函数,返回0:无,1:向下,2:向上,3:双向。 | 1081 1082## wifi.off('streamChange')<sup>7+</sup> 1083 1084off(type: "streamChange", callback?: Callback<number>): void 1085 1086取消注册WIFI流更改事件。 1087 1088**系统接口:** 此接口为系统接口。 1089 1090**需要权限:** ohos.permission.MANAGE_WIFI_CONNECTION 1091 1092**系统能力:** SystemCapability.Communication.WiFi.STA 1093 1094**参数:** 1095 1096| **参数名** | **类型** | **必填** | **说明** | 1097| -------- | -------- | -------- | -------- | 1098| type | string | 是 | 固定填"streamChange"字符串。 | 1099| callback | Callback<number> | 否| 状态改变回调函数,返回0:无,1:向下,2:向上,3:双向。 | 1100 1101**示例:** 1102```ts 1103import wifi from '@ohos.wifi'; 1104 1105let recvStreamChangeFunc = (result:number) => { 1106 console.info("Receive stream change event: " + result); 1107} 1108 1109// Register event 1110wifi.on("streamChange", recvStreamChangeFunc); 1111 1112// Unregister event 1113wifi.off("streamChange", recvStreamChangeFunc); 1114 1115``` 1116 1117 1118## wifi.on('hotspotStaJoin')<sup>7+</sup> 1119 1120on(type: "hotspotStaJoin", callback: Callback<StationInfo>): void 1121 1122注册wifi热点sta加入事件。 1123 1124**需要权限:** ohos.permission.MANAGE_WIFI_HOTSPOT 1125 1126**系统接口:** 此接口为系统接口。 1127 1128**系统能力:** SystemCapability.Communication.WiFi.AP.Core 1129 1130**参数:** 1131 1132 | **参数名** | **类型** | **必填** | **说明** | 1133 | -------- | -------- | -------- | -------- | 1134 | type | string | 是 | 固定填"hotspotStaJoin"字符串。 | 1135 | callback | Callback<StationInfo> | 是 | 状态改变回调函数。 | 1136 1137## wifi.off('hotspotStaJoin')<sup>7+</sup> 1138 1139off(type: "hotspotStaJoin", callback?: Callback<StationInfo>): void 1140 1141取消注册wifi热点sta加入事件。 1142 1143**需要权限:** ohos.permission.MANAGE_WIFI_HOTSPOT 1144 1145**系统接口:** 此接口为系统接口。 1146 1147**系统能力:** SystemCapability.Communication.WiFi.AP.Core 1148 1149**参数:** 1150 1151 | **参数名** | **类型** | **必填** | **说明** | 1152 | -------- | -------- | -------- | -------- | 1153 | type | string | 是 | 固定填"hotspotStaJoin"字符串。 | 1154 | callback | Callback<StationInfo> | 否 | 状态改变回调函数。 | 1155 1156 **示例:** 1157```ts 1158import wifi from '@ohos.wifi'; 1159 1160let recvHotspotStaJoinFunc = (result:wifi.StationInfo) => { 1161 console.info("Receive hotspot sta join event: " + result); 1162} 1163 1164// Register event 1165wifi.on("hotspotStaJoin", recvHotspotStaJoinFunc); 1166 1167// Unregister event 1168wifi.off("hotspotStaJoin", recvHotspotStaJoinFunc); 1169 1170``` 1171 1172## wifi.on('hotspotStaLeave')<sup>7+</sup> 1173 1174on(type: "hotspotStaLeave", callback: Callback<StationInfo>): void 1175 1176注册wifi热点sta离开事件。 1177 1178**需要权限:** ohos.permission.MANAGE_WIFI_HOTSPOT 1179 1180**系统接口:** 此接口为系统接口。 1181 1182**系统能力:** SystemCapability.Communication.WiFi.AP.Core 1183 1184**参数:** 1185 1186 | **参数名** | **类型** | **必填** | **说明** | 1187 | -------- | -------- | -------- | -------- | 1188 | type | string | 是 | 固定填"hotspotStaLeave"字符串。 | 1189 | callback | Callback<StationInf]> | 是 | 状态改变回调函数。 | 1190 1191## wifi.off('hotspotStaLeave')<sup>7+</sup> 1192 1193off(type: "hotspotStaLeave", callback?: Callback<StationInfo>): void 1194 1195取消注册wifi热点sta离开事件。 1196 1197**需要权限:** ohos.permission.MANAGE_WIFI_HOTSPOT 1198 1199**系统接口:** 此接口为系统接口。 1200 1201**系统能力:** SystemCapability.Communication.WiFi.AP.Core 1202 1203**参数:** 1204 1205 | **参数名** | **类型** | **必填** | **说明** | 1206 | -------- | -------- | -------- | -------- | 1207 | type | string | 是 | 固定填"hotspotStaLeave"字符串。 | 1208 | callback | Callback<StationInf]> | 否 | 状态改变回调函数。 | 1209 1210 **示例:** 1211```ts 1212import wifi from '@ohos.wifi'; 1213 1214let recvHotspotStaLeaveFunc = (result:wifi.StationInfo) => { 1215 console.info("Receive hotspot sta leave event: " + result); 1216} 1217 1218// Register event 1219wifi.on("hotspotStaLeave", recvHotspotStaLeaveFunc); 1220 1221// Unregister event 1222wifi.off("hotspotStaLeave", recvHotspotStaLeaveFunc); 1223 1224``` 1225 1226