communication_wifi/README.md
zhangfeng b3c8566ead update readme document
Signed-off-by: zhangfeng <hw.zhangfeng@huawei.com>
2021-09-30 04:05:59 +00:00

13 KiB
Executable File
Raw Blame History

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

Usage

Available APIs

The following table describes JavaScript APIs in @ohos.wifi_native_js 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 wifi_native_js from '@ohos.wifi_native_js'; // Import the @ohos.wifi_native_js class.
  • Obtaining the WLAN state
  1. Call the isWifiActive() method to check whether WLAN is enabled.

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

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

    // Start a scan.
    var isScanSuccess = wifi_native_js.scan(); // true
    
    // Wait for some time.
    
    // Obtain the scan result.
    wifi_native_js.getScanInfos(result => {
        var num = Object.keys(result).length;
        console.info("wifi scan result mum: " + num);
        for (var i = 0; i < num; ++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. 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.

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

Repositories Involved

communication_wifi