• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Wi-Fi Scan 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
11Wi-Fi scan is the process where devices (such as smartphones, PCs, and routers) search for available Wi-Fi networks in their surrounding environment. Through this scan process, devices can gather fundamental information about nearby networks—such as network name, signal strength, and encryption type—thereby enabling connection to, management of, or analysis of these networks.
12
13## Use Cases
14
15- [Wi-Fi Scan](#wi-fi-scan)
16- [PNO Scan](#pno-scan)
17- [Periodic Scan](#periodic-scan)
18- [Scan Control](#scan-control)
19
20## Available APIs
21
22For details about the JavaScript APIs and sample code, see the [SCAN API Reference](../../reference/apis-connectivity-kit/js-apis-wifiManager.md).
23
24The following table describes the related APIs.
25
26| API| Description|
27| -------- | -------- |
28| getScanInfoList() | Obtains the scan result.|
29| on(type: 'wifiScanStateChange') | Subscribes to Wi-Fi scan state changes.|
30| off(type: 'wifiScanStateChange') | Unsubscribes from Wi-Fi scan state changes.|
31
32
33## How to Develop
34
35### Wi-Fi Scan
361. Import the required Wi-Fi module.
372. Check that the **SystemCapability.Communication.WiFi.STA** capability is available.
383. Apply for the **ohos.permission.GET_WIFI_INFO** permission.
394. Enable Wi-Fi on the device.
405. Sample code:
41
42> **NOTE**
43> The Wi-Fi scan API is deprecated since API version 10. The substitute API is available only for system applications.
44
45   ```ts
46   import { wifiManager } from '@kit.ConnectivityKit';
47
48   try {
49     let recvWifiScanStateChangeFunc = (result:number) => {
50         console.info("Receive Wifi scan state change event: " + result);
51     }
52
53     // Obtain the scan status. The value 1 indicates that the scan is successful, and the value 0 indicates the opposite.
54     wifiManager.on("wifiScanStateChange", recvWifiScanStateChangeFunc);
55
56     let isWifiActive = wifiManager.isWifiActive();
57     if (!isWifiActive) {
58       console.error("wifi not enable"); // Enable Wi-Fi manually.
59       return;
60     }
61
62     let scanInfoList = wifiManager.getScanInfoList();
63
64     let len = scanInfoList.length;
65     console.info("wifi received scan info: " + len);
66     if(len > 0){
67       for (let i = 0; i < len; ++i) {
68         console.info("ssid: " + scanInfoList[i].ssid);
69         console.info("bssid: " + scanInfoList[i].bssid);
70         console.info("capabilities: " + scanInfoList[i].capabilities);
71         console.info("securityType: " + scanInfoList[i].securityType);
72         console.info("rssi: " + scanInfoList[i].rssi);
73         console.info("band: " + scanInfoList[i].band);
74         console.info("frequency: " + scanInfoList[i].frequency);
75         console.info("channelWidth: " + scanInfoList[i].channelWidth);
76         console.info("timestamp: " + scanInfoList[i].timestamp);
77         console.info("supportedWifiCategory: " + scanInfoList[i].supportedWifiCategory);
78         console.info("isHiLinkNetwork: " + scanInfoList[i].isHiLinkNetwork);
79       }
80     }
81
82     // Unsubscribe from Wi-Fi scan state changes.
83     wifiManager.off("wifiScanStateChange", recvWifiScanStateChangeFunc);
84   } catch (error) {
85     console.error(`WiFi scan fail. ${error.message}`);
86   }
87   ```
88
89 5. For details about error codes, see [Wi-Fi Error Codes](../../reference/apis-connectivity-kit/errorcode-wifi.md).
90
91 ### PNO Scan
92
93  Preferred Network Offload (PNO) scan is a Wi-Fi scan technology that reduces power consumption of mobile devices.<br>
94  PNO scan is triggered when a device is not connected to any Wi-Fi network and the screen is off. The device then searches for and connects to the preferred Wi-Fi network in the background.
95
96 ### Periodic Scan
97
98  Periodic scan is the process where a wireless device (such as a smartphone, tablet, or laptop) automatically searches for available Wi-Fi networks at the specified interval.<br>
99  This function can be used in the following scenarios:<br>
100  1. When the screen is on and the device is connected to Wi-Fi, the device checks whether the current link supports Internet access. If Internet access is supported, perform scan at an interval of 1 hour. If Internet access is not supported, check the device's mobility status:<br>- Stationary scenario: Scan cycle starts at 20 seconds and doubles exponentially, with a maximum of 300 seconds.<br>- Non-stationary scenario: Scan cycle starts at 20 seconds and caps at 160 seconds.<br>
101  2. When the screen is on and the device is not connected to Wi-Fi:<br>- Stationary scenario: Scan cycle starts at 20 seconds and doubles exponentially, with a maximum of 300 seconds.<br>- Non-stationary scenario: Scan cycle starts at 20 seconds and caps at 160 seconds.
102
103### Scan Control
104
105  Scan control allows management and control of Wi-Fi network scan by wireless devices.<br>
106
107  Typical scenarios include:<br>
108  1. Prohibiting periodic scan and PNO scan when Wi-Fi is disabled<br>
109  2. Prohibiting scan during Wi-Fi connection<br>
110  3. Initiating no more than four scans within 2 minutes<br>
111  4. Prohibiting scan when the device temperature reaches the specified threshold<br>
112  5. Using the substitute of the scan API only for system applications (The scan API is supported since API version 9 and deprecated since API version 10.)<br>
113  6. Requesting the **ohos.permission.GET_WIFI_PEERS_MAC** permission to obtain the actual BSSID in the scan result
114