public final class WifiDevice extends Object
This class provides methods for obtaining Wi-Fi status and information, such as checking whether Wi-Fi is enabled, obtaining the list of information about access points detected in a Wi-Fi scan, and checking whether a Wi-Fi connection has been set up.
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
boolean |
addUntrustedConfig(WifiDeviceConfig config) |
Adds a specified untrusted hotspot configuration.
|
String |
getCountryCode() |
Obtains the country code of this device.
|
static WifiDevice |
getInstance(Context context) |
Obtains a
WifiDevice instance. |
Optional<IpInfo> |
getIpInfo() |
Obtains the IP information of a Wi-Fi connection.
|
Optional<WifiLinkedInfo> |
getLinkedInfo() |
Obtains information about a Wi-Fi connection.
|
List<WifiScanInfo> |
getScanInfoList() |
Obtains the list of information about the access points detected in a Wi-Fi scan.
|
int |
getSignalLevel(int rssi,
int band) |
Calculates the Wi-Fi signal level based on the Wi-Fi RSSI and frequency band.
|
boolean |
isConnected() |
Checks whether a Wi-Fi connection has been set up.
|
boolean |
isFeatureSupported(long featureId) |
Checks whether this device supports a specified feature.
|
boolean |
isWifiActive() |
Checks whether Wi-Fi is enabled.
|
boolean |
removeUntrustedConfig(WifiDeviceConfig config) |
Removes a specified untrusted hotspot configuration.
|
boolean |
scan() |
Scans Wi-Fi hotspots.
|
public static WifiDevice getInstance(Context context) throws IllegalArgumentException
WifiDevice instance.
WifiDevice is a singleton class.
context - Indicates the application context of the caller.WifiDevice instance.IllegalArgumentException - if the caller is missing required context.public boolean isWifiActive()
The application must have the permission specified by ohos.permission.GET_WIFI_INFO.
true if Wi-Fi is enabled; returns false otherwise.public boolean scan()
The method is asynchronous.
After the scanning is complete, the WifiEvents#EVENT_SCAN_STATE notification will be
sent, and the getScanInfoList() method is invoked to obtain the result.
The application must have the permission specified by ohos.permission.SET_WIFI_INFO
and ohos.permission.LOCATION.
true if the method is called successfully; returns false otherwise.public List<WifiScanInfo> getScanInfoList()
The ohos.permission.LOCATION permission and ohos.permission.GET_WIFI_INFO
or ohos.permission.GET_WIFI_PEERS_MAC permission is required
for obtaining all available Wi-Fi access points.
public boolean isConnected()
The application must have the permission specified by ohos.permission.GET_WIFI_INFO.
true if a Wi-Fi connection has been set up; returns false otherwise.public Optional<WifiLinkedInfo> getLinkedInfo()
The connection information includes the SSID, BSSID, and RSSI of the Wi-Fi hotspot.
networkId is the unique ID of the Wi-Fi network.
The application must have the permission specified by ohos.permission.GET_WIFI_INFO.
public Optional<IpInfo> getIpInfo()
The IP information includes the host IP address, gateway address, and DNS information.
The application must have the permission specified by ohos.permission.GET_WIFI_INFO.
public int getSignalLevel(int rssi,
int band)
The Wi-Fi signal level ranges from 0 to 4.
rssi - Indicates the Wi-Fi RSSI.band - Indicates the Wi-Fi frequency band.public String getCountryCode()
The application must have the permission specified by ohos.permission.GET_WIFI_INFO
and ohos.permission.LOCATION.
public boolean isFeatureSupported(long featureId)
featureId - Indicates the ID of the feature to check. WifiUtils provides feature IDs,
for example, WifiUtils#WIFI_FEATURE_INFRA.
The application must have the permission specified by ohos.permission.GET_WIFI_INFO.
true if this device supports the specified feature; returns false otherwise.public boolean addUntrustedConfig(WifiDeviceConfig config)
This method adds one configuration at a time. After this configuration is added, your device will determine whether to connect to the hotspot.
You need to obtain the following permission before calling this method:
SystemPermission.SET_WIFI_INFO.
config - Indicates the untrusted hotspot configuration to add. The configuration can use
only the Simultaneous Authentication of Equals (SAE) or pre-shared key (PSK) mode.true if the untrusted hotspot configuration is added; returns false otherwise.public boolean removeUntrustedConfig(WifiDeviceConfig config)
This method removes one configuration at a time.
You need to obtain the following permission before calling this method:
SystemPermission.SET_WIFI_INFO.
config - Indicates the untrusted hotspot configuration to remove.true if the untrusted hotspot configuration is removed;
returns false otherwise.