Get Signal Strength in Android/Cordova

2 posts, 0 answers
  1. Jonathan
    Jonathan avatar
    7 posts
    Member since:
    Aug 2013

    Posted 17 Mar 2014 Link to this post

    Hello,

    I'm in the process of developing a Cordova Plugin which reads the carrier name and the current signal strength.
    I already got the Plugin to spit out the carrier name, but the getSignalStrength method won't return anything. (The callback on the JS Side never gets called)

    This is my Java Class:

    01.package com.inhji.cordova.plugin;
    02. 
    03.import org.json.JSONArray;
    04.import org.json.JSONException;
    05.import org.json.JSONObject;
    06. 
    07.import android.content.Intent;
    08.import android.net.Uri;
    09.import android.text.Html;
    10.import android.util.Log;
    11. 
    12.import android.content.Context;
    13.import android.telephony.TelephonyManager;
    14. 
    15.import org.apache.cordova.CordovaPlugin;
    16.import org.apache.cordova.CallbackContext;
    17.import org.apache.cordova.PluginResult;
    18.import org.apache.cordova.LOG;
    19. 
    20.import android.telephony.CellInfo;
    21.import android.telephony.CellInfoCdma;
    22.import android.telephony.CellInfoGsm;
    23.import android.telephony.CellInfoLte;
    24.import android.telephony.CellSignalStrengthCdma;
    25.import android.telephony.CellSignalStrengthGsm;
    26.import android.telephony.CellSignalStrengthLte;
    27. 
    28.public class Telephony extends CordovaPlugin {
    29.    private static final String TAG = "Telephony";
    30.    public String carrierName;
    31.    public int signalStrength = 0;
    32. 
    33.    @Override
    34.    public boolean execute(String action, JSONArray args, CallbackContext ctx) throws JSONException {
    35.        // signal strength
    36.        if ("getSignalStrength".equals(action)) {
    37.            getSignalStrength(ctx);
    38.            return true;
    39.        }
    40.        // provider information
    41.        else if("getCarrier".equals(action)) {
    42.            getCarrierName(ctx);
    43.            return true;
    44.        }
    45.        // throw method not found exception
    46.        else {
    47.            return false;
    48.        }
    49.    }
    50. 
    51.    private void getSignalStrength(CallbackContext ctx) {
    52.        signalStrength = 0;
    53. 
    54.         
    55.        try {
    56.            Context context = cordova.getActivity().getApplicationContext();
    57.            final TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
    58.            for (final CellInfo info : tm.getAllCellInfo()) {
    59.                if (info instanceof CellInfoGsm) {
    60.                    final CellSignalStrengthGsm gsm = ((CellInfoGsm) info).getCellSignalStrength();
    61.                    signalStrength = gsm.getDbm();
    62.                } else if (info instanceof CellInfoCdma) {
    63.                    final CellSignalStrengthCdma cdma = ((CellInfoCdma) info).getCellSignalStrength();
    64.                    signalStrength = cdma.getDbm();
    65.                } else if (info instanceof CellInfoLte) {
    66.                    final CellSignalStrengthLte lte = ((CellInfoLte) info).getCellSignalStrength();
    67.                    signalStrength = lte.getDbm();
    68.                } else {
    69.                    throw new Exception("Unknown type of cell signal!");
    70.                }
    71.            }
    72.        } catch (Exception e) {
    73.            Log.e(TAG, "Unable to obtain cell signal information", e);
    74.            //throw new Exception("Unable to obtain cell signal information!");
    75.            PluginResult result = new PluginResult(PluginResult.Status.ERROR, "Unable to obtain cell signal information");
    76.            ctx.sendPluginResult(result);
    77.        }
    78.         
    79. 
    80.        // Just a heads up: It seems that some devices (looking at you Samsung) don't
    81.        // properly implement getAllCellInfo() and will return null.
    82. 
    83.        PluginResult result = new PluginResult(PluginResult.Status.OK, signalStrength);
    84.        ctx.sendPluginResult(result);
    85.    }
    86. 
    87.    private void getCarrierName(CallbackContext ctx) {
    88.        // cannot find symbol context
    89.        Context context = cordova.getActivity().getApplicationContext();
    90.        TelephonyManager manager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
    91.        carrierName = manager.getNetworkOperatorName();
    92. 
    93.        PluginResult result = new PluginResult(PluginResult.Status.OK, carrierName);
    94.        ctx.sendPluginResult(result);
    95.    }
    96.     
    97. 
    98.}


    Is there something essential that I'm missing?

    Thanks in advance,
    Jonathan Jenne


  2. Zdravko
    Admin
    Zdravko avatar
    657 posts

    Posted 19 Mar 2014 Link to this post

    Hi Jonathan,

    I am afraid the development of Cordova custom plugins is not in the scope of our support service.
    You can find more info about plugin development process using the provided plugin development guide and open an inquiry at some of the forums like StackOverflow for further clarifications regarding your issue.
    Thanks.

    Regards,
    Zdravko
    Telerik
     
    Icenium is now Telerik AppBuilder, and is part of the Telerik Platform. Share what you think about AppBuilder and Telerik Platform with us in our feedback portal, so we can become even better!
Back to Top