2013-09-16 107 views
4

我想獲得與我的設備連接的這個wifi的信號強度。這可能很容易。但我是初學者。這是作業。如何得到它?如何獲得連接WiFi android的信號強度?

我試過這段代碼。但它不起作用。

registerReceiver(new BroadcastReceiver() { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     final WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
     int state = wifi.getWifiState(); 
     if(state == WifiManager.WIFI_STATE_ENABLED) { 
      List<ScanResult> results = wifi.getScanResults(); 

      for (ScanResult result : results) { 
       if(result.BSSID.equals(wifi.getConnectionInfo().getBSSID())) { 
        int level = WifiManager.calculateSignalLevel(wifi.getConnectionInfo().getRssi(), 
          result.level); 
        int difference = level * 100/result.level; 
        int signalStrangth= 0; 
        if(difference >= 100) 
         signalStrangth = 4; 
        else if(difference >= 75) 
         signalStrangth = 3; 
        else if(difference >= 50) 
         signalStrangth = 2; 
        else if(difference >= 25) 
         signalStrangth = 1; 
        tv.setText(tv.getText() + "\nDifference :" + difference + " signal state:" + signalStrangth); 

       } 

      } 
     } 
    } 
}, new IntentFilter(WifiManager.RSSI_CHANGED_ACTION)); 

我也試過這段代碼。但這是行不通的。

int numberOfLevels = 5; 
    WifiInfo wifiInfo = wifiManager.getConnectionInfo(); 
    int level = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), numberOfLevels); 
+1

如何?提示:發佈LogCat錯誤(s)。 – MarsAtomic

+0

https://www.dropbox.com/s/7sabuxgcnt5c6lu/log.txt –

回答

11

希望工程..

public void onReceive(WifiManager wifiManager) { 
         int numberOfLevels=5; 
         WifiInfo wifiInfo = wifiManager.getConnectionInfo(); 
         int level=WifiManager.calculateSignalLevel(wifiInfo.getRssi(), numberOfLevels); 
         System.out.println("Bars =" +level); 
       } 
0

獲取掃描WiFi網絡的信號強度。我在這裏處理碎片,所以你可以改變,如果你想相應的活動。

WifiManager mWifiManager; 
WifiReceiver mWifiReceiver; 
List<ScanResult> wifiList; 

@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
    mWifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE); 
    if (!mWifiManager.isWifiEnabled()) { 
     // If wifi disabled then enable it 
     Toast.makeText(getActivity(), "wifi is disabled..making it enabled", 
       Toast.LENGTH_LONG).show(); 
     mWifiManager.setWifiEnabled(true); 
    } 
    mWifiReceiver = new WifiReceiver(); 
    IntentFilter mIntentFilter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); 
    mIntentFilter.addAction(WifiManager.RSSI_CHANGED_ACTION); 
    getActivity().registerReceiver(mWifiReceiver, mIntentFilter); 
    mWifiManager.startScan(); 
} 


    public void onPause() { 
    getActivity().unregisterReceiver(mWifiReceiver); 
    super.onPause(); 
} 

public void onResume() { 
    getActivity().registerReceiver(mWifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); 
    super.onResume(); 
} 



class WifiReceiver extends BroadcastReceiver { 
     // This method call when number of wifi connections changed 
     public void onReceive(Context c, Intent intent) { 
      int state = mWifiManager.getWifiState(); 
      int maxLevel = 5; 
      if (state == WifiManager.WIFI_STATE_ENABLED) { 
       // Get Scanned results in an array List 
       wifiList = mWifiManager.getScanResults(); 
       // Iterate on the list 
       for (ScanResult result : wifiList) { 
        //The level of each wifiNetwork from 0-5 
        int level = WifiManager.calculateSignalLevel(
          result.level,maxLevel); 
        String SSID = result.SSID; 
        String capabilities = result.capabilities; 
        // TODO add your own code. 
       } 
      } 
     } 
    } 

您需要添加有關解釋什麼是 「不工作」 的意思,正好權限

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> 
+0

請解釋代碼是如何工作的,它提供了一個更有用的答案。 – SuperBiasedMan