1f1549183Sopenharmony_ci# communication\_wifi<a name="EN-US_TOPIC_0000001162030287"></a> 2f1549183Sopenharmony_ci 3f1549183Sopenharmony_ci- [Introduction](#section11660541593) 4f1549183Sopenharmony_ci- [Architecture](#section342962219551) 5f1549183Sopenharmony_ci- [Directory Structure](#section161941989596) 6f1549183Sopenharmony_ci- [Usage](#section1312121216216) 7f1549183Sopenharmony_ci - [Available APIs](#section1551164914237) 8f1549183Sopenharmony_ci - [Usage Guidelines](#section129654513264) 9f1549183Sopenharmony_ci 10f1549183Sopenharmony_ci- [Repositories Involved](#section1371113476307) 11f1549183Sopenharmony_ci 12f1549183Sopenharmony_ci## Introduction<a name="section11660541593"></a> 13f1549183Sopenharmony_ci 14f1549183Sopenharmony_ciA 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. 15f1549183Sopenharmony_ci 16f1549183Sopenharmony_ciThe 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. 17f1549183Sopenharmony_ci 18f1549183Sopenharmony_ci## Architecture<a name="section342962219551"></a> 19f1549183Sopenharmony_ci 20f1549183Sopenharmony_ci**Figure 1** WLAN architecture<a name="fig356513281301"></a> 21f1549183Sopenharmony_ci 22f1549183Sopenharmony_ci 23f1549183Sopenharmony_ci 24f1549183Sopenharmony_ci 25f1549183Sopenharmony_ci## Directory Structure<a name="section161941989596"></a> 26f1549183Sopenharmony_ci 27f1549183Sopenharmony_ci``` 28f1549183Sopenharmony_ci/foundation/communication/wifi 29f1549183Sopenharmony_ci├── figures # Figures 30f1549183Sopenharmony_ci├── interfaces # APIs 31f1549183Sopenharmony_ci│ ├── innerkits # Inner APIs 32f1549183Sopenharmony_ci│ └── kits # WLAN APIs 33f1549183Sopenharmony_ci├── services # Services 34f1549183Sopenharmony_ci│ └── wifi_standard # Service implementation 35f1549183Sopenharmony_ci├── tests # Test code 36f1549183Sopenharmony_ci│ └── wifi_standard # Test code for the service implementation module 37f1549183Sopenharmony_ci└── utils # Utility functions 38f1549183Sopenharmony_ci ├── inc # Header directory for utility functions 39f1549183Sopenharmony_ci └── src # Implementation directory for utility functions 40f1549183Sopenharmony_ci``` 41f1549183Sopenharmony_ci 42f1549183Sopenharmony_ci## Usage<a name="section1312121216216"></a> 43f1549183Sopenharmony_ci 44f1549183Sopenharmony_ci### Available APIs<a name="section1551164914237"></a> 45f1549183Sopenharmony_ci 46f1549183Sopenharmony_ciThe following table describes JavaScript APIs in **@ohos.wifi** available for basic WLAN functions. 47f1549183Sopenharmony_ci 48f1549183Sopenharmony_ci**Table 1** Major JavaScript APIs available for basic WLAN functions 49f1549183Sopenharmony_ci 50f1549183Sopenharmony_ci<a name="t96d5b1a809be46328adc684bff001cf7"></a> 51f1549183Sopenharmony_ci 52f1549183Sopenharmony_ci<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> 53f1549183Sopenharmony_ci</th> 54f1549183Sopenharmony_ci<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> 55f1549183Sopenharmony_ci</th> 56f1549183Sopenharmony_ci<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> 57f1549183Sopenharmony_ci</th> 58f1549183Sopenharmony_ci</tr> 59f1549183Sopenharmony_ci</thead> 60f1549183Sopenharmony_ci<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> 61f1549183Sopenharmony_ci</td> 62f1549183Sopenharmony_ci<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> 63f1549183Sopenharmony_ci</td> 64f1549183Sopenharmony_ci<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> 65f1549183Sopenharmony_ci</td> 66f1549183Sopenharmony_ci</tr> 67f1549183Sopenharmony_ci<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> 68f1549183Sopenharmony_ci</td> 69f1549183Sopenharmony_ci<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> 70f1549183Sopenharmony_ci</td> 71f1549183Sopenharmony_ci<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> 72f1549183Sopenharmony_ci</td> 73f1549183Sopenharmony_ci</tr> 74f1549183Sopenharmony_ci<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> 75f1549183Sopenharmony_ci</td> 76f1549183Sopenharmony_ci<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> 77f1549183Sopenharmony_ci</td> 78f1549183Sopenharmony_ci<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> 79f1549183Sopenharmony_ci</td> 80f1549183Sopenharmony_ci</tr> 81f1549183Sopenharmony_ci<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> 82f1549183Sopenharmony_ci</td> 83f1549183Sopenharmony_ci<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> 84f1549183Sopenharmony_ci</td> 85f1549183Sopenharmony_ci<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> 86f1549183Sopenharmony_ci</td> 87f1549183Sopenharmony_ci</tr> 88f1549183Sopenharmony_ci<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<Array<WifiScanInfo>></p> 89f1549183Sopenharmony_ci<p id="p1264072917366"><a name="p1264072917366"></a><a name="p1264072917366"></a>function getScanInfos(callback: AsyncCallback<Array<WifiScanInfo>>): void</p> 90f1549183Sopenharmony_ci</td> 91f1549183Sopenharmony_ci<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> 92f1549183Sopenharmony_ci</td> 93f1549183Sopenharmony_ci<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> 94f1549183Sopenharmony_ci</td> 95f1549183Sopenharmony_ci</tr> 96f1549183Sopenharmony_ci<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<number></p> 97f1549183Sopenharmony_ci<p id="p15642617133717"><a name="p15642617133717"></a><a name="p15642617133717"></a>function addDeviceConfig(config: WifiDeviceConfig, callback: AsyncCallback<number>): void</p> 98f1549183Sopenharmony_ci</td> 99f1549183Sopenharmony_ci<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> 100f1549183Sopenharmony_ci</td> 101f1549183Sopenharmony_ci<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> 102f1549183Sopenharmony_ci</td> 103f1549183Sopenharmony_ci</tr> 104f1549183Sopenharmony_ci<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> 105f1549183Sopenharmony_ci</td> 106f1549183Sopenharmony_ci<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> 107f1549183Sopenharmony_ci</td> 108f1549183Sopenharmony_ci<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> 109f1549183Sopenharmony_ci</td> 110f1549183Sopenharmony_ci</tr> 111f1549183Sopenharmony_ci<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> 112f1549183Sopenharmony_ci</td> 113f1549183Sopenharmony_ci<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> 114f1549183Sopenharmony_ci</td> 115f1549183Sopenharmony_ci<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> 116f1549183Sopenharmony_ci</td> 117f1549183Sopenharmony_ci</tr> 118f1549183Sopenharmony_ci<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> 119f1549183Sopenharmony_ci</td> 120f1549183Sopenharmony_ci<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> 121f1549183Sopenharmony_ci</td> 122f1549183Sopenharmony_ci<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> 123f1549183Sopenharmony_ci</td> 124f1549183Sopenharmony_ci</tr> 125f1549183Sopenharmony_ci<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> 126f1549183Sopenharmony_ci</td> 127f1549183Sopenharmony_ci<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> 128f1549183Sopenharmony_ci</td> 129f1549183Sopenharmony_ci<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> 130f1549183Sopenharmony_ci</td> 131f1549183Sopenharmony_ci</tr> 132f1549183Sopenharmony_ci</tbody> 133f1549183Sopenharmony_ci</table> 134f1549183Sopenharmony_ci 135f1549183Sopenharmony_ci### Usage Guidelines<a name="section129654513264"></a> 136f1549183Sopenharmony_ci 137f1549183Sopenharmony_ciBefore invoking WLAN JavaScript APIs, you need to import the **@ohos.wifi\_native\_js** class. 138f1549183Sopenharmony_ci 139f1549183Sopenharmony_ci```js 140f1549183Sopenharmony_ciimport wf from '@ohos.wifi'; // Import the @ohos.wifi class. 141f1549183Sopenharmony_ci``` 142f1549183Sopenharmony_ci 143f1549183Sopenharmony_ci- Obtaining the WLAN state 144f1549183Sopenharmony_ci 145f1549183Sopenharmony_ci1. Call the **isWifiActive\(\)** method to check whether the WLAN is active. 146f1549183Sopenharmony_ci 147f1549183Sopenharmony_ci ```js 148f1549183Sopenharmony_ci var isWifiActive = wf.isWifiActive(); // Value true indicates that WLAN is enabled, and false indicates the opposite. 149f1549183Sopenharmony_ci ``` 150f1549183Sopenharmony_ci 151f1549183Sopenharmony_ci 152f1549183Sopenharmony_ci- Starting a scan and obtaining the scan results. 153f1549183Sopenharmony_ci 154f1549183Sopenharmony_ci1. Call the **scan\(\)** method to start a scan. 155f1549183Sopenharmony_ci2. Call the **getScanInfoList\(\)** method to obtain the scan results. 156f1549183Sopenharmony_ci 157f1549183Sopenharmony_ci ```js 158f1549183Sopenharmony_ci // Start a scan. 159f1549183Sopenharmony_ci var isScanSuccess = wf.scan(); // true 160f1549183Sopenharmony_ci 161f1549183Sopenharmony_ci // Wait for some time. 162f1549183Sopenharmony_ci 163f1549183Sopenharmony_ci // Obtain the scan results. 164f1549183Sopenharmony_ci wf.getScanInfos((err, result) => { 165f1549183Sopenharmony_ci if (err) { 166f1549183Sopenharmony_ci console.error("get scan info error"); 167f1549183Sopenharmony_ci return; 168f1549183Sopenharmony_ci } 169f1549183Sopenharmony_ci 170f1549183Sopenharmony_ci var len = Object.keys(result).length; 171f1549183Sopenharmony_ci console.log("get scan info number: " + len); 172f1549183Sopenharmony_ci for (var i = 0; i < len; ++i) { 173f1549183Sopenharmony_ci console.info("ssid: " + result[i].ssid); 174f1549183Sopenharmony_ci console.info("bssid: " + result[i].bssid); 175f1549183Sopenharmony_ci console.info("securityType: " + result[i].securityType); 176f1549183Sopenharmony_ci console.info("rssi: " + result[i].rssi); 177f1549183Sopenharmony_ci console.info("band: " + result[i].band); 178f1549183Sopenharmony_ci console.info("frequency: " + result[i].frequency); 179f1549183Sopenharmony_ci console.info("timestamp: " + result[i].timestamp); 180f1549183Sopenharmony_ci } 181f1549183Sopenharmony_ci }); 182f1549183Sopenharmony_ci ``` 183f1549183Sopenharmony_ci 184f1549183Sopenharmony_ci 185f1549183Sopenharmony_ciSet up a WLAN connection. 186f1549183Sopenharmony_ci 187f1549183Sopenharmony_ci1. To set up a WLAN, you can call **addDeviceConfig** to add a hotspot configuration first, and then use the returned hotspot configuration ID to coonect to a WLAN. Or you can set up a WLAN by calling **connectToDevice** through the hotspot configuration directly. 188f1549183Sopenharmony_ci 189f1549183Sopenharmony_ci ```js 190f1549183Sopenharmony_ci // Configure WLAN information. 191f1549183Sopenharmony_ci var config = { 192f1549183Sopenharmony_ci "ssid":"test_wifi", 193f1549183Sopenharmony_ci "bssid":"", 194f1549183Sopenharmony_ci "preSharedKey":"12345678", 195f1549183Sopenharmony_ci "isHiddenSsid":false, 196f1549183Sopenharmony_ci "securityType":3, 197f1549183Sopenharmony_ci } 198f1549183Sopenharmony_ci 199f1549183Sopenharmony_ci Method 1: 200f1549183Sopenharmony_ci // Add a hotspot configuration. 201f1549183Sopenharmony_ci wf.addDeviceConfig(config, (err, result) => { 202f1549183Sopenharmony_ci if (err) { 203f1549183Sopenharmony_ci console.error("add device config error"); 204f1549183Sopenharmony_ci return; 205f1549183Sopenharmony_ci } 206f1549183Sopenharmony_ci console.info("config id: " + result); 207f1549183Sopenharmony_ci // Set up a WLAN based on the returned hotspot configuration ID. 208f1549183Sopenharmony_ci wf.connectToNetwork(result); 209f1549183Sopenharmony_ci }); 210f1549183Sopenharmony_ci 211f1549183Sopenharmony_ci Method 2: 212f1549183Sopenharmony_ci // Set up a WLAN by calling connectToDevice with the hotspot configuration directly. 213f1549183Sopenharmony_ci wf.connectToDevice(config); 214f1549183Sopenharmony_ci ``` 215f1549183Sopenharmony_ci 216f1549183Sopenharmony_ci 217f1549183Sopenharmony_ci## Repositories Involved<a name="section1371113476307"></a> 218f1549183Sopenharmony_ci 219f1549183Sopenharmony_ci[DSoftBus](https://gitee.com/openharmony/docs/blob/master/en/readme/dsoftbus.md) 220f1549183Sopenharmony_ci 221f1549183Sopenharmony_ci**communication\_wifi** 222f1549183Sopenharmony_ci 223