Java Code Examples for android.telephony.TelephonyManager#getAllCellInfo()

The following examples show how to use android.telephony.TelephonyManager#getAllCellInfo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MobileUtils.java    From TowerCollector with Mozilla Public License 2.0 6 votes vote down vote up
private static boolean isApi17CellInfoAvailable(Context context) {
    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    List<CellInfo> cells;
    try {
        cells = telephonyManager.getAllCellInfo();
    } catch (SecurityException ex) {
        Timber.d(ex, "isApi17CellInfoAvailable(): Result = coarse location permission is denied");
        return false;
    }
    if (cells == null || cells.size() == 0) {
        Timber.d("isApi17CellInfoAvailable(): Result = no cell info");
        return false;
    }
    CellIdentityValidator validator = new CellIdentityValidator();
    for (CellInfo cell : cells) {
        if (validator.isValid(cell)) {
            Timber.d("isApi17CellInfoAvailable(): Result = true");
            return true;
        }
    }
    Timber.d("isApi17CellInfoAvailable(): Result = false");
    return false;
}
 
Example 2
Source File: PlatformNetworksManager.java    From 365browser with Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private static Set<VisibleCell> getAllVisibleCellsPostJellyBeanMr1(
        TelephonyManager telephonyManager) {
    Set<VisibleCell> visibleCells = new HashSet<>();
    // Retrieve visible cell networks
    List<CellInfo> cellInfos = telephonyManager.getAllCellInfo();
    if (cellInfos == null) {
        return visibleCells;
    }

    long elapsedTime = sTimeProvider.getElapsedRealtime();
    long currentTime = sTimeProvider.getCurrentTime();
    for (int i = 0; i < cellInfos.size(); i++) {
        CellInfo cellInfo = cellInfos.get(i);
        VisibleCell visibleCell =
                getVisibleCellPostJellyBeanMr1(cellInfo, elapsedTime, currentTime);
        if (visibleCell.radioType() != VisibleCell.UNKNOWN_RADIO_TYPE) {
            visibleCells.add(visibleCell);
        }
    }
    return visibleCells;
}
 
Example 3
Source File: PlatformNetworksManager.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a CellInfo object representing the currently registered base stations, containing
 * its identity fields and signal strength. Null if no base station is active.
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@Nullable
private static CellInfo getActiveCellInfo(TelephonyManager telephonyManager) {
    int numRegisteredCellInfo = 0;
    List<CellInfo> cellInfos = telephonyManager.getAllCellInfo();

    if (cellInfos == null) {
        return null;
    }
    CellInfo result = null;

    for (int i = 0; i < cellInfos.size(); i++) {
        CellInfo cellInfo = cellInfos.get(i);
        if (cellInfo.isRegistered()) {
            numRegisteredCellInfo++;
            if (numRegisteredCellInfo > 1) {
                return null;
            }
            result = cellInfo;
        }
    }
    // Only found one registered cellinfo, so we know which base station was used to measure
    // network quality
    return result;
}
 
Example 4
Source File: LauncherActivity.java    From Android-Plugin-Framework with MIT License 6 votes vote down vote up
private void requestPermission() {
    if (Build.VERSION.SDK_INT >= 17) {
        boolean isGranted = true;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            if (checkSelfPermission("android.permission.ACCESS_COARSE_LOCATION") == PackageManager.PERMISSION_DENIED) {
                isGranted = false;
                requestPermissions(new String[]{"android.permission.ACCESS_COARSE_LOCATION"}, 10086);
            }
        }
        if (isGranted) {
            TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
            try {
	@SuppressLint("MissingPermission") List<CellInfo> list =  telephonyManager.getAllCellInfo();
	if (list != null) {
		LogUtil.v(list);
	}
} catch (Exception e) {
            	e.printStackTrace();
}
        }
    }
}
 
Example 5
Source File: AndroidCellularSignalStrength.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Returns all observed cell information from all radios on the device including the primary
 * and neighboring cells. Returns only the information of cells that are registered to a
 * mobile network. May return {@code null}.
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private static List<CellInfo> getRegisteredCellInfo() {
    if (!isAPIAvailable()) {
        return null;
    }

    TelephonyManager telephonyManager =
            (TelephonyManager) ContextUtils.getApplicationContext().getSystemService(
                    Context.TELEPHONY_SERVICE);
    if (telephonyManager == null) {
        return null;
    }

    List<CellInfo> cellInfos = telephonyManager.getAllCellInfo();
    if (cellInfos == null) {
        return null;
    }

    Iterator<CellInfo> iter = cellInfos.iterator();
    while (iter.hasNext()) {
        if (!iter.next().isRegistered()) {
            iter.remove();
        }
    }
    return cellInfos;
}
 
