Android如何获得wifi信号强度和连接子类型

时间:2011-08-09 11:00:21

标签: android

我正在通过调用此函数来检查网络类型

        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo Info = cm.getActiveNetworkInfo();
        if (Info == null || !Info.isConnectedOrConnecting()) {
            Log.i(TAG, "No connection");
        } else {
            int netType = Info.getType();
            int netSubtype = Info.getSubtype();

            if (netType == ConnectivityManager.TYPE_WIFI) {
                Log.i(TAG, "Wifi connection");
                //Need to get wifi strength
            } else if (netType == ConnectivityManager.TYPE_MOBILE) {
              Log.i(TAG, "GPRS/3G connection"); 
               //Need to get differentiate between 3G/GPRS
            } 
        }

我不想定期轮询wifi的力量,但当我做一些网络的东西,我需要检查连接的wifi强度,我无法得到。 其次,当我将netType()作为MOBILE时,如何确定其3G / GPRS格式netSubType,我的意思是3g / Gprs等的整数值是什么。

请有人知道如何做到这一点。

谢谢

1 个答案:

答案 0 :(得分:3)

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo Info = cm.getActiveNetworkInfo();
        if (Info == null || !Info.isConnectedOrConnecting()) {
            Log.i(TAG, "No connection");
        } else {
            int netType = Info.getType();
            int netSubtype = Info.getSubtype();

            if (netType == ConnectivityManager.TYPE_WIFI) {
                Log.i(TAG, "Wifi connection");
             wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
                 int linkSpeed = wifiManager.getConnectionInfo().getLinkSpeed();
            //Need to get wifi strength
        } else if (netType == ConnectivityManager.TYPE_MOBILE) {
          Log.i(TAG, "GPRS/3G connection"); 
           //Need to get differentiate between 3G/GPRS
        } 
    }