openharmony 鸿蒙 capi-oh-wifi-h

2026-08-25 浏览 (1)

oh_wifi.h

Overview

Defines APIs for obtaining Wi-Fi switch state.

File to include: <ConnectivityKit/wifi/oh_wifi.h>

Library: libwifi_ndk.so

System capability: SystemCapability.Communication.WiFi.STA

Since: 13

Related module: Wifi

Summary

Macros

NameDescription
WIFI_MAX_SSID_LEN 33Length of the ssid array of the OH_WifiLinkedInfo member variables. The value is 32 plus the terminator (\0).
Since: 24
WIFI_MAC_LEN 18Lengths of the macAddress and bssid arrays of the OH_WifiLinkedInfo member variables. The value is 17 plus the terminator (\0).
Since: 24

Enums

Nametypedef KeywordDescription
Wifi_ResultCodeWifi_ResultCodeEnumerates the error codes returned by Wi-Fi APIs.
OH_WifiConnStateOH_WifiConnStateEnumerates the Wi-Fi connection states.
OH_WifiChannelWidthOH_WifiChannelWidthEnumerates the channel bandwidths of the connected hotspot.
OH_WifiStandardOH_WifiStandardEnumerates the Wi-Fi standards used by the connected hotspot.
OH_WifiCategoryOH_WifiCategoryEnumerates the highest Wi-Fi categories supported by the hotspot.
OH_WifiLinkTypeOH_WifiLinkTypeEnumerates the Wi-Fi 7 connection types.

Structs

NameDescription
OH_WifiLinkedInfoProvides the WLAN connection information. Information about the connected Wi-Fi hotspot of the current device can be obtained.

Functions

NameDescription
Wifi_ResultCode OH_Wifi_IsWifiEnabled(bool *enabled)Checks whether Wi-Fi is enabled.
Wifi_ResultCode OH_Wifi_GetDeviceMacAddress(char *macAddr, unsigned int *macAddrLen)Obtains the actual MAC address of a device.
Wifi_ResultCode OH_Wifi_GetLinkedInfo(OH_WifiLinkedInfo *info)Obtains Wi-Fi connection information.

Enum Description

Wifi_ResultCode

enum Wifi_ResultCode

Description

Enumerates the error codes returned by Wi-Fi APIs.

Since: 13

Enum ItemDescription
WIFI_SUCCESS = 0Operation success.
WIFI_PERMISSION_DENIED = 201Permission verification fails.
WIFI_INVALID_PARAM = 401Invalid parameter.
Possible causes: 1. The input parameter is a null pointer. 2. The parameter value is out of the value range.
WIFI_NOT_SUPPORTED = 801Function not supported due to limited device capabilities.
WIFI_OPERATION_FAILED = 2501000Operation failed.
Possible cause: The internal execution of the service fails.
WIFI_STA_DISABLED = 2501001STA service not started.
Possible cause: Wi-Fi is disabled.
Since: 21

OH_WifiConnState

enum OH_WifiConnState

Description

Enumerates the WLAN connection states.

Since: 24

Enum ItemDescription
OH_WIFI_CONN_SCANNING = 0The device is scanning for available hotspots.
OH_WIFI_CONN_CONNECTING = 1A WLAN connection is being established.
OH_WIFI_CONN_AUTHENTICATING = 2An authentication is being performed for a WLAN connection.
OH_WIFI_CONN_OBTAINING_IPADDR = 3The IP address of the WLAN connection is being acquired.
OH_WIFI_CONN_CONNECTED = 4A WLAN connection is established.
OH_WIFI_CONN_DISCONNECTING = 5The WLAN connection is being disconnected.
OH_WIFI_CONN_DISCONNECTED = 6The WLAN connection is disconnected.
OH_WIFI_CONN_SPECIAL_CONNECT = 7The WLAN connection is in a special state.
OH_WIFI_CONN_UNKNOWN = 8Failed to establish the WLAN connection.

OH_WifiChannelWidth

enum OH_WifiChannelWidth

Description

Enumerates the WLAN channel bandwidths.

Since: 24

Enum ItemDescription
OH_WIFI_WIDTH_20MHZ = 0Channel bandwidth of 20 MHz.
OH_WIFI_WIDTH_40MHZ = 1Channel bandwidth of 40 MHz.
OH_WIFI_WIDTH_80MHZ = 2Channel bandwidth of 80 MHz.
OH_WIFI_WIDTH_160MHZ = 3Channel bandwidth of 160 MHz.
OH_WIFI_WIDTH_80MHZ_PLUS = 4Bandwidth of 80 MHz for two channels.
OH_WIFI_WIDTH_INVALID = 5Invalid channel bandwidth.

OH_WifiStandard

enum OH_WifiStandard

Description

