user934779
user934779

Reputation: 303

Android: CellID not available on all carriers?

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?

Upvotes: 3

Views: 5004

Answers (5)

Yogesh
Yogesh

Reputation: 111

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" />

Upvotes: 0

nkout
nkout

Reputation: 491

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;

Upvotes: 19

Eduardo
Eduardo

Reputation: 4382

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"/>

Upvotes: 2

13KZ
13KZ

Reputation: 1335

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;

Upvotes: -2

Mimminito
Mimminito

Reputation: 2853

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.

Upvotes: 0

Related Questions