目录

Wi-Fi

has.onWifiConnectedWithPartialInfo

has.onWifiConnectedWithPartialInfo(function callback)

Enables listening for Wi-Fi connection events.

Since: 1.0.14

Required permission: ohos.permission.GET_WIFI_INFO, which needs to be declared in module.json5

Parameters

Parameter Type Mandatory Description
callback function Yes Callback for the Wi-Fi connection event.

Return value of callback

Parameter Type Description
wifi WifiInfo WifiInfo object that contains only the SSID attribute.

Example:

const callback = (res) => {
  console.info('onWifiConnectedWithPartialInfo', res);
};

has.onWifiConnectedWithPartialInfo(callback);

has.offWifiConnectedWithPartialInfo

has.offWifiConnectedWithPartialInfo(function callback)

Disables listening for Wi-Fi connection events.

Since: 1.0.14

Required permission: ohos.permission.GET_WIFI_INFO, which needs to be declared in module.json5

Parameters

Parameter Type Mandatory Description
callback function No Callback for the Wi-Fi connection event. If this parameter is left empty, listening will be disabled for all callbacks.

Example:

let callback = (res) => {
  console.info('onWifiConnectedWithPartialInfo', res);
};

has.onWifiConnectedWithPartialInfo(callback);
has.offWifiConnectedWithPartialInfo(callback);
has.offWifiConnectedWithPartialInfo();

has.onWifiConnected

has.onWifiConnected(function callback)

Enables listening for Wi-Fi connection events.

Since: 1.0.4

Required permissions:

  1. ohos.permission.GET_WIFI_INFO, ohos.permission.SET_WIFI_INFO, ohos.permission.LOCATION, and ohos.permission.APPROXIMATELY_LOCATION, which need to be declared in the module.json5 file.

  2. scope.userLocation permission, which is applied for using has.authorize.

Parameters

Parameter Type Mandatory Description
callback function Yes Callback for the Wi-Fi connection event.

Return value of callback

Parameter Type Description
wifi WifiInfo Wi-Fi information.

Example:

const callback = (res) => {
  console.info('onWifiConnected', res);
};

has.onWifiConnected(callback);

has.offWifiConnected

has.offWifiConnected(function callback)

Disables listening for Wi-Fi connection events.

Since: 1.0.4

Required permission: ohos.permission.GET_WIFI_INFO, which needs to be declared in module.json5

Parameters

Parameter Type Mandatory Description
callback function No Callback for the Wi-Fi connection event. If this parameter is left empty, listening will be disabled for all callbacks.

Example:

let callback = (res) => {
  console.info('onWifiConnected', res);
};
has.onWifiConnected(callback);
has.offWifiConnected(callback); // Ensure that the listener is the same as that passed in has.onWifiConnected.
has.offWifiConnected();

has.onGetWifiList

has.onGetWifiList(function callback)

Enables listening for Wi-Fi list obtaining events.

Since: 1.0.4

Required permission: ohos.permission.GET_WIFI_INFO, which needs to be declared in module.json5

Parameters

Parameter Type Mandatory Description
callback function Yes Callback for the Wi-Fi list obtaining event.

Return value of callback

Parameter Type Description
wifiList Array<WifiInfo> Wi-Fi list data.

Example:

has.onGetWifiList(
  (res) => {
    console.info('onGetWifiList', res);
  }
);

has.offGetWifiList

has.offGetWifiList(function callback)

Disables listening for Wi-Fi list obtaining events.

Since: 1.0.4

Required permission: ohos.permission.GET_WIFI_INFO, which needs to be declared in module.json5

Parameters

Parameter Type Mandatory Description
callback function No Callback for the Wi-Fi list obtaining event. If this parameter is left empty, listening will be disabled for all callbacks.

Example:

let listener = (res) => {
  console.info('onGetWifiList', res);
};
has.onGetWifiList(listener);
has.offGetWifiList(listener);
has.offGetWifiList();

has.getWifiList

has.getWifiList(Object object)

Obtains the Wi-Fi list.

Since: 1.0.4

