# WLAN >**NOTE:** >The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version. ## Modules to Import ``` import wifi from '@ohos.wifi'; ``` ## wifi.isWifiActive isWifiActive\(\): boolean Checks whether the WLAN is activated. - Return values

Type

Description

boolean

Returns true if the WLAN is activated; returns false otherwise.

## wifi.scan scan\(\): boolean Starts a scan for WLAN. - Return values

Type

Description

boolean

Returns true if the scan is successful; returns false otherwise.

## wifi.getScanInfos getScanInfos\(\): Promise\> Obtains the scan result. This method uses a promise to return the result. - Return values

Type

Description

Promise< Array<WifiScanInfo> >

Promise used to return the scan result, which is a list of hotspot information.

## wifi.getScanInfos getScanInfos\(callback: AsyncCallback\>\): void Obtains the scan result. This method uses an asynchronous callback to return the result. - Parameters

Name

Type

Mandatory

Description

callback

AsyncCallback< Array<WifiScanInfo>>

Yes

Callback invoked to return the scan result, which is a list of hotspot information.

- Example ``` import wifi from '@ohos.wifi'; wifi.getScanInfos(result => { var len = Object.keys(result).length; console.log("wifi received scan info call back: " + len); for (var i = 0; i < len; ++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); } }); wifi.getScanInfos().then(result => { var len = Object.keys(result).length; console.log("wifi received scan info promise: " + len); for (var i = 0; i < len; ++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); } }); ``` ## WifiScanInfo Defines the WLAN hotspot information.

Name

Type

Readable/Writable

Description

ssid

string

Read-only

Hotspot service set identifier (SSID), in UTF-8 format.

bssid

string

Read-only

Basic service set identifier (BSSID) of the hotspot.

securityType

WifiSecurityType

Read-only

WLAN encryption type.

rssi

number

Read-only

Received Signal Strength Indicator (RSSI) of the hotspot, in dBm.

band

number

Read-only

Frequency band of the WLAN access point (AP).

frequency

number

Read-only

Frequency of the WLAN AP.

timestamp

number

Read-only

Timestamp.

## WifiSecurityType Enumerates the WLAN encryption types.

Name

Default Value

Description

WIFI_SEC_TYPE_INVALID

0

Invalid encryption.

WIFI_SEC_TYPE_OPEN

1

Open encryption.

WIFI_SEC_TYPE_WEP

2

Wired equivalent privacy (WEP) encryption.

WIFI_SEC_TYPE_PSK

3

PSK encryption.

WIFI_SEC_TYPE_SAE

4

Simultaneous authentication of equals (SAE) encryption.

## wifi.getSignalLevel getSignalLevel\(rssi: number, band: number\): number Obtains the WLAN signal strength. - Parameters

Name

Type

Mandatory

Description

rssi

number

Yes

RSSI of the hotspot, in dBm.

band

number

Yes

Frequency band of the WLAN AP.

- Return values

Type

Description

number

Signal strength obtained. The value range is 0 to 4.

## wifi.getIpInfo7+ getIpInfo\(\): IpInfo Obtains IP information. - Return values

Type

Description

IpInfo

IP information obtained.

## IpInfo Defines IP information.

Name

Type

Readable/Writable

Description

ipAddress

number

Read-only

IP address.

gateway

number

Read-only

Gateway.

netmask

number

Read-only

Subnet mask.

primaryDns

number

Read-only

IP address of the preferred DNS server.

secondDns

number

Read-only

IP address of the alternate DNS server.

serverIp

number

Read-only

IP address of the DHCP server.

leaseDuration

number

Read-only

Lease duration of the IP address.

## wifi.isConnected7+ isConnected\(\): boolean Checks whether the WLAN is connected. - Return values

Type

Description

boolean

Returns true if the WLAN is connected; returns false otherwise.

## wifi.getLinkedInfo7+ getLinkedInfo\(\): Promise Obtains WLAN connection information. This method uses a promise to return the result. - Return values

Type

Description

Promise<WifiLinkedInfo>

Promise used to return the WLAN connection information

## wifi.getLinkedInfo7+ getLinkedInfo\(callback: AsyncCallback\): void Obtains WLAN connection information. This method uses a callback to return the result. - Parameters

Name

Type

Mandatory

Description

callback

AsyncCallback<WifiLinkedInfo>

Yes

Callback invoked to return the WLAN connection information.

- Example ``` import wifi from '@ohos.wifi'; wifi.getLinkedInfo(data => { console.info("get wifi linked info [callback]: " + JSON.stringify(data)); }); wifi.getLinkedInfo().then(data => { console.info("get wifi linked info [promise]: " + JSON.stringify(data)); }).catch(error => { console.info("linked info promise then error"); }); ``` ## WifiLinkedInfo Defines the WLAN connection information.

Name

Type

Readable/Writable

Description

ssid

string

Read-only

Hotspot SSID, in UTF-8 format.

bssid

string

Read-only

BSSID of the hotspot.

rssi

number

Read-only

RSSI of the hotspot, in dBm.

band

number

Read-only

Frequency band of the WLAN AP.

linkSpeed

number

Read-only

Speed of the WLAN AP.

frequency

number

Read-only

Frequency of the WLAN AP.

isHidden

boolean

Read-only

Whether the WLAN AP is hidden.

isRestricted

boolean

Read-only

Whether data volume is restricted at the WLAN AP.

macAddress

string

Read-only

MAC address of the device that sets up the WLAN connection.

ipAddress

number

Read-only

IP address of the device that sets up the WLAN connection.

connState

ConnState

Read-only

WLAN connection state.

## ConnState Enumerates the WLAN connection states.

Name

Default Value

Description

SCANNING

0

The device is scanning for available APs.

CONNECTING

1

A WLAN connection is being established.

