I want to get signal strength of this wifi which is connected with my device. This is probably easy. But I am beginner. And It's homework. How to get it?
I tried this code. But it doesn't work.
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));
I also tried this code. But this is doesn't work too.
int numberOfLevels = 5;
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int level = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), numberOfLevels);