从电话管理器解析 getAllcellinfo 值

Parse getAllcellinfo values from telephony manager

我正在使用以下函数获取设备网络的 Allcellinfo,我正在获取字符串形式的值,现在我需要解析它以获取 CellSignalStrengthLte 数据,我该如何实现它

//代码

 TelephonyManager tm = (TelephonyManager) Context.getSystemService(mContext.TELEPHONY_SERVICE);
 List<CellInfo> cellInfos = tm.getAllCellInfo();
 String data = cellInfos.get(0).toString();
 Log.d("Info ", " "+data);

结果是

CellInfoLte:{mRegistered=YES mTimeStampType=oem_ril mTimeStamp=207387894126206ns CellIdentityLte:{ mMcc=405 mMnc=869 mCi=2971664 mPci=123 mTac=56} CellSignalStrengthLte: ss=25 rsrp=-91 rsrq=-7 rssnr=2147483647 cqi=2147483647 ta=2147483647}

如何解析此字符串以获取有关 CellinfoLte、CellIdentityLte 的详细信息

我也注意到 CellinfoLTE 和 CellIdentityLTE 目前不是很好,所以我只写了自己的解析 class。仅对此进行了几次测试,没有出现问题,但如果将来需要进行额外的调整,则应显示更多测试。

这是class:

public class LTEStruct
{
public static final int UNKNOWN = Integer.MAX_VALUE;   //Default value for unknown fields

public boolean isRegistered;
public long timeStamp;
public int MCC;
public int MNC;
public int CID;
public int PCI;
public int TAC;

public int SS;
public int RSRP;
public int RSRQ;
public int RSSNR;
public int CQI;
public int tAdvance;

Context mContext;

//Public constructor
public LTEStruct(Context context)
{
    mContext = context; //not used at the moment but possibly for future function
}

public void parse(String inTest)
{
    //get isRegistered
    int index = inTest.indexOf("mRegistered=") + ("mRegistered=").length();
    if(inTest.substring(index,index + 3).contains("YES"))
        isRegistered = true;
    else
        isRegistered = false;

    //getTimestamp
    timeStamp = getValue(inTest,"mTimeStamp=", "ns");

    //get Cell Identity paramters
    MCC = (int) getValue(inTest,"mMcc=", " ");      //get Mcc
    MNC = (int) getValue(inTest,"mMnc=", " ");      //get MNC
    CID = (int) getValue(inTest,"mCi=", " ");       //get CID
    PCI = (int) getValue(inTest,"mPci="," ");       //get PCI
    TAC = (int) getValue(inTest,"mTac=","}");       //get TAC

    //get RF related parameters
    SS = (int) getValue(inTest," ss="," ");         //get SS
    RSRP = (int)getValue(inTest,"rsrp=", " ");      //get RSRP
    RSRQ = (int)getValue(inTest,"rsrq=", " ");      //get RSRQ
    RSSNR = (int)getValue(inTest,"rssnr=", " ");    //get RSSNR
    CQI = (int)getValue(inTest," cqi=", " ");       //get CQI
    tAdvance = (int)getValue(inTest," ta=", "}");   //get timing advance
}

//internal function to help with parsing of raw LTE strings
private long getValue(String fullS, String startS, String stopS)
{
    int index = fullS.indexOf(startS) + (startS).length();
    int endIndex = fullS.indexOf(stopS,index);

    return Long.parseLong(fullS.substring(index,endIndex).trim());
}

}

所以如果我用输入的 LTE 字符串非常基本地实现它:

    //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);

    //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 CellInfoLte) {
                LTEStruct lte = new LTEStruct(this);
                lte.parse(info.toString());

                //write out parsed results for what it's worth
                Log.i("LTE parseOutput", "tAdvance: " + lte.tAdvance + "\r\nCQI: " + lte.CQI + "\r\nRSSNR: " + lte.RSSNR + "\r\nRSRP: " + lte.RSRP + "\r\nSS: " + lte.SS +
                        "\r\nCID: " + lte.CID + "\r\nTimestamp: " + lte.timeStamp + "\r\nTAC: " + lte.TAC + "\r\nPCI: " + lte.PCI + "\r\nMNC: " + lte.MNC + "\r\nMCC: " + lte.MCC + "\r\nRegistered: " + lte.isRegistered);
            } else
                Log.i("LTE testing", "not LTE cell info measured");

        } catch (Exception ex) {
            Log.i("neighboring error: ", ex.getMessage());
        }

    }

希望对您有所帮助 ;)