Get Signal Strength in Android
Asked Answered
R

10

40

I want to get the Signal Strength of the Device at the point I hit the API call. I have searched on all the related threads and I am not successful yet.

So I would like to get the signal strength like

SignalStrength ss = null  ; // some initialization

int n = ss.getGsmSignalStrength();

But while using this, it is obvious that I will get null pointer exception since I have initialised SignalStrength as null. But I don't know how to initialise this.

Also that I don't want to use PhoneStateListener because it is triggered only if the signal changes.

I am getting the Signal Strength using the below code

TelephonyManager telephonyManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
CellInfoGsm cellinfogsm = (CellInfoGsm)telephonyManager.getAllCellInfo().get(0);
CellSignalStrengthGsm cellSignalStrengthGsm = cellinfogsm.getCellSignalStrength();
cellSignalStrengthGsm.getDbm();

But I don't want to use CellSignalStrength because it is only added in API Level 17 and will not work under 17. I want the code to work on API Level 7+.

Or is there any other method, so that I could get the signal strength at the point of hitting the API call?

Ringer answered 6/11, 2013 at 6:55 Comment(3)
Possible duplicate of How to get cell service signal strength in Android?Spivey
@Spivey : but i dont want to use phonestatelistener . I have referred that link and that would not help me .Ringer
You said you didn't want to use PhoneStateListener because it's only triggered on changes, but as that other question indicates, it'll fire when your app starts up. At that point, you should only care when it changes.Spivey
P
30

Define Variables:

TelephonyManager mTelephonyManager;
MyPhoneStateListener mPhoneStatelistener;   
int mSignalStrength = 0;

Then add this class to your code:

class MyPhoneStateListener extends PhoneStateListener {

     @Override
     public void onSignalStrengthsChanged(SignalStrength signalStrength) {
         super.onSignalStrengthsChanged(signalStrength);
         mSignalStrength = signalStrength.getGsmSignalStrength();
         mSignalStrength = (2 * mSignalStrength) - 113; // -> dBm           
     }
 }

and in your onCreate method use:

