• Home
Name Date Size #Lines LOC

..--

figures/12-May-2024-

interfaces/12-May-2024-20,26112,363

services/wifi_standard/12-May-2024-96,93466,217

tests/wifi_standard/12-May-2024-41,12932,858

utils/12-May-2024-513332

.gitattributesD12-May-2024631 1615

LICENSED12-May-20249.9 KiB177150

OAT.xmlD12-May-20242.5 KiB5233

README.mdD12-May-202413.3 KiB221180

README_zh.mdD12-May-202412.9 KiB221181

bundle.jsonD12-May-20245.8 KiB128128

hisysevent.yamlD12-May-20241.6 KiB3917

wifi.gniD12-May-2024640 1715

README.md

1# communication\_wifi<a name="EN-US_TOPIC_0000001162030287"></a>
2
3-   [Introduction](#section11660541593)
4-   [Architecture](#section342962219551)
5-   [Directory Structure](#section161941989596)
6-   [Usage](#section1312121216216)
7    -   [Available APIs](#section1551164914237)
8    -   [Usage Guidelines](#section129654513264)
9
10-   [Repositories Involved](#section1371113476307)
11
12## Introduction<a name="section11660541593"></a>
13
14A wireless local area network \(WLAN\) uses the radio, infrared, or other technologies to transmit data between devices that are not physically connected with each other. It is widely applied in offices and public places where mobile devices are used.
15
16The WLAN module provides basic WLAN functions, peer-to-peer \(P2P\) connection, and WLAN notification, enabling your application to communicate with other devices through a WLAN.
17
18## Architecture<a name="section342962219551"></a>
19
20**Figure  1**  WLAN architecture<a name="fig356513281301"></a>
21
22
23![](figures/en-us_image_0000001115710400.png)
24
25## Directory Structure<a name="section161941989596"></a>
26
27```
28/foundation/communication/wifi
29├── figures            # Figures
30├── interfaces         # APIs
31│   ├── innerkits      # Inner APIs
32│   └── kits           # WLAN APIs
33├── services           # Services
34│   └── wifi_standard  # Service implementation
35├── tests              # Test code
36│   └── wifi_standard  # Test code for the service implementation module
37└── utils              # Utility functions
38    ├── inc            # Header directory for utility functions
39    └── src            # Implementation directory for utility functions
40```
41
42## Usage<a name="section1312121216216"></a>
43
44### Available APIs<a name="section1551164914237"></a>
45
46The following table describes JavaScript APIs in  **@ohos.wifi**  available for basic WLAN functions.
47
48**Table  1**  Major JavaScript APIs available for basic WLAN functions
49
50<a name="t96d5b1a809be46328adc684bff001cf7"></a>
51
52<table><thead align="left"><tr id="r5b6b790b6fe144e79e849b7987637f32"><th class="cellrowborder" valign="top" width="27.642764276427638%" id="mcps1.2.4.1.1"><p id="a4bf77d9c2b5c4b8d950a720edaf949c7"><a name="a4bf77d9c2b5c4b8d950a720edaf949c7"></a><a name="a4bf77d9c2b5c4b8d950a720edaf949c7"></a>API</p>
53</th>
54<th class="cellrowborder" valign="top" width="41.094109410941094%" id="mcps1.2.4.1.2"><p id="a11b93aaf8bfd4305acc4b4549f28d524"><a name="a11b93aaf8bfd4305acc4b4549f28d524"></a><a name="a11b93aaf8bfd4305acc4b4549f28d524"></a>Description</p>
55</th>
56<th class="cellrowborder" valign="top" width="31.26312631263126%" id="mcps1.2.4.1.3"><p id="a1d9f27631e6f466f8c2edc9833a94275"><a name="a1d9f27631e6f466f8c2edc9833a94275"></a><a name="a1d9f27631e6f466f8c2edc9833a94275"></a>Required Permission</p>
57</th>
58</tr>
59</thead>
60<tbody><tr id="r31629b385e294e248c668786519b6f87"><td class="cellrowborder" valign="top" width="27.642764276427638%" headers="mcps1.2.4.1.1 "><p id="aea622edc3df04a60a42bb8a1a63a8c4f"><a name="aea622edc3df04a60a42bb8a1a63a8c4f"></a><a name="aea622edc3df04a60a42bb8a1a63a8c4f"></a>function enableWifi(): boolean</p>
61</td>
62<td class="cellrowborder" valign="top" width="41.094109410941094%" headers="mcps1.2.4.1.2 "><p id="a799afb666cff4f03a2ee996c0c7335dd"><a name="a799afb666cff4f03a2ee996c0c7335dd"></a><a name="a799afb666cff4f03a2ee996c0c7335dd"></a>Enables WLAN.</p>
63</td>
64<td class="cellrowborder" valign="top" width="31.26312631263126%" headers="mcps1.2.4.1.3 "><p id="a78809eb41a8e46ec92ad886a2e491e09"><a name="a78809eb41a8e46ec92ad886a2e491e09"></a><a name="a78809eb41a8e46ec92ad886a2e491e09"></a>NA</p>
65</td>
66</tr>
67<tr id="r42d012d5690241a1a346b249bca0d0f4"><td class="cellrowborder" valign="top" width="27.642764276427638%" headers="mcps1.2.4.1.1 "><p id="a3d8f9fb87dd94d5da78f9d59ba31c9ec"><a name="a3d8f9fb87dd94d5da78f9d59ba31c9ec"></a><a name="a3d8f9fb87dd94d5da78f9d59ba31c9ec"></a>function disableWifi(): boolean</p>
68</td>
69<td class="cellrowborder" valign="top" width="41.094109410941094%" headers="mcps1.2.4.1.2 "><p id="a9d1794f64a4a4177b0c247a5378cc8a3"><a name="a9d1794f64a4a4177b0c247a5378cc8a3"></a><a name="a9d1794f64a4a4177b0c247a5378cc8a3"></a>Disables WLAN. </p>
70</td>
71<td class="cellrowborder" valign="top" width="31.26312631263126%" headers="mcps1.2.4.1.3 "><p id="a4bba00774fee45b991e03d5739eed45d"><a name="a4bba00774fee45b991e03d5739eed45d"></a><a name="a4bba00774fee45b991e03d5739eed45d"></a>NA</p>
72</td>
73</tr>
74<tr id="rb2088f3bae264f9fb6dee3d69f8e510b"><td class="cellrowborder" valign="top" width="27.642764276427638%" headers="mcps1.2.4.1.1 "><p id="a0db4a8a271844e289867b8686be5971e"><a name="a0db4a8a271844e289867b8686be5971e"></a><a name="a0db4a8a271844e289867b8686be5971e"></a>function isWifiActive(): boolean</p>
75</td>
76<td class="cellrowborder" valign="top" width="41.094109410941094%" headers="mcps1.2.4.1.2 "><p id="a251c902bc5a14e4dbbf6ae3e847d5a00"><a name="a251c902bc5a14e4dbbf6ae3e847d5a00"></a><a name="a251c902bc5a14e4dbbf6ae3e847d5a00"></a>Checks whether WLAN is enabled.</p>
77</td>
78<td class="cellrowborder" valign="top" width="31.26312631263126%" headers="mcps1.2.4.1.3 "><p id="ad01b019785b6434aa7b4e06badffda6a"><a name="ad01b019785b6434aa7b4e06badffda6a"></a><a name="ad01b019785b6434aa7b4e06badffda6a"></a>NA</p>
79</td>
80</tr>
81<tr id="r63090caeaf674f2e9a7efdd8f838564c"><td class="cellrowborder" valign="top" width="27.642764276427638%" headers="mcps1.2.4.1.1 "><p id="a9229822a49b6414dbb15ded55b808209"><a name="a9229822a49b6414dbb15ded55b808209"></a><a name="a9229822a49b6414dbb15ded55b808209"></a>function scan(): boolean</p>
82</td>
83<td class="cellrowborder" valign="top" width="41.094109410941094%" headers="mcps1.2.4.1.2 "><p id="a666bb97e527a4d5db4864d72bdb040b7"><a name="a666bb97e527a4d5db4864d72bdb040b7"></a><a name="a666bb97e527a4d5db4864d72bdb040b7"></a>Starts a WLAN scan.</p>
84</td>
85<td class="cellrowborder" valign="top" width="31.26312631263126%" headers="mcps1.2.4.1.3 "><p id="abdc11b1866b54d69a8acef2c13da1553"><a name="abdc11b1866b54d69a8acef2c13da1553"></a><a name="abdc11b1866b54d69a8acef2c13da1553"></a>NA</p>
86</td>
87</tr>
88<tr id="r5098f19894e947d9844839c81abdb431"><td class="cellrowborder" valign="top" width="27.642764276427638%" headers="mcps1.2.4.1.1 "><p id="p148521030134915"><a name="p148521030134915"></a><a name="p148521030134915"></a>function getScanInfos(): Promise&lt;Array&lt;WifiScanInfo&gt;&gt;</p>
89<p id="p1264072917366"><a name="p1264072917366"></a><a name="p1264072917366"></a>function getScanInfos(callback: AsyncCallback&lt;Array&lt;WifiScanInfo&gt;&gt;): void</p>
90</td>
91<td class="cellrowborder" valign="top" width="41.094109410941094%" headers="mcps1.2.4.1.2 "><p id="a833c28b165db44e49b49d84eb8f12d3a"><a name="a833c28b165db44e49b49d84eb8f12d3a"></a><a name="a833c28b165db44e49b49d84eb8f12d3a"></a>Obtains the WLAN scan result. This API can be called in either callback or promise mode.</p>
92</td>
93<td class="cellrowborder" valign="top" width="31.26312631263126%" headers="mcps1.2.4.1.3 "><p id="ad31109a27e344b27a2879ae7be60da6a"><a name="ad31109a27e344b27a2879ae7be60da6a"></a><a name="ad31109a27e344b27a2879ae7be60da6a"></a>NA</p>
94</td>
95</tr>
96<tr id="rd41bbd2a167e42c4854efaf2f95923a8"><td class="cellrowborder" valign="top" width="27.642764276427638%" headers="mcps1.2.4.1.1 "><p id="p126421517153711"><a name="p126421517153711"></a><a name="p126421517153711"></a>function addDeviceConfig(config: WifiDeviceConfig): Promise&lt;number&gt;</p>
97<p id="p15642617133717"><a name="p15642617133717"></a><a name="p15642617133717"></a>function addDeviceConfig(config: WifiDeviceConfig, callback: AsyncCallback&lt;number&gt;): void</p>
98</td>
99<td class="cellrowborder" valign="top" width="41.094109410941094%" headers="mcps1.2.4.1.2 "><p id="a88ec088696df4f4b8e0e240e66646d9c"><a name="a88ec088696df4f4b8e0e240e66646d9c"></a><a name="a88ec088696df4f4b8e0e240e66646d9c"></a>Adds a WLAN hotspot configuration. This API can be called in either callback or promise mode.</p>
100</td>
101<td class="cellrowborder" valign="top" width="31.26312631263126%" headers="mcps1.2.4.1.3 "><p id="a57539195f93c4af187781c5c7a64c74c"><a name="a57539195f93c4af187781c5c7a64c74c"></a><a name="a57539195f93c4af187781c5c7a64c74c"></a>NA</p>
102</td>
103</tr>
104<tr id="row18665444142915"><td class="cellrowborder" valign="top" width="27.642764276427638%" headers="mcps1.2.4.1.1 "><p id="p666517440295"><a name="p666517440295"></a><a name="p666517440295"></a>function connectToNetwork(networkId: number): boolean</p>
105</td>
106<td class="cellrowborder" valign="top" width="41.094109410941094%" headers="mcps1.2.4.1.2 "><p id="p13666344152913"><a name="p13666344152913"></a><a name="p13666344152913"></a>Sets up a WLAN based on the hotspot configuration ID.</p>
107</td>
108<td class="cellrowborder" valign="top" width="31.26312631263126%" headers="mcps1.2.4.1.3 "><p id="p56667442298"><a name="p56667442298"></a><a name="p56667442298"></a>NA</p>
109</td>
110</tr>
111<tr id="row176661944152919"><td class="cellrowborder" valign="top" width="27.642764276427638%" headers="mcps1.2.4.1.1 "><p id="p66661144122919"><a name="p66661144122919"></a><a name="p66661144122919"></a>function connectToDevice(config: WifiDeviceConfig): boolean</p>
112</td>
113<td class="cellrowborder" valign="top" width="41.094109410941094%" headers="mcps1.2.4.1.2 "><p id="p4666104410298"><a name="p4666104410298"></a><a name="p4666104410298"></a>Sets up a WLAN based on the hotspot configuration.</p>
114</td>
115<td class="cellrowborder" valign="top" width="31.26312631263126%" headers="mcps1.2.4.1.3 "><p id="p766644420298"><a name="p766644420298"></a><a name="p766644420298"></a>NA</p>
116</td>
117</tr>
118<tr id="r53c17c0621b74f9c9b3915fef99bb64d"><td class="cellrowborder" valign="top" width="27.642764276427638%" headers="mcps1.2.4.1.1 "><p id="p5681182313296"><a name="p5681182313296"></a><a name="p5681182313296"></a>function disconnect(): boolean</p>
119</td>
120<td class="cellrowborder" valign="top" width="41.094109410941094%" headers="mcps1.2.4.1.2 "><p id="a2939738c548a44838522d7ac772b697a"><a name="a2939738c548a44838522d7ac772b697a"></a><a name="a2939738c548a44838522d7ac772b697a"></a>Disconnects from a WLAN.</p>
121</td>
122<td class="cellrowborder" valign="top" width="31.26312631263126%" headers="mcps1.2.4.1.3 "><p id="a3862542954ef418b86ba8606721b4d30"><a name="a3862542954ef418b86ba8606721b4d30"></a><a name="a3862542954ef418b86ba8606721b4d30"></a>NA</p>
123</td>
124</tr>
125<tr id="row10868175614302"><td class="cellrowborder" valign="top" width="27.642764276427638%" headers="mcps1.2.4.1.1 "><p id="p586835643012"><a name="p586835643012"></a><a name="p586835643012"></a>function getSignalLevel(rssi: number, band: number): number</p>
126</td>
127<td class="cellrowborder" valign="top" width="41.094109410941094%" headers="mcps1.2.4.1.2 "><p id="p8868155614306"><a name="p8868155614306"></a><a name="p8868155614306"></a>Obtains the WLAN signal strength.</p>
128</td>
129<td class="cellrowborder" valign="top" width="31.26312631263126%" headers="mcps1.2.4.1.3 "><p id="p1786825673010"><a name="p1786825673010"></a><a name="p1786825673010"></a>NA</p>
130</td>
131</tr>
132</tbody>
133</table>
134
135### Usage Guidelines<a name="section129654513264"></a>
136
137Before invoking WLAN JavaScript APIs, you need to import the  **@ohos.wifi\_native\_js**  class.
138
139```js
140import wf from '@ohos.wifi'; // Import the @ohos.wifi class.
141```
142
143-   Obtaining the WLAN state
144
1451.  Call the  **isWifiActive​\(\)**  method to check whether WLAN is enabled.
146
147    ```js
148    var isWifiActive = wf.isWifiActive(); // Value true indicates that WLAN is enabled, and false indicates the opposite.
149    ```
150
151
152-   Starting a scan and obtaining the scan result
153
1541.  Call the  **scan​\(\)**  method to start a scan.
1552.  Call the  **getScanInfoList​\(\)**  method to obtain the scan result.
156
157    ```js
158    // Start a scan.
159    var isScanSuccess = wf.scan(); // true
160
161    // Wait for some time.
162
163    // Obtain the scan result.
164    wf.getScanInfos((err, result) => {
165        if (err) {
166            console.error("get scan info error");
167            return;
168        }
169
170        var len = Object.keys(result).length;
171        console.log("get scan info number: " + len);
172        for (var i = 0; i < len; ++i) {
173            console.info("ssid: " + result[i].ssid);
174            console.info("bssid: " + result[i].bssid);
175            console.info("securityType: " + result[i].securityType);
176            console.info("rssi: " + result[i].rssi);
177            console.info("band: " + result[i].band);
178            console.info("frequency: " + result[i].frequency);
179            console.info("timestamp: " + result[i].timestamp);
180        }
181    });
182    ```
183
184
185Set up a WLAN connection.
186
1871.  Call  **addDeviceConfig**  to add a hotspot configuration, and set up a WLAN based on the hotspot configuration ID or by calling  **connectToDevice**  with the hotspot configuration passed.
188
189    ```js
190    // Configure WLAN information.
191    var config = {
192        "ssid":"test_wifi",
193        "bssid":"",
194        "preSharedKey":"12345678",
195        "isHiddenSsid":false,
196        "securityType":3,
197    }
198
199    Method 1:
200    // Add a hotspot configuration.
201    wf.addDeviceConfig(config, (err, result) => {
202        if (err) {
203            console.error("add device config error");
204            return;
205        }
206        console.info("config id: " + result);
207        // Set up a WLAN based on the hotspot configuration ID.
208        wf.connectToNetwork(result);
209    });
210
211    Method 2:
212    // Set up a WLAN by calling connectToDevice with the hotspot configuration passed.
213    wf.connectToDevice(config);
214    ```
215
216
217## Repositories Involved<a name="section1371113476307"></a>
218
219communication\_wifi
220
221

README_zh.md

1# WLAN组件<a name="ZH-CN_TOPIC_0000001162030287"></a>
2
3-   [简介](#section11660541593)
4-   [系统架构](#section342962219551)
5-   [目录](#section161941989596)
6-   [说明](#section1312121216216)
7    -   [接口说明](#section1551164914237)
8    -   [使用说明](#section129654513264)
9
10-   [相关仓](#section1371113476307)
11
12## 简介<a name="section11660541593"></a>
13
14无线局域网(Wireless Local Area Networks,WLAN),是通过无线电、红外光信号或者其他技术发送和接收数据的局域网,用户可以通过WLAN实现结点之间无物理连接的网络通讯。常用于用户携带可移动终端的办公、公众环境中。
15
16WLAN组件子系统为用户提供WLAN基础功能、P2P(peer-to-peer)功能和WLAN消息通知的相应服务,让应用可以通过WLAN和其他设备互联互通。
17
18## 系统架构<a name="section342962219551"></a>
19
20**图 1**  WLAN组件架构图<a name="fig356513281301"></a>
21
22
23![](figures/zh-cn_image_0000001115710400.png)
24
25## 目录<a name="section161941989596"></a>
26
27```
28/foundation/communication/wifi
29├── figures            # 图片资源目录
30├── interfaces         # 接口适配目录
31│   ├── innerkits      # 内部接口适配目录
32│   └── kits           # WLAN组件接口的适配代码存放目录
33├── services           # service适配目录
34│   └── wifi_standard  # service实现目录
35├── tests              # 测试代码目录
36│   └── wifi_standard  # service实现模块测试代码
37└── utils              # 实用函数目录
38    ├── inc            # 实用函数头文件目录
39    └── src            # 实用函数实现目录
40```
41
42## 说明<a name="section1312121216216"></a>
43
44### 接口说明<a name="section1551164914237"></a>
45
46WLAN基础功能由@ohos.wifi类提供,其接口\(JS接口\)说明如下。
47
48**表 1**  WLAN基础功能的主要接口\(JS接口\)
49
50<a name="t96d5b1a809be46328adc684bff001cf7"></a>
51
52<table><thead align="left"><tr id="r5b6b790b6fe144e79e849b7987637f32"><th class="cellrowborder" valign="top" width="40.864086408640865%" id="mcps1.2.4.1.1"><p id="a4bf77d9c2b5c4b8d950a720edaf949c7"><a name="a4bf77d9c2b5c4b8d950a720edaf949c7"></a><a name="a4bf77d9c2b5c4b8d950a720edaf949c7"></a>接口名</p>
53</th>
54<th class="cellrowborder" valign="top" width="44.704470447044706%" id="mcps1.2.4.1.2"><p id="a11b93aaf8bfd4305acc4b4549f28d524"><a name="a11b93aaf8bfd4305acc4b4549f28d524"></a><a name="a11b93aaf8bfd4305acc4b4549f28d524"></a>描述</p>
55</th>
56<th class="cellrowborder" valign="top" width="14.431443144314432%" id="mcps1.2.4.1.3"><p id="a1d9f27631e6f466f8c2edc9833a94275"><a name="a1d9f27631e6f466f8c2edc9833a94275"></a><a name="a1d9f27631e6f466f8c2edc9833a94275"></a>所需权限</p>
57</th>
58</tr>
59</thead>
60<tbody><tr id="r31629b385e294e248c668786519b6f87"><td class="cellrowborder" valign="top" width="40.864086408640865%" headers="mcps1.2.4.1.1 "><p id="aea622edc3df04a60a42bb8a1a63a8c4f"><a name="aea622edc3df04a60a42bb8a1a63a8c4f"></a><a name="aea622edc3df04a60a42bb8a1a63a8c4f"></a>function enableWifi(): boolean</p>
61</td>
62<td class="cellrowborder" valign="top" width="44.704470447044706%" headers="mcps1.2.4.1.2 "><p id="a799afb666cff4f03a2ee996c0c7335dd"><a name="a799afb666cff4f03a2ee996c0c7335dd"></a><a name="a799afb666cff4f03a2ee996c0c7335dd"></a>打开WLAN。</p>
63</td>
64<td class="cellrowborder" valign="top" width="14.431443144314432%" headers="mcps1.2.4.1.3 "><p id="a78809eb41a8e46ec92ad886a2e491e09"><a name="a78809eb41a8e46ec92ad886a2e491e09"></a><a name="a78809eb41a8e46ec92ad886a2e491e09"></a>NA</p>
65</td>
66</tr>
67<tr id="r42d012d5690241a1a346b249bca0d0f4"><td class="cellrowborder" valign="top" width="40.864086408640865%" headers="mcps1.2.4.1.1 "><p id="a3d8f9fb87dd94d5da78f9d59ba31c9ec"><a name="a3d8f9fb87dd94d5da78f9d59ba31c9ec"></a><a name="a3d8f9fb87dd94d5da78f9d59ba31c9ec"></a>function disableWifi(): boolean</p>
68</td>
69<td class="cellrowborder" valign="top" width="44.704470447044706%" headers="mcps1.2.4.1.2 "><p id="a9d1794f64a4a4177b0c247a5378cc8a3"><a name="a9d1794f64a4a4177b0c247a5378cc8a3"></a><a name="a9d1794f64a4a4177b0c247a5378cc8a3"></a>关闭WLAN。</p>
70</td>
71<td class="cellrowborder" valign="top" width="14.431443144314432%" headers="mcps1.2.4.1.3 "><p id="a4bba00774fee45b991e03d5739eed45d"><a name="a4bba00774fee45b991e03d5739eed45d"></a><a name="a4bba00774fee45b991e03d5739eed45d"></a>NA</p>
72</td>
73</tr>
74<tr id="rb2088f3bae264f9fb6dee3d69f8e510b"><td class="cellrowborder" valign="top" width="40.864086408640865%" headers="mcps1.2.4.1.1 "><p id="a0db4a8a271844e289867b8686be5971e"><a name="a0db4a8a271844e289867b8686be5971e"></a><a name="a0db4a8a271844e289867b8686be5971e"></a>function isWifiActive(): boolean</p>
75</td>
76<td class="cellrowborder" valign="top" width="44.704470447044706%" headers="mcps1.2.4.1.2 "><p id="a251c902bc5a14e4dbbf6ae3e847d5a00"><a name="a251c902bc5a14e4dbbf6ae3e847d5a00"></a><a name="a251c902bc5a14e4dbbf6ae3e847d5a00"></a>查询WLAN是否处于打开状态。</p>
77</td>
78<td class="cellrowborder" valign="top" width="14.431443144314432%" headers="mcps1.2.4.1.3 "><p id="ad01b019785b6434aa7b4e06badffda6a"><a name="ad01b019785b6434aa7b4e06badffda6a"></a><a name="ad01b019785b6434aa7b4e06badffda6a"></a>NA</p>
79</td>
80</tr>
81<tr id="r63090caeaf674f2e9a7efdd8f838564c"><td class="cellrowborder" valign="top" width="40.864086408640865%" headers="mcps1.2.4.1.1 "><p id="a9229822a49b6414dbb15ded55b808209"><a name="a9229822a49b6414dbb15ded55b808209"></a><a name="a9229822a49b6414dbb15ded55b808209"></a>function scan(): boolean</p>
82</td>
83<td class="cellrowborder" valign="top" width="44.704470447044706%" headers="mcps1.2.4.1.2 "><p id="a666bb97e527a4d5db4864d72bdb040b7"><a name="a666bb97e527a4d5db4864d72bdb040b7"></a><a name="a666bb97e527a4d5db4864d72bdb040b7"></a>发起WLAN扫描。</p>
84</td>
85<td class="cellrowborder" valign="top" width="14.431443144314432%" headers="mcps1.2.4.1.3 "><p id="abdc11b1866b54d69a8acef2c13da1553"><a name="abdc11b1866b54d69a8acef2c13da1553"></a><a name="abdc11b1866b54d69a8acef2c13da1553"></a>NA</p>
86</td>
87</tr>
88<tr id="r5098f19894e947d9844839c81abdb431"><td class="cellrowborder" valign="top" width="40.864086408640865%" headers="mcps1.2.4.1.1 "><p id="p148521030134915"><a name="p148521030134915"></a><a name="p148521030134915"></a>function getScanInfos(): Promise&lt;Array&lt;WifiScanInfo&gt;&gt;</p>
89<p id="p1264072917366"><a name="p1264072917366"></a><a name="p1264072917366"></a>function getScanInfos(callback: AsyncCallback&lt;Array&lt;WifiScanInfo&gt;&gt;): void</p>
90</td>
91<td class="cellrowborder" valign="top" width="44.704470447044706%" headers="mcps1.2.4.1.2 "><p id="a833c28b165db44e49b49d84eb8f12d3a"><a name="a833c28b165db44e49b49d84eb8f12d3a"></a><a name="a833c28b165db44e49b49d84eb8f12d3a"></a>获取WLAN扫描结果,接口可采用promise或callback方式调用。</p>
92</td>
93<td class="cellrowborder" valign="top" width="14.431443144314432%" headers="mcps1.2.4.1.3 "><p id="ad31109a27e344b27a2879ae7be60da6a"><a name="ad31109a27e344b27a2879ae7be60da6a"></a><a name="ad31109a27e344b27a2879ae7be60da6a"></a>NA</p>
94</td>
95</tr>
96<tr id="rd41bbd2a167e42c4854efaf2f95923a8"><td class="cellrowborder" valign="top" width="40.864086408640865%" headers="mcps1.2.4.1.1 "><p id="p126421517153711"><a name="p126421517153711"></a><a name="p126421517153711"></a>function addDeviceConfig(config: WifiDeviceConfig): Promise&lt;number&gt;</p>
97<p id="p15642617133717"><a name="p15642617133717"></a><a name="p15642617133717"></a>function addDeviceConfig(config: WifiDeviceConfig, callback: AsyncCallback&lt;number&gt;): void</p>
98</td>
99<td class="cellrowborder" valign="top" width="44.704470447044706%" headers="mcps1.2.4.1.2 "><p id="a88ec088696df4f4b8e0e240e66646d9c"><a name="a88ec088696df4f4b8e0e240e66646d9c"></a><a name="a88ec088696df4f4b8e0e240e66646d9c"></a>添加WLAN热点的配置信息,接口可采用promise或callback方式调用。</p>
100</td>
101<td class="cellrowborder" valign="top" width="14.431443144314432%" headers="mcps1.2.4.1.3 "><p id="a57539195f93c4af187781c5c7a64c74c"><a name="a57539195f93c4af187781c5c7a64c74c"></a><a name="a57539195f93c4af187781c5c7a64c74c"></a>NA</p>
102</td>
103</tr>
104<tr id="row18665444142915"><td class="cellrowborder" valign="top" width="40.864086408640865%" headers="mcps1.2.4.1.1 "><p id="p666517440295"><a name="p666517440295"></a><a name="p666517440295"></a>function connectToNetwork(networkId: number): boolean</p>
105</td>
106<td class="cellrowborder" valign="top" width="44.704470447044706%" headers="mcps1.2.4.1.2 "><p id="p13666344152913"><a name="p13666344152913"></a><a name="p13666344152913"></a>连接到WLAN网络。</p>
107</td>
108<td class="cellrowborder" valign="top" width="14.431443144314432%" headers="mcps1.2.4.1.3 "><p id="p56667442298"><a name="p56667442298"></a><a name="p56667442298"></a>NA</p>
109</td>
110</tr>
111<tr id="row176661944152919"><td class="cellrowborder" valign="top" width="40.864086408640865%" headers="mcps1.2.4.1.1 "><p id="p66661144122919"><a name="p66661144122919"></a><a name="p66661144122919"></a>function connectToDevice(config: WifiDeviceConfig): boolean</p>
112</td>
113<td class="cellrowborder" valign="top" width="44.704470447044706%" headers="mcps1.2.4.1.2 "><p id="p4666104410298"><a name="p4666104410298"></a><a name="p4666104410298"></a>连接到WLAN网络。</p>
114</td>
115<td class="cellrowborder" valign="top" width="14.431443144314432%" headers="mcps1.2.4.1.3 "><p id="p766644420298"><a name="p766644420298"></a><a name="p766644420298"></a>NA</p>
116</td>
117</tr>
118<tr id="r53c17c0621b74f9c9b3915fef99bb64d"><td class="cellrowborder" valign="top" width="40.864086408640865%" headers="mcps1.2.4.1.1 "><p id="p5681182313296"><a name="p5681182313296"></a><a name="p5681182313296"></a>function disconnect(): boolean</p>
119</td>
120<td class="cellrowborder" valign="top" width="44.704470447044706%" headers="mcps1.2.4.1.2 "><p id="a2939738c548a44838522d7ac772b697a"><a name="a2939738c548a44838522d7ac772b697a"></a><a name="a2939738c548a44838522d7ac772b697a"></a>断开WLAN连接。</p>
121</td>
122<td class="cellrowborder" valign="top" width="14.431443144314432%" headers="mcps1.2.4.1.3 "><p id="a3862542954ef418b86ba8606721b4d30"><a name="a3862542954ef418b86ba8606721b4d30"></a><a name="a3862542954ef418b86ba8606721b4d30"></a>NA</p>
123</td>
124</tr>
125<tr id="row10868175614302"><td class="cellrowborder" valign="top" width="40.864086408640865%" headers="mcps1.2.4.1.1 "><p id="p586835643012"><a name="p586835643012"></a><a name="p586835643012"></a>function getSignalLevel(rssi: number, band: number): number</p>
126</td>
127<td class="cellrowborder" valign="top" width="44.704470447044706%" headers="mcps1.2.4.1.2 "><p id="p8868155614306"><a name="p8868155614306"></a><a name="p8868155614306"></a>获取WLAN信号强度。</p>
128</td>
129<td class="cellrowborder" valign="top" width="14.431443144314432%" headers="mcps1.2.4.1.3 "><p id="p1786825673010"><a name="p1786825673010"></a><a name="p1786825673010"></a>NA</p>
130</td>
131</tr>
132</tbody>
133</table>
134
135### 使用说明<a name="section129654513264"></a>
136
137在调用WLAN JS接口前需要导入接口类。
138
139```js
140import wf from '@ohos.wifi'; // 导入js接口类
141```
142
143(一)获取WLAN状态
144
1451.  调用isWifiActive​\(\)接口查询WLAN是否打开。
146
147    ```js
148    var isWifiActive = wf.isWifiActive(); // 若WLAN打开,则返回true,否则返回false
149    ```
150
151
152(二)发起扫描并获取结果
153
1541.  调用scan​\(\)接口发起扫描。
1552.  调用getScanInfoList​\(\)接口获取扫描结果。
156
157    ```js
158    // 调用WLAN扫描接口
159    var isScanSuccess = wf.scan(); // true
160
161    // 延迟一定时间
162
163    // 获取扫描结果
164    wf.getScanInfos((err, result) => {
165        if (err) {
166            console.error("get scan info error");
167            return;
168        }
169
170        var len = Object.keys(result).length;
171        console.log("get scan info number: " + len);
172        for (var i = 0; i < len; ++i) {
173            console.info("ssid: " + result[i].ssid);
174            console.info("bssid: " + result[i].bssid);
175            console.info("securityType: " + result[i].securityType);
176            console.info("rssi: " + result[i].rssi);
177            console.info("band: " + result[i].band);
178            console.info("frequency: " + result[i].frequency);
179            console.info("timestamp: " + result[i].timestamp);
180        }
181    });
182    ```
183
184
185(三)连接WLAN
186
1871.  调用addDeviceConfig添加配置,然后通过返回的配置id连接WLAN或调用connectToDevice通过配置直接连接WLAN。
188
189    ```js
190    // WLAN配置信息
191    var config = {
192        "ssid":"test_wifi",
193        "bssid":"",
194        "preSharedKey":"12345678",
195        "isHiddenSsid":false,
196        "securityType":3,
197    }
198    方式一:
199    // 添加配置
200    wf.addDeviceConfig(config, (err, result) => {
201        if (err) {
202            console.error("add device config error");
203            return;
204        }
205        console.info("config id: " + result);
206        // 通过配置id连接WLAN
207        wf.connectToNetwork(result);
208    });
209    方式二:
210    // 通过配置信息直接连接WLAN
211    wf.connectToDevice(config);
212    ```
213
214
215## 相关仓<a name="section1371113476307"></a>
216
217分布式软总线子系统
218
219communication\_wifi
220
221