Enumerates the Wi-Fi standards.

Since: 24

Enum ItemDescription
OH_WIFI_STANDARD_UNDEFINED = 0Invalid Wi-Fi standard.
OH_WIFI_STANDARD_11A = 1802.11a.
OH_WIFI_STANDARD_11B = 2802.11b.
OH_WIFI_STANDARD_11G = 3802.11g.
OH_WIFI_STANDARD_11N = 4802.11n.
OH_WIFI_STANDARD_11AC = 5802.11ac.
OH_WIFI_STANDARD_11AX = 6802.11ax.
OH_WIFI_STANDARD_11AD = 7802.11ad.

OH_WifiCategory

enum OH_WifiCategory

Description

Enumerates the Wi-Fi categories.

Since: 24

Enum ItemDescription
OH_WIFI_CATEGORY_DEFAULT = 1Default category.
OH_WIFI_CATEGORY_WIFI6 = 2WI-FI 6.
OH_WIFI_CATEGORY_WIFI6_PLUS = 3WI-FI 6+.
OH_WIFI_CATEGORY_WIFI7 = 4WI-FI 7.
OH_WIFI_CATEGORY_WIFI7_PLUS = 5WI-FI 7+.

OH_WifiLinkType

enum OH_WifiLinkType

Description

Enumerates the Wi-Fi 7 connection types.

Since: 24

Enum ItemDescription
OH_WIFI_LINK_DISCONNECT = -1Not connected.
OH_WIFI_LINK_DEFAULT_LINK = 0Default connection.
OH_WIFI_LINK_WIFI7_SINGLE_LINK = 1Wi-Fi 7 single-link connection.
OH_WIFI_LINK_WIFI7_MLSR = 2Wi-Fi 7 multi-link single radio (MLSR) connection.
OH_WIFI_LINK_WIFI7_EMLSR = 3Wi-Fi 7 enhanced multi-link single radio (EMLSR) connection.
OH_WIFI_LINK_WIFI7_STR = 4Wi-Fi 7 simultaneous transmit and receive (STR) connection.
OH_WIFI_LINK_WIFI7_LEGACY = 5Wi-Fi 7 traditional mode.

Struct Description

OH_WifiLinkedInfo

Struct OH_WifiLinkedInfo

Description

Provides the WLAN connection information. Information about the connected Wi-Fi hotspot of the current device can be obtained.

Since: 24

NameTypeRead-OnlyOptionalDescription
ssidchar[]NoNoService set identifier (SSID) of the hotspot, which is used to obtain the public name (name of the wireless network) of the Wi-Fi hotspot connected to the current device. The encoding format is UTF-8.
The length is defined by WIFI_MAX_SSID_LEN whose value is 18.
bssidchar[]NoNoBasic service set identifier (BSSID) of the hotspot, which is the MAC address of the wireless network. If the application has the ohos.permission.GET_WIFI_PEERS_MAC permission, bssid in the return value is a real BSSID; otherwise, bssid is a random device address.
The format is AA:BB:CC:DD:EE:FF.
The maximum length is defined by WIFI_MAC_LEN whose value is 33.
rssiint32_tNoNoReceived signal strength indicator (RSSI) of the hotspot, in dBm.
bandint32_tNoNoFrequency band of the WLAN access point (AP). The value 1 indicates 2.4 GHz, and the value 2 indicates 5 GHz.
linkSpeedint32_tNoNoUplink speed of the WLAN AP, in Mbit/s.
rxLinkSpeedint32_tNoNoDownlink speed of the WLAN AP, in Mbit/s.
maxSupportedTxLinkSpeedint32_tNoNoMaximum uplink speed supported, in Mbit/s.
maxSupportedRxLinkSpeedint32_tNoNoMaximum downlink speed supported, in Mbit/s.
frequencyint32_tNoNoFrequency of the WLAN AP, in MHz.
isHiddenboolNoNoWhether the WLAN AP is hidden. The value true indicates that the WLAN AP is hidden; the value false indicates the opposite.
isRestrictedboolNoNoWhether data volume is restricted at the WLAN AP. The value true indicates that data volume is restricted, and the value false indicates the opposite.
macTypeint32_tNoNoMAC address type. The value 0 indicates a random MAC address, and the value 1 indicates a device MAC address.
macAddresschar[]NoNoMAC address of the device. If macType is set to 1, you also need to obtain the ohos.permission.GET_WIFI_LOCAL_MAC permission.
The format is AA:BB:CC:DD:EE:FF.
The length is defined by WIFI_MAC_LEN whose value is 18.
ipAddressuint32_tNoNoIP address of the device that sets up the WLAN connection.
connStateOH_WifiConnStateNoNoWLAN connection state. For details, see OH_WifiConnState.
channelWidthOH_WifiChannelWidthNoNoChannel bandwidth of the connected hotspot. For details, see OH_WifiChannelWidth.
wifiStandardOH_WifiStandardNoNoWi-Fi standard used by the connected hotspot. For details, see OH_WifiStandard.
supportedWifiCategoryOH_WifiCategoryNoNoHighest Wi-Fi category supported by the hotspot. For details, see OH_WifiCategory.
isHiLinkNetworkboolNoNoWhether HiLink is supported by the hotspot. The value true indicates that HiLink is supported, and the value false indicates the opposite.
wifiLinkTypeOH_WifiLinkTypeNoNoWi-Fi 7 connection type. For details, see OH_WifiLinkType.

