On my test device (OnePlus 6) monitoring signal strength is very inaccurate or at least differs quite a lot from the indicator in the notification bar.
I use two different approaches to check whether the signal is lost:
The first by registering a phone state change listener:
telephonyManager.listen(signalStrengthListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS)
inner class SignalStrengthListener: PhoneStateListener() {
override fun onSignalStrengthsChanged(signalStrength: SignalStrength?) {
val isSignalProbablyLost = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
signalStrength!!.level == 0
} else {
val asu = signalStrength!!.gsmSignalStrength
asu <= 2 || asu == 99
}
}
}
And a second one by gathering the data from the cells directly:
val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
var bestLevel = 0
for (cellInfo in telephonyManager.allCellInfo) {
if (!cellInfo.isRegistered) continue
if (cellInfo is CellInfoCdma) {
val strength = cellInfo.cellSignalStrength
if (strength.level > bestLevel) bestLevel = strength.level
} else if (cellInfo is CellInfoGsm) {
val strength = cellInfo.cellSignalStrength
if (strength.level > bestLevel) bestLevel = strength.level
} else if (cellInfo is CellInfoLte) {
val strength = cellInfo.cellSignalStrength
if (strength.level > bestLevel) bestLevel = strength.level
} else if (cellInfo is CellInfoWcdma) {
val strength = cellInfo.cellSignalStrength
if (strength.level > bestLevel) bestLevel = strength.level
}
}
val isSignalLost = bestLevel == 0
Even those results differ quite a lot from each other. The first one often reports that the signal is lost and the second one says level 2 or so. But even if the second approach reports level 0 then often the built-in indicator sometimes has one or more bars and I am still reachable (tested it by calling the phone).
Are there other (maybe more accurate) ways to measure the cell signal strength that I could give a try?