mPhoneStatelistener = new MyPhoneStateListener();
mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
mTelephonyManager.listen(mPhoneStatelistener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
Philipp answered 26/9, 2014 at 10:50 Comment(5)
Please, try to follow java and Android code conventions at least... source.android.com/source/code-style.htmlSurroundings
@Philipp : If I want to dBm value in kbps or mbps then how I can get that?Maladminister
You want kbps or mbps. If you mean the up or download speed from Server you have to measure the time needed for a download divided by the file size. e.g. 100MB / 60s = 1.66 mbpsPhilipp
@ShwetaChauhan dBm measures signal strength(i.e power) whereas kbps/mbps is internet speed, which is differentApocrine
This answer is outdated.Rumple
O
6

Global Define :

TelephonyManager telephonyManager;
myPhoneStateListener psListener;
TextView txtSignalStr;

onCreate Method :

@Override
protected void onCreate(final Bundle savedInstanceState) {
 txtSignalStr = (TextView)findViewById(R.id.signalStrength);
 psListener = new myPhoneStateListener();
 telephonyManager = (TelephonyManager)getActivity().getSystemService(Context.TELEPHONY_SERVICE);
 telephonyManager.listen(psListener,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}

Create myPhoneStateListener Class :

public class myPhoneStateListener extends PhoneStateListener {
    public int signalStrengthValue;

    public void onSignalStrengthsChanged(SignalStrength signalStrength) {
        super.onSignalStrengthsChanged(signalStrength);
        if (signalStrength.isGsm()) {
            if (signalStrength.getGsmSignalStrength() != 99)
                signalStrengthValue = signalStrength.getGsmSignalStrength() * 2 - 113;
            else
                signalStrengthValue = signalStrength.getGsmSignalStrength();
        } else {
            signalStrengthValue = signalStrength.getCdmaDbm();
        }
        txtSignalStr.setText("Signal Strength : " + signalStrengthValue);
    }
}
Overfill answered 19/6, 2015 at 12:1 Comment(1)
What is this getActivity() methodCircuitry
V
6
public class PhoneCustomStateListener extends PhoneStateListener {

    public int signalSupport = 0;

    @Override
    public void onSignalStrengthsChanged(SignalStrength signalStrength) {
        super.onSignalStrengthsChanged(signalStrength);

        signalSupport = signalStrength.getGsmSignalStrength();
        Log.d(getClass().getCanonicalName(), "------ gsm signal --> " + signalSupport);

        if (signalSupport > 30) {
            Log.d(getClass().getCanonicalName(), "Signal GSM : Good");


        } else if (signalSupport > 20 && signalSupport < 30) {
            Log.d(getClass().getCanonicalName(), "Signal GSM : Avarage");


        } else if (signalSupport < 20 && signalSupport > 3) {
            Log.d(getClass().getCanonicalName(), "Signal GSM : Weak");


        } else if (signalSupport < 3) {
            Log.d(getClass().getCanonicalName(), "Signal GSM : Very weak");


        }
    }
}
Ventilation answered 8/3, 2017 at 15:13 Comment(0)
A
4

We should not initialize signalstrength, instead of that use phonelistener and override the method onSignalStrengthsChanged(SignalStrength signalStrength).

For eg., have a look at following code snippet

class SamplePhoneStateListener extends PhoneStateListener {

        int signalStrength = 0;

        @Override
        public void onSignalStrengthsChanged(SignalStrength signalStrength) {
            super.onSignalStrengthsChanged(signalStrength);
            signalStrength = signalStrength.getGsmSignalStrength();
            //You can check the signal strength value here..
        }

    }

using TelephonyManager object you can listen to the above class like

TelephonyManagerObject.listen(myListener,
            PhoneStateListener.LISTEN_SIGNAL_STRENGTHS)
Ashtoreth answered 22/7, 2014 at 21:41 Comment(0)
M
2

This worked for me:

Which services are needed:

ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

How to gather the metrics:

public void startGatherMetrics() {
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo != null) {
        networkInfoStr = connectivityManager.getActiveNetworkInfo().toString();

        // gather Network Capabilities
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            Network network = connectivityManager.getActiveNetwork();
            networkInfoStr += "; " + connectivityManager.getNetworkCapabilities(network).toString();
        }
    }
    Log.d("A_NETWORK_INFO", networkInfoStr);

    new Thread(new Runnable() {
        @Override
        public void run() {
            quitLooper = false;
            Looper.prepare();
            telephonyManager.listen(new PhoneStateListener() {
                @Override
                public void onSignalStrengthsChanged(SignalStrength signalStrength) {
                    super.onSignalStrengthsChanged(signalStrength);
                    Log.d("A_NETWORK_METRICS",
                        "Signal Strength (0-4 / dBm):" + getLevel(signalStrength) + " / "
                            + getDbm(signalStrength));

                    if (quitLooper)
                        Looper.myLooper().quit();
                }
            }, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);

            Looper.loop();
        }
    }).start();

}

public void stop() {
    quitLooper = true;
}

The "getLevel(signalStrength)" and "getDbm(signalStrength)" can be found here.

Marek answered 22/6, 2018 at 14:26 Comment(0)
C
1

You don't instantiate SignalStrength (and possibly you cannot). from application code.

You must use a PhoneStateListener (subclass), and implement onSignalStrengthsChanged:

http://developer.android.com/reference/android/telephony/PhoneStateListener.html#onSignalStrengthsChanged(android.telephony.SignalStrength)

A SignalStrength will be created for you and passed into your override.

Celik answered 26/4, 2014 at 14:41 Comment(0)
H
1

PhoneStateListener is deprecated we can use telephony Manager
TO get a signal strength we can use the below code . it needs fine location permission also

