1# @ohos.wifiManager (WLAN)(系统接口) 2该模块主要提供WLAN基础功能、P2P(peer-to-peer)功能和WLAN消息通知的相应服务,让应用可以通过WLAN和其他设备互联互通。 3 4> **说明:** 5> 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 6> 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.wifiManager (WLAN)](js-apis-wifiManager.md) 7 8## 导入模块 9 10```ts 11import wifiManager from '@ohos.wifiManager'; 12``` 13 14## wifiManager.enableWifi<sup>9+</sup> 15 16enableWifi(): void 17 18使能WLAN,异步接口,需要通过注册"wifiStateChange"事件的回调来监听是否打开成功。 19 20**系统接口:** 此接口为系统接口。 21 22**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION 仅系统应用可用。 23 24**系统能力:** SystemCapability.Communication.WiFi.STA 25 26**错误码:** 27 28以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 29 30| **错误码ID** | **错误信息** | 31 | -------- | -------- | 32| 2501000 | Operation failed.| 33| 2501003 | Failed for wifi is closing.| 34 35**示例:** 36 37```ts 38 import wifiManager from '@ohos.wifiManager'; 39 40 try { 41 wifiManager.enableWifi(); 42 }catch(error){ 43 console.error("failed:" + JSON.stringify(error)); 44 } 45``` 46 47## wifiManager.disableWifi<sup>9+</sup> 48 49disableWifi(): void 50 51去使能WLAN,异步接口,需要通过注册"wifiStateChange"事件的回调来监听是否关闭成功。 52 53**系统接口:** 此接口为系统接口。 54 55**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 56 57**系统能力:** SystemCapability.Communication.WiFi.STA 58 59**错误码:** 60 61以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 62 63| **错误码ID** | **错误信息** | 64 | -------- | -------- | 65| 2501000 | Operation failed.| 66| 2501004 | Failed for wifi is opening.| 67 68**示例:** 69 70```ts 71 import wifiManager from '@ohos.wifiManager'; 72 73 try { 74 wifiManager.disableWifi(); 75 }catch(error){ 76 console.error("failed:" + JSON.stringify(error)); 77 } 78``` 79 80## wifiManager.startScan<sup>10+</sup> 81 82startScan(): void 83 84**系统接口:** 此接口为系统接口。 85 86启动WLAN扫描。 87 88**需要权限:** ohos.permission.SET_WIFI_INFO 和ohos.permission.MANAGE_WIFI_CONNECTION 89 90**系统能力:** SystemCapability.Communication.WiFi.STA 91 92**错误码:** 93 94以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 95 96| **错误码ID** | **错误信息** | 97 | -------- | -------- | 98| 2501000 | Operation failed.| 99 100**示例:** 101 102```ts 103 import wifiManager from '@ohos.wifiManager'; 104 105 try { 106 wifiManager.startScan(); 107 }catch(error){ 108 console.error("failed:" + JSON.stringify(error)); 109 } 110``` 111 112## wifiManager.setScanAlwaysAllowed<sup>10+</sup> 113 114setScanAlwaysAllowed(isScanAlwaysAllowed: boolean): void 115 116设置是否始终允许扫描。 117 118**系统接口:** 此接口为系统接口。 119 120**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.SET_WIFI_CONFIG 121 122**系统能力:** SystemCapability.Communication.WiFi.STA 123 124**参数:** 125 126| **参数名** | **类型** | **必填** | **说明** | 127| -------- | -------- | -------- | -------- | 128| isScanAlwaysAllowed | boolean | 是 | 是否始终允许扫描。 | 129 130**错误码:** 131 132以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 133 134| **错误码ID** | **错误信息** | 135 | -------- | -------- | 136| 2501000 | Operation failed.| 137 138```ts 139 import wifiManager from '@ohos.wifiManager'; 140 141 try { 142 let isScanAlwaysAllowed = true; 143 wifiManager.setScanAlwaysAllowed(isScanAlwaysAllowed); 144 }catch(error){ 145 console.error("failed:" + JSON.stringify(error)); 146 } 147``` 148 149## wifiManager.getScanAlwaysAllowed<sup>10+</sup> 150 151getScanAlwaysAllowed(): boolean 152 153获取是否始终允许扫描。 154 155**系统接口:** 此接口为系统接口。 156 157**需要权限:** ohos.permission.GET_WIFI_INFO 和 ohos.permission.GET_WIFI_CONFIG 158 159**系统能力:** SystemCapability.Communication.WiFi.STA 160 161**返回值:** 162 163| **类型** | **说明** | 164| -------- | -------- | 165| boolean| 是否始终允许扫描。 true 表示允许触发扫描,false表示在禁用wifi时不允许触发扫描| 166 167**错误码:** 168 169以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 170 171| **错误码ID** | **错误信息** | 172 | -------- | -------- | 173| 2501000 | Operation failed.| 174 175**示例:** 176 177```ts 178 import wifiManager from '@ohos.wifiManager'; 179 180 try { 181 let isScanAlwaysAllowed = wifiManager.getScanAlwaysAllowed(); 182 console.info("isScanAlwaysAllowed:" + isScanAlwaysAllowed); 183 }catch(error){ 184 console.error("failed:" + JSON.stringify(error)); 185 } 186``` 187 188## wifiManager.addDeviceConfig<sup>9+</sup> 189 190addDeviceConfig(config: WifiDeviceConfig): Promise<number> 191 192添加网络配置,使用Promise异步回调。 193 194**系统接口:** 此接口为系统接口。 195 196**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.SET_WIFI_CONFIG 197 198**系统能力:** SystemCapability.Communication.WiFi.STA 199 200**参数:** 201 202| **参数名** | **类型** | **必填** | **说明** | 203| -------- | -------- | -------- | -------- | 204| config | [WifiDeviceConfig](#wifideviceconfig9) | 是 | WLAN配置信息。如果bssidType未指定值,则bssidType默认为随机设备地址类型。 | 205 206**返回值:** 207 208 | **类型** | **说明** | 209 | -------- | -------- | 210 | Promise<number> | Promise对象。返回添加的网络配置ID,如果值为-1表示添加失败。 | 211 212**错误码:** 213 214以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 215 216| **错误码ID** | **错误信息** | 217 | -------- | -------- | 218| 2501000 | Operation failed.| 219 220**示例:** 221 222```ts 223 import wifiManager from '@ohos.wifiManager'; 224 225 try { 226 let config:wifiManager.WifiDeviceConfig = { 227 ssid : "****", 228 preSharedKey : "****", 229 securityType : 0 230 } 231 wifiManager.addDeviceConfig(config).then(result => { 232 console.info("result:" + JSON.stringify(result)); 233 }).catch((err:number) => { 234 console.error("failed:" + JSON.stringify(err)); 235 }); 236 }catch(error){ 237 console.error("failed:" + JSON.stringify(error)); 238 } 239``` 240 241## WifiDeviceConfig<sup>9+</sup> 242 243WLAN配置信息。 244 245**系统能力:** SystemCapability.Communication.WiFi.STA 246 247 248| **名称** | **类型** | **可读** | **可写** | **说明** | 249| -------- | -------- | -------- | -------- | -------- | 250| creatorUid | number | 是 | 否 | 创建用户的ID。 <br /> **系统接口:** 此接口为系统接口。 | 251| disableReason | number | 是 | 否 | 禁用原因。 <br /> **系统接口:** 此接口为系统接口。 | 252| netId | number | 是 | 否 | 分配的网络ID。 <br /> **系统接口:** 此接口为系统接口。 | 253| randomMacType | number | 是 | 否 | MAC地址类型。0 - 随机MAC地址,1 - 设备MAC地址 <br /> **系统接口:** 此接口为系统接口。 | 254| randomMacAddr | string | 是 | 否 | MAC地址。<br /> **系统接口:** 此接口为系统接口。 | 255| ipType | [IpType](#iptype9) | 是 | 否 | IP地址类型。 <br /> **系统接口:** 此接口为系统接口。 | 256| staticIp | [IpConfig](#ipconfig9) | 是 | 否 | 静态IP配置信息。 <br /> **系统接口:** 此接口为系统接口。 | 257| proxyConfig<sup>10+</sup> | [WifiProxyConfig](#wifiproxyconfig10) | 是 | 否 | 代理配置。 <br /> **系统接口:** 此接口为系统接口。| 258 259## IpType<sup>9+</sup> 260 261表示IP类型的枚举。 262 263**系统接口:** 此接口为系统接口。 264 265**系统能力:** SystemCapability.Communication.WiFi.STA 266 267 268| 名称 | 值 | 说明 | 269| -------- | -------- | -------- | 270| STATIC | 0 | 静态IP。 | 271| DHCP | 1 | 通过DHCP获取。 | 272| UNKNOWN | 2 | 未指定。 | 273 274 275## IpConfig<sup>9+</sup> 276 277IP配置信息。 278 279**系统接口:** 此接口为系统接口。 280 281**系统能力:** SystemCapability.Communication.WiFi.STA 282 283| **名称** | **类型** | **可读** | **可写** | **说明** | 284| -------- | -------- | -------- | -------- | -------- | 285| ipAddress | number | 是 | 否 | IP地址。 | 286| gateway | number | 是 | 否 | 网关。 | 287| prefixLength | number | 是 | 否 | 掩码。 | 288| dnsServers | number[] | 是 | 否 | DNS服务器。 | 289| domains | Array<string> | 是 | 否 | 域信息。 | 290 291 292## WifiProxyConfig<sup>10+</sup> 293 294Wifi 代理配置。 295 296**系统接口:** 此接口为系统接口。 297 298**系统能力:** SystemCapability.Communication.WiFi.STA 299 300| **名称** | **类型** | **可读** | **可写** | **说明** | 301| -------- | -------- | -------- | -------- | -------- | 302| proxyMethod | ProxyMethod | 是 | 否 | 代理方法 | 303| pacWebAddress | string | 是 | 否 | 自动配置代理的PAC web 地址。 | 304| serverHostName | string | 是 | 否 | 手动配置代理的服务器主机名。 | 305| serverPort | string | 是 | 否 | 手动配置代理的服务器端口。 | 306| exclusionObjects | string | 是 | 否 | 手动配置代理的排除对象,对象用“,”分隔。| 307 308## ProxyMethod<sup>10+</sup> 309 310表示WiFi代理方法的枚举。 311 312**系统接口:** 此接口为系统接口。 313 314**系统能力:** SystemCapability.Communication.WiFi.STA 315 316| 名称 | 值 | 说明 | 317| -------- | -------- | -------- | 318| METHOD_NONE | 0 | 不使用代理。 | 319| METHOD_AUTO | 1 | 使用自动配置的代理。 | 320| METHOD_MANUAL | 2 | 使用手动配置的代理。 | 321 322## wifiManager.addDeviceConfig<sup>9+</sup> 323 324addDeviceConfig(config: WifiDeviceConfig, callback: AsyncCallback<number>): void 325 326添加网络配置,使用callback异步回调。 327 328**系统接口:** 此接口为系统接口。 329 330**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.SET_WIFI_CONFIG 331 332**系统能力:** SystemCapability.Communication.WiFi.STA 333 334**参数:** 335 336| **参数名** | **类型** | **必填** | **说明** | 337| -------- | -------- | -------- | -------- | 338| config | [WifiDeviceConfig](#wifideviceconfig9) | 是 | WLAN配置信息。如果bssidType未指定值,则bssidType默认为随机设备地址类型。 | 339| callback | AsyncCallback<number> | 是 | 回调函数。当操作成功时,err为0,data为添加的网络配置ID,如果data值为-1,表示添加失败。当error为非0,表示处理出现错误。 | 340 341**错误码:** 342 343以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 344 345| **错误码ID** | **错误信息** | 346 | -------- | -------- | 347| 2501000 | Operation failed.| 348 349**示例:** 350 351```ts 352 import wifiManager from '@ohos.wifiManager'; 353 354 try { 355 let config:wifiManager.WifiDeviceConfig = { 356 ssid : "****", 357 preSharedKey : "****", 358 securityType : 0 359 } 360 wifiManager.addDeviceConfig(config,(error,result) => { 361 console.info("result:" + JSON.stringify(result)); 362 }); 363 }catch(error){ 364 console.error("failed:" + JSON.stringify(error)); 365 } 366``` 367 368 369## wifiManager.connectToNetwork<sup>9+</sup> 370 371connectToNetwork(networkId: number): void 372 373连接到指定网络(如果当前已经连接到热点,请先使用disconnet()接口断开连接)。 374 375**系统接口:** 此接口为系统接口。 376 377**需要权限:** ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 378 379**系统能力:** SystemCapability.Communication.WiFi.STA 380 381**参数:** 382 383 | **参数名** | **类型** | **必填** | **说明** | 384 | -------- | -------- | -------- | -------- | 385 | networkId | number | 是 | 待连接的网络配置ID。 | 386 387**错误码:** 388 389以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 390 391| **错误码ID** | **错误信息** | 392 | -------- | -------- | 393| 2501000 | Operation failed.| 394| 2501001 | Wifi is closed.| 395 396**示例:** 397 398```ts 399 import wifiManager from '@ohos.wifiManager'; 400 401 try { 402 let networkId = 0; 403 wifiManager.connectToNetwork(networkId); 404 }catch(error){ 405 console.error("failed:" + JSON.stringify(error)); 406 } 407``` 408 409## wifiManager.connectToDevice<sup>9+</sup> 410 411connectToDevice(config: WifiDeviceConfig): void 412 413连接到指定网络(如果当前已经连接到热点,请先使用disconnet()接口断开连接)。 414 415**系统接口:** 此接口为系统接口。 416 417**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.SET_WIFI_CONFIG 和 ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 418 419**系统能力:** 420 SystemCapability.Communication.WiFi.STA 421 422**参数:** 423 424| **参数名** | **类型** | **必填** | **说明** | 425| -------- | -------- | -------- | -------- | 426| config | [WifiDeviceConfig](#wifideviceconfig9) | 是 | WLAN配置信息。如果bssidType未指定值,则bssidType默认为随机设备地址类型。 | 427 428**错误码:** 429 430以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 431 432| **错误码ID** | **错误信息** | 433 | -------- | -------- | 434| 2501000 | Operation failed.| 435| 2501001 | Wifi is closed.| 436 437**示例:** 438```ts 439 import wifiManager from '@ohos.wifiManager'; 440 441 try { 442 let config:wifiManager.WifiDeviceConfig = { 443 ssid : "****", 444 preSharedKey : "****", 445 securityType : 3 446 } 447 wifiManager.connectToDevice(config); 448 449 }catch(error){ 450 console.error("failed:" + JSON.stringify(error)); 451 } 452``` 453 454## wifiManager.disconnect<sup>9+</sup> 455 456disconnect(): void 457 458断开连接的网络。 459 460**系统接口:** 此接口为系统接口。 461 462**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 463 464**系统能力:** 465 SystemCapability.Communication.WiFi.STA 466 467**错误码:** 468 469以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 470 471| **错误码ID** | **错误信息** | 472 | -------- | -------- | 473| 2501000 | Operation failed.| 474 475**示例:** 476```ts 477 import wifiManager from '@ohos.wifiManager'; 478 479 try { 480 wifiManager.disconnect(); 481 }catch(error){ 482 console.error("failed:" + JSON.stringify(error)); 483 } 484``` 485 486 487## WifiLinkedInfo<sup>9+</sup> 488 489提供WLAN连接的相关信息。 490 491**系统能力:** SystemCapability.Communication.WiFi.STA 492 493| 名称 | 类型 | 可读 | 可写 | 说明 | 494| -------- | -------- | -------- | -------- | -------- | 495| networkId | number | 是 | 否 | 网络配置ID。 <br /> **系统接口:** 此接口为系统接口。 | 496| chload | number | 是 | 否 | 连接负载,值越大表示负载约高。 <br /> **系统接口:** 此接口为系统接口。 | 497| snr | number | 是 | 否 | 信噪比。 <br /> **系统接口:** 此接口为系统接口。 | 498| suppState | [SuppState](#suppstate9) | 是 | 否 | 请求状态。 <br /> **系统接口:** 此接口为系统接口。 | 499 500 501 502## SuppState<sup>9+</sup> 503 504表示请求状态的枚举。 505 506**系统接口:** 此接口为系统接口。 507 508**系统能力:** SystemCapability.Communication.WiFi.STA 509 510| 名称 | 值 | 说明 | 511| -------- | -------- | -------- | 512| DISCONNECTED | 0 | 已断开。 | 513| INTERFACE_DISABLED | 1 | 接口禁用。 | 514| INACTIVE | 2 | 未激活。 | 515| SCANNING | 3 | 扫描中。 | 516| AUTHENTICATING | 4 | 认证中。 | 517| ASSOCIATING | 5 | 关联中。 | 518| ASSOCIATED | 6 | 已关联。 | 519| FOUR_WAY_HANDSHAKE | 7 | 四次握手。 | 520| GROUP_HANDSHAKE | 8 | 组握手。 | 521| COMPLETED | 9 | 所有认证已完成。 | 522| UNINITIALIZED | 10 | 连接建立失败。 | 523| INVALID | 11 | 无效值。 | 524 525 526## wifiManager.getSupportedFeatures<sup>9+</sup> 527 528getSupportedFeatures(): number 529 530查询设备支持的特性。 531 532**系统接口:** 此接口为系统接口。 533 534**需要权限:** ohos.permission.GET_WIFI_INFO 535 536**系统能力:** SystemCapability.Communication.WiFi.Core 537 538**返回值:** 539 540 | **类型** | **说明** | 541 | -------- | -------- | 542 | number | 支持的特性值。 | 543 544**特性ID值枚举:** 545 546| 枚举值 | 说明 | 547| -------- | -------- | 548| 0x0001 | 基础结构模式特性。 | 549| 0x0002 | 5 GHz带宽特性。 | 550| 0x0004 | GAS/ANQP特性。 | 551| 0x0008 | Wifi-Direct特性。 | 552| 0x0010 | Soft AP特性。 | 553| 0x0040 | Wi-Fi AWare组网特性。 | 554| 0x8000 | AP STA共存特性。 | 555| 0x8000000 | WPA3-Personal SAE特性。 | 556| 0x10000000 | WPA3-Enterprise Suite-B | 557| 0x20000000 | 增强开放特性。 | 558 559**错误码:** 560 561以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 562 563| **错误码ID** | **错误信息** | 564 | -------- | -------- | 565| 2401000 | Operation failed.| 566 567**示例:** 568```ts 569 import wifiManager from '@ohos.wifiManager'; 570 571 try { 572 let ret = wifiManager.getSupportedFeatures(); 573 console.info("supportedFeatures:" + ret); 574 }catch(error){ 575 console.error("failed:" + JSON.stringify(error)); 576 } 577 578``` 579 580 581## wifiManager.getDeviceMacAddress<sup>9+</sup> 582 583getDeviceMacAddress(): string[] 584 585获取设备的MAC地址。 586 587**系统接口:** 此接口为系统接口。 588 589**需要权限:** ohos.permission.GET_WIFI_LOCAL_MAC 和 ohos.permission.GET_WIFI_INFO,仅系统应用可用。 590 591**系统能力:** SystemCapability.Communication.WiFi.STA 592 593**返回值:** 594 595 | **类型** | **说明** | 596 | -------- | -------- | 597 | string[] | MAC地址。 | 598 599**错误码:** 600 601以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 602 603| **错误码ID** | **错误信息** | 604 | -------- | -------- | 605| 2501000 | Operation failed.| 606| 2501001 | wifi is closed.| 607 608**示例:** 609```ts 610 import wifiManager from '@ohos.wifiManager'; 611 612 try { 613 let ret = wifiManager.getDeviceMacAddress(); 614 console.info("deviceMacAddress:" + JSON.stringify(ret)); 615 }catch(error){ 616 console.error("failed:" + JSON.stringify(error)); 617 } 618 619``` 620 621## wifiManager.reassociate<sup>9+</sup> 622 623reassociate(): void 624 625重新关联网络。 626 627**系统接口:** 此接口为系统接口。 628 629**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 630 631**系统能力:** SystemCapability.Communication.WiFi.STA 632 633**错误码:** 634 635以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 636 637| **错误码ID** | **错误信息** | 638 | -------- | -------- | 639| 2501000 | Operation failed.| 640| 2501001 | Wifi is closed.| 641 642**示例:** 643```ts 644 import wifiManager from '@ohos.wifiManager'; 645 646 try { 647 wifiManager.reassociate(); 648 }catch(error){ 649 console.error("failed:" + JSON.stringify(error)); 650 } 651``` 652 653## wifiManager.reconnect<sup>9+</sup> 654 655reconnect(): void 656 657重新连接网络。 658 659**系统接口:** 此接口为系统接口。 660 661**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 662 663**系统能力:** SystemCapability.Communication.WiFi.STA 664 665**错误码:** 666 667以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 668 669| **错误码ID** | **错误信息** | 670 | -------- | -------- | 671| 2501000 | Operation failed.| 672| 2501001 | Wifi is closed.| 673 674**示例:** 675```ts 676 import wifiManager from '@ohos.wifiManager'; 677 678 try { 679 wifiManager.reconnect(); 680 }catch(error){ 681 console.error("failed:" + JSON.stringify(error)); 682 } 683``` 684 685## wifiManager.getDeviceConfigs<sup>9+</sup> 686 687getDeviceConfigs(): Array<[WifiDeviceConfig](#wifideviceconfig9)> 688 689获取网络配置。 690 691**系统接口:** 此接口为系统接口。 692 693**需要权限:** 694 695API 9:ohos.permission.GET_WIFI_INFO、ohos.permission.LOCATION、ohos.permission.APPROXIMATELY_LOCATION 和 ohos.permission.GET_WIFI_CONFIG 696 697API 10起:ohos.permission.GET_WIFI_INFO 和 ohos.permission.GET_WIFI_CONFIG 698 699**系统能力:** SystemCapability.Communication.WiFi.STA 700 701**返回值:** 702 703 | **类型** | **说明** | 704 | -------- | -------- | 705 | Array<[WifiDeviceConfig](#wifideviceconfig9)> | 网络配置信息的数组。 | 706 707**错误码:** 708 709以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 710 711| **错误码ID** | **错误信息** | 712 | -------- | -------- | 713| 2501000 | Operation failed.| 714 715**示例:** 716```ts 717 import wifiManager from '@ohos.wifiManager'; 718 719 try { 720 let configs = wifiManager.getDeviceConfigs(); 721 console.info("configs:" + JSON.stringify(configs)); 722 }catch(error){ 723 console.error("failed:" + JSON.stringify(error)); 724 } 725``` 726 727## wifiManager.updateNetwork<sup>9+</sup> 728 729updateNetwork(config: WifiDeviceConfig): number 730 731更新网络配置。 732 733**系统接口:** 此接口为系统接口。 734 735**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.SET_WIFI_CONFIG 736 737**系统能力:** SystemCapability.Communication.WiFi.STA 738 739**参数:** 740 741 | **参数名** | **类型** | **必填** | **说明** | 742 | -------- | -------- | -------- | -------- | 743 | config | [WifiDeviceConfig](#wifideviceconfig9) | 是 | WLAN配置信息。 | 744 745**返回值:** 746 747 | **类型** | **说明** | 748 | -------- | -------- | 749 | number | 返回更新的网络配置ID,如果值为-1表示更新失败。 | 750 751**错误码:** 752 753以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 754 755| **错误码ID** | **错误信息** | 756 | -------- | -------- | 757| 2501000 | Operation failed.| 758 759**示例:** 760```ts 761 import wifiManager from '@ohos.wifiManager'; 762 763 try { 764 let config:wifiManager.WifiDeviceConfig = { 765 ssid : "****", 766 preSharedKey : "****", 767 securityType : 3 768 } 769 let ret = wifiManager.updateNetwork(config); 770 console.info("ret:" + ret); 771 }catch(error){ 772 console.error("failed:" + JSON.stringify(error)); 773 } 774``` 775 776## wifiManager.disableNetwork<sup>9+</sup> 777 778disableNetwork(netId: number): void 779 780去使能网络配置。 781 782**系统接口:** 此接口为系统接口。 783 784**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 785 786**系统能力:** SystemCapability.Communication.WiFi.STA 787 788**参数:** 789 790 | **参数名** | **类型** | **必填** | **说明** | 791 | -------- | -------- | -------- | -------- | 792 | netId | number | 是 | 网络配置ID。 | 793 794**错误码:** 795 796以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 797 798| **错误码ID** | **错误信息** | 799 | -------- | -------- | 800| 2501000 | Operation failed.| 801 802**示例:** 803```ts 804 import wifiManager from '@ohos.wifiManager'; 805 806 try { 807 let netId = 0; 808 wifiManager.disableNetwork(netId); 809 }catch(error){ 810 console.error("failed:" + JSON.stringify(error)); 811 } 812``` 813 814## wifiManager.removeAllNetwork<sup>9+</sup> 815 816removeAllNetwork(): void 817 818移除所有网络配置。 819 820**系统接口:** 此接口为系统接口。 821 822**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 823 824**系统能力:** SystemCapability.Communication.WiFi.STA 825 826**错误码:** 827 828以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 829 830| **错误码ID** | **错误信息** | 831 | -------- | -------- | 832| 2501000 | Operation failed.| 833 834**示例:** 835```ts 836 import wifiManager from '@ohos.wifiManager'; 837 838 try { 839 wifiManager.removeAllNetwork(); 840 }catch(error){ 841 console.error("failed:" + JSON.stringify(error)); 842 } 843``` 844 845## wifiManager.removeDevice<sup>9+</sup> 846 847removeDevice(id: number): void 848 849移除指定的网络配置。 850 851**系统接口:** 此接口为系统接口。 852 853**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 854 855**系统能力:** SystemCapability.Communication.WiFi.STA 856 857**参数:** 858 859 | **参数名** | **类型** | **必填** | **说明** | 860 | -------- | -------- | -------- | -------- | 861 | id | number | 是 | 网络配置ID。 | 862 863**错误码:** 864 865以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 866 867| **错误码ID** | **错误信息** | 868 | -------- | -------- | 869| 2501000 | Operation failed.| 870 871**示例:** 872```ts 873 import wifiManager from '@ohos.wifiManager'; 874 875 try { 876 let id = 0; 877 wifiManager.removeDevice(id); 878 }catch(error){ 879 console.error("failed:" + JSON.stringify(error)); 880 } 881``` 882 883## wifiManager.get5GChannelList<sup>10+</sup> 884 885get5GChannelList(): Array<number> 886 887获取当前设备支持的5G信道列表。 888 889**系统接口:** 此接口为系统接口。 890 891**需要权限:** ohos.permission.GET_WIFI_INFO 和 ohos.permission.GET_WIFI_CONFIG 892 893**系统能力:** SystemCapability.Communication.WiFi.STA 894 895**返回值:** 896 897 | **类型** | **说明** | 898 | -------- | -------- | 899 | Array<number> | 设备支持的5G信道列表。 | 900 901**错误码:** 902 903以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 904 905| **错误码ID** | **错误信息** | 906 | -------- | -------- | 907| 2501000 | Operation failed.| 908 909**示例:** 910```ts 911 import wifiManager from '@ohos.wifiManager'; 912 913 try { 914 let channelList = wifiManager.get5GChannelList(); 915 console.info("channelList:" + JSON.stringify(channelList)); 916 }catch(error){ 917 console.error("failed:" + JSON.stringify(error)); 918 } 919``` 920## wifiManager.getDisconnectedReason<sup>10+</sup> 921 922getDisconnectedReason(): DisconnectedReason 923 924获取最近一次断连原因。 925 926**系统接口:** 此接口为系统接口。 927 928**需要权限:** ohos.permission.GET_WIFI_INFO 和 ohos.permission.GET_WIFI_CONFIG 929 930**系统能力:** SystemCapability.Communication.WiFi.STA 931 932**错误码:** 933 934以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 935 936| **错误码ID** | **错误信息** | 937 | -------- | -------- | 938| 2501000 | Operation failed.| 939 940**返回值:** 941 942| **类型** | **说明** | 943| -------- | -------- | 944| [DisconnectedReason](#disconnectedreason-10) | 最近断开的原因 | 945 946**示例:** 947```ts 948 import wifiManager from '@ohos.wifiManager'; 949 950 try { 951 let disconnectedReason = wifiManager.getDisconnectedReason(); 952 console.info("disconnectedReason:" + disconnectedReason); 953 }catch(error){ 954 console.error("failed:" + JSON.stringify(error)); 955 } 956``` 957 958## DisconnectedReason <sup>10+</sup> 959 960表示wifi断开原因的枚举。 961 962**系统接口:** 此接口为系统接口。 963 964**系统能力:** SystemCapability.Communication.WiFi.STA 965 966| 名称 | 值 | 说明 | 967| -------- | -------- | -------- | 968| DISC_REASON_DEFAULT | 0 | 默认原因。 | 969| DISC_REASON_WRONG_PWD | 1 | 密码错误。 | 970| DISC_REASON_CONNECTION_FULL | 2 | 路由器的连接数已达到最大数量限制。 | 971 972## wifiManager.startPortalCertification<sup>11+</sup> 973 974startPortalCertification(): void 975 976**系统接口:** 此接口为系统接口。 977 978启动portal认证。 979 980**需要权限:** ohos.permission.SET_WIFI_INFO 和ohos.permission.MANAGE_WIFI_CONNECTION 981 982**系统能力:** SystemCapability.Communication.WiFi.STA 983 984**错误码:** 985 986以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 987 988| **错误码ID** | **错误信息** | 989 | -------- | -------- | 990| 2501000 | Operation failed.| 991 992**示例:** 993 994```ts 995 import wifiManager from '@ohos.wifiManager'; 996 997 try { 998 wifiManager.startPortalCertification(); 999 }catch(error){ 1000 console.error("failed:" + JSON.stringify(error)); 1001 } 1002``` 1003 1004 1005## wifiManager.factoryReset<sup>11+</sup> 1006 1007factoryReset(): void 1008 1009**系统接口:** 此接口为系统接口。 1010 1011重置wifi相关配置。 1012 1013**需要权限:** ohos.permission.SET_WIFI_INFO 和ohos.permission.SET_WIFI_CONFIG 1014 1015**系统能力:** SystemCapability.Communication.WiFi.STA 1016 1017**错误码:** 1018 1019以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1020 1021| **错误码ID** | **错误信息** | 1022 | -------- | -------- | 1023| 2501000 | Operation failed.| 1024 1025**示例:** 1026 1027```ts 1028 import wifiManager from '@ohos.wifiManager'; 1029 1030 try { 1031 wifiManager.factoryReset(); 1032 }catch(error){ 1033 console.error("failed:" + JSON.stringify(error)); 1034 } 1035``` 1036## wifiManager.enableHotspot<sup>9+</sup> 1037 1038enableHotspot(): void 1039 1040使能热点,异步接口,是否打开成功需要注册并监听hotspotStateChange的回调。 1041 1042**系统接口:** 此接口为系统接口。 1043 1044**需要权限:** ohos.permission.MANAGE_WIFI_HOTSPOT,仅系统应用可用。 1045 1046**系统能力:** SystemCapability.Communication.WiFi.AP.Core 1047 1048**错误码:** 1049 1050以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1051 1052| **错误码ID** | **错误信息** | 1053 | -------- | -------- | 1054| 2601000 | Operation failed.| 1055 1056**示例:** 1057```ts 1058 import wifiManager from '@ohos.wifiManager'; 1059 1060 try { 1061 wifiManager.enableHotspot(); 1062 }catch(error){ 1063 console.error("failed:" + JSON.stringify(error)); 1064 } 1065``` 1066 1067## wifiManager.disableHotspot<sup>9+</sup> 1068 1069disableHotspot(): void 1070 1071去使能热点 ,异步接口,是否关闭成功需要注册并监听hotspotStateChange的回调。 1072 1073**系统接口:** 此接口为系统接口。 1074 1075**需要权限:** ohos.permission.MANAGE_WIFI_HOTSPOT,仅系统应用可用。 1076 1077**系统能力:** SystemCapability.Communication.WiFi.AP.Core 1078 1079**错误码:** 1080 1081以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1082 1083| **错误码ID** | **错误信息** | 1084 | -------- | -------- | 1085| 2601000 | Operation failed.| 1086 1087**示例:** 1088```ts 1089 import wifiManager from '@ohos.wifiManager'; 1090 1091 try { 1092 wifiManager.disableHotspot(); 1093 }catch(error){ 1094 console.error("failed:" + JSON.stringify(error)); 1095 } 1096``` 1097 1098## wifiManager.isHotspotDualBandSupported<sup>9+</sup> 1099 1100isHotspotDualBandSupported(): boolean 1101 1102热点是否支持双频。 1103 1104**系统接口:** 此接口为系统接口。 1105 1106**需要权限:** ohos.permission.GET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_HOTSPOT,仅系统应用可用。 1107 1108**系统能力:** SystemCapability.Communication.WiFi.AP.Core 1109 1110**返回值:** 1111 1112 | **类型** | **说明** | 1113 | -------- | -------- | 1114 | boolean | true:支持, false:不支持.| 1115 1116**错误码:** 1117 1118以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1119 1120| **错误码ID** | **错误信息** | 1121 | -------- | -------- | 1122| 2601000 | Operation failed.| 1123 1124**示例:** 1125```ts 1126 import wifiManager from '@ohos.wifiManager'; 1127 1128 try { 1129 let ret = wifiManager.isHotspotDualBandSupported(); 1130 console.info("result:" + ret); 1131 }catch(error){ 1132 console.error("failed:" + JSON.stringify(error)); 1133 } 1134``` 1135 1136## wifiManager.isHotspotActive<sup>9+</sup> 1137 1138isHotspotActive(): boolean 1139 1140热点是否已使能。 1141 1142**系统接口:** 此接口为系统接口。 1143 1144**需要权限:** ohos.permission.GET_WIFI_INFO 1145 1146**系统能力:** SystemCapability.Communication.WiFi.AP.Core 1147 1148**返回值:** 1149 1150 | **类型** | **说明** | 1151 | -------- | -------- | 1152 | boolean | true:已使能, false:未使能.| 1153 1154**错误码:** 1155 1156以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1157 1158| **错误码ID** | **错误信息** | 1159 | -------- | -------- | 1160| 2601000 | Operation failed.| 1161 1162**示例:** 1163```ts 1164 import wifiManager from '@ohos.wifiManager'; 1165 1166 try { 1167 let ret = wifiManager.isHotspotActive(); 1168 console.info("result:" + ret); 1169 }catch(error){ 1170 console.error("failed:" + JSON.stringify(error)); 1171 } 1172``` 1173 1174## wifiManager.setHotspotConfig<sup>9+</sup> 1175 1176setHotspotConfig(config: HotspotConfig): void 1177 1178设置热点配置信息。 1179 1180**系统接口:** 此接口为系统接口。 1181 1182**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.GET_WIFI_CONFIG 1183 1184**系统能力:** SystemCapability.Communication.WiFi.AP.Core 1185 1186**参数:** 1187 1188 | **参数名** | **类型** | **必填** | **说明** | 1189 | -------- | -------- | -------- | -------- | 1190 | config | [HotspotConfig](#hotspotconfig9) | 是 | 热点配置信息。 | 1191 1192**错误码:** 1193 1194以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1195 1196| **错误码ID** | **错误信息** | 1197 | -------- | -------- | 1198| 2601000 | Operation failed.| 1199 1200**示例:** 1201```ts 1202 import wifiManager from '@ohos.wifiManager'; 1203 1204 try { 1205 let config:wifiManager.HotspotConfig = { 1206 ssid: "****", 1207 securityType: 3, 1208 band: 0, 1209 channel: 0, 1210 preSharedKey: "****", 1211 maxConn: 0 1212 } 1213 let ret = wifiManager.setHotspotConfig(config); 1214 console.info("result:" + ret); 1215 }catch(error){ 1216 console.error("failed:" + JSON.stringify(error)); 1217 } 1218``` 1219 1220## HotspotConfig<sup>9+</sup> 1221 1222热点配置信息。 1223 1224**系统接口:** 此接口为系统接口。 1225 1226**系统能力:** SystemCapability.Communication.WiFi.AP.Core 1227 1228| **名称** | **类型** | **可读** | **可写** | **说明** | 1229| -------- | -------- | -------- | -------- | -------- | 1230| ssid | string | 是 | 是 | 热点的SSID,编码格式为UTF-8。 | 1231| securityType | [WifiSecurityType](js-apis-wifiManager.md#wifisecuritytype9)| 是 | 是 | 加密类型。 | 1232| band | number | 是 | 是 | 热点的带宽。1: 2.4G, 2: 5G, 3: 双模频段 | 1233| channel<sup>10+</sup> | number | 是 | 是 | 热点的信道(2.4G:1~14,5G:7~196,双模频段:暂不支持)。 | 1234| preSharedKey | string | 是 | 是 | 热点的密钥。 | 1235| maxConn | number | 是 | 是 | 最大设备连接数。 | 1236 1237## wifiManager.getHotspotConfig<sup>9+</sup> 1238 1239getHotspotConfig(): HotspotConfig 1240 1241获取热点配置信息。 1242 1243**系统接口:** 此接口为系统接口。 1244 1245**需要权限:** ohos.permission.GET_WIFI_INFO 和 ohos.permission.GET_WIFI_CONFIG 1246 1247**系统能力:** SystemCapability.Communication.WiFi.AP.Core 1248 1249**返回值:** 1250 1251 | **类型** | **说明** | 1252 | -------- | -------- | 1253 | [HotspotConfig](#hotspotconfig9) | 热点的配置信息。 | 1254 1255**错误码:** 1256 1257以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1258 1259| **错误码ID** | **错误信息** | 1260 | -------- | -------- | 1261| 2601000 | Operation failed.| 1262 1263**示例:** 1264```ts 1265 import wifiManager from '@ohos.wifiManager'; 1266 1267 try { 1268 let config = wifiManager.getHotspotConfig(); 1269 console.info("result:" + JSON.stringify(config)); 1270 }catch(error){ 1271 console.error("failed:" + JSON.stringify(error)); 1272 } 1273``` 1274 1275## wifiManager.getStations<sup>9+</sup> 1276 1277getStations(): Array<StationInfo> 1278 1279获取连接的设备。 1280 1281**系统接口:** 此接口为系统接口。 1282 1283**需要权限:** 1284 1285API 9:ohos.permission.GET_WIFI_INFO、ohos.permission.LOCATION、ohos.permission.APPROXIMATELY_LOCATION 和 ohos.permission.MANAGE_WIFI_HOTSPOT,仅系统应用可用。 1286 1287API 10起:ohos.permission.GET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_HOTSPOT,仅系统应用可用。 1288 1289**系统能力:** SystemCapability.Communication.WiFi.AP.Core 1290 1291**返回值:** 1292 1293| **类型** | **说明** | 1294| -------- | -------- | 1295| Array<[StationInfo](#stationinfo9)> | 连接的设备数组。如果应用申请了ohos.permission.GET_WIFI_PEERS_MAC权限,则返回结果中的macAddress为真实设备地址,否则为随机设备地址。 | 1296 1297**错误码:** 1298 1299以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1300 1301| **错误码ID** | **错误信息** | 1302 | -------- | -------- | 1303| 2601000 | Operation failed.| 1304 1305**示例:** 1306```ts 1307 import wifiManager from '@ohos.wifiManager'; 1308 1309 try { 1310 let stations = wifiManager.getStations(); 1311 console.info("result:" + JSON.stringify(stations)); 1312 }catch(error){ 1313 console.error("failed:" + JSON.stringify(error)); 1314 } 1315``` 1316 1317## StationInfo<sup>9+</sup> 1318 1319接入的设备信息。 1320 1321**系统接口:** 此接口为系统接口。 1322 1323**系统能力:** SystemCapability.Communication.WiFi.AP.Core 1324 1325| **名称** | **类型** | **可读** | **可写** | **说明** | 1326| -------- | -------- | -------- | -------- | -------- | 1327| name | string | 是 | 否 | 设备名称。 | 1328| macAddress | string | 是 | 否 | MAC地址。 | 1329| macAddressType<sup>10+</sup> | [DeviceAddressType](js-apis-wifiManager.md#deviceaddresstype10) | 是 | 否 | MAC地址类型。 | 1330| ipAddress | string | 是 | 否 | IP地址。 | 1331 1332## wifiManager.addHotspotBlockList<sup>11+</sup> 1333 1334addHotspotBlockList(stationInfo: StationInfo) 1335 1336将设备添加到热点的阻止连接设备列表中,列表中的设备将不能访问热点。 1337 1338**系统接口:** 此接口为系统接口。 1339 1340**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_HOTSPOT,仅系统应用可用。 1341 1342**系统能力:** SystemCapability.Communication.WiFi.AP.Core 1343 1344**参数:** 1345 1346| **参数名** | **类型** | **必填** | **说明** | 1347| -------- | -------- | -------- | -------- | 1348| stationInfo | [StationInfo](#stationinfo9) | 是 | 将添加到热点的阻止列表中的设备。 | 1349 1350**错误码:** 1351 1352以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1353 1354| **错误码ID** | **错误信息** | 1355 | -------- | -------- | 1356| 2601000 | Operation failed.| 1357 1358**示例:** 1359 1360```ts 1361 import wifiManager from '@ohos.wifiManager'; 1362 1363 try { 1364 let config:wifiManager.StationInfo = { 1365 name : "testSsid", 1366 macAddress : "11:22:33:44:55:66", 1367 ipAddress : "192.168.1.111" 1368 } 1369 // 热点开启后,才能正常将设备添加到连接阻止列表中 1370 wifiManager.addHotspotBlockList(config); 1371 }catch(error){ 1372 console.error("failed:" + JSON.stringify(error)); 1373 } 1374``` 1375 1376## wifiManager.delHotspotBlockList<sup>11+</sup> 1377 1378delHotspotBlockList(stationInfo: StationInfo) 1379 1380将设备从热点的阻止列表中删除。 1381 1382**系统接口:** 此接口为系统接口。 1383 1384**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_HOTSPOT,仅系统应用可用。 1385 1386**系统能力:** SystemCapability.Communication.WiFi.AP.Core 1387 1388**参数:** 1389 1390| **参数名** | **类型** | **必填** | **说明** | 1391| -------- | -------- | -------- | -------- | 1392| stationInfo | [StationInfo](#stationinfo9) | 是 | 将从热点的阻止列表中删除的设备。 | 1393 1394**错误码:** 1395 1396以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1397 1398| **错误码ID** | **错误信息** | 1399 | -------- | -------- | 1400| 2601000 | Operation failed.| 1401 1402**示例:** 1403 1404```ts 1405 import wifiManager from '@ohos.wifiManager'; 1406 1407 try { 1408 let config:wifiManager.StationInfo = { 1409 name : "testSsid", 1410 macAddress : "11:22:33:44:55:66", 1411 ipAddress : "192.168.1.111" 1412 } 1413 wifiManager.delHotspotBlockList(config); 1414 }catch(error){ 1415 console.error("failed:" + JSON.stringify(error)); 1416 } 1417``` 1418 1419## wifiManager.getHotspotBlockList<sup>11+</sup> 1420 1421getHotspotBlockList(): Array<StationInfo> 1422 1423获取热点的阻止列表。 1424 1425**系统接口:** 此接口为系统接口。 1426 1427**需要权限:** ohos.permission.GET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_HOTSPOT,仅系统应用可用。 1428 1429**系统能力:** SystemCapability.Communication.WiFi.AP.Core 1430 1431**返回值:** 1432 1433| **类型** | **说明** | 1434| -------- | -------- | 1435| Array<[StationInfo](#stationinfo9)> | 热点的阻止列表。 | 1436 1437**错误码:** 1438 1439以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1440 1441| **错误码ID** | **错误信息** | 1442 | -------- | -------- | 1443| 2601000 | Operation failed.| 1444 1445**示例:** 1446 1447```ts 1448 import wifiManager from '@ohos.wifiManager'; 1449 1450 try { 1451 let data = wifiManager.getHotspotBlockList(); 1452 console.info("result:" + JSON.stringify(data)); 1453 }catch(error){ 1454 console.error("failed:" + JSON.stringify(error)); 1455 } 1456``` 1457 1458## wifiManager.deletePersistentGroup<sup>9+</sup> 1459 1460deletePersistentGroup(netId: number): void 1461 1462删除永久组。 1463 1464**系统接口:** 此接口为系统接口。 1465 1466**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION 1467 1468**系统能力:** SystemCapability.Communication.WiFi.P2P 1469 1470**参数:** 1471 1472 1473 | **参数名** | **类型** | 必填 | **说明** | 1474 | -------- | -------- | -------- | -------- | 1475 | netId | number | 是 | 组的ID。 | 1476 1477**错误码:** 1478 1479以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1480 1481| **错误码ID** | **错误信息** | 1482 | -------- | -------- | 1483| 2801000 | Operation failed.| 1484 1485**示例:** 1486```ts 1487 import wifiManager from '@ohos.wifiManager'; 1488 1489 try { 1490 let netId = 0; 1491 wifiManager.deletePersistentGroup(netId); 1492 }catch(error){ 1493 console.error("failed:" + JSON.stringify(error)); 1494 } 1495``` 1496 1497## wifiManager.getP2pGroups<sup>9+</sup> 1498 1499getP2pGroups(): Promise<Array<WifiP2pGroupInfo>> 1500 1501获取创建的所有P2P群组信息,使用Promise异步回调。 1502 1503**系统接口:** 此接口为系统接口。 1504 1505**需要权限:** 1506 1507API 9:ohos.permission.GET_WIFI_INFO、ohos.permission.LOCATION 和 ohos.permission.APPROXIMATELY_LOCATION 1508 1509API 10起:ohos.permission.GET_WIFI_INFO 1510 1511**系统能力:** SystemCapability.Communication.WiFi.P2P 1512 1513**返回值:** 1514 1515| 类型 | 说明 | 1516| -------- | -------- | 1517| Promise< Array<[WifiP2pGroupInfo](js-apis-wifiManager.md#wifip2pgroupinfo9)> > | Promise对象。表示所有群组信息。如果应用申请了ohos.permission.GET_WIFI_PEERS_MAC权限,则返回结果中的deviceAddress为真实设备地址,否则为随机设备地址。 | 1518 1519**错误码:** 1520 1521以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1522 1523| **错误码ID** | **错误信息** | 1524 | -------- | -------- | 1525| 2801000 | Operation failed.| 1526 1527**示例:** 1528```ts 1529 import wifiManager from '@ohos.wifiManager'; 1530 1531 wifiManager.getP2pGroups((err, data) => { 1532 if (err) { 1533 console.error("get P2P groups error"); 1534 return; 1535 } 1536 console.info("get P2P groups: " + JSON.stringify(data)); 1537 }); 1538 1539 wifiManager.getP2pGroups().then(data => { 1540 console.info("get P2P groups: " + JSON.stringify(data)); 1541 }); 1542 1543``` 1544 1545 1546## wifiManager.getP2pGroups<sup>9+</sup> 1547 1548getP2pGroups(callback: AsyncCallback<Array<WifiP2pGroupInfo>>): void 1549 1550获取创建的所有P2P群组信息,使用callback方式作为异步方法。 1551 1552**系统接口:** 此接口为系统接口。 1553 1554**需要权限:** 1555 1556API 9:ohos.permission.GET_WIFI_INFO、ohos.permission.LOCATION 和 ohos.permission.APPROXIMATELY_LOCATION 1557 1558API 10起:ohos.permission.GET_WIFI_INFO 1559 1560**系统能力:** SystemCapability.Communication.WiFi.P2P 1561 1562**参数:** 1563 1564| 参数名 | 类型 | 必填 | 说明 | 1565| -------- | -------- | -------- | -------- | 1566| callback | AsyncCallback< Array<[WifiP2pGroupInfo](js-apis-wifiManager.md#wifip2pgroupinfo9)>> | 是 | 回调函数。当操作成功时,err为0,data表示所有群组信息。如果error为非0,表示处理出现错误。如果应用申请了ohos.permission.GET_WIFI_PEERS_MAC权限,则返回结果中的deviceAddress为真实设备地址,否则为随机设备地址。 | 1567 1568**错误码:** 1569 1570以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1571 1572| **错误码ID** | **错误信息** | 1573 | -------- | -------- | 1574| 2801000 | Operation failed.| 1575 1576## wifiManager.setDeviceName<sup>9+</sup> 1577 1578setDeviceName(devName: string): void 1579 1580设置设备名称。 1581 1582**系统接口:** 此接口为系统接口。 1583 1584**需要权限:** ohos.permission.SET_WIFI_INFO 和 ohos.permission.MANAGE_WIFI_CONNECTION,仅系统应用可用。 1585 1586**系统能力:** SystemCapability.Communication.WiFi.P2P 1587 1588**参数:** 1589 1590 | **参数名** | **类型** | **必填** | **说明** | 1591 | -------- | -------- | -------- | -------- | 1592 | devName | string | 是 | 设备名称。 | 1593 1594**错误码:** 1595 1596以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1597 1598| **错误码ID** | **错误信息** | 1599 | -------- | -------- | 1600| 2801000 | Operation failed.| 1601 1602**示例:** 1603```ts 1604 import wifiManager from '@ohos.wifiManager'; 1605 1606 try { 1607 let name = "****"; 1608 wifiManager.setDeviceName(name); 1609 }catch(error){ 1610 console.error("failed:" + JSON.stringify(error)); 1611 } 1612``` 1613 1614 1615## wifiManager.on('streamChange')<sup>9+</sup> 1616 1617on(type: "streamChange", callback: Callback<number>): void 1618 1619注册WIFI流变更事件。 1620 1621**系统接口:** 此接口为系统接口。 1622 1623**需要权限:** ohos.permission.MANAGE_WIFI_CONNECTION 1624 1625**系统能力:** SystemCapability.Communication.WiFi.STA 1626 1627**参数:** 1628 1629| **参数名** | **类型** | **必填** | **说明** | 1630| -------- | -------- | -------- | -------- | 1631| type | string | 是 | 固定填"streamChange"字符串。 | 1632| callback | Callback<number> | 是 | 状态改变回调函数,返回0:无,1:向下,2:向上,3:双向。 | 1633 1634**错误码:** 1635 1636以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1637 1638| **错误码ID** | **错误信息** | 1639 | -------- | -------- | 1640| 2501000 | Operation failed.| 1641 1642## wifiManager.off('streamChange')<sup>9+</sup> 1643 1644off(type: "streamChange", callback?: Callback<number>): void 1645 1646取消注册WIFI流变更事件。 1647 1648**系统接口:** 此接口为系统接口。 1649 1650**需要权限:** ohos.permission.MANAGE_WIFI_CONNECTION 1651 1652**系统能力:** SystemCapability.Communication.WiFi.STA 1653 1654**参数:** 1655 1656| **参数名** | **类型** | **必填** | **说明** | 1657| -------- | -------- | -------- | -------- | 1658| type | string | 是 | 固定填"streamChange"字符串。 | 1659| callback | Callback<number> | 否 | 状态改变回调函数,返回0:无,1:向下,2:向上,3:双向。 | 1660 1661**错误码:** 1662 1663以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1664 1665| **错误码ID** | **错误信息** | 1666 | -------- | -------- | 1667| 2501000 | Operation failed.| 1668 1669**示例:** 1670```ts 1671import wifi from '@ohos.wifi'; 1672 1673let recvStreamChangeFunc = (result:number) => { 1674 console.info("Receive stream change event: " + result); 1675} 1676 1677// Register event 1678wifi.on("streamChange", recvStreamChangeFunc); 1679 1680// Unregister event 1681wifi.off("streamChange", recvStreamChangeFunc); 1682 1683``` 1684## wifiManager.on('deviceConfigChange')<sup>9+</sup> 1685 1686on(type: "deviceConfigChange", callback: Callback<number>): void 1687 1688注册WIFI设备配置更改事件。 1689 1690**系统接口:** 此接口为系统接口。 1691 1692**需要权限:** ohos.permission.GET_WIFI_INFO 1693 1694**系统能力:** SystemCapability.Communication.WiFi.STA 1695 1696**参数:** 1697 1698| **参数名** | **类型** | **必填** | **说明** | 1699| -------- | -------- | -------- | -------- | 1700| type | string | 是 | 固定填"streamChange"字符串。 | 1701| callback | Callback<number> | 是 | 状态改变回调函数,返回0: 添加配置, 1: 更改配置, 2: 删除配置. | 1702 1703**错误码:** 1704 1705以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1706 1707| **错误码ID** | **错误信息** | 1708 | -------- | -------- | 1709| 2501000 | Operation failed.| 1710 1711## wifiManager.off('deviceConfigChange')<sup>9+</sup> 1712 1713off(type: "deviceConfigChange", callback?: Callback<number>): void 1714 1715取消注册WIFI设备配置更改事件。 1716 1717**系统接口:** 此接口为系统接口。 1718 1719**需要权限:** ohos.permission.GET_WIFI_INFO 1720 1721**系统能力:** SystemCapability.Communication.WiFi.STA 1722 1723**参数:** 1724 1725| **参数名** | **类型** | **必填** | **说明** | 1726| -------- | -------- | -------- | -------- | 1727| type | string | 是 | 固定填"streamChange"字符串。 | 1728| callback | Callback<number> | 否 | 状态改变回调函数,返回0: 添加配置, 1: 更改配置, 2: 删除配置.| 1729 1730**错误码:** 1731 1732以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1733 1734| **错误码ID** | **错误信息** | 1735 | -------- | -------- | 1736| 2501000 | Operation failed.| 1737 1738**示例:** 1739```ts 1740import wifi from '@ohos.wifiManager'; 1741 1742let recvDeviceConfigChangeFunc = (result:number) => { 1743 console.info("Receive device config change event: " + result); 1744} 1745 1746// Register event 1747wifi.on("deviceConfigChange", recvDeviceConfigChangeFunc); 1748 1749// Unregister event 1750wifi.off("deviceConfigChange", recvDeviceConfigChangeFunc); 1751 1752``` 1753 1754## wifiManager.on('hotspotStaJoin')<sup>9+</sup> 1755 1756on(type: "hotspotStaJoin", callback: Callback<StationInfo>): void 1757 1758注册wifi热点sta加入事件。 1759 1760**系统接口:** 此接口为系统接口。 1761 1762**需要权限:** ohos.permission.MANAGE_WIFI_HOTSPOT 1763 1764**系统能力:** SystemCapability.Communication.WiFi.AP.Core 1765 1766**参数:** 1767 1768| **参数名** | **类型** | **必填** | **说明** | 1769| -------- | -------- | -------- | -------- | 1770| type | string | 是 | 固定填"hotspotStaJoin"字符串。 | 1771| callback | Callback<StationInfo> | 是 | 状态改变回调函数。 | 1772 1773**错误码:** 1774 1775以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1776 1777| **错误码ID** | **错误信息** | 1778 | -------- | -------- | 1779| 2601000 | Operation failed.| 1780 1781## wifiManager.off('hotspotStaJoin')<sup>9+</sup> 1782 1783off(type: "hotspotStaJoin", callback?: Callback<StationInfo>): void 1784 1785取消注册wifi热点sta加入事件。 1786 1787**系统接口:** 此接口为系统接口。 1788 1789**需要权限:** ohos.permission.MANAGE_WIFI_HOTSPOT 1790 1791**系统能力:** SystemCapability.Communication.WiFi.AP.Core 1792 1793**参数:** 1794 1795| **参数名** | **类型** | **必填** | **说明** | 1796| -------- | -------- | -------- | -------- | 1797| type | string | 是 | 固定填"hotspotStaJoin"字符串。 | 1798| callback | Callback<StationInfo> | 否 | 状态改变回调函数。 | 1799 1800**错误码:** 1801 1802以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1803 1804| **错误码ID** | **错误信息** | 1805 | -------- | -------- | 1806| 2601000 | Operation failed.| 1807 1808**示例:** 1809```ts 1810import wifiManager from '@ohos.wifiManager'; 1811 1812let recvHotspotStaJoinFunc = (result:wifiManager.StationInfo) => { 1813 console.info("Receive hotspot sta join event: " + result); 1814} 1815 1816// Register event 1817wifiManager.on("hotspotStaJoin", recvHotspotStaJoinFunc); 1818 1819// Unregister event 1820wifiManager.off("hotspotStaJoin", recvHotspotStaJoinFunc); 1821 1822``` 1823 1824## wifiManager.on('hotspotStaLeave')<sup>9+</sup> 1825 1826on(type: "hotspotStaLeave", callback: Callback<StationInfo>): void 1827 1828注册wifi热点sta离开事件。 1829 1830**系统接口:** 此接口为系统接口。 1831 1832**需要权限:** ohos.permission.MANAGE_WIFI_HOTSPOT 1833 1834**系统能力:** SystemCapability.Communication.WiFi.AP.Core 1835 1836**参数:** 1837 1838 | **参数名** | **类型** | **必填** | **说明** | 1839 | -------- | -------- | -------- | -------- | 1840 | type | string | 是 | 固定填"hotspotStaLeave"字符串。 | 1841 | callback | Callback<StationInf]> | 是 | 状态改变回调函数。 | 1842 1843**错误码:** 1844 1845以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1846 1847| **错误码ID** | **错误信息** | 1848 | -------- | -------- | 1849| 2601000 | Operation failed.| 1850 1851## wifiManager.off('hotspotStaLeave')<sup>9+</sup> 1852 1853off(type: "hotspotStaLeave", callback?: Callback<StationInfo>): void 1854 1855取消注册wifi热点sta离开事件。 1856 1857**系统接口:** 此接口为系统接口。 1858 1859**需要权限:** ohos.permission.MANAGE_WIFI_HOTSPOT 1860 1861**系统能力:** SystemCapability.Communication.WiFi.AP.Core 1862 1863**参数:** 1864 1865| **参数名** | **类型** | **必填** | **说明** | 1866| -------- | -------- | -------- | -------- | 1867| type | string | 是 | 固定填"hotspotStaLeave"字符串。 | 1868| callback | Callback<StationInf]> | 否 | 状态改变回调函数。 | 1869 1870**错误码:** 1871 1872以下错误码的详细介绍请参见[WIFI错误码](errorcode-wifi.md)。 1873 1874| **错误码ID** | **错误信息** | 1875 | -------- | -------- | 1876| 2601000 | Operation failed.| 1877 1878**示例:** 1879```ts 1880import wifiManager from '@ohos.wifiManager'; 1881 1882let recvHotspotStaLeaveFunc = (result:wifiManager.StationInfo) => { 1883 console.info("Receive hotspot sta leave event: " + result); 1884} 1885 1886// Register event 1887wifiManager.on("hotspotStaLeave", recvHotspotStaLeaveFunc); 1888 1889// Unregister event 1890wifiManager.off("hotspotStaLeave", recvHotspotStaLeaveFunc); 1891 1892``` 1893 1894