How to get LTE signal strength in Android?

前端 未结 4 847
渐次进展
渐次进展 2020-12-02 19:13

I am using Verizon\'s new LTE handset from HTC Thunderbolt. I cannot find the API to query for the signal strength while the handset is on LTE. By entering the field test mo

相关标签:
4条回答
  • 2020-12-02 19:44

    This is complete working implementation of a sample app which uses a reflection to get LTE signal strength:

    import java.lang.reflect.Method;
    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.telephony.PhoneStateListener;
    import android.telephony.SignalStrength;
    import android.telephony.TelephonyManager;
    import android.util.Log;
    
    public class MainActivity extends Activity
    {
        /*
         * Some significant methods for LTE: getLteSignalStrength, getLteRssi,
         * getLteRsrp and getRsrq. RSRP provides information about signal strength
         * and RSSI helps in determining interference and noise information. RSRQ
         * (Reference Signal Receive Quality) measurement and calculation is based
         * on both RSRP and RSSI.
         */
    
        private SignalStrength      signalStrength;
        private TelephonyManager    telephonyManager;
        private final static String LTE_TAG             = "LTE_Tag";
        private final static String LTE_SIGNAL_STRENGTH = "getLteSignalStrength";
    
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    
            // Listener for the signal strength.
            final PhoneStateListener mListener = new PhoneStateListener()
            {
                @Override
                public void onSignalStrengthsChanged(SignalStrength sStrength)
                {
                    signalStrength = sStrength;
                    getLTEsignalStrength();
                }
            };
    
            // Register the listener for the telephony manager
            telephonyManager.listen(mListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
        }
    
        private void getLTEsignalStrength()
        {
                try
                {
                    Method[] methods = android.telephony.SignalStrength.class.getMethods();
    
                    for (Method mthd : methods)
                    {
                        if (mthd.getName().equals(LTE_SIGNAL_STRENGTH))
                        {
                            int LTEsignalStrength = (Integer) mthd.invoke(signalStrength, new Object[] {});
                            Log.i(LTE_TAG, "signalStrength = " + LTEsignalStrength);
                            return;
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.e(LTE_TAG, "Exception: " + e.toString());
                }
        }
    }
    
    0 讨论(0)
  • 2020-12-02 19:46

    In order to solve this question I created an application called Signal Strength Detector and with source code on GitHub. In my past experience, some devices running Android ICS 4.0 and up have a getLevel method on SignalStrength that returns an integer from 0 - 4 reporting the signal strength. On some other LTE devices (I do not believe the HTC Thunderbolt), there are some methods like getLteCqi getLteRsrp getLteRsrq and getLteRssnr which I will leave to you to determine how to use these values to calculate a signal strength. Finally, I found that on some devices (I believe the HTC Thunderbolt) the LTE signal strength is actually reported with the methods labelled for GSM signal strength! It's crazy, but true. Feel free to download Signal Strength Detector and check out the results on your device and/ or modify code as necessary.

    As a side note, you will need to use Reflection to access these methods, again which I will leave to you to determine how to best implement this. It's fairly simple, but you need to do a lot of try-catch to determine if the method is present, and sometimes setAccessible(true) just to ignore any issues with private methods.

    Hope this helps!

    0 讨论(0)
  • 2020-12-02 19:51

    You need to register a PhoneStateListener with LISTEN_SIGNAL_STRENGTHS. You will then get a callback with the current signal strength, and future callbacks with updates.

    0 讨论(0)
  • 2020-12-02 20:03

    Warning : this solution should work for Android below API17. Some people may have troubles with newer api or specific phone brand (like Huawei)

    You need to use this code. In fact, all the information you need is here :

    String ssignal = signalStrength.toString();
    
    String[] parts = ssignal.split(" ");
    

    The parts[] array will then contain these elements:

    part[0] = "Signalstrength:"  _ignore this, it's just the title_
    
    parts[1] = GsmSignalStrength
    
    parts[2] = GsmBitErrorRate
    
    parts[3] = CdmaDbm
    
    parts[4] = CdmaEcio
    
    parts[5] = EvdoDbm
    
    parts[6] = EvdoEcio
    
    parts[7] = EvdoSnr
    
    parts[8] = LteSignalStrength
    
    parts[9] = LteRsrp
    
    parts[10] = LteRsrq
    
    parts[11] = LteRssnr
    
    parts[12] = LteCqi
    
    parts[13] = gsm|lte|cdma
    
    parts[14] = _not really sure what this number is_
    

    So, LTEdBm is :

    TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
    
    int dbm = 0;
    
    if ( tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_LTE){
    
        // For Lte SignalStrength: dbm = ASU - 140.
        dbm = Integer.parseInt(parts[8])-140;
    
    }
    else{
    
        // For GSM Signal Strength: dbm =  (2*ASU)-113.
        if (signalStrength.getGsmSignalStrength() != 99) {
                        int intdbm = -113 + 2
                                * signalStrength.getGsmSignalStrength();
                        dbm = Integer.toString(intdbm);
                    }
    }
    

    bye

    0 讨论(0)
提交回复
热议问题