Go to file
openharmony_ci a81763a209
!3398 dhcp offer selfcure bugfix
Merge pull request !3398 from yiyajun/OpenHarmony-5.0.1-Release
2024-11-26 12:04:10 +00:00
figures move the position of figures dir 2022-04-25 12:34:44 +00:00
wifi !3398 dhcp offer selfcure bugfix 2024-11-26 12:04:10 +00:00
CODEOWNERS 适配系统服务接口管控 2023-07-04 11:39:45 +08:00
LICENSE #I54KZI File location adjustment 2022-04-25 07:33:58 +00:00
OAT.xml wifi蓝区release告警修改 2024-09-03 16:32:28 +08:00
README_zh.md remove file executable permission 2022-07-18 23:03:07 +08:00
README.md remove file executable permission 2022-07-18 23:03:07 +08:00

communication_wifi

Introduction

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

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

Architecture

Figure 1 WLAN architecture

Directory Structure

/foundation/communication/wifi
├── figures            # Figures
├── interfaces         # APIs
│   ├── innerkits      # Inner APIs
│   └── kits           # WLAN APIs
├── services           # Services
│   └── wifi_standard  # Service implementation
├── tests              # Test code
│   └── wifi_standard  # Test code for the service implementation module
└── utils              # Utility functions
    ├── inc            # Header directory for utility functions
    └── src            # Implementation directory for utility functions

Usage

Available APIs

The following table describes JavaScript APIs in @ohos.wifi available for basic WLAN functions.

Table 1 Major JavaScript APIs available for basic WLAN functions

API

Description

Required Permission

function enableWifi(): boolean

Enables WLAN.

NA

function disableWifi(): boolean

Disables WLAN.

NA

function isWifiActive(): boolean

Checks whether WLAN is enabled.

NA

function scan(): boolean

Starts a WLAN scan.

NA

function getScanInfos(): Promise<Array<WifiScanInfo>>

function getScanInfos(callback: AsyncCallback<Array<WifiScanInfo>>): void

Obtains the WLAN scan result. This API can be called in either callback or promise mode.

NA

function addDeviceConfig(config: WifiDeviceConfig): Promise<number>

function addDeviceConfig(config: WifiDeviceConfig, callback: AsyncCallback<number>): void

Adds a WLAN hotspot configuration. This API can be called in either callback or promise mode.

NA

function connectToNetwork(networkId: number): boolean

Sets up a WLAN based on the hotspot configuration ID.

NA

function connectToDevice(config: WifiDeviceConfig): boolean

Sets up a WLAN based on the hotspot configuration.

NA

function disconnect(): boolean

Disconnects from a WLAN.

NA

function getSignalLevel(rssi: number, band: number): number

Obtains the WLAN signal strength.

NA

Usage Guidelines

Before invoking WLAN JavaScript APIs, you need to import the @ohos.wifi_native_js class.

import wf from '@ohos.wifi'; // Import the @ohos.wifi class.
  • Obtaining the WLAN state
  1. Call the isWifiActive() method to check whether the WLAN is active.

    var isWifiActive = wf.isWifiActive(); // Value true indicates that WLAN is enabled, and false indicates the opposite.
    
  • Starting a scan and obtaining the scan results.
  1. Call the scan() method to start a scan.

  2. Call the getScanInfoList() method to obtain the scan results.

    // Start a scan.
    var isScanSuccess = wf.scan(); // true
    
    // Wait for some time.
    
    // Obtain the scan results.
    wf.getScanInfos((err, result) => {
        if (err) {
            console.error("get scan info error");
            return;
        }
    
        var len = Object.keys(result).length;
        console.log("get scan info number: " + len);
        for (var i = 0; i < len; ++i) {
            console.info("ssid: " + result[i].ssid);
            console.info("bssid: " + result[i].bssid);
            console.info("securityType: " + result[i].securityType);
            console.info("rssi: " + result[i].rssi);
            console.info("band: " + result[i].band);
            console.info("frequency: " + result[i].frequency);
            console.info("timestamp: " + result[i].timestamp);
        }
    });
    

Set up a WLAN connection.

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

    // Configure WLAN information.
    var config = {
        "ssid":"test_wifi",
        "bssid":"",
        "preSharedKey":"12345678",
        "isHiddenSsid":false,
        "securityType":3,
    }
    
    Method 1:
    // Add a hotspot configuration.
    wf.addDeviceConfig(config, (err, result) => {
        if (err) {
            console.error("add device config error");
            return;
        }
        console.info("config id: " + result);
        // Set up a WLAN based on the returned hotspot configuration ID.
        wf.connectToNetwork(result);
    });
    
    Method 2:
    // Set up a WLAN by calling connectToDevice with the hotspot configuration directly.
    wf.connectToDevice(config);
    

Repositories Involved

DSoftBus

communication_wifi