Search code examples
androidcordovapluginssignal-strength

How can I create a signal quality plugin in Cordova?


I've tried to use some answers here to create a Cordova plugin for Signal Quality (it can be in dBm or 0-4 level), but nothing worked.

Those with the PhoneStateListener did not work as well, as in Cordova you need to create this type of class:

public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
    if (action.equals("signal")){

Any help will be very appreciated.

Thanks in advance.

EDIT: I've read the Android Developers page related to this: http://developer.android.com/reference/android/telephony/PhoneStateListener.html http://developer.android.com/reference/android/telephony/SignalStrength.html

But nothing came up to me. I'm getting really frustrated with this plugin, as it's the last one, once it's finished, the project is done too.

Thanks, MYS77

EDIT 2

I've already used these codes:

1:

CellInfo allCellInfo = (CellInfo) tm.getAllCellInfo().get(0);
              CellSignalStrength cellSignalStrength = ((CellInfoGsm) allCellInfo).getCellSignalStrength();
              level = cellSignalStrength.getLevel();

2:

List<NeighboringCellInfo> neighbors = tm.getNeighboringCellInfo();
          for (NeighboringCellInfo n : neighbors) {
              if (n.getRssi() != NeighboringCellInfo.UNKNOWN_RSSI) {
                  dbm = n.getRssi();
              }

3:

TelephonyManager tm = (TelephonyManager)cordova.getActivity().getSystemService(Context.TELEPHONY_SERVICE);
MyPhoneStateListener MyListener;

  @Override
  public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
    boolean result = false;
    String level = "";
    if (action.equals("signal")){
        try{
            tm.listen(MyListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
            callbackContext.success("Hey!");
            level = MyListener.getStrength();
        }
        catch (Exception ex) {
            callbackContext.error("ERROR");
        }
        finally{
            if (level != "") {
                callbackContext.success("Signal Strength = "+level);
            }
            else{
                callbackContext.success("Could not retrieve signal quality.");
            }
        }
    }
    return result;
  }
}

class MyPhoneStateListener extends PhoneStateListener{
  String gsmStrength = "";

  @Override
  public void onSignalStrengthsChanged(SignalStrength signalStrength) {
    super.onSignalStrengthsChanged(signalStrength);
    gsmStrength = String.valueOf(signalStrength.getGsmSignalStrength()* 2 - 113);           
}
public String getStrength() {
    return gsmStrength;
}

They do not generate any errors but when I test on my Xperia M2 Aqua it keeps sending "Invalid action" as a response.


Solution

  • Nevermind again, as nobody is helping... I found a way to do this in GSM network:

    EDIT 2: Deleted my previous answers, as this code is working for both Samsung and non-Samsung devices.

    public string signalStrength(){
     TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
       int dBmlevel = 0;
       int asulevel = 0;
       String res = "";
                try {
                    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{
                                res = "Unknown type of cell signal.";
                            }
                        }
                    }
                    else{
                        //Mostly for Samsung devices, after checking if the list is indeed empty.
                        MyListener = new MyPhoneStateListener();
                        tm.listen(MyListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
                        int cc = 0;
                        while ( signalLevel == -1){
                            Thread.sleep(200);
                            if (cc++ >= 5)
                            {
                                break;
                            }
                        }
                        asulevel = signalLevel;
                        dBmlevel = -113 + 2 * asulevel;
                        tm.listen(MyListener, PhoneStateListener.LISTEN_NONE);
                        signalLevel = -1;
                    }
                    result = true;
                }
                catch (Exception ex){
                    res = "Failed to retrieve signal strength";
                }
                finally{
                    res = "Signal strength: " + dBmlevel + " dBm, "+ asulevel + " asu";
                }
         return res;
      }