Example usage for android.telephony CellSignalStrengthGsm getDbm

List of usage examples for android.telephony CellSignalStrengthGsm getDbm

Introduction

In this page you can find the example usage for android.telephony CellSignalStrengthGsm getDbm.

Prototype

@Override
public int getDbm() 

Source Link

Document

Get the signal strength as dBm

Usage

From source file:com.esri.cordova.geolocation.utils.JSONHelper.java

/**
 * Converts CellInfoGsm into JSON/*w w w .  j a v a 2 s. c o  m*/
 * @param cellInfo CellInfoGsm
 * @return JSON
 */
public static String cellInfoGSMJSON(CellInfoGsm cellInfo, boolean returnSignalStrength) {

    final Calendar calendar = Calendar.getInstance();
    final JSONObject json = new JSONObject();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 && cellInfo != null) {
        try {
            json.put("provider", CELLINFO_PROVIDER);
            json.put("type", GSM);
            json.put("timestamp", calendar.getTimeInMillis());

            final CellIdentityGsm identityGsm = cellInfo.getCellIdentity();

            json.put("cid", identityGsm.getCid());
            json.put("lac", identityGsm.getLac());
            json.put("mcc", identityGsm.getMcc());
            json.put("mnc", identityGsm.getMnc());

            if (returnSignalStrength) {
                final JSONObject jsonSignalStrength = new JSONObject();
                final CellSignalStrengthGsm cellSignalStrengthGsm = cellInfo.getCellSignalStrength();
                jsonSignalStrength.put("asuLevel", cellSignalStrengthGsm.getAsuLevel());
                jsonSignalStrength.put("dbm", cellSignalStrengthGsm.getDbm());
                jsonSignalStrength.put("level", cellSignalStrengthGsm.getLevel());

                json.put("cellSignalStrengthGsm", jsonSignalStrength);
            }
        } catch (JSONException exc) {
            logJSONException(exc);
        }
    }
    return json.toString();
}

From source file:org.restcomm.app.qoslib.UtilsOld.CellHistory.java

