How to check network strength in android
suggest changeConnectivityManager 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 = (WifiManager) getApplication().getSystemService(Context.WIFI_SERVICE);
List<ScanResult> scanResult = wifiManager.getScanResults();
if (scanResult != null) {
for (int i = 0; i < scanResult.size(); i++) {
Log.d("scanResult", "RSSI dBm " + scanResult.get(i).level); // signal strength in dBm
}
}
// Need to get wifi strength
} else if (netType == ConnectivityManager.TYPE_MOBILE) {
Log.i(TAG, "GPRS/3G connection");
// Need to differentiate 3G and GPRS
}
}
ScanResult.level is RSSI in dBm. The loop logs every network from the last scan, and getScanResults() can return null. A scan requires ACCESS_WIFI_STATE, and on API 23 and later a location permission as well. The current link's RSSI is WifiManager.getConnectionInfo().getRssi(). TAG is not declared in this snippet.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents