目录

Network

has.getNetworkType

has.getNetworkType(Object object)

Obtains the network type.

Since: 1.0.0

Required permission: ohos.permission.GET_NETWORK_INFO, which must be declared in module.json5.

Parameters

The input parameter is an object containing the following fields.

Name

Type

Mandatory

Description

success

function

No

Callback to be invoked when the API call is successful.

fail

function

No

Callback to be invoked when the API call fails.

complete

function

No

Callback to be invoked when the API call is complete, regardless of success or failure.

Return value of callback

Name

Type

Description

networkType

string

Network type.

  • cellular: cellular network
  • wifi: Wi-Fi network
  • bluetooth: Bluetooth network
  • ethernet: Ethernet network
  • vpn: VPN network
  • none: no network
  • unknown: unknown network

Example

has.getNetworkType({
  success: (res) => {
    console.log(`getNetworkType success, res = ${res}`);
  },
  fail: (err) => {
    console.log(`getNetworkType fail, err = ${err}`);
  },
  complete: (res) => {
    console.log(`getNetworkType complete, res = ${res}`);
  }
});

has.getLocalIPAddress

has.getLocalIPAddress(Object object)

Obtains the LAN IP address.

Since: 1.0.12

Parameters

The input parameter is an object containing the following fields.

Name

Type

Mandatory

Description

success

function

No

Callback to be invoked when the API call is successful.

fail

function

No

Callback to be invoked when the API call fails.

complete

function

No

Callback to be invoked when the API call is complete, regardless of success or failure.

Return value of callback

Name

Type

Description

errMsg

string

Error message.

localip

string

LAN IP address of the local device.

netmask

string

LAN subnet mask of the local device.

Example
has.getLocalIPAddress({
  success: (res) => {
    const localip = res.localip;
  }
})

has.onNetworkStatusChange

has.onNetworkStatusChange(function callback)

Enables listening for network status change events.

Since: 1.0.4

Required permission: ohos.permission.GET_NETWORK_INFO, which must be declared in module.json5.

Parameters

Name

Type

Mandatory

Description

callback

function

Yes

Callback for the network status change event.

Return value of callback

An object containing the following fields.

Name

Type

Description

isConnected

boolean

Whether the network is connected.

networkType

string

Network type.

  • cellular: cellular network
  • wifi: Wi-Fi network
  • bluetooth: Bluetooth network
  • ethernet: Ethernet network
  • vpn: VPN network
  • none: no network
  • unknown: unknown network
Example
has.onNetworkStatusChange(function (res) {  
  console.log(res.isConnected);
  console.log(res.networkType);
});

has.offNetworkStatusChange

has.offNetworkStatusChange(function callback)

Disables listening for network status change events.

Since: 1.0.4

Parameters

Name

Type

Mandatory

Description

callback

function

No

Callback passed to onNetworkStatusChange. If this parameter is left empty, listening will be disabled for all callbacks.

Example
const listener = function (res) {
  console.log(res)
};
has.onNetworkStatusChange(listener);
has.offNetworkStatusChange(listener) ; // Ensure that the listener is the same as that passed in has.onNetworkStatusChange.

has.onNetworkWeakChange

has.onNetworkWeakChange(function callback)

Enables listening for weak network status changes.

Since: 1.0.12

Parameters

Name

Type

Mandatory

Description

callback

function

Yes

Callback for the weak network status change event.

Return value of callback

An object containing the following fields.

Name

Type

Description

weakNet

boolean

Whether weak network is present.

networkType

string

Network type.

  • cellular: cellular network
  • wifi: Wi-Fi network
Example
has.onNetworkWeakChange(function (res) {
  console.log(res.weakNet);
  console.log(res.networkType);
});
// Cancel listening.
has.offNetworkWeakChange();

has.offNetworkWeakChange

has.offNetworkWeakChange(function callback)

Disables listening for weak network status changes.

Since: 1.0.12

Parameters

Name

Type

Mandatory

Description

callback

function

No

Callback passed to onNetworkWeakChange. If this parameter is left empty, listening will be disabled for all callbacks.

Example
const listener = function (res) {
  console.log(res);
};
has.onNetworkWeakChange(listener);
has.offNetworkWeakChange(listener) ; // Ensure that the listener is the same as that passed in has.onNetworkWeakChange.