has.getNetworkType(Object object)
Obtains the network type.
Since: 1.0.0
Required permission: ohos.permission.GET_NETWORK_INFO, which needs to be declared in module.json5
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. |
Return value of success
| Parameter | 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 |
Error code information
| Error Code | Error Message |
|---|---|
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2100001 | Invalid parameter value. |
| 2100002 | Failed to connect to the service. |
| 2100003 | System internal error. |
| 40000401 | getNetWorkType error. Possible causes: net type is invalid. For details, see Network Connection Management Error Codes. |
Example:
has.getNetworkType({
success: (res) => {
console.info('getNetworkType success', res);
},
fail: (err) => {
console.error('getNetworkType fail', err);
},
complete: (res) => {
console.info('getNetworkType complete', res);
}
}); has.getLocalIPAddress(Object object)
Obtains the LAN IP address.
Since: 1.0.12
Required permission: ohos.permission.GET_WIFI_INFO, which needs to be declared in module.json5
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. |
Return value of success
| Parameter | Type | Description |
|---|---|---|
| errMsg | string | Error message. |
| localip | string | LAN IP address of the local device. |
| netmask | string | LAN subnet mask of the local device. |
Error code information
| Error Code | Error Message |
|---|---|
| 201 | Permission denied. |
| 801 | Capability not supported. |
| 2501000 | Operation failed. |
| 40001201 | getLocalIPAddress error. |
Example:
has.getLocalIPAddress({
success: (res) => {
console.info('getLocalIPAddress success', res);
},
fail: (err) => {
console.error('getLocalIPAddress fail', err);
},
complete: (res) => {
console.info('getLocalIPAddress complete', res);
}
}); has.onNetworkStatusChange(function callback)
Enables listening for network status change events.
Since: 1.0.4
Required permission: ohos.permission.GET_NETWORK_INFO, which needs to be declared in module.json5
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Listener function for the network status change event. |
Return value of callback
An object containing the following fields.
| Parameter | 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 |
Error code information
| Error Code | Error Message |
|---|---|
| 102 | Parameter error. Possible causes: listener should be a function. |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2100002 | Failed to connect to the service. |
| 2100003 | System internal error. For details, see Network Connection Management Error Codes. |
Example:
has.onNetworkStatusChange(function(res) {
console.info('onNetworkStatusChange callback triggered', res);
}); has.offNetworkStatusChange(function callback)
Removes the listener function for network status change events.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | No | Listener function passed by has.onNetworkStatusChange. If this parameter is not passed, all listener functions will be removed. |
Error code information
| Error Code | Error Message |
|---|---|
| 102 | Parameter error. Possible causes: listener should be a function. |
Example:
const onNetworkStatusChangeFn = function(res) {
console.info('onNetworkStatusChange callback triggered', res);
};
has.onNetworkStatusChange(onNetworkStatusChangeFn);
has.offNetworkStatusChange(onNetworkStatusChangeFn); // Ensure that the listener is the same as that passed in has.onNetworkStatusChange. has.onNetworkWeakChange(function callback)
Enables listening for weak network status changes.
Since: 1.0.12
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Listener function for the weak network status change event. |
Return value of callback
An object containing the following fields.
| Parameter | Type | Description |
|---|---|---|
| weakNet | boolean | Whether weak network is present. |
| networkType | string | Network type. - cellular: cellular network - wifi: Wi-Fi network |
Error code information
| Error Code | Error Message |
|---|---|
| 102 | Parameter error. Possible causes: listener should be a function. |
| 201 | Permission denied. |
| 401 | Invalid parameter. |
| 801 | Capability not supported. |
Example:
has.onNetworkWeakChange(function(res) {
console.info('onNetworkWeakChange callback triggered', res);
});
// Cancel listening.
has.offNetworkWeakChange(); has.offNetworkWeakChange(function callback)
Removes the listener function for weak network status changes.
Since: 1.0.12
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | No | Listener function passed by has.onNetworkWeakChange. If this parameter is not passed, all listener functions will be removed. |
Error code information
| Error Code | Error Message |
|---|---|
| 102 | Parameter error. Possible causes: listener should be a function. |
| 201 | Permission verify error. |
| 401 | Invalid parameter. |
| 801 | Capability not supported. |
Example:
const onNetworkWeakChangeFn = function(res) {
console.info('onNetworkWeakChange callback triggered', res);
};
has.onNetworkWeakChange(onNetworkWeakChangeFn);
has.offNetworkWeakChange(onNetworkWeakChangeFn); // Ensure that the listener is the same as that passed in has.onNetworkWeakChange.