Example 6
Source File: PhoneUtils.java    From Mobilyzer with Apache License 2.0 5 votes vote down vote up
public HashMap<String, Integer> getAllCellInfoSignalStrength(){
	if(!(ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION)==PackageManager.PERMISSION_GRANTED)){
		return null;
	}
	TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
	List<CellInfo> cellInfos = (List<CellInfo>) telephonyManager.getAllCellInfo();
	
	HashMap<String,	 Integer> results = new HashMap<String, Integer>();
	
	if(cellInfos==null){
		return results;
	}
	
	for(CellInfo cellInfo : cellInfos){
		if(cellInfo.getClass().equals(CellInfoLte.class)){
			results.put("LTE", ((CellInfoLte) cellInfo).getCellSignalStrength().getDbm());
		}else if(cellInfo.getClass().equals(CellInfoGsm.class)){
			results.put("GSM", ((CellInfoGsm) cellInfo).getCellSignalStrength().getDbm());
		}else if(cellInfo.getClass().equals(CellInfoCdma.class)){
			results.put("CDMA", ((CellInfoCdma) cellInfo).getCellSignalStrength().getDbm());
		}else if(cellInfo.getClass().equals(CellInfoWcdma.class)){
             results.put("WCDMA", ((CellInfoWcdma) cellInfo).getCellSignalStrength().getDbm());
		}
	}
	
	return results;
}
 
