3

When I request the Cell ID and LAC information, on some devices I cannot retreive them.

I use this code:

TelephonyManager tm =(TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
location = (GsmCellLocation) tm.getCellLocation();

cellID = location.getCid();

lac = location.getLac();
  1. Does anyone know why some GSM carriers do not provide them?
  2. Do I need permissions for that?
  3. What else is there to know about retreiving the CellID and LAC?
Padma Kumar
  • 19,893
  • 17
  • 73
  • 130
user934779
  • 303
  • 1
  • 8
  • 14

5 Answers5

19

In order to find CellId, you should use 0xffff as bit-mask, NOT mod.

WRONG

new_cid = cellLocation.getCid() % 0xffff;

RIGHT

new_cid = cellLocation.getCid() & 0xffff;
nkout
  • 491
  • 4
  • 11
2

Try to use a PhoneStateListener as following:

First, create the listener.

public PhoneStateListener phoneStateListener = new PhoneStateListener() {
    @Override
    public void onCellLocationChanged (CellLocation location) {
        StringBuffer str = new StringBuffer();
        // GSM
        if (location instanceof GsmCellLocation) {
            GsmCellLocation loc = (GsmCellLocation) location;
            str.append("gsm ");
            str.append(loc.getCid());
            str.append(" ");
            str.append(loc.getLac());
            Log.d(TAG, str.toString());
            }
    }
};

And then register, on onCreate(), the listener as following:

telephonyManager = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CELL_LOCATION);

As stated on the documentation, the LISTEN_CELL_LOCATION requires you to add the following permission:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
Eduardo
  • 4,282
  • 2
  • 49
  • 63
  • What is solution for CDMA? Is it work when the user location service (setting) is off? – afruzan Feb 10 '16 at 20:42
  • @guidomocha, Solution is similar, but CDMA system does not contain LAC, CID, you have instead Network ID and System ID. Check the documentation of http://developer.android.com/reference/android/telephony/cdma/CdmaCellLocation.html – Eduardo Feb 13 '16 at 16:39
0

You need to use TelephonyManager

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    GsmCellLocation cellLocation = (GsmCellLocation) telephonyManager
            .getCellLocation();

    // Cell Id, LAC
    int cellid = cellLocation.getCid();
    int lac = cellLocation.getLac();

    // MCC
    String MCC = telephonyManager.getNetworkOperator();
    int mcc = Integer.parseInt(MCC.substring(0, 3));

    // Operator name
    String operatoprName = telephonyManager.getNetworkOperatorName();

For permission you need to add followin in the Manifest.xml file

 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Yogesh
  • 111
  • 1
  • 9
0

I think this is due to the way the manufacturers have implemented the underlying kernel code on the device, not allowing you to access certain information.

Mimminito
  • 2,803
  • 3
  • 21
  • 27
-2

So you can try something like. I have got cell id and the location area code for GSM. But for UMTS, getCid () returns a big number for exple 33 166 248. So i add modulo operator (exple xXx.getCid() % 0xffff).

GsmCellLocation cellLocation = (GsmCellLocation)telm.getCellLocation();

    new_cid = cellLocation.getCid() % 0xffff;
    new_lac = cellLocation.getLac() % 0xffff;
13KZ
  • 1,295
  • 5
  • 24
  • 43