2011-08-09 93 views
2

我正在檢查網絡類型通過調用此函數Android如何獲得wifi信號強度和連接子類型

  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等的整數值是多少.

請有人知道如何做到這一點。

謝謝

回答

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 
     } 
    } 
+0

感謝您的回答,什麼約3G/GPRS連接類型之間的區別。 – Rishi

+2

鏈路速度不測量信號強度。 RSSI確實。 – Jordi

相關問題