如何在Android应用程序中获得准确的信号强度?

时间:2019-05-18 01:36:27

标签: android kotlin cellinfo

我使用CellInfo每秒捕获一次信号强度和ARFCN / UARFCN / EARFCN。

但是,我认为这不是很准确。它总是比“电话”设置中显示的信号强度更差-5〜-10 dBm。有时,我也得到0或integer.MAX_VALUE。我的代码有问题吗?或者还有其他方法可以捕获实际信号强度吗?

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"
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。问题出在for循环上。最新的信号强度是在第一个循环,因此以下循环将覆盖我需要的结果。