我想獲得與我的設備連接的這個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);
如何?提示:發佈LogCat錯誤(s)。 – MarsAtomic
https://www.dropbox.com/s/7sabuxgcnt5c6lu/log.txt –