Kotlin allCellInfo returns null when GPS is off

Viewed 524

I am having an issue in app that has a function which shows current CellId.

Tested this app on various phones but some of them don't want to show even allCellInfo list, however, the app called Network Cell Info works perfect and shows everything about current CellId.

Only when I turn on GPS process works fine and I can see updates of CIDs.

Here is a piece of code (tested on SDK 29):

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@SuppressLint("MissingPermission")
fun getcids(manager: TelephonyManager): Pair<Int, String> {
    val cellInfoList = manager.allCellInfo
    if (cellInfoList != null) {
        for (networkType in cellInfoList) {
            if (networkType is CellInfoLte) {
                cid = networkType.cellIdentity.ci
                break
            }
            if (networkType is CellInfoWcdma) {
                val num = (networkType as CellInfoWcdma).cellIdentity
                cid = num.cid
                break
            }
            if (networkType is CellInfoGsm) {
                val num = (networkType as CellInfoGsm).cellIdentity
                cid = num.cid
                break
            }
        }
        return Pair(cid, cellInfoList.toString())
    }
    else{
        cid = 0
        return Pair(cid, "")
    }
}

What can be done here to get cell info on every phone?

Update

So far I tried this one. Downgraded SDK to 28 and got methods called getNeighboringCellInfo() and getCellLocation(). Tested getCellLocation() on some emulators with Android 8 and lower (as far as I know already Android 9 needs GPS to be turned on to get cell info). Everything was working fine without GPS until I decided to test it on my phones.

As a result app crash on getCellLocation() because it returns null value. However I also checked Network Cell Info where I saw that information about current connections shows without GPS.

1 Answers

From my knowledge, Android 10 forced the ACCESS_FINE_LOCATION permission to be granted. With that, the GPS needs to be on as well. Only then, if the information is available, this method will return the info you need. For Android 9.0 (API 28) or below, it should work as it expected.

So, here is what I would check.

1) To see if app holds permission READ_PHONE_STATE.

2) To see if app holds permission ACCESS_FINE_LOCATION. (For API 29 and above)

3) To see if GPS is actually on. (For API 29 and above)

4) To check if there is a SIM attached to the device and there is service.

If these are in place and still the information is null, then I'd suspect the device itself on this case. I'm not sure what the issue could be if all of them are in place.

Related