25

Does anybody know how to retrieve cell tower list on GSM and CDMA on Android.

I have been trying to use Google Maps Locations API: https://developers.google.com/maps/documentation/business/geolocation/

And I want to get cell towers information with these fields:

  • cellId: Unique identifier of the cell. On GSM, this is the Cell ID (CID); CDMA networks use the Base Station ID (BID).
  • locationAreaCode: The Location Area Code (LAC) for GSM networks; CDMA networks use Network ID (NID).
  • mobileCountryCode: The cell tower's Mobile Country Code (MCC).
  • mobileNetworkCode: The cell tower's Mobile Network Code. This is the MNC for GSM, or the System ID (SID) for CDMA.
  • age: The number of milliseconds since this cell was primary. If age is 0, the cellId represents a current measurement.
  • signalStrength: Radio signal strength measured in dBm.
  • timingAdvance: The timing advance value.

This code doesn't especially getting cell towers information.

TelephonyManager tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

// Type of the network
int phoneTypeInt = tel.getPhoneType();
String phoneType = null;
phoneType = phoneTypeInt == TelephonyManager.PHONE_TYPE_GSM ? "gsm" : phoneType;
phoneType = phoneTypeInt == TelephonyManager.PHONE_TYPE_CDMA ? "cdma" : phoneType;
try {
  if (phoneType != null) {
    params.put("radioType", phoneType);
  }
} catch (Exception e) {}

/*
 * The below code doesn't work I think.
 */
JSONArray cellList = new JSONArray();
List<NeighboringCellInfo> neighCells = tel.getNeighboringCellInfo();
for (int i = 0; i < neighCells.size(); i++) {
  try {
    JSONObject cellObj = new JSONObject();
    NeighboringCellInfo thisCell = neighCells.get(i);
    cellObj.put("cellId", thisCell.getCid());
    cellList.put(cellObj);
  } catch (Exception e) {}
}
if (cellList.length() > 0) {
  try {
    params.put("cellTowers", cellList);
  } catch (JSONException e) {}
}

And I set permissions like this:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_UPDATES"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>

Please help me, thank you.

3
  • 4
    "My code works except getting cell towers information" is a useless description of your symptoms. Please take the time to explain, in detail, what is and is not working. Nov 8, 2012 at 17:42
  • I'm sorry, English is not my primary language. When I run the code on my Android (carrier is sprint), "tel.getNeighboringCellInfo();" returns always null.
    – wf9a5m75
    Nov 8, 2012 at 18:00
  • And even if I try the code on other android of GSM networks, I got the same result. So my question is how to get cell towers information.
    – wf9a5m75
    Nov 8, 2012 at 18:02

3 Answers 3

12

I had this problem much more recently and it ended up being the fact that

getNeighboringCellInfo

is deprecated from API 23 up. To get around this use something like the following (it's quite annoying, really):

public static JSONArray getCellInfo(Context ctx){
        TelephonyManager tel = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);

        JSONArray cellList = new JSONArray();

// Type of the network
        int phoneTypeInt = tel.getPhoneType();
        String phoneType = null;
        phoneType = phoneTypeInt == TelephonyManager.PHONE_TYPE_GSM ? "gsm" : phoneType;
        phoneType = phoneTypeInt == TelephonyManager.PHONE_TYPE_CDMA ? "cdma" : phoneType;

        //from Android M up must use getAllCellInfo
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {



            List<NeighboringCellInfo> neighCells = tel.getNeighboringCellInfo();
            for (int i = 0; i < neighCells.size(); i++) {
                try {
                    JSONObject cellObj = new JSONObject();
                    NeighboringCellInfo thisCell = neighCells.get(i);
                    cellObj.put("cellId", thisCell.getCid());
                    cellObj.put("lac", thisCell.getLac());
                    cellObj.put("rssi", thisCell.getRssi());
                    cellList.put(cellObj);
                } catch (Exception e) {
                }
            }

        } else {
            List<CellInfo> infos = tel.getAllCellInfo();
            for (int i = 0; i<infos.size(); ++i) {
                try {
                    JSONObject cellObj = new JSONObject();
                    CellInfo info = infos.get(i);
                    if (info instanceof CellInfoGsm){
                        CellSignalStrengthGsm gsm = ((CellInfoGsm) info).getCellSignalStrength();
                        CellIdentityGsm identityGsm = ((CellInfoGsm) info).getCellIdentity();
                        cellObj.put("cellId", identityGsm.getCid());
                        cellObj.put("lac", identityGsm.getLac());
                        cellObj.put("dbm", gsm.getDbm());
                        cellList.put(cellObj);
                    } else if (info instanceof CellInfoLte) {
                        CellSignalStrengthLte lte = ((CellInfoLte) info).getCellSignalStrength();
                        CellIdentityLte identityLte = ((CellInfoLte) info).getCellIdentity();
                        cellObj.put("cellId", identityLte.getCi());
                        cellObj.put("tac", identityLte.getTac());
                        cellObj.put("dbm", lte.getDbm());
                        cellList.put(cellObj);
                    }

                } catch (Exception ex) {

                }
            }
        }

        return cellList;
    }
1
  • 1
    Note that "deprecetad" and "no longer works" are two different things—there are many deprecated APIs that still work as they used to, the only limitation being that they lack support for new features which possibly got introduced since (as with this API, which never had any proper support for LTE). However, vendor support of these APIs varies greatly.
    – user149408
    Jun 6, 2017 at 23:08
9

Your phone might not support this function.

See this: http://code.google.com/p/android/issues/detail?id=24136

and this: Which Android phone models support getNeighboringCellInfo()?

3
  • 1
    I see. Well, do you know another way to get the cell tower list instead of getNeighboringCellInfo()?
    – wf9a5m75
    Nov 8, 2012 at 20:08
  • Sorry. I'm new to this as well. Nov 8, 2012 at 20:26
  • 1
    Thank you, Bernd. I give up to try this.
    – wf9a5m75
    Nov 8, 2012 at 20:39
0

It's not your phone, which doesn't support this info, it is purely crappy implementation of the RIL related C and Java used to provide those API calls, which prevent this. If you manage to go into the Service Mode menus (which vary widely from phone to phone) you will have full and instant access to RSSI and loads of other types of signals. You should blame Google (or your OEM) for not fixing those problems and provide better access to RF related variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.