如何从getAllCellInfo获取相邻的单元格信息?

时间:2017-11-01 15:10:47

标签: android networking

这是我用来检测更改的手机状态监听器

public PhoneStateListener mPhoneStateListener = new PhoneStateListener(){

    @Override
    public void onSignalStrengthsChanged(SignalStrength signalStrength) {


        Log.d("SIGNAL ", "  " + signalStrength.toString());






        TelephonyManager tm = (TelephonyManager) mContext.getSystemService(mContext.TELEPHONY_SERVICE);


        List<CellInfo> cellInfos = tm.getAllCellInfo();


        for (int i=0 ;i<cellInfos.size() ;i++)
        {

            Log.d("test ",cellInfos.get(0).toString() );

        }

    }


};

问题是目前我正在获取所有小区信息,如下所示

结果 - &gt;日志 CellInfoLte:{mRegistered = YES mTimeStampType = oem_ril mTimeStamp = 192908703241551ns CellIdentityLte:{mMcc = 405 mMnc = 869 mCi = 2971664 mPci = 123 mTac = 56} CellSignalStrengthLte:ss = 27 rsrp = -90 rsrq = -10 rssnr = 2147483647 cqi = 2147483647 TA = 2147483647}

我如何解析这个以从上面的结果中获取CellInfoLte,CellIdentityLte和CellSignalStrengthLte

2 个答案:

答案 0 :(得分:0)

试试这个(根据cocorico):

String ssignal = signalStrength.toString();
String[] parts = ssignal.split(" ");

signalStrength.toString()来自signalStrength

parts []数组将包含以下元素:

part[0] = "Signalstrength:"  _ignore this, it's just the title_
parts[1] = GsmSignalStrength
parts[2] = GsmBitErrorRate
parts[3] = CdmaDbm
parts[4] = CdmaEcio
parts[5] = EvdoDbm
parts[6] = EvdoEcio
parts[7] = EvdoSnr
parts[8] = LteSignalStrength
parts[9] = LteRsrp
parts[10] = LteRsrq
parts[11] = LteRssnr
parts[12] = LteCqi
parts[13] = gsm|lte|cdma
parts[14] = _not really sure what this number is_

因此,LTEdBm是:

TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
int dbm = 0;

if ( tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_LTE){
    // For Lte SignalStrength: dbm = ASU - 140.
    dbm = Integer.parseInt(parts[8])-140;
} else {
    // For GSM Signal Strength: dbm =  (2*ASU)-113.
    if (signalStrength.getGsmSignalStrength() != 99) {
        int intdbm = -113 + 2
                        * signalStrength.getGsmSignalStrength();
        dbm = Integer.toString(intdbm);
    }
}

答案 1 :(得分:0)

如果你还在努力解决问题,这就是我正在使用的方法:

//permission check
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
        ActivityCompat.requestPermissions((Activity)this,new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},1);

    String list = "";  //I'm just adding everything to a string to display, but you can do whatever

    //get cell info
    TelephonyManager tel = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
    List<CellInfo> infos = tel.getAllCellInfo();
    for (int i = 0; i<infos.size(); ++i) 
        {
            try {
                CellInfo info = infos.get(i);
                if (info instanceof CellInfoGsm) //if GSM connection
                {
                    list += "Site_"+i + "\r\n";
                    list += "Registered: " + info.isRegistered() + "\r\n";
                    CellSignalStrengthGsm gsm = ((CellInfoGsm) info).getCellSignalStrength();
                    CellIdentityGsm identityGsm = ((CellInfoGsm) info).getCellIdentity();
                    list += "cellID: "+ identityGsm.getCid() + "\r\n";
                    list += "dBm: " + gsm.getDbm() + "\r\n\r\n";
                    //call whatever you want from gsm / identitydGsm
                }
                else if (info instanceof CellInfoLte)  //if LTE connection
                {
                    list += "Site_"+i + "\r\n";
                    list += "Registered: " + info.isRegistered() + "\r\n";
                    CellSignalStrengthLte lte = ((CellInfoLte) info).getCellSignalStrength();
                    CellIdentityLte identityLte = ((CellInfoLte) info).getCellIdentity();
                    //call whatever you want from lte / identityLte
                }
                else if (info instanceof CellInfoWcdma)  //if wcdma connection
                {
                    CellSignalStrengthWcdma wcdmaS = ((CellInfoWcdma) info).getCellSignalStrength();
                    CellIdentityWcdma wcdmaid = ((CellInfoWcdma)info).getCellIdentity();
                    list += "Site_"+i + "\r\n";
                    list += "Registered: " + info.isRegistered() + "\r\n";
                    //call whatever you want from wcdmaS / wcdmaid

                }

            } catch (Exception ex) {
                Log.i("neighboring error 2: " ,ex.getMessage());
            }
        }
    Log.i("Info display", list);  //display everything.

虽然我们很忙,但只是一个抬头,LTE解析都是搞砸(你会看到你是否做了getCi(),getTac()等),所以我写了自己的解析类,并发布了我的回答here

警告警告,当你开始看到你的邻近细胞信息完全错误时......(我目前面临的问题),这个困境也被张贴here。祝你好运,如果这部分不是你的问题,请告诉我