Function Description

OH_Wifi_IsWifiEnabled()

Wifi_ResultCode OH_Wifi_IsWifiEnabled(bool *enabled)

Description

Checks whether Wi-Fi is enabled.

Since: 13

Parameters

NameDescription
bool *enabledPointer to the boolean value indicating the Wi-Fi state.
The value true indicates that Wi-Fi is enabled, and the value false indicates the opposite.
A null pointer is prohibited. If a null pointer is passed in, an error is returned.

Returns

TypeDescription
Wifi_ResultCodeOperation result, which is specified by Wifi_ResultCode. The value can be:
WIFI_SUCCESS: The Wi-Fi switch status is obtained successfully.
WIFI_INVALID_PARAM: The input parameter is a null pointer.
WIFI_OPERATION_FAILED: An internal error occurs during service execution.

OH_Wifi_GetDeviceMacAddress()

Wifi_ResultCode OH_Wifi_GetDeviceMacAddress(char *macAddr, unsigned int *macAddrLen)

Description

Obtains the actual MAC address of a device.

Required permissions: ohos.permission.GET_WIFI_LOCAL_MAC and ohos.permission.GET_WIFI_INFO

Since: 21

Parameters

NameDescription
char *macAddrCharacter array of the device MAC address, which ends with \0.
unsigned int *macAddrLenMemory size allocated to macAddr.

Returns

TypeDescription
Wifi_ResultCodeOperation result, which is specified by Wifi_ResultCode. The value can be:
WIFI_SUCCESS: The MAC address of the device is obtained successfully.
WIFI_PERMISSION_DENIED: The permission is denied.
WIFI_NOT_SUPPORTED: This capability is not supported.
WIFI_INVALID_PARAM: The input macAddr is a null pointer.
WIFI_OPERATION_FAILED: An internal error occurs during internal execution.
WIFI_STA_DISABLED: The Wi-Fi STA mode is disabled.

OH_Wifi_GetLinkedInfo()

Wifi_ResultCode OH_Wifi_GetLinkedInfo(OH_WifiLinkedInfo *info)

Description

Obtains Wi-Fi connection information.

NOTE

  • If macType is set to 1 (device MAC address), you also need to apply for the ohos.permission.GET_WIFI_LOCAL_MAC permission to obtain the value of macAddress. (For API version 8 to 15, this permission is available only to system applications. For API version 16 and later, this permission is available to common applications on PCs/2-in-1 devices, and is available only to system applications on other devices.) If the ohos.permission.GET_WIFI_LOCAL_MAC permission is not granted, no value is returned for macAddress.
  • If the application has the ohos.permission.GET_WIFI_PEERS_MAC permission, bssid in the return value is a real BSSID; otherwise, bssid is a random device address.

Required permissions: ohos.permission.GET_WIFI_INFO

Since: 24

Parameters

NameDescription
OH_WifiLinkedInfo *infoOH_WifiLinkedInfo struct.

Returns

TypeDescription
Wifi_ResultCodeOperation result, which is specified by Wifi_ResultCode. The value can be:
WIFI_SUCCESS: The Wi-Fi connection information is obtained successfully.
WIFI_PERMISSION_DENIED: The permission is denied.
WIFI_NOT_SUPPORTED: The connection information cannot be obtained.
WIFI_INVALID_PARAM: The input info is a null pointer.
WIFI_OPERATION_FAILED: An internal error occurs during internal execution.

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 js-apis-bluetooth-a2dp

openharmony 鸿蒙 js-apis-bluetooth-map

openharmony 鸿蒙 capi-oh-bluetooth-h

openharmony 鸿蒙 js-apis-nfcTag

openharmony 鸿蒙 js-apis-wifiManagerExt

openharmony 鸿蒙 js-apis-bluetooth-ble

openharmony 鸿蒙 errorcode-wifi

openharmony 鸿蒙 capi-bluetooth

openharmony 鸿蒙 js-apis-wifiext

openharmony 鸿蒙 js-apis-fusionConnectivity-partnerAgent-sys

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/woF90tac