harmony 鸿蒙STA Development
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
- Import the required Wi-Fi module.
- Check that the SystemCapability.Communication.WiFi.STA capability is available.
- Apply for the ohos.permission.GET_WIFI_INFO permission.
- Enable Wi-Fi on the device.
Sample code:
import { wifiManager } from '@kit.ConnectivityKit';
try {
let recvPowerNotifyFunc = (result:number) => {
let wifiState = "";
switch (result) {
case 0:
wifiState += 'DISABLEING';
break;
case 1:
wifiState += 'DISABLED';
break;
case 2:
wifiState += 'ENABLING';
break;
case 3:
wifiState += 'ENABLED';
break;
default:
wifiState += 'UNKNOWN STATUS';
break;
}
}
// Subscribe to Wi-Fi state changes.
wifiManager.on("wifiStateChange", recvPowerNotifyFunc);
// Check whether Wi-Fi is enabled.
let isWifiActive = wifiManager.isWifiActive();
if (!isWifiActive) {
console.info("wifi not enable"); // Enable Wi-Fi manually.
return;
}
wifiManager.off("wifiStateChange", recvPowerNotifyFunc);
}
Establishing a Wi-Fi Connection
- Import the required Wi-Fi module.
- Enable Wi-Fi on the device.
- Check that the SystemCapability.Communication.WiFi.STA capability is available.
- Apply for the ohos.permission.GET_WIFI_INFO and ohos.permission.SET_WIFI_INFO permissions.
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("wifi not conneted");
}
// Obtain link information.
wifiManager.getLinkedInfo().then(data => {
console.info("get wifi linked info: " + JSON.stringify(data));
})
// Query the signal strength.
let level = wifiManager.getSignalLevel(rssi,band);
console.info("level:" + JSON.stringify(level));
// Unsubscribe from Wi-Fi connection state changes.
wifiManager.off("wifiConnectionChange", recvWifiConnectionChangeFunc);
}
- Check the Wi-Fi connection status. For details, see ConnState. For details about error codes, see Wi-Fi Error Codes.
你可能感兴趣的鸿蒙文章
harmony 鸿蒙P2P Mode Development Guide
0
赞
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