Android - Signal Strength in Level, DBM, and ASU
Asked Answered
G

3

6

I am currently writing an application for a client who wants to gather data regarding the signal strength at set intervals.

Currently I am using this code:

private static class MyPhoneStateListener extends PhoneStateListener
{
  @Override
  public void onSignalStrengthsChanged(SignalStrength signalStrength)
  {
     super.onSignalStrengthsChanged(signalStrength);
     telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
     InfoStore.setSignal(String.valueOf(signalStrength.getGsmSignalStrength()));
  }
};

This works fine, however the client wants the signal strength in both level (I guess how many bars?), DBM, and ASU.

Anyone have any clue how to read the signal strengths using those different forms?

Godbeare answered 20/2, 2013 at 1:16 Comment(0)
G
8

As mentioned by Charles Ma and Kevin Krumwiede the relevant Android methods are hidden (probably for good reason), however it is still possible to get the values by reflection. Thus one solution to original question:

private class MyPhoneStateListener extends PhoneStateListener
{
    public static final int INVALID = Integer.MAX_VALUE;

    public int signalStrengthDbm = INVALID;
    public int signalStrengthAsuLevel = INVALID;

    @Override
    public void onSignalStrengthsChanged(SignalStrength signalStrength)
    {
        signalStrengthDbm = getSignalStrengthByName(signalStrength, "getDbm");
        signalStrengthAsuLevel = getSignalStrengthByName(signalStrength, "getAsuLevel");
    }

    private int getSignalStrengthByName(SignalStrength signalStrength, String methodName)
    {
        try
        {
            Class classFromName = Class.forName(SignalStrength.class.getName());
            java.lang.reflect.Method method = classFromName.getDeclaredMethod(methodName);
            Object object = method.invoke(signalStrength);
            return (int)object;
        }
        catch (Exception ex)
        {
            return INVALID;
        }
    }
}
Gypsophila answered 17/3, 2016 at 21:7 Comment(0)
F
2

In android 4.x the SignalStrength class has getAsuLevel, getDbm, as well as getLevel (bars) methods.

If you need this to work for older android versions, have a look at the source code and you can copy the implementations of those methods over. http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/4.1.2_r1/android/telephony/SignalStrength.java/

The only thing that you can't get is the Lte measurements in older android versions, but you can probably use java reflection to see if the getLte* methods exist and call it.

Fourdrinier answered 27/2, 2013 at 20:1 Comment(1)
Those methods in SignalStrength are hidden.Silkweed
Z
0

Calculate dBm by

int SignalStrength_ASU = signalStrength.getGsmSignalStrength();
int SignalStrength_dBm = (2 * SignalStrength_ASU) - 113; // -> dBm
Zellers answered 26/9, 2014 at 10:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.