如何在 Cordova 中创建信号质量插件?

How can I create a signal quality plugin in Cordova?

我尝试使用此处的一些答案来为信号质量创建一个 Cordova 插件(它可以是 dBm 或 0-4 级别),但没有任何效果。

那些带有 PhoneStateListener 的不能很好地工作,因为在 Cordova 中你需要创建这种类型的类:

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

任何帮助将不胜感激。

提前致谢。

编辑: 我已经阅读了与此相关的 Android 开发者页面: http://developer.android.com/reference/android/telephony/PhoneStateListener.html http://developer.android.com/reference/android/telephony/SignalStrength.html

但我什么也没想到。我对这个插件感到非常沮丧,因为它是最后一个,一旦完成,项目也完成了。

谢谢, MYS77

编辑 2

我已经使用过这些代码:

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;
}

它们不会产生任何错误,但是当我在我的 Xperia M2 Aqua 上进行测试时,它会不断发送 "Invalid action" 作为响应。

没关系,因为没人帮忙...我找到了一种在 GSM 网络中执行此操作的方法:

编辑 2: 删除了我之前的回答,因为此代码适用于三星和非三星设备。

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;
  }