STA Development

Introduction

The 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.

Use Cases

Available APIs

For details about the JavaScript APIs and sample code, see the STA API Reference.

The following table describes the related APIs.

API Description
isWifiActive() Checks whether WLAN is enabled.
addCandidateConfig() Adds candidate network configurations. Enable WLAN before using this API.
connectToCandidateConfig() Connects to a candidate network.
isConnected() Checks whether WLAN is connected.
removeCandidateConfig() Removes candidate network configurations.
getCandidateConfigs() Obtains candidate network configurations.
on(type: 'wifiStateChange') Subscribes to WLAN state changes.
off(type: 'wifiStateChange') Unsubscribes from WLAN state changes.
on(type: 'wifiConnectionChange') Subscribes to WLAN connection state changes.
off(type: 'wifiConnectionChange') Unsubscribes from WLAN connection state changes.

How to Develop

Checking the Wi-Fi Status

  1. Import the required Wi-Fi module.

  2. Check that the SystemCapability.Communication.WiFi.STA capability is available.

  3. Apply for the ohos.permission.GET_WIFI_INFO permission.

  4. Enable Wi-Fi on the device.

  5. Sample code:

    import { wifiManager } from '@kit.ConnectivityKit';
    
    let recvPowerNotifyFunc: (result: number) => void = (result: number) => {
      let wifiState = "";
      switch (result) {
        case 0:
          wifiState += 'DISABLED';
          break;
        case 1:
          wifiState += 'ENABLED';
          break;
        case 2:
          wifiState += 'ENABLING';
          break;
        case 3:
          wifiState += 'DISABLING';
          break;
        default:
          wifiState += 'UNKNOWN STATUS';
          break;
      }
      console.info(`Wi-Fi state changed: ${wifiState}`);
    };
    try {
      wifiManager.on("wifiStateChange", recvPowerNotifyFunc);
      let isWifiActive = wifiManager.isWifiActive();
      if (!isWifiActive) {
        console.info("Wi-Fi not enabled. Skipping monitor.");
      } else {
        console.info("Wi-Fi is enabled. Starting monitor...");
      }
    } catch (error) {
      console.error(`WiFi state monitor failed: ${error.message}`);
    } finally {
      try {
        wifiManager.off("wifiStateChange", recvPowerNotifyFunc);
        console.info("Wi-Fi monitor off: listener removed.");
      } catch (e) {
         console.error(`WiFi state monitor failed. ${e.message}`);
      }
    }
    

Establishing a Wi-Fi Connection

  1. Import the required Wi-Fi module.

  2. Enable Wi-Fi on the device.

  3. Check that the SystemCapability.Communication.WiFi.STA capability is available.

  4. Apply for the ohos.permission.GET_WIFI_INFO and ohos.permission.SET_WIFI_INFO permissions.

  5. Sample code:

    import { wifiManager } from '@kit.ConnectivityKit';
    
    try {
      let recvWifiConnectionChangeFunc = (result: number) => {
        console.info("Receive wifi connection change event: " + result);
      }
    
      let config: wifiManager.WifiDeviceConfig = {
        ssid: "****",
        bssid: "****",
        preSharedKey: "****",
        securityType: 0
      }
    
      // Update the current Wi-Fi connection status.
      wifiManager.on("wifiConnectionChange", recvWifiConnectionChangeFunc);
      // Add candidate network configurations.
      wifiManager.addCandidateConfig(config).then(result => {
        // Connect to the specified network.
        wifiManager.connectToCandidateConfig(result);
      });
    
      if (!wifiManager.isConnected()) {
        console.info("Wi-Fi not connected");
      }
      // Obtain link information.
      wifiManager.getLinkedInfo().then(data => {
        console.info("get Wi-Fi linked info: " + JSON.stringify(data));
      })
      // Query the signal strength.
      let rssi = -88;
      let band = 1;
      let level = wifiManager.getSignalLevel(rssi, band);
      console.info("level:" + JSON.stringify(level));
    
      // Unsubscribe from Wi-Fi connection state changes.
      wifiManager.off("wifiConnectionChange", recvWifiConnectionChangeFunc);
    } catch (error) {
      console.error(`WiFi Connection failed. ${error.message}`);
    }
    
  6. Check the Wi-Fi connection status. For details, see ConnState.

  7. For details about error codes, see Wi-Fi Error Codes.