1# STA Development 2 3<!--Kit: Connectivity Kit--> 4<!--Subsystem: Communication--> 5<!--Owner: @qq_43802146--> 6<!--Designer: @qq_43802146--> 7<!--Tester: @furryfurry123--> 8<!--Adviser: @zhang_yixin13--> 9 10## Introduction 11The Wi-Fi STA mode (that is, station mode) enables wireless devices to connect to a wireless local area network (WLAN) as clients. In this mode, devices such as mobile phones, computers, and tablets can access the network by connecting to an access point (AP) or wireless router. 12 13 14## Use Cases 15 16- [Checking the Wi-Fi Status](#checking-the-wi-fi-status) 17- [Establishing a Wi-Fi Connection](#establishing-a-wi-fi-connection) 18 19## Available APIs 20 21For details about the JavaScript APIs and sample code, see the [STA API Reference](../../reference/apis-connectivity-kit/js-apis-wifiManager.md). 22 23The following table describes the related APIs. 24 25| API| Description| 26| -------- | -------- | 27| isWifiActive() | Checks whether WLAN is enabled.| 28| addCandidateConfig() | Adds candidate network configurations. Enable WLAN before using this API.| 29| connectToCandidateConfig() | Connects to a candidate network.| 30| isConnected() | Checks whether WLAN is connected.| 31| removeCandidateConfig() | Removes candidate network configurations.| 32| getCandidateConfigs() | Obtains candidate network configurations.| 33| on(type: 'wifiStateChange') | Subscribes to WLAN state changes.| 34| off(type: 'wifiStateChange') | Unsubscribes from WLAN state changes.| 35| on(type: 'wifiConnectionChange') | Subscribes to WLAN connection state changes.| 36| off(type: 'wifiConnectionChange') | Unsubscribes from WLAN connection state changes.| 37 38 39## How to Develop 40 41### Checking the Wi-Fi Status 421. Import the required Wi-Fi module. 432. Check that the **SystemCapability.Communication.WiFi.STA** capability is available. 443. Apply for the **ohos.permission.GET_WIFI_INFO** permission. 454. Enable Wi-Fi on the device. 465. Sample code: 47 48```ts 49 import { wifiManager } from '@kit.ConnectivityKit'; 50 try { 51 let recvPowerNotifyFunc = (result:number) => { 52 let wifiState = ""; 53 switch (result) { 54 case 0: 55 wifiState += 'DISABLING'; 56 break; 57 case 1: 58 wifiState += 'DISABLED'; 59 break; 60 case 2: 61 wifiState += 'ENABLING'; 62 break; 63 case 3: 64 wifiState += 'ENABLED'; 65 break; 66 default: 67 wifiState += 'UNKNOWN STATUS'; 68 break; 69 } 70 } 71 // Subscribe to Wi-Fi connection state changes. 72 wifiManager.on("wifiStateChange", recvPowerNotifyFunc); 73 // Check whether Wi-Fi is enabled. 74 let isWifiActive = wifiManager.isWifiActive(); 75 if (!isWifiActive) { 76 console.info("Wi-Fi not enabled"); // Enable Wi-Fi manually. 77 return; 78 } 79 80 wifiManager.off("wifiStateChange", recvPowerNotifyFunc); 81 } catch (error) { 82 console.error(`WiFi state monitor failed. ${error.message}`); 83 } 84``` 85 86### Establishing a Wi-Fi Connection 871. Import the required Wi-Fi module. 882. Enable Wi-Fi on the device. 893. Check that the **SystemCapability.Communication.WiFi.STA** capability is available. 904. Apply for the **ohos.permission.GET_WIFI_INFO** and **ohos.permission.SET_WIFI_INFO** permissions. 915. Sample code: 92 93 ```ts 94 import { wifiManager } from '@kit.ConnectivityKit'; 95 96 try { 97 let recvWifiConnectionChangeFunc = (result:number) => { 98 console.info("Receive wifi connection change event: " + result); 99 } 100 101 let config:wifiManager.WifiDeviceConfig = { 102 ssid : "****", 103 bssid : "****", 104 preSharedKey : "****", 105 securityType : 0 106 } 107 108 // Update the current Wi-Fi connection status. 109 wifiManager.on("wifiConnectionChange", recvWifiConnectionChangeFunc); 110 // Add candidate network configurations. 111 wifiManager.addCandidateConfig(config).then(result => { 112 // Connect to the specified network. 113 wifiManager.connectToCandidateConfig(result); 114 }); 115 116 if (!wifiManager.isConnected()) { 117 console.info("Wi-Fi not connected"); 118 } 119 // Obtain link information. 120 wifiManager.getLinkedInfo().then(data => { 121 console.info("get Wi-Fi linked info: " + JSON.stringify(data)); 122 }) 123 // Query the signal strength. 124 let rssi = -88; 125 let band = 1; 126 let level = wifiManager.getSignalLevel(rssi,band); 127 console.info("level:" + JSON.stringify(level)); 128 129 // Unsubscribe from Wi-Fi connection state changes. 130 wifiManager.off("wifiConnectionChange", recvWifiConnectionChangeFunc); 131 } catch (error) { 132 console.error(`WiFi Connection failed. ${error.message}`); 133 } 134 ``` 1356. Check the Wi-Fi connection status. For details, see [ConnState](../../reference/apis-connectivity-kit/js-apis-wifiManager.md#connstate9). 1367. For details about error codes, see [Wi-Fi Error Codes](../../reference/apis-connectivity-kit/errorcode-wifi.md). 137