Tuesday, December 9, 2014

android how to known mobile 2g/3g/4g signal signalstrength value in china or world?

1. sign and asu change fomula in gsm

    public static int sign2asu(int sign){
return (int)(sign+113)/2;
}

public static int asu2sign(int asu){
return -113+(2*asu);
}



2.get gsm 2g sign value,or in cdma



    protected TelephonyManager _tm;
private PhoneStateListener _listener;

public static PhoneStateListener signListener = new PhoneStateListener() {

public void onSignalStrengthsChanged(SignalStrength signalStrength){
int asu = 0;
isGsm=signalStrength.isGsm();
if (isGsm){
asu = signalStrength.getGsmSignalStrength();
sign = CommType.asu2sign(asu);
}else{
sign = signalStrength.getCdmaDbm();
}

}

public void onSignalStrengthChanged(int asu) {
// Log.e("BeanDateUtils", "asu"+asu);
}
};

// register it
_tm = (TelephonyManager) service.getSystemService(Context.TELEPHONY_SERVICE);

_listener = new PhoneStateMonitor();

_tm.listen(_listener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);

// unregister it

_tm.listen(_listener, PhoneStateListener.LISTEN_NONE);
_listener = null;
_tm = null;


3.get 2g gsm/cdma other value



    public static Map<String, Integer> getPhoneDatas(TelephonyManager tm){
/**
* MCC(Mobile Country Code,移动国家号码),用于唯一地标识移动客户属于的国家。它由三位十进制数组成(000-999),例如我国的MCC为460。
*/
int mcc = 0;
/**
* 定义
MNC(Mobile Network Code,移动网络号码),用于识别移动客户所属的移动网络。 
格式
MNC由二个十进制数组成,编码范围为十进制的00-99,例如中国移动的MNC为00和02[1],中国联通的MNC为01,中国电信的MNC为03。
*/
int mnc = 0;
int cid = 0;
int lac = 0;
Map<String, Integer> map=new HashMap<String, Integer>();

CellLocation cl = tm.getCellLocation();

// for no sim card phone
if( cl == null)
return map;

if(cl.getClass().equals(GsmCellLocation.class)){
mcc = 0;
mnc = 0;
GsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation();
if (null!=gcl){
cid = gcl.getCid();
lac = gcl.getLac();
}

}else if(cl.getClass().equals(CdmaCellLocation.class)){
CdmaCellLocation gcl = (CdmaCellLocation) tm.getCellLocation();
if (null!=gcl){
cid = gcl.getBaseStationId();
lac = gcl.getSystemId();
}
}

String str1 = Build.MODEL;
if (str1.equals("HUAWEI MT2-L01"))
mnc = ((113 + mnc) / 2);

map.put(LABEL_CELL_MCC, mcc);
map.put(LABEL_CELL_MNC, mnc);
map.put(LABEL_CELL_CID, cid);
map.put(LABEL_CELL_LAC, lac);
return map;
}


4.get gsm cell info



    
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private static Map<String,String> _fillCellInfoGsm(CellInfoGsm cig){
Map<String,String> ret = new HashMap<String,String>();

CellIdentityGsm cg = cig.getCellIdentity();
CellSignalStrengthGsm cssg = cig.getCellSignalStrength();

int tmp = cg.getCid();
ret.put(LABEL_CELLTYPE,LABEL_CELL_TYPE_GSM);
ret.put(LABEL_CELL_CID,"" + cg.getCid());
ret.put(LABEL_CELL_LAC,"" + cg.getLac());
ret.put(LABEL_CELL_MCC,"" + cg.getMcc());
ret.put(LABEL_CELL_MNC,"" + cg.getMnc());

ret.put(LABEL_CELL_ASULEVEL,"" + cssg.getAsuLevel());
// ret.put(LABEL_CELL_DBM,"" + cssg.getDbm()); // ?
ret.put(LABEL_CELL_SIGN,"" + cssg.getDbm());
ret.put(LABEL_CELL_LEVEL,"" + cssg.getLevel());

do{
String s = cssg.toString();
String[] ss = s.split(" ");
if(ss == null)
break;

for(String kev : ss){
String[] ssss = kev.split("=");
if(ssss.length<2)
continue;
if(ssss[0].equals("ber"))
ret.put(LABEL_CELL_BITERRORRATE,ssss[1]);
}
}while(false);
return ret;
}


