androidkotlintelephonymanagersignal-strength

Parsing CellSignalStrength in Kotlin: how can I get all strength parameters individually?


right now i am trying to make an app to show signal strength information as hobby while learning programming. here is the code i have.

val tm = getSystemService(TELEPHONY_SERVICE) as TelephonyManager
val ss = tm.allCellInfo[0].cellSignalStrength
Log.d("AmbilData",ss.toString())
Log.d("AmbilData",ss.dbm.toString())

and it resulted like this

2023-06-03 11:29:09.246 31107-31107 AmbilData               com.example.ssdt                     D  CellSignalStrengthLte: rssi=-53 rsrp=-93 rsrq=-18 rssnr=2147483647 cqi=2147483647 ta=1 level=3 parametersUseForLevel=0
2023-06-03 11:29:09.256 31107-31107 AmbilData               com.example.ssdt                     D  -93

from logs above, we can see that there is a lot of parameters in signal strength like rssi,rsrp, and others, but dbm from getDbm according to this reference seems to only have rsrp parameter as an output. is there a way to get each parameters in an Integer format like rsrp? i tried to search from this reference but no luck.

i have tried writing like these

for ((a,b) in ss){}  //give this error : For-loop range must have an 'iterator()' method
ss.forEach() //give this error : <html>Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:<br/>public inline fun &lt;T&gt; Iterable&lt;TypeVariable(T)&gt;.forEach(action: (TypeVariable(T)) -&gt; Unit): Unit defined in kotlin.collections<br/>public inline fun &lt;K, V&gt; Map&lt;out TypeVariable(K), TypeVariable(V)&gt;.forEach(action: (Map.Entry&lt;TypeVariable(K), TypeVariable(V)&gt;) -&gt; Unit): Unit defined in kotlin.collections
ss.rssi //give this error : Unresolved reference: rssi

it would be good if it can be presented as mutableList or mutableMap. but mainly i would like to be able to use operator such as +, -, *, /, that way i can try to make this app be able to calculate average number for each parameters.


Solution

  • I have finally found the answer to my own question. it's just that, I am still lacking in understanding Kotlin. back then, when I just post this question, I wonder why I can't take the parameters. and if I can't take them directly, how about turning it into a string and then parsing them with things like "substring" and "indexof". It's quite a hassle but I seriously think, it may become my solution. but I found another solution, which I will post here.

    before the solution, there is something to explain related to my case. CellSignalStrength and CellSignalStrengthLte are two different classes. but CellSignalStrengthLte is like an extension of CellSignalStrength. That's why we can't take CellSignalStrengthLte parameters directly from CellSignalStrength. so, my problem in understanding previously is here :

    val ss = tm.allCellInfo[0].cellSignalStrength //this code have ss in CellSignalStrength class
    Log.d("AmbilData",ss.toString()) //this code print CellSignalStrengthLte class of ss variable
    

    since the class variable which is stored and put in the log is different, no wonder I can't take out the variable.

    the solution to this problem that I found is to use "Is" in comparison. the code is below:

    val tm = getSystemService(TELEPHONY_SERVICE) as TelephonyManager
    val ss = tm.allCellInfo[0].cellSignalStrength //this code have ss in CellSignalStrength class
    if (ss is CellSignalStrengthLte){ //ss became a variable with CellSignalStrengthLte class 
        Log.d("RSSI",ss.rssi.toString) //show rssi value in log
        Log.d("RSRQ",ss.rsrq.toString) //show rsrq value in log
        Log.d("RSRP",ss.rsrp.toString) //show rsrp value in log
        Log.d("SINR",ss.rssnr.toString) //show rssnr value in log
    }
    when (ss){ //using when for multiple type
        is CellSignalStrengthLte -> //ss became a variable with CellSignalStrengthLte class
            Log.d("RSSI",ss.rssi.toString) //show rssi value in log
            Log.d("RSRQ",ss.rsrq.toString) //show rsrq value in log
            Log.d("RSRP",ss.rsrp.toString) //show rsrp value in log
            Log.d("SINR",ss.rssnr.toString) //show rssnr value in log
        is CellSignalStrengthGsm -> //ss became a variable with CellSignalStrengthGsm class
        is CellSignalStrengthNr -> //ss became a variable with CellSignalStrengthNr class
    }