onCellInfoChanged() callback is always null
Asked Answered
C

3

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.

Correia answered 18/11, 2013 at 13:53 Comment(1)
Any use to call CellLocation.requestLocationUpdate(); before adding listener.Sidran
T
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.

Toodleoo answered 19/4, 2016 at 22:7 Comment(0)
R
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.

Retire answered 18/11, 2013 at 16:39 Comment(8)
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 :)Correia
i think the galaxy nexus is simply not capable of doing this. My cellInfo is always null. cant stop, wont stop.Correia
@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...Retire
CellInfo requires API 17 and the available s3 can't do that.Correia
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.Retire
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 itCorreia
@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...Retire
when it is rocksolid, i will!Correia
S
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

Sidran answered 11/3, 2019 at 9:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.