Example usage for android.telephony SignalStrength getGsmSignalStrength

List of usage examples for android.telephony SignalStrength getGsmSignalStrength

Introduction

In this page you can find the example usage for android.telephony SignalStrength getGsmSignalStrength.

Prototype

@Deprecated
public int getGsmSignalStrength() 

Source Link

Document

Get the GSM RSSI in ASU.

Usage

From source file:Main.java

public static int getSignalByReflection(SignalStrength s) {

    return 2 * s.getGsmSignalStrength() - 113;

}

From source file:pk.development.sms.detection.CellSniffer.java

@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
    btsData.setSignalStrength(signalStrength.getGsmSignalStrength());
}

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

/**
 * Originates from a change in signal strength
 * @param signalStrength SignalStrength//w  w w.j a va 2s. c o m
 * @return JSON
 */
public static String signalStrengthJSON(SignalStrength signalStrength) {
    final Calendar calendar = Calendar.getInstance();
    final JSONObject json = new JSONObject();

    try {
        json.put("provider", SIGNAL_STRENGTH); // Yep provider and type are same values
        json.put("type", SIGNAL_STRENGTH);
        json.put("timestamp", calendar.getTimeInMillis());
        json.put("cdmaDbm", signalStrength.getCdmaDbm());
        json.put("cdmaEcio", signalStrength.getCdmaEcio());
        json.put("evdoDbm", signalStrength.getEvdoDbm());
        json.put("evdoEcio", signalStrength.getEvdoEcio());
        json.put("evdoSnr", signalStrength.getEvdoSnr());
        json.put("gsmBitErrorRate", signalStrength.getGsmBitErrorRate());
        json.put("gsmSignalStrength", signalStrength.getGsmSignalStrength());

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            json.put("level", signalStrength.getLevel());
        }

        json.put("isGSM", signalStrength.isGsm());
    } catch (JSONException exc) {
        logJSONException(exc);
    }

    return json.toString();
}

From source file:org.kaaproject.kaa.demo.cellmonitor.CellMonitorFragment.java

private void updateGsmSignalStrength() {
    int gsmSignalStrength = UNDEFINED;
    SignalStrength signalStrength = mApplication.getSignalStrength();
    if (signalStrength != null) {
        gsmSignalStrength = signalStrength.getGsmSignalStrength();
    }//from w  w  w  . jav a2  s.  co m
    if (gsmSignalStrength != UNDEFINED) {
        mGsmSignalStrengthValue.setText(String.valueOf(gsmSignalStrength));
    } else {
        mGsmSignalStrengthValue.setText(R.string.unavailable);
    }
}

From source file:org.wso2.emm.agent.services.DeviceNetworkStatus.java

@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
    super.onSignalStrengthsChanged(signalStrength);
    if (signalStrength.isGsm()) {
        if (signalStrength.getGsmSignalStrength() != 99) {
            // this is the equation used to convert a valid gsm signal to dbm.
            setCellSignalStrength(signalStrength.getGsmSignalStrength() * 2 - 113);
        } else {//from   w  w  w. j av  a 2s . c  o  m
            setCellSignalStrength(signalStrength.getGsmSignalStrength());
        }
    } else {
        setCellSignalStrength(signalStrength.getCdmaDbm());
    }
}

From source file:alaindc.crowdroid.CustomPhoneStateListener.java

@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
    super.onSignalStrengthsChanged(signalStrength);
    int strength = 0;
    String[] values = signalStrength.toString().split(" ");
    String wcdmaStrength = values[3];
    ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    String type = RadioUtils.getNetClass(mContext);
    //if (activeNetwork != null && activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE && (type.equals("2G") || type.equals("3G") || type.equals("4G"))) {
    try {/*  w w  w  .  j a va 2  s.  c  om*/
        strength = signalStrength.getGsmSignalStrength();
        strength = (strength != 0) ? (2 * strength) - 113 : Integer.parseInt(wcdmaStrength); // GSM / UMTS(WCDMA)
    } catch (Exception e) {
        strength = 0;
        Log.d("Hack", "Big hack");
    }
    //}

    SharedPreferences sharedPref = mContext.getSharedPreferences(Constants.PREF_FILE, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString(Constants.PREF_SENSOR_ + Constants.TYPE_TEL, Integer.toString(strength));
    editor.commit();

    // Update view
    Intent senseintent = new Intent(Constants.INTENT_UPDATE_SENSORS);
    senseintent.putExtra(Constants.INTENT_RECEIVED_DATA_EXTRA_DATA, "Sensor "
            + Constants.getNameOfSensor(Constants.TYPE_TEL) + " value: " + Integer.toString(strength));
    LocalBroadcastManager.getInstance(mContext).sendBroadcast(senseintent);
}