val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        val cellInfoList = telephonyManager.allCellInfo
        if (cellInfoList != null) {
            for (cellInfo in cellInfoList) {
                when (cellInfo) {
                    is CellInfoGsm -> {
                        val cellSignalStrengthGsm = cellInfo.cellSignalStrength
                        val signalStrengthDbm = cellSignalStrengthGsm.dbm
                        Log.i("SignalStrength1","mobileSignalStrength${signalStrengthDbm}")
                        // Do something with signalStrengthDbm for GSM network
                    }
                    is CellInfoLte -> {
                        val cellSignalStrengthLte = cellInfo.cellSignalStrength
                        val signalStrengthDbm = cellSignalStrengthLte.dbm
                        Log.i("SignalStrength2","mobileSignalStrength${signalStrengthDbm}")
                        // Do something with signalStrengthDbm for LTE network
                    }
                    // Handle other cell info types if necessary (e.g., CellInfoWcdma, CellInfoCdma)
                }
            }
        } else {
            // No cell info available
        }
Hadhramaut answered 13/10, 2023 at 4:58 Comment(0)
T
0

The PhoneStateListener object should be instantiated in a looper thread. So, if you are using worker thread (non - GUI thread) the following can be used;

private HandlerThread handlerThreadCellularSignal = null;

public void startListen() {     
        handlerThreadCellularSignal = new HandlerThread("CELLULAR_INFO_THREAD");
        handlerThreadCellularSignal.start();
        Looper looper = handlerThreadCellularSignal.getLooper();
        Handler handler = new Handler(looper);
        handler.post(new Runnable() {
            @Override
            public void run() {
                phoneStatelistener = new PhoneStateListenerEx();
                TelephonyManager telephonyManager = (TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE);
                telephonyManager.listen(phoneStatelistener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
            }
        });
}

private void stopListen() {
    handlerThreadCellularSignal.quit();
}


public class PhoneStateListenerEx extends PhoneStateListener {

    @Override
    public void onSignalStrengthsChanged(SignalStrength signalStrength) {
        super.onSignalStrengthsChanged(signalStrength);
        int signalStrengthdBm = (2 * signalStrength.getGsmSignalStrength()) - 113; // -> dBm
        Log.d("Cellular Signal Strength | " + String.valueOf(signalStrengthdBm));
    }
}
Terrorist answered 7/8, 2019 at 9:46 Comment(0)
G
0
public class myPhoneStateListener extends PhoneStateListener {
    public int signalStrengthValue;

    public void onSignalStrengthsChanged(SignalStrength signalStrength) {
        super.onSignalStrengthsChanged(signalStrength);
        if (signalStrength.isGsm()) {
            if (signalStrength.getGsmSignalStrength() != 99)
                signalStrengthValue = signalStrength.getGsmSignalStrength() * 2 - 113;
            else
                signalStrengthValue = signalStrength.getGsmSignalStrength();
        } else {
            signalStrengthValue = signalStrength.getCdmaDbm();
        }
        txtSignalStr.setText("Signal Strength : " + signalStrengthValue);
    }
}
Guru answered 5/6, 2020 at 14:40 Comment(2)
accessing signal strength will it require fine location permission ?Contrast
Karthikeyan Ve, I think you only need the "Fine Location" permission for Snowcone and up, and in that case, you would probably be using sathish mobapp dev's approach instead: https://mcmap.net/q/397929/-get-signal-strength-in-androidPrager
S
0

Kotlin Version :

class myPhoneStateListener : PhoneStateListener() {
    var signalStrengthValue = 0
    override fun onSignalStrengthsChanged(signalStrength: SignalStrength) {
        super.onSignalStrengthsChanged(signalStrength)
        signalStrengthValue = if (signalStrength.isGsm) {
            if (signalStrength.gsmSignalStrength != 99) signalStrength.gsmSignalStrength * 2 - 113 else signalStrength.gsmSignalStrength
        } else {
            signalStrength.cdmaDbm
        }
        txtSignalStr.setText("Signal Strength : $signalStrengthValue")
    }
}
Septimal answered 5/6, 2020 at 15:4 Comment(1)
PhoneStateListener is DeprecatedEndurant

© 2022 - 2024 — McMap. All rights reserved.