I'm tying to get imformation from getLinkDownstreamBandwidthKbps(), getLinkUpstreamBandwidthKbps() and getSignalStrength() of NetworkCapabilities class.
For this I do the following in MainActivity:
private static ConnectivityManager connectivityManager;
...
@Override
protected void onCreate(final Bundle savedInstanceState) {
connectivityManager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
}
private final NetworkRequest networkRequest = new NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.build();
private final NetworkCallback networkCallback = new NetworkCallback() {
@Override
public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities) {
int dLBandwidth = networkCapabilities.getLinkDownstreamBandwidthKbps();
int uLBandwidth = networkCapabilities.getLinkUpstreamBandwidthKbps();
int signalStrength = networkCapabilities.getSignalStrength();
final String timestamp = Util.getCurrentTimestamp();
Util.log(String.format(Locale.US, "CM::%s::dLBandwidth=%d::uLBandwidth=%d::sStrength=%d\n",
timestamp, dLBandwidth, uLBandwidth, signalStrength));
}
};
private View getStartButton() { // button click handler
connectivityManager.registerNetworkCallback(networkRequest, networkCallback);
}
private View getStopButton() { // button click handler
connectivityManager.unregisterNetworkCallback(networkCallback);
}
By some reason I get only one entry in log.
CM::2023-09-17 13:11:07.882::dLBandwidth=30000::uLBandwidth=15000::sStrength=-2147483648
So, NetworkCallback is called only once, although I'm moving with the phone around the home so that the signal strength changes.
Could somebody advise what the reason of such behaviour? What I'm doing wrong?
In my case minSdkVersion 29, compileSdk 33.
Thanks!
It looks like such behaviour is predictable.
SignalStrength==-2147483648==Integer.MIN_VALUE== SIGNAL_STRENGTH_UNSPECIFIED.According to documentation it means the following:
NetworkCapabilities.java:1758
So, it looks like in my case signal strength is not provided.
LinkDownstreamBandwidth==30000andLinkUpstreamBandwidth==15000.In my case I get these magic numbers and they are never updated. If we trace the sequence of calls of setters for these values we get the following sequence:
setLinkDownstreamBandwidthKbps:3013
updateNetworkCapabilities:2080
NetworkBandwidth:715
updateBandwidths:801
KEY_BANDWIDTH_STRING_ARRAY:2996
sDefaults.putStringArray:9168
So, I can assume for devices which I used for test I can get only default values for
CELLULARtransport type. Perhaps modem firmware/driver doesn't expose such data. In the same time this API provides correct values forWIFItransport type.