From source file:com.karpenstein.signalmon.NetServerService.java

@Override
public void onCreate() {
    super.onCreate();
    try {/*w ww  .  jav a  2 s  .  co m*/
        jsonState = new JSONObject();
        jsonState.put("dataActivity", TelephonyManager.DATA_ACTIVITY_NONE);
    } catch (JSONException ex) {
        Log.d("NetServerService", "Failed to put data activity in the JSONObject");
    }

    // Get the telephony manager
    telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    if (telephonyManager == null)
        Log.d("NetServerService", "TelephonyManager was null.");
    Log.d("NetServerService", "about to create PhoneStateListener");
    // Create a new PhoneStateListener
    psListener = new PhoneStateListener() {
        @Override
        public void onDataActivity(int direction) {
            Log.d("NetServerService", "received onDataActivity message");
            try {
                jsonState.put("dataActivity", direction);
            } catch (JSONException ex) {
            }
            notifyListeners();
        }

        @Override
        public void onSignalStrengthsChanged(SignalStrength signalStrength) {
            Log.d("NetServerService", "received onSignalStrength message");
            try {
                jsonState.put("cdmaDbm", signalStrength.getCdmaDbm());
                jsonState.put("cdmaEcio", signalStrength.getCdmaEcio());
                jsonState.put("evdoDbm", signalStrength.getEvdoDbm());
                jsonState.put("evdoEcio", signalStrength.getEvdoEcio());
                jsonState.put("evdoSnr", signalStrength.getEvdoSnr());
                jsonState.put("gsmBitErrorRate", signalStrength.getGsmBitErrorRate());
                jsonState.put("gsmSignalStrength", signalStrength.getGsmSignalStrength());
                jsonState.put("isGsm", signalStrength.isGsm());
            } catch (JSONException ex) {
            }
            notifyListeners();
        }

        @Override
        public void onDataConnectionStateChanged(int state, int networkType) {
            Log.d("NetServerService", "received onDataConnectionStateChanged message");
            try {
                jsonState.put("connState", state);
                jsonState.put("netType", networkType);
            } catch (JSONException ex) {
            }
            notifyListeners();
        }
    };

    Log.d("NetServerService", "about to call telephonyManager.listen");
    // Register the listener with the telephony manager
    telephonyManager.listen(psListener, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE
            | PhoneStateListener.LISTEN_SIGNAL_STRENGTHS | PhoneStateListener.LISTEN_DATA_ACTIVITY);
    Log.d("NetServerService", "done calling telephonyManager.listen -- exiting onCreate");
}

From source file:com.google.android.apps.mytracks.services.TrackRecordingService.java

@Override
public void onSignalStrengthSampled(SignalStrength signal) {
    if (signal.isGsm()) {
        signalStrength = signal;/*from www . ja  v a 2  s  .  c o m*/
        gsmBitErrorRate = signal.getGsmBitErrorRate();
        if (signal.getGsmSignalStrength() < 2 || signal.getGsmSignalStrength() > 30) {
            switch (signal.getGsmSignalStrength()) {
            case 0:
                gsmSignal = -113;
            case 1:
                gsmSignal = -111;
            case 31:
                gsmSignal = -51; // or better 100%
            case 99:
                gsmSignal = Constants.UNKNOWN_SIGNAL;
            }
        } else {
            gsmSignal = -109 + (signal.getGsmSignalStrength() - 2) * 2;
        }
    }
}

From source file:uk.ac.horizon.ubihelper.service.channel.CellStrengthChannel.java

