Вопрос

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?
Это было полезно?

Решение

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;

Другие советы

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;

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

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.

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" />
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top