3

after seeing that question and the answers (thanks by the way) I wrote this code wich is pretty much the same as in the answers:

try {
        List<CellInfo> cellInfoList = telephonyManager.getAllCellInfo();
        for (CellInfo cellInfo : cellInfoList) {
            if (cellInfo instanceof CellInfoGsm) {
                CellInfoGsm cellInfoGsm = (CellInfoGsm) cellInfo;
                CellIdentityGsm cellIdentity = cellInfoGsm.getCellIdentity();
                final CellSignalStrengthGsm gsm = ((CellInfoGsm) cellInfo).getCellSignalStrength();
                rsrpValue = gsm.getDbm();
                pciValue = cellIdentity.getCid();
            } else if (cellInfo instanceof CellInfoCdma) {
                CellInfoCdma cellInfoCdma = (CellInfoCdma) cellInfo;
                CellIdentityCdma cellIdentity = cellInfoCdma.getCellIdentity();
                pciValue = cellIdentity.getBasestationId();
            } else if (cellInfo instanceof CellInfoLte){
                    CellInfoLte cellInfoLte = (CellInfoLte) cellInfo;
                    CellIdentityLte cellIdentity = cellInfoLte.getCellIdentity();
                    pciValue = cellIdentity.getPci();
            } else {
                throw new Exception("Unknown type of cell signal!");
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "Unable to obtain cell signal information", e);
    }

But when I display rsrpValue or pciValue for GSM, I always get the maximum integer value (2147483647). I tried this on a phone with the API 17. Is there something wrong in my code?

Thanks

Community
  • 1
  • 1
Grego
  • 33
  • 1
  • 5
  • is your phone a gsm ? – njzk2 Nov 07 '13 at 15:57
  • S4 mini i9195 GSM and LTE. Plus, It is really an instance of CellInfoGsm since rsrpValue is displaying 2147483647 (Integer.MAX_VALUE) and not 0 as I initialized it. – Grego Nov 07 '13 at 16:03
  • did you try to get the Asu level instead ? – njzk2 Nov 07 '13 at 16:24
  • shouldn't you be calling `cellIngGsm.getCellSignalStrength()` rather than `((CellInfoGsm) cellInfo).getCell...`? (not that it would change anything, though) – njzk2 Nov 07 '13 at 16:26
  • 1
    The code for getDbm is at https://android.googlesource.com/platform/frameworks/base.git/+/master/telephony/java/android/telephony/CellSignalStrengthGsm.java line 128. Apparently MAX_VALUES means unknown. – njzk2 Nov 07 '13 at 16:27
  • 1
    Yes it means unknown but I don't know why it is unknown. I tried with a LTE sim card and getPci() works. So I guess I am not using the good function for GSM. My goal is to get the info for LTE network. GSM was for testing while I was waiting for a LTE sim card. I will look for this error later. But for whoever is looking for LTE network information, this code works. – Grego Nov 07 '13 at 16:48
  • Thanks anyway for your help njkz2 – Grego Nov 07 '13 at 17:02

1 Answers1

1

Try to use the good old PhoneStateListener to listen for signal strength changes. Here is what worked for me. Use this to register the listener:

private TelephonyManager telManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int currentSignalStrength = 0;
int asu = 0;

telManager.listen(new SignalStrengthListener(), PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);

And this is the actual listener:

private class SignalStrengthListener extends PhoneStateListener{

    @Override
    public void onSignalStrengthsChanged(SignalStrength signalStrength)
        {
            super.onSignalStrengthsChanged(signalStrength);
            if (telManager.getPhoneType()== TelephonyManager.PHONE_TYPE_CDMA)
                currentSignalStrength = signalStrength.getCdmaDbm();                  
            else
                asu = signalStrength.getGsmSignalStrength();
        }
    }

Tested on Xperia Z which has the same problems using CellInfoGsm.

Thilo
  • 989
  • 1
  • 17
  • 27
  • Thanks, this is what I did for LTE signal. I do not know why but CellSignalStrengthGsm (or CDMA or LTE) does not work on a galaxy S4 and S4 mini. It has been added since the API 17 but does not work on those devices even with android 4.2.2 maybe it will work on 4.3 – Grego Nov 24 '13 at 17:47
  • On HTC One (Android 4.3) it's even worse. telephonyManager.getAllCellInfo(); is returning null...It's very manufacturer dependent. Best results on Nexus 5 so far. – Thilo Nov 26 '13 at 12:33