protected void update(CellLocation loc, SignalStrength ss) {
    if (loc == null && telephony != null)
        loc = telephony.getCellLocation();
    // TODO Auto-generated method stub
    JSONObject value = new JSONObject();
    try {/*from w w w  . j  a v a2 s  .  c  o m*/
        value.put("timestamp", System.currentTimeMillis());
        if (loc instanceof GsmCellLocation) {
            GsmCellLocation gsm = (GsmCellLocation) loc;
            if (gsm.getCid() != (-1))
                value.put("cid", gsm.getCid());
            if (gsm.getLac() != (-1))
                value.put("lac", gsm.getLac());
            value.put("type", "gsm");
        } else if (loc instanceof CdmaCellLocation) {
            CdmaCellLocation cdma = (CdmaCellLocation) loc;
            if (cdma.getBaseStationId() != (-1))
                value.put("baseStationId", cdma.getBaseStationId());
            if (cdma.getBaseStationLatitude() != Integer.MAX_VALUE)
                value.put("baseStationLat", cdma.getBaseStationLatitude());
            if (cdma.getBaseStationLongitude() != Integer.MAX_VALUE)
                value.put("baseStationLon", cdma.getBaseStationLongitude());
            if (cdma.getNetworkId() != (-1))
                value.put("baseStationId", cdma.getNetworkId());
            if (cdma.getNetworkId() != (-1))
                value.put("networkId", cdma.getNetworkId());
            if (cdma.getSystemId() != (-1))
                value.put("systemId", cdma.getSystemId());
            value.put("type", "cdma");
        } else if (loc != null) {
            value.put("type", loc.getClass().getName());
        }

        if (ss != null) {
            if (ss.getCdmaDbm() != (-1))
                value.put("cdmsDbm", ss.getCdmaDbm());
            if (ss.getCdmaEcio() != (-1))
                value.put("cdmaEcio", ss.getCdmaEcio());
            if (ss.getEvdoDbm() != (-1))
                value.put("evdoDbm", ss.getEvdoDbm());
            if (ss.getEvdoEcio() != (-1))
                value.put("evdiEcio", ss.getEvdoEcio());
            if (ss.getEvdoSnr() != (-1))
                value.put("evdoSnr", ss.getEvdoSnr());
            if (ss.getGsmBitErrorRate() != (-1))
                value.put("gsmBER", ss.getGsmBitErrorRate());
            if (ss.getGsmSignalStrength() != (-1))
                value.put("gsmSS", ss.getGsmSignalStrength());
            value.put("gsm", ss.isGsm());
        }
    } catch (JSONException e) {
        // shouldn't
    }
    onNewValue(value);
}

From source file:com.vonglasow.michael.satstat.RadioSectionFragment.java

