1

I was trying to check if the device is connected to internet and what is the network type. Here is an example of how we check it in android

public boolean isConnectingToInternet(Activity act){
        boolean isthere = false;
        TelephonyManager tm = (TelephonyManager) act.getSystemService(Context.TELEPHONY_SERVICE); 
        if (tm.getSimState() != TelephonyManager.SIM_STATE_UNKNOWN){
            ConnectivityManager connectivityManager = (ConnectivityManager) act.getSystemService(Context.CONNECTIVITY_SERVICE);
            if ((connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED))
                isthere = true;
        } else {
            ConnectivityManager connectivityManager = (ConnectivityManager) act.getSystemService(Context.CONNECTIVITY_SERVICE);
            if ((connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED))
                isthere = true;
        }
        return isthere;
    }
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
Raj Bhagat
  • 81
  • 5

2 Answers2

1

Add permission ohos.permission.GET_NETWORK_INFO, which is used to obtain network information. Add permission ohos.permission.INTERNET, which is used to access the network.

• Check if network is connected

public static boolean hasInternetConnection(Context context) {
        NetManager netManager = NetManager.getInstance(context);
        NetCapabilities netCapabilities = netManager.getNetCapabilities(netManager.getDefaultNet());
        return netCapabilities.hasCap(NetCapabilities.NET_CAPABILITY_VALIDATED);
}

• Check if WiFi is connected

public static boolean isWifiConnectedInternet(Context context) {
       NetManager netManager = NetManager.getInstance(context);
       NetCapabilities netCapabilities = netManager.getNetCapabilities(netManager.getDefaultNet());
       return netCapabilities.hasCap(NetCapabilities.NET_CAPABILITY_VALIDATED) &&
                netCapabilities.hasBearer(NetCapabilities.BEARER_WIFI) ||
                netCapabilities.hasBearer(NetCapabilities.BEARER_WIFI_AWARE);
}

• Check if Mobile network is connected

public static boolean isMobileConnectedInternet(Context context) {
        NetManager netManager = NetManager.getInstance(context);
        NetCapabilities netCapabilities = netManager.getNetCapabilities(netManager.getDefaultNet());
        return netCapabilities.hasCap(NetCapabilities.NET_CAPABILITY_VALIDATED) &&
                netCapabilities.hasBearer(NetCapabilities.BEARER_CELLULAR);
    }
Marek Potkan
  • 452
  • 2
  • 9
Zinna
  • 1,947
  • 2
  • 5
  • 20
  • How to do that in smart devices, so JS not Java? – lacas Jan 25 '22 at 07:47
  • @lacas Please refer to HarmonyOS JS API References of Network Management and Communications and Connections in https://developer.harmonyos.com/en/docs/documentation/doc-references/js-apis-overview-0000001056361791 For example: Network State: https://developer.harmonyos.com/en/docs/documentation/doc-references/js-apis-system-network-0000000000626092 WiFi: https://developer.harmonyos.com/en/docs/documentation/doc-references/js-apis-wlan-0000001121342036 – Zinna Jan 27 '22 at 19:29
0

You could refer to this Docs to check the status of the connection to the Internet.

And the TelephonyConstants.NETWORK_TYPE_LTE can determine the network type.

// Obtain the RadioInfoManager object.
RadioInfoManager radioInfoManager = RadioInfoManager.getInstance(context);

// Obtain the signal information.
List<SignalInformation> signalList = radioInfoManager.getSignalInfoList(slotId);

// Check the size of the signal information list.
if (signalList.size() == 0) {
    return;
}
// Traverse the signal information list to obtain the signal information of the current network type.
LteSignalInformation lteSignal = null;
for (SignalInformation signal : signalList) {
    int signalNetworkType = signal.getNetworkType();
    if (signalNetworkType == TelephonyConstants.NETWORK_TYPE_LTE) {
        lteSignal = (LteSignalInformation) signal;
    }
}
// Obtain the signal strength of the specified RAT (methods in the child class).
int signalLevel = lteSignal != null ? lteSignal.getSignalLevel() : 0;
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108