Reputation: 2362
I want to detect if a phone has a slow internet connection or high-speed internet.
Now they have deprecated NetworkInfo
and suggesting that we should use ConnectivityManager#getNetworkCapabilities
using this I am able to get the signal strength but not able to figure out how to use integer value returned by networkCapabilities.getSignalStrength()
It is returning an Integer value I am getting these values (-39, -71, -31). My question is how should we define that signal strength is good/poor.
Here is my code to get Signal Strength:
ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
Network activeNetwork = cm.getActiveNetwork();
NetworkCapabilities networkCapabilities = cm.getNetworkCapabilities(activeNetwork);
int signalStrength = networkCapabilities.getSignalStrength();
Upvotes: 6
Views: 2908
Reputation: 40898
First this is a directive approach rather than being a direct answer to the question.
int signalStrength = networkCapabilities.getSignalStrength();
Doc:
This is a signed integer, with higher values indicating a stronger signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units reported by wifi code.
This means that signalStrength
holds a value that is relevant to the signal bearer; for instance if the bearer is WiFi, then the signalStrength
will reflect the same WiFi RSSI units.
RSSI, or “Received Signal Strength Indicator,” is a measurement of how well your device can hear a signal from an access point or router. It’s a value that is useful for determining if you have enough signal to get a good wireless connection.
So, you need to map those units to some quality gauge to know whether the signal is weak/strong. This is communication/signal dependent rather than a programming point of view... This thread and also this one may help you for that in case of WiFi bearer.
But you need to customize this quality level for other types of signal bearers the same-wise according to their RSSI units.
GSM signal for instance you may use CellSignalStrengthGsm which has getRssi()
CellSignalStrengthLte
is for LTE and so on.
You may also get the level of signal strength from the android.telephony API's SignalStrength
class... there is a getLevel()
method which returns an integer from 0 to 4 representing the general signal quality. Here you can find a listener to that.
Upvotes: 6