Retrieve GSM signal strength in Android

前端 未结 3 1587
后悔当初
后悔当初 2021-02-10 00:54

I\'m a newbie to Android.

How do I get the GSM signal Strength in terms of percentage (1 - 100%)?

3条回答
  •  北荒
    北荒 (楼主)
    2021-02-10 01:18

    Be aware that .getGsmSignalStrength(); now only returns bars: 0-5,99

    The actual values are now hidden. You can still get to them using reflection:

                    int strength=signalStrength.getGsmSignalStrength();//number of bars not ASU
                    Log.v("Mobile","BARS: "+strength);
                    try{//Actual signal strength is hidden
                        Class classFromName = Class.forName(SignalStrength.class.getName());
                        java.lang.reflect.Method method = classFromName.getDeclaredMethod("getAsuLevel");//getDbm
                        strength = (int) method.invoke(signalStrength);
                    }catch (Exception ex){Log.v("Mobile","cant retreive");}
                    if (strength == 99 ) { Log.v("Mobile", "ERROR!  GSM signal strength not available!");return;}//99 = Unknown
                    if (strength == 255) { Log.v("Mobile", "ERROR!  UMTS signal strength not available!");return;}//255 = Unknown
    

    The above example is for ASU only, which seems to work better than Dbm. After you get the ASU value, you can then dump it into percentage:

                    Log.v("Mobile","ASU: "+strength);
                    //Data.mobile_signal=strength*20;//Number of bars 0-5
                    //Data.mobile_signal = 100-((strength-113)/62);//GSM DBM
                    Data.mobile_signal =(int)((double)strength*100/31);//GSM ASU
                    Data.mobile_signal =(int)((double)strength*100/91);//UMTS ASU
                   Log.v("Mobile","Set GSM signal from "+strength+" to "+Data.mobile_signal);
    

    As a reminder, this is for when you have a GSM signal not a CDMA signal. Use TelephonyManager.getPhoneType(); to determine which: 1=GSM, 2=CDMA, 3=SIP

    BUT WAIT! This says I only have a 50% signal strength yet I have 5 bars! This is wrong!

    Well, not exactly. Unless your phone is right in front of the transmitter, it's probably not going to be 100%. But 50% signal is about 100% quality. So from here you have to get creative.

提交回复
热议问题