get SignalStrength in dbm

我的未来我决定 提交于 2020-01-06 04:23:48

问题


I would like to show the the signal strength in dbm. But with my code i always become the result -1. But when i see the signal strength in the settings it's around -81.. Can you find a mistake? thank you!

public class MainActivity extends Activity implements OnClickListener {
    TextView dateAndTimeLabel;
    private int signalDBM = 0;

    public class GetParams extends PhoneStateListener {
        @Override  
        public void onSignalStrengthsChanged(SignalStrength signalStrength) {
            super.onSignalStrengthsChanged(signalStrength);
            signalDBM = signalStrength.getEvdoDbm();
        }
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        GetParams listener = new GetParams();
        TelephonyManager TelManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        TelManager.listen(listener ,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);

        Button btn = (Button)findViewById(R.id.start);
        btn.setOnClickListener(this);             
    }

    public void onClick(View v) {
        Toast.makeText(this, "CDMA signal strength is " + this.signalDBM, Toast.LENGTH_SHORT).show();               
    }

    /* Create options menu etc*/
}

回答1:


Try this:

List<CellInfo> cellInfoList = tm.getAllCellInfo();
          //Checking if list values are not null
            if (cellInfoList != null) {
                for (final CellInfo info : cellInfoList) {
                    if (info instanceof CellInfoGsm) {
                        //GSM Network
                        CellSignalStrengthGsm cellSignalStrength = ((CellInfoGsm)info).getCellSignalStrength();
                        dBmlevel = cellSignalStrength.getDbm();
                        asulevel = cellSignalStrength.getAsuLevel();
                    }
                    else if (info instanceof CellInfoCdma) {
                        //CDMA Network
                        CellSignalStrengthCdma cellSignalStrength = ((CellInfoCdma)info).getCellSignalStrength();
                        dBmlevel = cellSignalStrength.getDbm();
                        asulevel = cellSignalStrength.getAsuLevel();
                    }
                    else if (info instanceof CellInfoLte) {
                        //LTE Network
                        CellSignalStrengthLte cellSignalStrength = ((CellInfoLte)info).getCellSignalStrength();
                        dBmlevel = cellSignalStrength.getDbm();
                        asulevel = cellSignalStrength.getAsuLevel();
                    }
                    else if  (info instanceof CellInfoWcdma) {
                        //WCDMA Network
                        CellSignalStrengthWcdma cellSignalStrength = ((CellInfoWcdma)info).getCellSignalStrength();
                        dBmlevel = cellSignalStrength.getDbm();
                        asulevel = cellSignalStrength.getAsuLevel();
                    }
                    else{
                        //Developed as a Cordova plugin, that's why I'm using callbackContext
                        callbackContext.error("Unknown type of cell signal.");
                    }
                }
          }

If your cellphone is Samsung, it'll return 0 dBm, 0 asu. If not, then it should work (it worked in my Xperia M2 Aqua).



来源:https://stackoverflow.com/questions/24781009/get-signalstrength-in-dbm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!