public String updateNeighborHistory(int[] _list, int[] _list_rssi) {
    int i;//from   w ww .jav a2s . c o m
    //if (tmLastNeighborUpdate + 2000 > System.currentTimeMillis())
    //   return "";

    //if (!_isTravelling)
    {
        String txt = "";
        String stringNeighboring = "";
        String[] _type = null;
        try {
            int netType = telephonyManager.getNetworkType();
            int gen = PhoneState.getNetworkGeneration(netType);
            int n = 0, len = 0;
            boolean bValid = true;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                try {
                    List<CellInfo> cells = null;
                    cells = telephonyManager.getAllCellInfo();
                    if (cells != null && cells.size() > 0 && cells.get(0) instanceof CellInfoLte
                            && telephonyManager.getNetworkType() == PhoneState.NETWORK_NEWTYPE_LTE) {
                        return updateLteNeighborHistory(cells);
                    }
                    //if (cells == null)
                    //   LoggerUtil.logToFile(LoggerUtil.Level.ERROR, TAG, "updateNeighborHistory", "cells = null");

                    if (cells != null && cells.size() > 1)//1)
                    {

                        len = cells.size();
                        if (len > 8)
                            len = 8;
                        _list = new int[len];
                        _list_rssi = new int[len];
                        _type = new String[len];
                        for (i = 0; i < len; i++) {
                            bValid = true;
                            CellInfo neighbor = cells.get(i);
                            String msg = "cells[" + i + "]=" + neighbor.toString();
                            String classname = neighbor.getClass().toString();
                            //LoggerUtil.logToFile(LoggerUtil.Level.ERROR, TAG, "updateNeighborHistory", "cells[" + i + "]=" + neighbor.toString());
                            //Log.d(TAG, "cells[" + c + "]=" + cells.get(c).toString());
                            if (gen < 3)
                                _type[i] = "2G";
                            else
                                _type[i] = "3G";
                            if (neighbor.isRegistered())
                                _type[i] += "*";
                            if (neighbor instanceof CellInfoCdma) {
                                CellIdentityCdma cdmacell = ((CellInfoCdma) neighbor).getCellIdentity();
                                CellSignalStrengthCdma cdmasig = ((CellInfoCdma) neighbor)
                                        .getCellSignalStrength();
                                if (cdmacell.getBasestationId() > 0) {
                                    _list[n] = cdmacell.getBasestationId();
                                    _list_rssi[n] = cdmasig.getEvdoDbm();
                                    n++;
                                    //else if (neighbor.getSystemId() > 0)
                                    //   _list[n] = (neighbor.getLac()<<16) + neighbor.getCid();
                                } else
                                    bValid = false;
                            } else if (neighbor instanceof CellInfoGsm) {
                                CellIdentityGsm gsmcell = ((CellInfoGsm) neighbor).getCellIdentity();
                                CellSignalStrengthGsm gsmsig = ((CellInfoGsm) neighbor).getCellSignalStrength();
                                if (gsmcell.getPsc() > 0 && gsmcell.getPsc() < 1000) {
                                    _list[n] = gsmcell.getPsc();
                                    _list_rssi[n] = gsmsig.getDbm();
                                    n++;
                                } else if (gsmcell.getCid() > 0) {
                                    _list[n] = (gsmcell.getLac() << 16) + gsmcell.getCid();
                                    _list_rssi[n] = gsmsig.getDbm();
                                } else
                                    bValid = false;
                            } else if (neighbor.getClass().toString()
                                    .equals("class android.telephony.CellInfoWcdma")) {
                                try {
                                    Object gsmcell = neighbor.getClass().getDeclaredMethod("getCellIdentity")
                                            .invoke(neighbor, (Object[]) null);
                                    Object gsmsig = neighbor.getClass()
                                            .getDeclaredMethod("getCellSignalStrength")
                                            .invoke(neighbor, (Object[]) null);
                                    Integer psc = (Integer) gsmcell.getClass().getDeclaredMethod("getPsc")
                                            .invoke(gsmcell, (Object[]) null);
                                    Integer cid = (Integer) gsmcell.getClass().getDeclaredMethod("getCid")
                                            .invoke(gsmcell, (Object[]) null);
                                    Integer dbm = (Integer) gsmsig.getClass().getDeclaredMethod("getDbm")
                                            .invoke(gsmsig, (Object[]) null);
                                    if (psc > 0 && psc < 1000) {
                                        _list[n] = psc;
                                        //if (neighbor.isRegistered())
                                        //   _list[n] += 10000;
                                        _list_rssi[n] = dbm;
                                        n++;
                                    } else if (cid > 0 && cid < 65536) {
                                        _list[n] = cid;
                                        //if (neighbor.isRegistered())
                                        //   _list[n] += 10000;
                                        _list_rssi[n] = dbm;
                                        n++;
                                    } else
                                        bValid = false;
                                } catch (Exception e) {
                                    //Log.d(TAG, "Field does not exist - " + fieldname);
                                }

                                //                     CellIdentityWcdma gsmcell =((CellInfoWcdma) neighbor).getCellIdentity();
                                //                     CellSignalStrengthWcdma gsmsig = ((CellInfoWcdma) neighbor).getCellSignalStrength();
                                //                     if (gsmcell.getPsc() > 0)
                                //                     {   _list[n] = gsmcell.getPsc();
                                //                        if (neighbor.isRegistered())
                                //                           _list[n] += 0;
                                //                        _list_rssi[n] = gsmsig.getDbm();
                                //                        n++;
                                //                     }
                                //                     else if (gsmcell.getCid() > 0)
                                //                     {
                                //                        _list[n] = (gsmcell.getLac()<<16) + gsmcell.getCid();
                                //                     }
                                //                     else
                                //                        bValid = false;
                            }
                        }

                    }

                } catch (Exception e) {

                }
            }
            if (_list == null) {
                // Try to obtain the Neighbor list from the API
                // If the API neighbor list has more active entries than what the log provided, use it instead
                List<NeighboringCellInfo> neighboringList = telephonyManager.getNeighboringCellInfo();
                len = neighboringList.size();
                if (neighboringList != null && len > 0) {
                    int activeN = 0; // how many of the API neighbors are active?

                    for (i = 0; i < neighboringList.size(); i++) {
                        NeighboringCellInfo neighbor = neighboringList.get(i);
                        if (neighbor.getPsc() > 0 && neighbor.getRssi() > -120 && neighbor.getRssi() < -10)
                            activeN++;
                        else if (neighbor.getCid() > 0 && neighbor.getRssi() > 0 && neighbor.getRssi() < 32)
                            activeN++;
                    }
                    _list = new int[len];
                    _list_rssi = new int[len];
                    _type = new String[len];

                    for (i = 0; i < len; i++) {
                        bValid = true;
                        NeighboringCellInfo neighbor = neighboringList.get(i);
                        if (gen < 3)
                            _type[n] = "2G";
                        else
                            _type[n] = "3G";
                        if (neighbor.getPsc() > 0)
                            _list[n] = neighbor.getPsc();
                        else if (neighbor.getLac() > 0)
                            _list[n] = (neighbor.getLac() << 16) + neighbor.getCid();
                        else
                            bValid = false;

                        if (bValid) {
                            _list_rssi[n] = neighbor.getRssi();
                            if (_list_rssi[n] >= 0)
                                _list_rssi[n] = getDbmValue(_list_rssi[n]);
                            n++;
                        }
                    }
                }
            }

            if (_list == null)
                return null;
            tmLastNeighborUpdate = System.currentTimeMillis();
            // create and add a neighbor cell item for each neighbor
            for (i = 0; i < _list.length; i++) {
                if (_list[i] != 0) {
                    CellidSample smp = new CellidSample(_type[i], _list[i], _list_rssi[i]);
                    // Update Neighbor list history a maximum of once per 2 seconds
                    neighbor_history.add(smp);
                    stringNeighboring = stringNeighboring + String.valueOf(_list[i] & 0xFFFF) + "@"
                            + _list_rssi[i] + ",";

                }
            }
            // Report the API neighbor list in the Log, build a string of neighbors
            if (lastCellString == stringNeighboring)
                return null;
            lastCellString = stringNeighboring;
            //if (neighboringList.size() > 0)
            //MMCLogger.logToFile(MMCLogger.Level.DEBUG, TAG, "updateNeighborHistory", stringNeighboring);

            return stringNeighboring;
        } catch (Exception e) {
            return "";
        } finally {
            //if (txt.length() > 0 && (txt.indexOf("might") > 0 || txt.indexOf("detect") > 0))
            //   MMCLogger.logToFile(MMCLogger.Level.DEBUG, TAG, "detectTravellingFromNeighbors", "\n"+txt.substring(0,txt.length()));
        }
    }
}