Example 7
Source File: Net.java    From HttpInfo with Apache License 2.0 4 votes vote down vote up
public static void getMobileDbm(Context context, NetBean netBean) {
    int dbm = 0;
    int level = 0;
    int asu = 0;
    try {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        List<CellInfo> cellInfoList;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            if (tm == null) {
                return;
            }
            cellInfoList = tm.getAllCellInfo();
            if (null != cellInfoList) {
                for (CellInfo cellInfo : cellInfoList) {
                    if (cellInfo instanceof CellInfoGsm) {
                        CellSignalStrengthGsm cellSignalStrengthGsm = ((CellInfoGsm) cellInfo).getCellSignalStrength();
                        dbm = cellSignalStrengthGsm.getDbm();
                        level = cellSignalStrengthGsm.getLevel();
                        asu = cellSignalStrengthGsm.getAsuLevel();
                    } else if (cellInfo instanceof CellInfoCdma) {
                        CellSignalStrengthCdma cellSignalStrengthCdma =
                                ((CellInfoCdma) cellInfo).getCellSignalStrength();
                        dbm = cellSignalStrengthCdma.getDbm();
                        level = cellSignalStrengthCdma.getLevel();
                        asu = cellSignalStrengthCdma.getAsuLevel();
                    } else if (cellInfo instanceof CellInfoLte) {
                        CellSignalStrengthLte cellSignalStrengthLte = ((CellInfoLte) cellInfo).getCellSignalStrength();
                        dbm = cellSignalStrengthLte.getDbm();
                        level = cellSignalStrengthLte.getLevel();
                        asu = cellSignalStrengthLte.getAsuLevel();
                    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                        if (cellInfo instanceof CellInfoWcdma) {
                            CellSignalStrengthWcdma cellSignalStrengthWcdma =
                                    ((CellInfoWcdma) cellInfo).getCellSignalStrength();
                            dbm = cellSignalStrengthWcdma.getDbm();
                            level = cellSignalStrengthWcdma.getLevel();
                            asu = cellSignalStrengthWcdma.getAsuLevel();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        HttpLog.e("signal info:" + e.toString());
    }
    netBean.setMobAsu(asu);
    netBean.setMobDbm(dbm);
    netBean.setMobLevel(level);
}
 
Example 8
Source File: SignalInfo.java    From MobileInfo with Apache License 2.0 4 votes vote down vote up
/**
 * mobile
 *
 * @param context
 * @return
 */
@SuppressLint("MissingPermission")
private static void getMobileDbm(Context context, SignalBean signalBean) {
    int dbm = -1;
    int level = 0;
    try {
        signalBean.setnIpAddress(getNetIPV4());
        signalBean.setnIpAddressIpv6(getNetIP());
        signalBean.setMacAddress(getMac(context));
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        List<CellInfo> cellInfoList;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            if (tm == null) {
                return;
            }
            cellInfoList = tm.getAllCellInfo();
            if (null != cellInfoList) {
                for (CellInfo cellInfo : cellInfoList) {
                    if (cellInfo instanceof CellInfoGsm) {
                        CellSignalStrengthGsm cellSignalStrengthGsm = ((CellInfoGsm) cellInfo).getCellSignalStrength();
                        dbm = cellSignalStrengthGsm.getDbm();
                        level = cellSignalStrengthGsm.getLevel();
                    } else if (cellInfo instanceof CellInfoCdma) {
                        CellSignalStrengthCdma cellSignalStrengthCdma =
                                ((CellInfoCdma) cellInfo).getCellSignalStrength();
                        dbm = cellSignalStrengthCdma.getDbm();
                        level = cellSignalStrengthCdma.getLevel();
                    } else if (cellInfo instanceof CellInfoLte) {
                        CellSignalStrengthLte cellSignalStrengthLte = ((CellInfoLte) cellInfo).getCellSignalStrength();
                        dbm = cellSignalStrengthLte.getDbm();
                        level = cellSignalStrengthLte.getLevel();

                    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                        if (cellInfo instanceof CellInfoWcdma) {
                            CellSignalStrengthWcdma cellSignalStrengthWcdma =
                                    ((CellInfoWcdma) cellInfo).getCellSignalStrength();
                            dbm = cellSignalStrengthWcdma.getDbm();
                            level = cellSignalStrengthWcdma.getLevel();
                        }
                    }
                }
            }
        }
        signalBean.setRssi(dbm );
        signalBean.setLevel(level);
    } catch (Exception e) {
        Log.i(TAG, e.toString());
    }
}
 
Example 9
Source File: DeviceApi17.java    From AIMSICDL with GNU General Public License v3.0 4 votes vote down vote up
public static void loadCellInfo(TelephonyManager tm, Device pDevice) {
    int lCurrentApiVersion = android.os.Build.VERSION.SDK_INT;
    try {
        if (pDevice.mCell == null) {
            pDevice.mCell = new Cell();
        }
        List<CellInfo> cellInfoList = tm.getAllCellInfo();
        if (cellInfoList != null) {
            for (final CellInfo info : cellInfoList) {

                //Network Type
                pDevice.mCell.setNetType(tm.getNetworkType());

                if (info instanceof CellInfoGsm) {
                    final CellSignalStrengthGsm gsm = ((CellInfoGsm) info)
                            .getCellSignalStrength();
                    final CellIdentityGsm identityGsm = ((CellInfoGsm) info)
                            .getCellIdentity();
                    //Signal Strength
                    pDevice.mCell.setDBM(gsm.getDbm()); // [dBm]
                    //Cell Identity
                    pDevice.mCell.setCID(identityGsm.getCid());
                    pDevice.mCell.setMCC(identityGsm.getMcc());
                    pDevice.mCell.setMNC(identityGsm.getMnc());
                    pDevice.mCell.setLAC(identityGsm.getLac());

                } else if (info instanceof CellInfoCdma) {
                    final CellSignalStrengthCdma cdma = ((CellInfoCdma) info)
                            .getCellSignalStrength();
                    final CellIdentityCdma identityCdma = ((CellInfoCdma) info)
                            .getCellIdentity();
                    //Signal Strength
                    pDevice.mCell.setDBM(cdma.getDbm());
                    //Cell Identity
                    pDevice.mCell.setCID(identityCdma.getBasestationId());
                    pDevice.mCell.setMNC(identityCdma.getSystemId());
                    pDevice.mCell.setLAC(identityCdma.getNetworkId());
                    pDevice.mCell.setSID(identityCdma.getSystemId());

                } else if (info instanceof CellInfoLte) {
                    final CellSignalStrengthLte lte = ((CellInfoLte) info)
                            .getCellSignalStrength();
                    final CellIdentityLte identityLte = ((CellInfoLte) info)
                            .getCellIdentity();
                    //Signal Strength
                    pDevice.mCell.setDBM(lte.getDbm());
                    pDevice.mCell.setTimingAdvance(lte.getTimingAdvance());
                    //Cell Identity
                    pDevice.mCell.setMCC(identityLte.getMcc());
                    pDevice.mCell.setMNC(identityLte.getMnc());
                    pDevice.mCell.setCID(identityLte.getCi());

                } else if  (lCurrentApiVersion >= Build.VERSION_CODES.JELLY_BEAN_MR2 &&
                        info instanceof CellInfoWcdma) {
                    final CellSignalStrengthWcdma wcdma = ((CellInfoWcdma) info)
                            .getCellSignalStrength();
                    final CellIdentityWcdma identityWcdma = ((CellInfoWcdma) info)
                            .getCellIdentity();
                    //Signal Strength
                    pDevice.mCell.setDBM(wcdma.getDbm());
                    //Cell Identity
                    pDevice.mCell.setLAC(identityWcdma.getLac());
                    pDevice.mCell.setMCC(identityWcdma.getMcc());
                    pDevice.mCell.setMNC(identityWcdma.getMnc());
                    pDevice.mCell.setCID(identityWcdma.getCid());
                    pDevice.mCell.setPSC(identityWcdma.getPsc());

                } else {
                    Log.i(TAG, mTAG + "Unknown type of cell signal! "
                            + "ClassName: " + info.getClass().getSimpleName()
                            + " ToString: " + info.toString());
                }
                if (pDevice.mCell.isValid()) {
                    break;
                }
            }
        }
    } catch (NullPointerException npe) {
       Log.e(TAG, mTAG + "loadCellInfo: Unable to obtain cell signal information: ", npe);
    }

}
 
Example 10
Source File: DeviceApi18.java    From AIMSICDL with GNU General Public License v3.0 4 votes vote down vote up
public static void loadCellInfo(TelephonyManager tm, Device pDevice) {
    int lCurrentApiVersion = android.os.Build.VERSION.SDK_INT;
    try {
        if (pDevice.mCell == null) {
            pDevice.mCell = new Cell();
        }
        List<CellInfo> cellInfoList = tm.getAllCellInfo();
        if (cellInfoList != null) {
            for (final CellInfo info : cellInfoList) {

                //Network Type
                pDevice.mCell.setNetType(tm.getNetworkType());

                if (info instanceof CellInfoGsm) {
                    final CellSignalStrengthGsm gsm = ((CellInfoGsm) info).getCellSignalStrength();
                    final CellIdentityGsm identityGsm = ((CellInfoGsm) info).getCellIdentity();
                    // Signal Strength
                    pDevice.mCell.setDBM(gsm.getDbm()); // [dBm]
                    // Cell Identity
                    pDevice.mCell.setCID(identityGsm.getCid());
                    pDevice.mCell.setMCC(identityGsm.getMcc());
                    pDevice.mCell.setMNC(identityGsm.getMnc());
                    pDevice.mCell.setLAC(identityGsm.getLac());

                } else if (info instanceof CellInfoCdma) {
                    final CellSignalStrengthCdma cdma = ((CellInfoCdma) info).getCellSignalStrength();
                    final CellIdentityCdma identityCdma = ((CellInfoCdma) info).getCellIdentity();
                    // Signal Strength
                    pDevice.mCell.setDBM(cdma.getDbm());
                    // Cell Identity
                    pDevice.mCell.setCID(identityCdma.getBasestationId());
                    pDevice.mCell.setMNC(identityCdma.getSystemId());
                    pDevice.mCell.setLAC(identityCdma.getNetworkId());
                    pDevice.mCell.setSID(identityCdma.getSystemId());

                } else if (info instanceof CellInfoLte) {
                    final CellSignalStrengthLte lte = ((CellInfoLte) info).getCellSignalStrength();
                    final CellIdentityLte identityLte = ((CellInfoLte) info).getCellIdentity();
                    // Signal Strength
                    pDevice.mCell.setDBM(lte.getDbm());
                    pDevice.mCell.setTimingAdvance(lte.getTimingAdvance());
                    // Cell Identity
                    pDevice.mCell.setMCC(identityLte.getMcc());
                    pDevice.mCell.setMNC(identityLte.getMnc());
                    pDevice.mCell.setCID(identityLte.getCi());

                } else if  (lCurrentApiVersion >= Build.VERSION_CODES.JELLY_BEAN_MR2 && info instanceof CellInfoWcdma) {
                    final CellSignalStrengthWcdma wcdma = ((CellInfoWcdma) info).getCellSignalStrength();
                    final CellIdentityWcdma identityWcdma = ((CellInfoWcdma) info).getCellIdentity();
                    // Signal Strength
                    pDevice.mCell.setDBM(wcdma.getDbm());
                    // Cell Identity
                    pDevice.mCell.setLAC(identityWcdma.getLac());
                    pDevice.mCell.setMCC(identityWcdma.getMcc());
                    pDevice.mCell.setMNC(identityWcdma.getMnc());
                    pDevice.mCell.setCID(identityWcdma.getCid());
                    pDevice.mCell.setPSC(identityWcdma.getPsc());

                } else {
                    Log.i(TAG, mTAG + "Unknown type of cell signal!"
                            + "\n ClassName: " + info.getClass().getSimpleName()
                            + "\n ToString: " + info.toString());
                }
                if (pDevice.mCell.isValid()) {
                    break;
                }
            }
        }
    } catch (NullPointerException npe) {
       Log.e(TAG, mTAG + "loadCellInfo: Unable to obtain cell signal information: ", npe);
    }
}
 
Example 11
Source File: DeviceInfo.java    From proofmode with GNU General Public License v3.0 4 votes vote down vote up
public static String getCellInfo(Context ctx) throws SecurityException {
        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 (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {

            List<CellInfo> infos = null;
            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.toString();
    }