androidkotlincellinfo

How to get accurate signal strength in Android application?


I use CellInfo to capture signal strength and ARFCN/UARFCN/EARFCN every second.

But, I think it is not very accurate. It is always worse -5 ~ -10 dBm than the signal strength displayed in Phone settings. Sometimes, I also got 0 or integer.MAX_VALUE. Is my code any problem or is there any other way to capture real signal strength?

var cellInfoList: List<CellInfo> = mTelephonyManager.allCellInfo
if (cellInfoList.isNotEmpty()) {
    for (cellInfo in cellInfoList) {
        if (cellInfo is CellInfoLte) {
            val cellSignalStrengthLte = cellInfo.cellSignalStrength
            val cellIdentityLte = cellInfo.cellIdentity
            var arfcn = cellIdentityLte.earfcn
            intSignalStrength = cellSignalStrengthLte.dbm
            networkStatus = "${Integer.toString(intSignalStrength)} dBm | $arfcn"
        } else if (cellInfo is CellInfoGsm) {
            val cellSignalStrengthGsm = cellInfo.cellSignalStrength
            val cellIdentityGsm = cellInfo.cellIdentity
            var arfcn = cellIdentityGsm.arfcn
            intSignalStrength = cellSignalStrengthGsm.dbm
            networkStatus = "${Integer.toString(intSignalStrength)} dBm | $arfcn"
        } else if (cellInfo is CellInfoWcdma) {
            val cellSignalStrengthWcdma = cellInfo.cellSignalStrength
            val cellIdentityWcdma = cellInfo.cellIdentity
            var arfcn = uarfcnToBand(cellIdentityWcdma.uarfcn)
            intSignalStrength = cellSignalStrengthWcdma.dbm
            networkStatus = "${Integer.toString(intSignalStrength)} dBm | $arfcn"
        } else if (cellInfo is CellInfoCdma) {
            val cellSignalStrengthCdma = cellInfo.cellSignalStrength
            intSignalStrength = cellSignalStrengthCdma.dbm
            networkStatus = "${Integer.toString(intSignalStrength)} dBm"
        }
    }
}

Solution

  • I got the solution. The problem is at for loop. The latest signal strength is at first loop so the following loop will cover the result I need.