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 th
Get Signal Strength of Scanned Wifi Networks. I'm working with fragments here, so you can change if you want for Activity accordingly.
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.
}
}
}
}
You need to add permissions
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
Hope it works..
public void onReceive(WifiManager wifiManager) {
int numberOfLevels=5;
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int level=WifiManager.calculateSignalLevel(wifiInfo.getRssi(), numberOfLevels);
System.out.println("Bars =" +level);
}