检索单元塔信息

时间:2012-11-08 17:35:21

标签: android

有人知道如何在Android上检索GSM和CDMA上的手机信号塔列表。

我一直在尝试使用Google Maps Locations API: https://developers.google.com/maps/documentation/business/geolocation/

我希望通过这些字段获取手机信息塔的信息:

  • cellId:单元格的唯一标识符。在GSM上,这是小区ID(CID); CDMA网络使用基站ID(BID)。
  • locationAreaCode:GSM网络的位置区域代码(LAC); CDMA网络使用网络ID(NID)。
  • mobileCountryCode:手机信号塔的移动国家代码(MCC)。
  • mobileNetworkCode:手机信号塔的移动网络代码。这是用于GSM的MNC,或用于CDMA的系统ID(SID)。
  • age:自此单元格为主要数据以来的毫秒数。如果age为0,则cellId表示当前测量值。
  • signalStrength:以dBm为单位测量的无线电信号强度。
  • timingAdvance:时间提前值。

此代码并不特别获取手机信息塔信息。

TelephonyManager tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

// Type of the network
int phoneTypeInt = tel.getPhoneType();
String phoneType = null;
phoneType = phoneTypeInt == TelephonyManager.PHONE_TYPE_GSM ? "gsm" : phoneType;
phoneType = phoneTypeInt == TelephonyManager.PHONE_TYPE_CDMA ? "cdma" : phoneType;
try {
  if (phoneType != null) {
    params.put("radioType", phoneType);
  }
} catch (Exception e) {}

/*
 * The below code doesn't work I think.
 */
JSONArray cellList = new JSONArray();
List<NeighboringCellInfo> neighCells = tel.getNeighboringCellInfo();
for (int i = 0; i < neighCells.size(); i++) {
  try {
    JSONObject cellObj = new JSONObject();
    NeighboringCellInfo thisCell = neighCells.get(i);
    cellObj.put("cellId", thisCell.getCid());
    cellList.put(cellObj);
  } catch (Exception e) {}
}
if (cellList.length() > 0) {
  try {
    params.put("cellTowers", cellList);
  } catch (JSONException e) {}
}

我设置了这样的权限:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_UPDATES"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>

请帮帮我,谢谢。

3 个答案:

答案 0 :(得分:8)

答案 1 :(得分:7)

我最近遇到了这个问题,结果是

  

getNeighboringCellInfo

已从API 23弃用。为了解决这个问题,可以使用以下内容(真的很烦人):

public static JSONArray getCellInfo(Context ctx){
        TelephonyManager tel = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);

        JSONArray cellList = new JSONArray();

// Type of the network
        int phoneTypeInt = tel.getPhoneType();
        String phoneType = null;
        phoneType = phoneTypeInt == TelephonyManager.PHONE_TYPE_GSM ? "gsm" : phoneType;
        phoneType = phoneTypeInt == TelephonyManager.PHONE_TYPE_CDMA ? "cdma" : phoneType;

        //from Android M up must use getAllCellInfo
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {



            List<NeighboringCellInfo> neighCells = tel.getNeighboringCellInfo();
            for (int i = 0; i < neighCells.size(); i++) {
                try {
                    JSONObject cellObj = new JSONObject();
                    NeighboringCellInfo thisCell = neighCells.get(i);
                    cellObj.put("cellId", thisCell.getCid());
                    cellObj.put("lac", thisCell.getLac());
                    cellObj.put("rssi", thisCell.getRssi());
                    cellList.put(cellObj);
                } catch (Exception e) {
                }
            }

        } else {
            List<CellInfo> infos = tel.getAllCellInfo();
            for (int i = 0; i<infos.size(); ++i) {
                try {
                    JSONObject cellObj = new JSONObject();
                    CellInfo info = infos.get(i);
                    if (info instanceof CellInfoGsm){
                        CellSignalStrengthGsm gsm = ((CellInfoGsm) info).getCellSignalStrength();
                        CellIdentityGsm identityGsm = ((CellInfoGsm) info).getCellIdentity();
                        cellObj.put("cellId", identityGsm.getCid());
                        cellObj.put("lac", identityGsm.getLac());
                        cellObj.put("dbm", gsm.getDbm());
                        cellList.put(cellObj);
                    } else if (info instanceof CellInfoLte) {
                        CellSignalStrengthLte lte = ((CellInfoLte) info).getCellSignalStrength();
                        CellIdentityLte identityLte = ((CellInfoLte) info).getCellIdentity();
                        cellObj.put("cellId", identityLte.getCi());
                        cellObj.put("tac", identityLte.getTac());
                        cellObj.put("dbm", lte.getDbm());
                        cellList.put(cellObj);
                    }

                } catch (Exception ex) {

                }
            }
        }

        return cellList;
    }

答案 2 :(得分:0)

这不是你的手机,它不支持这个信息,它是用于提供那些API调用的RIL相关C和Java的纯粹蹩脚的实现,这阻止了这一点。如果您设法进入Service Mode菜单(因电话而异),您可以完全即时访问RSSI和其他类型的信号。您应该责怪谷歌(或您的OEM)没有解决这些问题并提供更好的RF相关变量访问权。

相关问题