5.get 4g lte cell info



    
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private static Map<String,String> _fillCellInfoLte(CellInfoLte cil,TelephonyManager tm){
Map<String,String> ret = new HashMap<String,String>();

CellIdentityLte cid = cil.getCellIdentity();
CellSignalStrengthLte css = cil.getCellSignalStrength();

ret.put(LABEL_CELLTYPE,LABEL_CELL_TYPE_LTE);
ret.put(LABEL_CELL_CI,"" + cid.getCi());
ret.put(LABEL_CELL_MCC,"" + cid.getMcc());
ret.put(LABEL_CELL_MNC,"" + cid.getMnc());
ret.put(LABEL_CELL_PCI,"" + cid.getPci());
ret.put(LABEL_CELL_TAC,"" + cid.getTac());

ret.put(LABEL_CELL_ASULEVEL,"" + css.getAsuLevel());
ret.put(LABEL_CELL_TIMEADVANCE,"" + css.getTimingAdvance());
ret.put(LABEL_CELL_RSRP,"" + css.getDbm()); // rsrp
// ret.put(LABEL_CELL_SIGN,"" + CommType.asu2sign(css.getDbm()));
ret.put(LABEL_CELL_LEVEL,"" + css.getLevel());

do{
String s = css.toString();
String[] ss = s.split(" ");
if(ss == null)
break;

for(String kev : ss){
String[] ssss = kev.split("=");
if(ssss.length<2)
continue;
if(ssss[0].equals("ss"))
ret.put(LABEL_CELL_SIGN,ssss[1]);
else if(ssss[0].equals("rsrq"))
ret.put(LABEL_CELL_RSRQ,ssss[1]);
else if(ssss[0].equals("rssnr"))
ret.put(LABEL_CELL_RSSNR,ssss[1]);
else if(ssss[0].equals("cqi"))
ret.put(LABEL_CELL_CQI,ssss[1]);
}
}while(false);

do{ // get td-lte
// cmcc
if(cid.getMcc() != 460 || (cid.getMnc()!=0 && cid.getMnc()!=2))
break;

CellLocation cl = tm.getCellLocation();
if(cl == null)
break;

if(!cl.getClass().equals(GsmCellLocation.class))
break;


}while(false);

return ret;
}


6. get cdma cell info



    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) 
private static Map<String,String> _fillCellInfoCdma(CellInfoCdma cil){
Map<String,String> ret = new HashMap<String,String>();

CellIdentityCdma cid = cil.getCellIdentity();
CellSignalStrengthCdma css = cil.getCellSignalStrength();

ret.put(LABEL_CELLTYPE,LABEL_CELL_TYPE_CDMA);
ret.put(LABEL_CELL_NETWORKID,"" + cid.getNetworkId());
ret.put(LABEL_CELL_SYSTEMID,"" + cid.getSystemId());
ret.put(LABEL_CELL_CID,"" + cid.getBasestationId());
ret.put(LABEL_CELL_LONGTITUDE,"" + cid.getLongitude());
ret.put(LABEL_CELL_LATITUDE,"" + cid.getLatitude());

ret.put(LABEL_CELL_LEVEL,"" + css.getLevel());
ret.put(LABEL_CELL_ASULEVEL,"" + css.getAsuLevel());
ret.put(LABEL_CELL_CDMALEVEL,"" + css.getCdmaLevel());
ret.put(LABEL_CELL_EVDOLEVEL,"" + css.getEvdoLevel());
ret.put(LABEL_CELL_DBM,"" + css.getDbm());
ret.put(LABEL_CELL_CDMADBM,"" + css.getCdmaDbm());
ret.put(LABEL_CELL_CDMAECIO,"" + css.getCdmaEcio());
ret.put(LABEL_CELL_EVDODBM,"" + css.getEvdoDbm());
ret.put(LABEL_CELL_EVDOECIO,"" + css.getEvdoEcio());
ret.put(LABEL_CELL_EVDOSNR,"" + css.getEvdoSnr());

return ret;
}



7.get wcdma cell info



    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) 
