歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android GSM+CDMA基站定位

Android GSM+CDMA基站定位

日期:2017/3/1 10:19:42   编辑:Linux編程

在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. TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  3. ArrayList<CellIDInfo> CellID = new ArrayList<CellIDInfo>();
  4. CellIDInfo currentCell = new CellIDInfo();
  5. int type = manager.getNetworkType();
  6. Log.d(TAG, "getCellIDInfo--> NetworkType = " + type);
  7. int phoneType = manager.getPhoneType();
  8. Log.d(TAG, "getCellIDInfo--> phoneType = " + phoneType);
  9. if (type == TelephonyManager.NETWORK_TYPE_GPRS // GSM網
  10. || type == TelephonyManager.NETWORK_TYPE_EDGE
  11. || type == TelephonyManager.NETWORK_TYPE_HSDPA)
  12. {
  13. GsmCellLocation gsm = ((GsmCellLocation) manager.getCellLocation());
  14. if (gsm == null)
  15. {
  16. Log.e(TAG, "GsmCellLocation is null!!!");
  17. return null;
  18. }
  19. int lac = gsm.getLac();
  20. String mcc = manager.getNetworkOperator().substring(0, 3);
  21. String mnc = manager.getNetworkOperator().substring(3, 5);
  22. int cid = gsm.getCid();
  23. currentCell.cellId = gsm.getCid();
  24. currentCell.mobileCountryCode = mcc;
  25. currentCell.mobileNetworkCode = mnc;
  26. currentCell.locationAreaCode = lac;
  27. currentCell.radioType = "gsm";
  28. CellID.add(currentCell);
  29. // 獲得鄰近基站信息
  30. List<NeighboringCellInfo> list = manager.getNeighboringCellInfo();
  31. int size = list.size();
  32. for (int i = 0; i < size; i++) {
  33. CellIDInfo info = new CellIDInfo();
  34. info.cellId = list.get(i).getCid();
  35. info.mobileCountryCode = mcc;
  36. info.mobileNetworkCode = mnc;
  37. info.locationAreaCode = lac;
  38. CellID.add(info);
  39. }
  40. }else if (type == TelephonyManager.NETWORK_TYPE_CDMA // 電信cdma網
  41. || type == TelephonyManager.NETWORK_TYPE_1xRTT
  42. || type == TelephonyManager.NETWORK_TYPE_EVDO_0
  43. || type == TelephonyManager.NETWORK_TYPE_EVDO_A)
  44. {
  45. CdmaCellLocation cdma = (CdmaCellLocation) manager.getCellLocation();
  46. if (cdma == null)
  47. {
  48. Log.e(TAG, "CdmaCellLocation is null!!!");
  49. return null;
  50. }
  51. int lac = cdma.getNetworkId();
  52. String mcc = manager.getNetworkOperator().substring(0, 3);
  53. String mnc = String.valueOf(cdma.getSystemId());
  54. int cid = cdma.getBaseStationId();
  55. currentCell.cellId = cid;
  56. currentCell.mobileCountryCode = mcc;
  57. currentCell.mobileNetworkCode = mnc;
  58. currentCell.locationAreaCode = lac;
  59. currentCell.radioType = "cdma";
  60. CellID.add(currentCell);
  61. // 獲得鄰近基站信息
  62. List<NeighboringCellInfo> list = manager.getNeighboringCellInfo();
  63. int size = list.size();
  64. for (int i = 0; i < size; i++) {
  65. CellIDInfo info = new CellIDInfo();
  66. info.cellId = list.get(i).getCid();
  67. info.mobileCountryCode = mcc;
  68. info.mobileNetworkCode = mnc;
  69. info.locationAreaCode = lac;
  70. CellID.add(info);
  71. }
  72. }
  73. return CellID;
  74. }

從GOOGLE的API文檔裡總共有14鐘網絡類型,這裡只羅列了其中7種,其他的主要是本人也不太清楚其對應到的網絡制式是怎樣的

Copyright © Linux教程網 All Rights Reserved