Required permissions:

  1. ohos.permission.GET_WIFI_INFO, ohos.permission.SET_WIFI_INFO, ohos.permission.LOCATION, and ohos.permission.APPROXIMATELY_LOCATION, which need to be declared in the module.json5 file.

  2. scope.userLocation permission, which is applied for using has.authorize.

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Error code information

Error Code Error Message
12010001 wifi not turned on.
12010101 scan wifi error, try later.

Example:

has.getWifiList({
  success: (res) => {
    console.info('getWifiList success', res);
  },
  fail: (err) => {
    console.error('getWifiList fail', err);
  },
  complete: (res) => {
    console.info('getWifiList complete', res);
  }
});

has.getConnectedWifi

has.getConnectedWifi(Object object)

Obtains connected Wi-Fi information.

Since: 1.0.4

Required permissions:

  1. ohos.permission.GET_WIFI_INFO, ohos.permission.SET_WIFI_INFO, ohos.permission.LOCATION, and ohos.permission.APPROXIMATELY_LOCATION, which need to be declared in the module.json5 file.

  2. scope.userLocation permission, which is applied for using has.authorize.

Parameters

The input parameter is an object containing the following fields.

Parameter Type Default Value Mandatory Description
partialInfo boolean false No Whether to return partial Wi-Fi information. If the value is true, the returned WifiInfo contains only the SSID and BSSID fields.
success function - No Callback triggered when the API call is successful.
fail function - No Callback triggered when the API call fails.
complete function - No Callback triggered when the API call is complete, regardless of success or failure.

Return value of success

Parameter Type Description
wifi WifiInfo Wi-Fi information.

Error code information

Error Code Error Message
12010401

1. no connected wifi.

2. get wifiInfo failed.

3. get connect wifi failed.

Example:

has.getConnectedWifi({
  success: (res) => {
    console.info('getConnectedWifi success', res);
  },
  fail: (err) => {
    console.error('getConnectedWifi fail', err);
  },
  complete: (res) => {
    console.info('getConnectedWifi complete', res);
  }
});

has.connectWifi

has.connectWifi(Object object)

Connects to the Wi-Fi network. If Wi-Fi information is known, you can use this API to connect to the Wi-Fi network.

Since: 1.0.4

Required permissions:

  1. ohos.permission.GET_WIFI_INFO, ohos.permission.SET_WIFI_INFO, ohos.permission.LOCATION, and ohos.permission.APPROXIMATELY_LOCATION, which need to be declared in the module.json5 file.

  2. scope.userLocation permission, which is applied for using has.authorize.

Parameters

The input parameter is an object containing the following fields.

Parameter Type Default Value Mandatory Description
SSID string - Yes Wi-Fi device SSID.
BSSID string - No Wi-Fi device BSSID.
password string - Yes Wi-Fi device password.
manual boolean false No Redirects to the system settings screen for connection.
success function - No Callback triggered when the API call is successful.
fail function - No Callback triggered when the API call fails.
complete function - No Callback triggered when the API call is complete, regardless of success or failure.

Error code information

Error Code Error Message
101 context is undefined.
12010701

1. connectWifi fail.

2. connectWifi error.

12010702

1. start wifiSettings page error.

2. no wifi found.

For details, see Wi-Fi Error Codes.

Example:

has.connectWifi({
  SSID: 'SSID',
  BSSID: 'BSSID',
  password: 'password',
  success: (res) => {
    console.info('connectWifi success', res);
  },
  fail: (err) => {
    console.error('connectWifi fail', err);
  },
  complete: (res) => {
    console.info('connectWifi complete', res);
  }
});

WifiInfo

Defines the Wi-Fi information.

Since: 1.0.4

Parameter Type Description
SSID string Wi-Fi device SSID.
BSSID string Wi-Fi device BSSID, which is a random device address.
secure boolean

Whether the Wi-Fi network is secure. Invalid and open encryption modes are considered insecure.

- true: secure.

- false: insecure.

signalStrength number Wi-Fi signal strength. The value range is [0, 1].
frequency number Wi-Fi frequency band, in MHz.