Parse getAllcellinfo values from telephony manager

萝らか妹 提交于 2019-12-11 17:56:09

问题


i'm using the following function to get Allcellinfo of an device network and im getting the values as an string now i need to parse it in order to get the CellSignalStrengthLte data how can i achieve it

//code

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

Result is

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}

How can parse this string to get details regaridng CellinfoLte,CellIdentityLte


回答1:


I also noticed that CellinfoLTE and CellIdentityLTE is not so great at the moment, so I just wrote my own parsing class. Only tested this a few times, and didn't have problems, but more testing should display if additional future tweaking will be necessary.

Here's the 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());
}

}

So if I implement this very basically with the input LTE string:

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

    }

Hope it helps ;)



来源:https://stackoverflow.com/questions/47069350/parse-getallcellinfo-values-from-telephony-manager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!