将 RSRP 从 CellSignalStrengthLte 转换为 RSSI

发布于 2025-01-11 07:37:05 字数 811 浏览 0 评论 0原文

所以我需要获取 LTE 网络的 RSSI,并且我知道 CellSignalStrengthLte 有获取 RSSI 的特殊方法,但它适用于 API >= 29,但我的应用程序的最小 API 为 23。 不过,我可以获得信号强度的 RSRP 值,并且我看到有一些公式可以将 RSSI 转换为 RSRP 并返回,但我不知道如何在 Android 中应用它们。

那么有人有解决办法吗?这是代码的一个小示例:

    val telephonyManager = requireContext().getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        telephonyManager.allCellInfo.first().let { info ->
            when (info) {
                is CellInfoLte -> {
                    val signalStrengthLte = info.cellSignalStrength as CellSignalStrengthLte
                    signalStrengthLte.rssi // requires API >= 29
                    signalStrengthLte.dbm  // returns RSRP value
                    // Needs to convert RSRP to RSSI
                }
            }
        }

So I need to get RSSI of LTE network and I know that CellSignalStrengthLte has special method for getting RSSI, but it is available for API >= 29, but my app has min API of 23.
However I can get a RSRP value for signal strength and I saw that there are some formulas to convert RSSI to RSRP and back, but I don't know how to apply them in Android.

So does anyone have a solution? Here is a small example of code:

    val telephonyManager = requireContext().getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        telephonyManager.allCellInfo.first().let { info ->
            when (info) {
                is CellInfoLte -> {
                    val signalStrengthLte = info.cellSignalStrength as CellSignalStrengthLte
                    signalStrengthLte.rssi // requires API >= 29
                    signalStrengthLte.dbm  // returns RSRP value
                    // Needs to convert RSRP to RSSI
                }
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰火雁神 2025-01-18 07:37:05

最后我无法将 RSRP 转换为 RSSI,但我设法获得了近似的 RSSI 值。在查看了 CellSignalStrengthLte 类的源代码之后 - 我发现了一个旧的已弃用的信号强度专用字段以及将该信号转换为近似 RSSI 值的方法。

因此,为了访问这个 mSignalStrength 私有属性,我使用了反射,然后应用了源代码中的公式。

用于从类获取私有属性的扩展:

@Suppress("UNCHECKED_CAST")
inline fun <reified T : Any, R> T.getPrivateProperty(name: String): R? =
    T::class
        .memberProperties
        .firstOrNull { it.name == name }
        ?.apply { isAccessible = true }
        ?.get(this) as? R

以及从问题中的代码计算近似 RSSI 值:

val signalStrength = signalStrengthLte.getPrivateProperty<CellSignalStrengthLte, Int>("mSignalStrength")
val rssiValue = (signalStrength?.times(2))?.minus(113)

同样,此解决方案仅需要针对使用 API << 的设备实现。 29,因为对于较新的版本有一个变量,它返回几乎(如果不是完全)相同的值。

In the end I wasn't able to convert RSRP to RSSI, but I have managed to get approximate RSSI value. After going through source code of CellSignalStrengthLte class - I found an old deprecated private field for the signal strength and the method to convert this signal to approximate RSSI value.

So to access this mSignalStrength private property I used reflection and then applied the formula from the source code.

Extension for getting private property from the class:

@Suppress("UNCHECKED_CAST")
inline fun <reified T : Any, R> T.getPrivateProperty(name: String): R? =
    T::class
        .memberProperties
        .firstOrNull { it.name == name }
        ?.apply { isAccessible = true }
        ?.get(this) as? R

And the calculation of approximate RSSI value from the code in the question:

val signalStrength = signalStrengthLte.getPrivateProperty<CellSignalStrengthLte, Int>("mSignalStrength")
val rssiValue = (signalStrength?.times(2))?.minus(113)

And again - this solution needs to be implemented only for devices which uses API < 29, because for newer versions there is a variable, which returns almost(if not fully) the same value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文