Android: CellID not available on all carriers?
Asked Answered
M

5

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?
Mcclurg answered 21/3, 2012 at 16:4 Comment(0)
V
-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;
Vermont answered 30/3, 2012 at 14:45 Comment(1)
This is wrong. @nkout's answer is the correct answer.Greerson
K
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;
Kissee answered 19/10, 2012 at 7:45 Comment(2)
Correct Indeed. This should be marked as the answer.Ringdove
To put it other way, cellLocation.getCid() % 65536 should also work.Greerson
S
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"/>
Shoemake answered 2/4, 2012 at 14:44 Comment(2)
What is solution for CDMA? Is it work when the user location service (setting) is off?Tact
@guidomocha, Solution is similar, but CDMA system does not contain LAC, CID, you have instead Network ID and System ID. Check the documentation of developer.android.com/reference/android/telephony/cdma/…Shoemake
H
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.

Haiphong answered 21/3, 2012 at 16:7 Comment(0)
R
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" />
Roe answered 31/1, 2014 at 11:48 Comment(1)
What is solution for CDMA? Is it work when the user location service (setting) is off?Tact
V
-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;
Vermont answered 30/3, 2012 at 14:45 Comment(1)
This is wrong. @nkout's answer is the correct answer.Greerson

© 2022 - 2024 — McMap. All rights reserved.