AUTHENTICATING

2

An authentication is being performed for a WLAN connection.

OBTAINING_IPADDR

3

The IP address of the WLAN connection is being acquired.

CONNECTED

4

A WLAN connection is established.

DISCONNECTING

5

The WLAN connection is being disconnected.

DISCONNECTED

6

The WLAN connection is disconnected.

UNKNOWN

7

Failed to set up a WLAN connection.

## wifi.getCountryCode7+ getCountryCode\(\): string Obtains the country code. - Return values

Type

Description

string

Country code obtained.

## wifi.isFeatureSupported7+ isFeatureSupported\(featureId: number\): boolean Checks whether the device supports the specified WLAN feature. - Parameters

Name

Type

Mandatory

Description

featureId

number

Yes

Feature ID.

- Return values

Type

Description

boolean

Returns true if the feature is supported; returns false otherwise.

- Enumerates the WLAN features.

Value

Description

0x0001

WLAN infrastructure mode.

0x0002

5 GHz bandwidth.

0x0004

Generic Advertisement Service (GAS)/Access Network Query Protocol (ANQP) feature.

0x0008

Wi-Fi Direct.

0x0010

SoftAP.

0x0040

Wi-Fi AWare.

0x8000

WLAN AP/STA concurrency

0x8000000

WPA3 Personal (WPA-3 SAE) feature.

0x10000000

WPA3 Enterprise Suite B feature.

0x20000000

Enhanced open feature.

## wifi.on\('wifiStateChange'\)7+ on\(type: "wifiStateChange", callback: Callback\): void Registers the WLAN state change events. - Parameters

Name

Type

Mandatory

Description

type

string

Yes

Event type. The value is wifiStateChange.

callback

Callback<number>

Yes

Callback invoked to return the WLAN state.

- Enumerates the WLAN states.

Value

Description

0

Deactivated.

1

Activated.

2

Activating.

3

Deactivating.

## wifi.off\('wifiStateChange'\)7+ off\(type: "wifiStateChange", callback?: Callback\): void Unregisters the WLAN state change events. - Parameters

Name

Type

Mandatory

Description

type

string

Yes

Event type. The value is wifiStateChange.

callback

Callback<number>

No

Callback used to report the WLAN state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.

- Example ``` import wifi from '@ohos.wifi'; import { EventListener } from '@ohos.wifi'; var WIFI_POWER_STATE = "wifiStateChange"; var listener = new EventListener(); var recvPowerNotifyFunc = result => { console.info("power state receive event: " + result); } // Register event listener.on(WIFI_POWER_STATE, recvPowerNotifyFunc); // Unregister event listener.off(WIFI_POWER_STATE, recvPowerNotifyFunc); ``` ## wifi.on\('wifiConnectionChange'\)7+ on\(type: "wifiConnectionChange", callback: Callback\): void Registers the WLAN connection state change events. - Parameters

Name

Type

Mandatory

Description

type

string

Yes

Event type. The value is wifiConnectionChange.

callback

Callback<number>

Yes

Callback invoked to return the WLAN connection state.

- Enumerates the WLAN connection states.

Value

Description

0

Disconnected.

1

Connected.

## wifi.off\('wifiConnectionChange'\)7+ off\(type: "wifiConnectionChange", callback?: Callback\): void Unregisters the WLAN connection state change events. - Parameters

Name

Type

Mandatory

Description

type

string

Yes

Event type. The value is wifiConnectionChange.

callback

Callback<number>

No

Callback used to report the WLAN connection state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.

## wifi.on\('wifiScanStateChange'\)7+ on\(type: "wifiScanStateChange", callback: Callback\): void Registers the WLAN scan state change events. - Parameters

Name

Type

Mandatory

Description

type

string

Yes

Event type. The value is wifiScanStateChange.

callback

Callback<number>

Yes

Callback invoked to return the WLAN scan state.

- Enumerates the WLAN scan states.

Value

Description

0

The scan failed.

1

The scan is successful.

## wifi.off\('wifiScanStateChange'\)7+ off\(type: "wifiScanStateChange", callback?: Callback\): void Unregisters the WLAN scan state change events. - Parameters

Name

Type

Mandatory

Description

type

string

Yes

Event type. The value is wifiScanStateChange.

callback

Callback<number>

No

Callback used to report the WLAN scan state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.

## wifi.on\('wifiRssiChange'\)7+ on\(type: "wifiRssiChange", callback: Callback\): void Registers the RSSI state change events. - Parameters

Name

Type

Mandatory

Description

type

string

Yes

Event type. The value is wifiRssiChange.

callback

Callback<number>

Yes

Callback invoked to return the RSSI, in dBm.

## wifi.off\('wifiRssiChange'\)7+ off\(type: "wifiRssiChange", callback?: Callback\): void Unregisters the RSSI state change events. - Parameters

Name

Type

Mandatory

Description

type

string

Yes

Event type. The value is wifiRssiChange.

callback

Callback<number>

No

Callback used to report the RSSI. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.

## wifi.on\('hotspotStateChange'\)7+ on\(type: "hotspotStateChange", callback: Callback\): void Registers the hotspot state change events. - Parameters

Name

Type

Mandatory

Description

type

string

Yes

Event type. The value is hotspotStateChange.

callback

Callback<number>

Yes

Callback invoked to return the hotspot state.

- Enumerates the hotspot states.

Value

Description

0

Deactivated.

1

Activated.

2

Activating.

3

Deactivating.

## wifi.off\('hotspotStateChange'\)7+ off\(type: "hotspotStateChange", callback?: Callback\): void Unregisters the hotspot state change events. - Parameters

Name

Type

Mandatory

Description

type

string

Yes

Event type. The value is hotspotStateChange.

callback

Callback<number>

No

Callback used to report the hotspot state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.