Android GSM+CDMA基站定位


在googleAPI里提供了基站信息的获取类TelephonyManager,通过其方法getCellLocation得到CellLocation即可获取到基站相关信息

但CellLocation是个抽象类,所以在具体使用时需要判断接入的网络制式来用其子类CdmaCellLocationGsmCellLocation 来强转

CdmaCellLocation对应CDMA网,GsmCellLocation对应GSM网

三大网络运营商的网络制式对应如下:

移动2G 网   -->      GSM

移动3G 网   -->      TD-SCDMA

 

电信2G 网   -->      CDMA

电信3G 网   -->      CDMA2000

 

联通2G 网   -->      GSM

联通3G 网   -->      WCDMA

 

由此可见移动,联通2G 网都可使用GsmCellLocation

电信2G,3G网则使用CdmaCellLocation

那么移动3G和联通3G又当如何

 

其实经本人亲测,移动3G网也可使用GsmCellLocation,听说是TD-SCDMA衍生于GSM,具体原因咱也不用纠结了,反正能用就是了

而联通的WCDMA据说也可使用GsmCellLocation,那姑且就是这样吧,有条件的童鞋试一试吧。

 

 

对于网络制式的判断调用TelephonyManager.getNetworkType()可有多种情况,如下:

  • NETWORK_TYPE_UNKNOWN
  • NETWORK_TYPE_GPRS
  • NETWORK_TYPE_EDGE
  • NETWORK_TYPE_UMTS
  • NETWORK_TYPE_HSDPA
  • NETWORK_TYPE_HSUPA
  • NETWORK_TYPE_HSPA
  • NETWORK_TYPE_CDMA
  • NETWORK_TYPE_EVDO_0
  • NETWORK_TYPE_EVDO_A
  • NETWORK_TYPE_EVDO_B
  • NETWORK_TYPE_1xRTT
  • NETWORK_TYPE_IDEN
  • NETWORK_TYPE_LTE
  • NETWORK_TYPE_EHRPD

通过对网络类型判断后获取对应基站信息代码片段如下:

  1. public static ArrayList<CellIDInfo> getCellIDInfo(Context context) throws Exception{  
  2.           
  3.         TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);  
  4.           
  5.         ArrayList<CellIDInfo> CellID = new ArrayList<CellIDInfo>();  
  6.         CellIDInfo currentCell = new CellIDInfo();  
  7.   
  8.         int type = manager.getNetworkType();  
  9.         Log.d(TAG, "getCellIDInfo-->         NetworkType = " + type);  
  10.         int phoneType = manager.getPhoneType();  
  11.         Log.d(TAG, "getCellIDInfo-->         phoneType = " + phoneType);  
  12.           
  13.         if (type == TelephonyManager.NETWORK_TYPE_GPRS              // GSM网  
  14.                 || type == TelephonyManager.NETWORK_TYPE_EDGE  
  15.                 || type == TelephonyManager.NETWORK_TYPE_HSDPA)  
  16.         {  
  17.             GsmCellLocation gsm = ((GsmCellLocation) manager.getCellLocation());  
  18.             if (gsm == null)  
  19.             {  
  20.                 Log.e(TAG, "GsmCellLocation is null!!!");  
  21.                 return null;  
  22.             }  
  23.                   
  24.   
  25.             int lac = gsm.getLac();  
  26.             String mcc = manager.getNetworkOperator().substring(0, 3);  
  27.             String mnc = manager.getNetworkOperator().substring(3, 5);  
  28.             int cid = gsm.getCid();  
  29.               
  30.             currentCell.cellId = gsm.getCid();  
  31.             currentCell.mobileCountryCode = mcc;  
  32.             currentCell.mobileNetworkCode = mnc;  
  33.             currentCell.locationAreaCode = lac;  
  34.               
  35.             currentCell.radioType = "gsm";  
  36.               
  37.             CellID.add(currentCell);  
  38.               
  39.             // 获得邻近基站信息  
  40.             List<NeighboringCellInfo> list = manager.getNeighboringCellInfo();  
  41.             int size = list.size();  
  42.             for (int i = 0; i < size; i++) {  
  43.   
  44.                 CellIDInfo info = new CellIDInfo();  
  45.                 info.cellId = list.get(i).getCid();  
  46.                 info.mobileCountryCode = mcc;  
  47.                 info.mobileNetworkCode = mnc;  
  48.                 info.locationAreaCode = lac;  
  49.               
  50.                 CellID.add(info);  
  51.             }  
  52.               
  53.         }else if (type == TelephonyManager.NETWORK_TYPE_CDMA        // 电信cdma网  
  54.                 || type == TelephonyManager.NETWORK_TYPE_1xRTT  
  55.                 || type == TelephonyManager.NETWORK_TYPE_EVDO_0  
  56.                 || type == TelephonyManager.NETWORK_TYPE_EVDO_A)  
  57.         {  
  58.               
  59.             CdmaCellLocation cdma = (CdmaCellLocation) manager.getCellLocation();     
  60.             if (cdma == null)  
  61.             {  
  62.                 Log.e(TAG, "CdmaCellLocation is null!!!");  
  63.                 return null;  
  64.             }  
  65.               
  66.             int lac = cdma.getNetworkId();  
  67.             String mcc = manager.getNetworkOperator().substring(0, 3);  
  68.             String mnc = String.valueOf(cdma.getSystemId());  
  69.             int cid = cdma.getBaseStationId();  
  70.               
  71.             currentCell.cellId = cid;  
  72.             currentCell.mobileCountryCode = mcc;  
  73.             currentCell.mobileNetworkCode = mnc;  
  74.             currentCell.locationAreaCode = lac;  
  75.       
  76.             currentCell.radioType = "cdma";  
  77.               
  78.             CellID.add(currentCell);  
  79.               
  80.             // 获得邻近基站信息  
  81.             List<NeighboringCellInfo> list = manager.getNeighboringCellInfo();  
  82.             int size = list.size();  
  83.             for (int i = 0; i < size; i++) {  
  84.   
  85.                 CellIDInfo info = new CellIDInfo();  
  86.                 info.cellId = list.get(i).getCid();  
  87.                 info.mobileCountryCode = mcc;  
  88.                 info.mobileNetworkCode = mnc;  
  89.                 info.locationAreaCode = lac;  
  90.               
  91.                 CellID.add(info);  
  92.             }  
  93.         }  
  94.           
  95.         return CellID;  
  96.               
  97.     }  

从GOOGLE的API文档里总共有14钟网络类型,这里只罗列了其中7种,其他的主要是本人也不太清楚其对应到的网络制式是怎样的

  • 1
  • 2
  • 3
  • 下一页

相关内容