5

i am trying to get a list of all available cells the device can find. But i am stuck, as my CellInfo is always null and i don't figure why. Can someone give me a hint? There is pretty few info on onCellInfoChanged() at google.

MainActivity:

 CellListener cellListener = new CellListener(this);
 cellListener.start();

CellListener:

public class CellListener extends PhoneStateListener {

private static final String TAG = "CellListener";
private TelephonyManager telephonyManager = null;
private PhoneStateListener listener = null;
private String newCell = null;  
private int events = PhoneStateListener.LISTEN_CELL_LOCATION | PhoneStateListener.LISTEN_CELL_INFO;
private Context context = null;

public CellListener(Context context) {
    this.context = context;
}

public void start() {

    telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    CellLocation.requestLocationUpdate();

    telephonyManager.listen(this, events);
}

@Override
public void onCellInfoChanged(List<CellInfo> cellInfo) {
    Log.i("CellListener","onCellInfoChanged(List<CellInfo> cellInfo) ");
    super.onCellInfoChanged(cellInfo);

     if(cellInfo == null) return;     // this always null here

     for (CellInfo c : cellInfo) {          
        Log.i("CellListener"," c = "+c);
    }       
 }

 @Override
    public void onCellLocationChanged(CellLocation location) {
        if (!(location instanceof GsmCellLocation)) {
            return;
        }
        GsmCellLocation gsmCell = (GsmCellLocation) location;
        String operator = telephonyManager.getNetworkOperator();
        if (operator == null || operator.length() < 4) {
            return;
        }
        newCell = operator.substring(0, 3) + ':' + operator.substring(3) + ':'
                + gsmCell.getLac() + ':' + gsmCell.getCid();

        Log.i(TAG,"newCell = "+newCell);     
    }
}

Logcat:

11-18 14:50:23.806: I/CellListener(4953): newCell = 262:02:4311:99031735
11-18 14:50:23.814: I/CellListener(4953): onCellInfoChanged(List<CellInfo> cellInfo) 

As you can see both events (onCellInfoChanged & onCellLocationChanged) get triggered once and the latter is correctly returning the current cell the device is using.

bofredo
  • 2,348
  • 6
  • 32
  • 51

3 Answers3

5

The real reason you are only being called exactly once and with null as an argument is the following: there appears to be a rate-limiting setting in place by default, as can be observed in the "testing" app, which can be accessed by dialing *#*#INFO#*#* (i.e. *#*#4636#*#*).

In the testing app, choose "Phone Information" and scroll down to the button CELLINFOLISTRATE xxxx, in your case presumably CELLINFOLISTRATE 2147483647. As 2147483647 == MAX_INT, this probably means no calls at all

For me (stock android 6.0, nexus 6), there's the a choice between MAX_INT (one call with null), 0 and 1000.

I'm not 100% sure what these values mean, but presumably the 0 stands for instant (and thus very many) calls, and 1000 for something like at least a second between calls. Keep in mind though, this is pure speculation.

I will edit this answer as I find out more, for example by looking at the implementation of said testing app.

bimmlerd
  • 153
  • 1
  • 6
2

@bofredo: It is returning null because you haven't defined CellInfo yet.

Can't see from your question if you're using CDMA, GSM, or LTE. From memory, CDMA doesn't seem to return anything, but GSM (CellInfoGsm) and LTE (CellInfoLte) do. So doing something like for example:

CellInfoGsm cellInfoGsm = (CellInfoGsm) cellInfo;
CellInfoLte cellInfoLte = (CellInfoLte) cellInfo;

will return an instance of CellInfoGsm or CellInfoLte which will then allow you to retrieve more information about a cell like:

CellIdentityGsm cellIdentityGsm = cellInfoGsm.getCellIdentity();
CellIdentityLte cellIdentityLte = cellInfoLte.getCellIdentity();  

or

CellSignalStrengthGsm cellSignalStrengthGsm = cellInfoGsm.getCellSignalStrength();
CellSignalStrengthLte cellSignalStrengthLte = cellInfoLte.getCellSignalStrength();

Then use cellIdentityGsm, cellIdentityLte, cellSignalStrengthGsm and cellSignalStrengthLte to do what you want in onCellInfoChanged.

ChuongPham
  • 4,761
  • 8
  • 43
  • 53
  • it is in context with a femto-cell, so i guess cellInfoGsm will be suitable. thx, i will start implement this and keep you updated :) – bofredo Nov 18 '13 at 16:46
  • i think the galaxy nexus is simply not capable of doing this. My cellInfo is always null. cant stop, wont stop. – bofredo Nov 19 '13 at 10:55
  • @bofredo: Yes, there are a few devices out there that won't return the result that you want. Can you try a different device, preferably from another manufacturer (LG, Google, etc) and see if you get the same result i.e. null returned. I think, from memory, even the emulators of later Android versions (4+) do return something... – ChuongPham Nov 19 '13 at 12:37
  • CellInfo requires API 17 and the available s3 can't do that. – bofredo Nov 19 '13 at 12:55
  • Try other manufacturers of smartphones, not Samsung. Or, try an Android 4.3 or 4.4 emulator and see what you get- for testing purposes. – ChuongPham Nov 19 '13 at 13:09
  • emulator got no antenna to find cells :> i think i found a way, i forced the phone to only use 2G network and i think i founbd it – bofredo Nov 19 '13 at 13:12
  • 2
    @bofredo: Good on you! Please post back what you did and found so others who read your post in the future can also learn something... – ChuongPham Nov 19 '13 at 13:30
  • 1
    when it is rocksolid, i will! – bofredo Nov 19 '13 at 14:12
0

In addition to @bimmlerd answer. If you are updating the Phone Information via dialing *#*#INFO#*#* (i.e. *#*#4636#*#*). There are chances that the hidden menu doesn't appear. In that case, use default dialer if you are using some 3rd party call management application (Worked for me as it was not showing any hidden menu for phone information).

https://www.reddit.com/r/GooglePixel/comments/6xc40q/4636_service_menu_not_available_in_oreo/

Note: The following picture will help you with the latest Android OS. Just update the mobile info refresh rate in phone information 1/2 option (I hope multiple phone information because of multiple sim cards)

Hidden Menu enter image description here

Shubham AgaRwal
  • 4,355
  • 8
  • 41
  • 62