private static Map<String,String> _fillCellInfo_18(CellInfo ci){
Map<String,String> ret = new HashMap<String,String>();

if(ci.getClass().equals(CellInfoWcdma.class)){
CellInfoWcdma cil = (CellInfoWcdma)ci;

CellIdentityWcdma cid = cil.getCellIdentity();
CellSignalStrengthWcdma css = cil.getCellSignalStrength();

ret.put(LABEL_CELLTYPE,LABEL_CELL_TYPE_WCDMA);
ret.put(LABEL_CELL_MCC,"" + cid.getMcc());
ret.put(LABEL_CELL_MNC,"" + cid.getMnc());
ret.put(LABEL_CELL_TAC,"" + cid.getLac());
ret.put(LABEL_CELL_CID,"" + cid.getCid());
ret.put(LABEL_CELL_PSC,"" + cid.getPsc());

ret.put(LABEL_CELL_LEVEL,"" + css.getLevel());
ret.put(LABEL_CELL_SIGN,"" + css.getDbm());
ret.put(LABEL_CELL_ASULEVEL,"" + css.getAsuLevel());

do{
String s = css.toString();
String[] ss = s.split(" ");
if(ss == null)
break;

for(String kev : ss){
String[] ssss = kev.split("=");
if(ssss.length<2)
continue;
if(ssss[0].equals("ber"))
ret.put(LABEL_CELL_BITERRORRATE,ssss[1]);
}
}while(false);
}

return ret;
}



9.get android 4.2.2+ 3/4 g cell info code



    
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private static List<Map<String,String> > _get4GPhoneCellDatas_17(TelephonyManager tm){
List<Map<String,String> > ret = null;

if(tm == null)
return ret;

List<CellInfo> ciList = tm.getAllCellInfo();
do{
if(ciList == null || ciList.size()==0)
break;

Map<String,String> retitem;

for(CellInfo ci : ciList){

retitem = null;

if(ci.getClass().equals(CellInfoGsm.class)){

retitem = _fillCellInfoGsm((CellInfoGsm)ci);

}else if(ci.getClass().equals(CellInfoLte.class)){

retitem = _fillCellInfoLte((CellInfoLte)ci,tm);

} else if(ci.getClass().equals(CellInfoCdma.class)){

retitem = _fillCellInfoCdma((CellInfoCdma)ci);

} else{

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2)
retitem = _fillCellInfo_18(ci);

}

if(retitem != null){

retitem.put(LABEL_CELL_TIME,microSecondsToString(ci.getTimeStamp()));

if(ret == null)
ret = new ArrayList<Map<String,String> >();

ret.add(retitem);
}
}

}while(false);

return ret;
}



10.get android all plateform 4g all cell code



    public static List<Map<String,String>> get4GPhoneCellAllDatas(TelephonyManager tm){
List<Map<String,String>> ret = null;

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1){
ret = _get4GPhoneCellDatas_17(tm);
}else{
Map<String, Integer> item = getPhoneDatas(tm);
if(item != null){
Map<String,String> tmp = new HashMap<String,String>();
for(Entry<String, Integer> a:item.entrySet()){
tmp.put(a.getKey(), "" + a.getValue());
}
ret = new ArrayList<Map<String,String>>();
ret.add(tmp);
}
}


return ret;
}



11. get td-lte 4g signalstreth value in china



    public static SignalStrengthData getLtePhoneSignalStrengthData(SignalStrength signalStrength){
SignalStrengthData ret = null;

do{
if(signalStrength == null)
break;

String s = signalStrength.toString();
String[] ss = s.split(" ");

if(ss.length<14)
break;

ret = new SignalStrengthData();

ret.Sign = Integer.parseInt(ss[8]);
ret.Rsrp = Integer.parseInt(ss[9]);
ret.Rsrq = Integer.parseInt(ss[10]);
ret.Rssnr = Integer.parseInt(ss[11]) / 10;
ret.Cqi = Integer.parseInt(ss[12]);

}while(false);

return ret;
}



Note:



in some mobile liked huawei some mode,those sign value need manul handle.



 



x.Last



Thanks.



Above code,we only test in cdma / gsm / td-lte some logic in china.

No comments: