How to know device is connected to Wifi or 3G, programatically

前端 未结 2 1620
小鲜肉
小鲜肉 2020-12-16 04:37

How i can know device is connected to Wifi or 3G, programmatically

Thanks

相关标签:
2条回答
  • 2020-12-16 04:45

    you can use WifiManager class as mentioned here

    Edit: by calling getConnectionInfo() function of WifiManager class you will get WifiInfo object

    WifiInfo has function getBSSID() which gives you connected AP's name

    if its null that means it is not connected to any AP via Wifi ( Wifi is not enabled )

    btw while looking for more info, i found this which should answer all your questions about mobile connectivity and wifi connectivity

    0 讨论(0)
  • 2020-12-16 05:00

    here is my working sample:

    public boolean isNetworkTypeMobile() {
        final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        return (cm!=null && cm.getActiveNetworkInfo()!=null && isNetworkTypeMobile(cm.getActiveNetworkInfo().getType()));
    }
    
    public static boolean isNetworkTypeMobile(int networkType) {
        switch (networkType) {
            case ConnectivityManager.TYPE_MOBILE: //0
            case ConnectivityManager.TYPE_MOBILE_MMS: //2
            case ConnectivityManager.TYPE_MOBILE_SUPL: //3
            case ConnectivityManager.TYPE_MOBILE_DUN: //4
            case ConnectivityManager.TYPE_MOBILE_HIPRI: //5
            case 10:
            case 11:
            case 12:
            case 14:
                return true;
            default:
                return false;
        }
    }
    
    0 讨论(0)
提交回复
热议问题