Search code examples
javaandroidtelephonymanager

Android: Can't get LTE CellId


I am trying to get the LTE cell ID number in my app:

TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
List<CellInfo> cellInfoList = tm.getAllCellInfo();

for (CellInfo cellInfo : cellInfoList)
    {
    if (cellInfo instanceof CellInfoLte) {
        CellIdentityLte cellIdentityLte = ((CellInfoLte) cellInfo).getCellIdentity();
        cellID = cellIdentityLte.getCi();
    }
}

But cellID = cellIdentityLte.getCi(); Always return 2147483647, the max values telling that something is wrong.

I guess the permissions ACCESS_FINE_LOCATION and READ_PHONE_STATE are enough. Other app on the play store are giving me the correct cellID, so the device is not the issue (and tested on several phones). Running SDK v.30 by the way.


Solution

  • When fetching the value in a for loop, neighbors cells are also iterated, which have a null cid because the UE is not attached to them.

    Here is a fix:

    if (cellIdentityLte.getCi() != 0 && cellIdentityLte.getCi() != 2147483647) 
    {
       cellID = cellIdentityLte.getCi();
    }