7

I just started developing for android and I am trying to figure out how to get a users signal strength. The following code gives me the Dbm for a GSM network:

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

// Get GSM Signal Strength (Dbm)
CellInfoGsm GSM = (CellInfoGsm)telephonyManager.getAllCellInfo().get(0);
CellSignalStrengthGsm cellSignalStrengthGsm = GSM.getCellSignalStrength();
signalStrength = cellSignalStrengthGsm.getDbm();

However I want to be able to get the signal strength from any kind of network. May it be GSM, LTE, CDMA, or WCDMA. I have looked at the telephony documentation but I'm lost in how to go about this. I have tried to take the same code as above and just replace Gsm with Lte but that ended up crashing my emulator.

Any help would be greatly appreciated.

Updated and added the listener from the answer below. However now as soon as i run the app it crashes. This is the log file.

05-26 11:25:21.140: D/AndroidRuntime(921): Shutting down VM
05-26 11:25:21.140: W/dalvikvm(921): threadid=1: thread exiting with uncaught exception (group=0xb3aecba8)
05-26 11:25:21.170: E/AndroidRuntime(921): FATAL EXCEPTION: main
05-26 11:25:21.170: E/AndroidRuntime(921): Process: com.example.androidcellinfo, PID: 921
05-26 11:25:21.170: E/AndroidRuntime(921): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidcellinfo/com.example.androidcellinfo.MainActivity}: java.lang.NullPointerException
05-26 11:25:21.170: E/AndroidRuntime(921):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-26 11:25:21.170: E/AndroidRuntime(921):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-26 11:25:21.170: E/AndroidRuntime(921):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-26 11:25:21.170: E/AndroidRuntime(921):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-26 11:25:21.170: E/AndroidRuntime(921):  at android.os.Handler.dispatchMessage(Handler.java:102)
05-26 11:25:21.170: E/AndroidRuntime(921):  at android.os.Looper.loop(Looper.java:136)
05-26 11:25:21.170: E/AndroidRuntime(921):  at android.app.ActivityThread.main(ActivityThread.java:5017)
05-26 11:25:21.170: E/AndroidRuntime(921):  at java.lang.reflect.Method.invokeNative(Native Method)
05-26 11:25:21.170: E/AndroidRuntime(921):  at java.lang.reflect.Method.invoke(Method.java:515)
05-26 11:25:21.170: E/AndroidRuntime(921):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-26 11:25:21.170: E/AndroidRuntime(921):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-26 11:25:21.170: E/AndroidRuntime(921):  at dalvik.system.NativeStart.main(Native Method)
05-26 11:25:21.170: E/AndroidRuntime(921): Caused by: java.lang.NullPointerException
05-26 11:25:21.170: E/AndroidRuntime(921):  at com.example.androidcellinfo.CellInfo.<init>(CellInfo.java:74)
05-26 11:25:21.170: E/AndroidRuntime(921):  at com.example.androidcellinfo.MainActivity.onCreate(MainActivity.java:28)
05-26 11:25:21.170: E/AndroidRuntime(921):  at android.app.Activity.performCreate(Activity.java:5231)
05-26 11:25:21.170: E/AndroidRuntime(921):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-26 11:25:21.170: E/AndroidRuntime(921):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-26 11:25:21.170: E/AndroidRuntime(921):  ... 11 more
05-26 11:25:25.090: I/Process(921): Sending signal. PID: 921 SIG: 9

Here is the code for line 74 (CellInfo) Code Snippet 1

Here is the code for line 28 (MainActivity) Code Snippet 2

RockPaperScissors
  • 171
  • 1
  • 2
  • 11

1 Answers1

0

Duplicate question?
How to get cell service signal strength in Android?
Android: How do I get GSM signal strength for all available network operators
Retrieve GSM signal strength in Android


Anyway, check those links above or these references:

http://developer.android.com/reference/android/telephony/SignalStrength.html

http://www.firstdroid.com/2010/05/12/get-provider-gsm-signal-strength/


If you are lazy to check that, this seems to work (didn't check it):

PhoneStateListener phoneStateListener = new PhoneStateListener() {
    public void onCallForwardingIndicatorChanged(boolean cfi) {}
    public void onCallStateChanged(int state, String incomingNumber) {}
    public void onCellLocationChanged(CellLocation location) {}
    public void onDataActivity(int direction) {}
    public void onDataConnectionStateChanged(int state) {}
    public void onMessageWaitingIndicatorChanged(boolean mwi) {}
    public void onServiceStateChanged(ServiceState serviceState) {}
    //Deprecated
    //public void onSignalStrengthChanged(int asu) 
    public void onSignalStrengthChanged (SignalStrength signalStrength)
    {
         //Do your thing
    }
};

Once you’ve created your own PhoneStateListener, register it with the Telephony Manager using a bitmask to indicate the events you want to listen for, as shown in the following code snippet:

telephonyManager.listen(phoneStateListener,
                        PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR |
                        PhoneStateListener.LISTEN_CALL_STATE |
                        PhoneStateListener.LISTEN_CELL_LOCATION |
                        PhoneStateListener.LISTEN_DATA_ACTIVITY |
                        PhoneStateListener.LISTEN_DATA_CONNECTION_STATE |
                        PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR |
                        PhoneStateListener.LISTEN_SERVICE_STATE |
                        PhoneStateListener.LISTEN_SIGNAL_STRENGTH);

How to get a list of all available networks:

Android: How do I get GSM signal strength for all available network operators

Community
  • 1
  • 1
Fernando Carvalhosa
  • 1,098
  • 1
  • 15
  • 23
  • Sorry but i just started developing for android. How would I actually get the Dbm info for all of the networks? I understand that I can make a listener for when it changes but what I really need is for a way to get the Dbm of Lte, Cdma, and Wcdma networks. – RockPaperScissors May 25 '14 at 19:54
  • Did you check this answer? http://stackoverflow.com/questions/10454388/android-how-do-i-get-gsm-signal-strength-for-all-available-network-operators – Fernando Carvalhosa May 25 '14 at 19:58
  • That post still docent provide me with the information that I need. I somehow need to figure out how the get the Cdma networks dbm. – RockPaperScissors May 25 '14 at 21:07
  • Getting all availabe networks + signal strength doesn't help you? – Fernando Carvalhosa May 25 '14 at 21:13
  • `PhoneStateListener phoneStateListener = new PhoneStateListener() { public void onSignalStrengthChanged(int asu) { System.out.println("Phone Stats" + asu); setSignalLevel(asu); } private void setSignalLevel(int level){ int sLevel = (int) ((level / 31.0) * 100); System.out.println("signalLevel " +sLevel); } };` This is what is was able to get but my app crashes as soon as this code runs. – RockPaperScissors May 26 '14 at 00:28
  • Could you share with us the error log? – Fernando Carvalhosa May 26 '14 at 02:42
  • Could you provide us with this two section in your code? at com.example.androidcellinfo.CellInfo.(CellInfo.java:74) at com.example.androidcellinfo.MainActivity.onCreate(MainActivity.java:28) – Fernando Carvalhosa May 27 '14 at 02:01
  • 1
    Ok I added the code you asked for. BTW thank you a lot for your help on this – RockPaperScissors May 27 '14 at 19:14
  • here's a typo: instead of deprecated `onSignalStrengthChanged(int)` use `onSignalStrengthsChanged(SignalStrength)` – Sam Sch May 14 '18 at 08:21