/**
 * Updates all cell data./*from  w w  w  .j  av  a2 s. com*/
 * 
 * This method is called whenever any change in the cell environment (cells in view or signal
 * strengths) is signaled, e.g. by a call to a {@link android.telephony.PhoneStateListener}. The
 * arguments of this method should be filled with the data passed to the
 * {@link android.telephony.PhoneStateListener} where possible, and null passed for all others.
 * 
 * To force an update of all cell data, simply call this method with each argument set to null.
 * 
 * If any of the arguments is null, this method will try to obtain that data by querying
 * {@link android.telephony.TelephonyManager}. The only exception is {@code signalStrength}, which
 * will not be explicitly queried if missing.
 * 
 * It will first process {@code aCellInfo}, then {@code aLocation}, querying current values from
 * {@link android.telephony.TelephonyManager} if one of these arguments is null. Next it will process
 * {@code signalStrength}, if supplied, and eventually obtain neighboring cells by calling
 * {@link android.telephony.TelephonyManager#getNeighboringCellInfo()} and process these. Eventually
 * it will refresh the list of cells.
 * 
 * @param aLocation The {@link android.telephony.CellLocation} reported by a
 * {@link android.telephony.PhoneStateListener}. If null, the current value will be queried.
 * @param aSignalStrength The {@link android.telephony.SignalStrength} reported by a
 * {@link android.telephony.PhoneStateListener}. If null, the signal strength of the serving cell
 * will either be taken from {@code aCellInfo}, if available, or not be updated at all.
 * @param aCellInfo A list of {@link android.telephony.CellInfo} instances reported by a
 * {@link android.telephony.PhoneStateListener}. If null, the current value will be queried.
 */
@SuppressLint("NewApi")
public void updateCellData(CellLocation aLocation, SignalStrength signalStrength, List<CellInfo> aCellInfo) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        try {
            /*
             * CellInfo requires API 17+ and should in theory return all cells in view. In practice,
             * some devices do not implement it or return only a partial list. On some devices,
             * PhoneStateListener#onCellInfoChanged() will fire but always receive a null argument.
             */
            List<CellInfo> cellInfo = (aCellInfo != null) ? aCellInfo
                    : mainActivity.telephonyManager.getAllCellInfo();
            mCellsGsm.updateAll(cellInfo);
            mCellsCdma.updateAll(cellInfo);
            mCellsLte.updateAll(cellInfo);
        } catch (SecurityException e) {
            // Permission not granted, can't retrieve cell data
        }
    }

    try {
        /*
         * CellLocation should return the serving cell, unless it is LTE (in which case it should
         * return null). In practice, however, some devices do return LTE cells. The approach of
         * this method does not work well for devices with multiple radios.
         */
        CellLocation location = (aLocation != null) ? aLocation
                : mainActivity.telephonyManager.getCellLocation();
        String networkOperator = mainActivity.telephonyManager.getNetworkOperator();
        mCellsGsm.removeSource(CellTower.SOURCE_CELL_LOCATION);
        mCellsCdma.removeSource(CellTower.SOURCE_CELL_LOCATION);
        mCellsLte.removeSource(CellTower.SOURCE_CELL_LOCATION);
        if (location instanceof GsmCellLocation) {
            if (mLastNetworkGen < 4) {
                mServingCell = mCellsGsm.update(networkOperator, (GsmCellLocation) location);
                if ((mServingCell.getDbm() == CellTower.DBM_UNKNOWN) && (mServingCell instanceof CellTowerGsm))
                    ((CellTowerGsm) mServingCell).setAsu(mLastCellAsu);
            } else {
                mServingCell = mCellsLte.update(networkOperator, (GsmCellLocation) location);
                if (mServingCell.getDbm() == CellTower.DBM_UNKNOWN)
                    ((CellTowerLte) mServingCell).setAsu(mLastCellAsu);
            }
        } else if (location instanceof CdmaCellLocation) {
            mServingCell = mCellsCdma.update((CdmaCellLocation) location);
            if (mServingCell.getDbm() == CellTower.DBM_UNKNOWN)
                ((CellTowerCdma) mServingCell).setDbm(mLastCellDbm);
        }
        networkTimehandler.removeCallbacks(networkTimeRunnable);
    } catch (SecurityException e) {
        // Permission not granted, can't retrieve cell data
    }

    if ((mServingCell == null) || (mServingCell.getGeneration() <= 0)) {
        if ((mLastNetworkGen != 0) && (mServingCell != null))
            mServingCell.setGeneration(mLastNetworkGen);
        NetworkInfo netinfo = mainActivity.connectivityManager.getActiveNetworkInfo();
        if ((netinfo == null) || (netinfo.getType() < ConnectivityManager.TYPE_MOBILE_MMS)
                || (netinfo.getType() > ConnectivityManager.TYPE_MOBILE_HIPRI)) {
            networkTimehandler.postDelayed(networkTimeRunnable, NETWORK_REFRESH_DELAY);
        }
    } else if (mServingCell != null) {
        mLastNetworkGen = mServingCell.getGeneration();
    }

    if ((signalStrength != null) && (mServingCell != null)) {
        int pt = mainActivity.telephonyManager.getPhoneType();
        if (pt == PHONE_TYPE_GSM) {
            mLastCellAsu = signalStrength.getGsmSignalStrength();
            updateNeighboringCellInfo();
            if (mServingCell instanceof CellTowerGsm)
                ((CellTowerGsm) mServingCell).setAsu(mLastCellAsu);
            else
                Log.w(MainActivity.class.getSimpleName(),
                        "Got SignalStrength for PHONE_TYPE_GSM but serving cell is not GSM");
        } else if (pt == PHONE_TYPE_CDMA) {
            mLastCellDbm = signalStrength.getCdmaDbm();
            if ((mServingCell != null) && (mServingCell instanceof CellTowerCdma))
                mServingCell.setDbm(mLastCellDbm);
            else
                Log.w(MainActivity.class.getSimpleName(),
                        "Got SignalStrength for PHONE_TYPE_CDMA but serving cell is not CDMA");
        } else
            Log.w(MainActivity.class.getSimpleName(),
                    String.format("Got SignalStrength for unknown phone type (%d)", pt));
    } else if (mServingCell == null) {
        Log.w(MainActivity.class.getSimpleName(), "Got SignalStrength but serving cell is null");
    }

    try {
        /*
         * NeighboringCellInfo is not supported on some devices and will return no data. It lists
         * only GSM and successors' cells, but not CDMA cells.
         */
        List<NeighboringCellInfo> neighboringCells = mainActivity.telephonyManager.getNeighboringCellInfo();
        String networkOperator = mainActivity.telephonyManager.getNetworkOperator();
        mCellsGsm.updateAll(networkOperator, neighboringCells);
        mCellsLte.updateAll(networkOperator, neighboringCells);
    } catch (SecurityException e) {
        // Permission not granted, can't retrieve cell data
    }

    showCells();
}