Aaron Turecki
Aaron Turecki

Reputation: 355

PhoneGap - return custom plugin callback value from method other than execute()

I have a custom PhoneGap plugin which imports a jar file. The jar file allows me to create a number of @Override functions and I need to return a value from one of these functions with the plugin. Therefore I cannot simply call callbackContext.success(value) in execute() to return my value.

I've tried to call callbackContext.success(value) in my @Override function but this doesn't work. It simply causes the application to crash.

Does anyone know how to pass a value in callbackContext in an @Override function? The functions are all in the same class. Do I need to start a new Intent/Activity?

Plugin Java Code:

package com.cordova.plugin.scan;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import android.app.Activity;
import android.app.AlertDialog;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Message;
import android.util.Log;
import android.content.Context;

import com.phychips.rcp.*;

public class Scan extends CordovaPlugin implements iRcpEvent2, OnCompletionListener {

public CallbackContext callbackContext;

@Override
public boolean execute (String action, JSONArray args, CallbackContext callbackContext) throws JSONException
{
        try {
            RcpApi2 rcpAPI = RcpApi2.getInstance();
            rcpAPI.setOnRcpEventListener(this);
            try {
                boolean t = rcpAPI.open();
                setVolumeMax();
                if (t = true) {
                    try {                   
                        boolean k = rcpAPI.startReadTagsWithRssi(maxTags,
                                maxTime, repeatCycle);
                        if (k = true) {     

                            return true;
                        }
                    } catch (final Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    return false;
                }
            } catch (final Exception e) {
                e.printStackTrace();
            }
        }
        catch (final Exception e) {
            e.printStackTrace();
        }
        return false;
}

@Override
public void onTagMemoryReceived(final int[] data) {
    // TODO Auto-generated method stub
    try {
        cordova.getActivity().runOnUiThread(new Runnable() {
            public void run(){
                String dataText = RcpLib.int2str(data);
                callbackContext.success(dataText);
            }
        });
    }
    catch (final Exception e) {
            e.printStackTrace();
    }
}
}

Upvotes: 0

Views: 1141

Answers (1)

Aaron Turecki
Aaron Turecki

Reputation: 355

Figured this out in case anyone wants to know...

Set CallbackContext callbackContext in your class. Set this.callbackContext in your exec() function. Use callbackContext.sendPluginResult(value) in your @Override function.

public class Scan extends CordovaPlugin{
    private CallbackContext callbackContext;

    @Override
    public boolean execute (String action, JSONArray args, CallbackContext callbackContext) throws JSONException
    {
        this.callbackContext = callbackContext;
    }

    @Override
    public void onTagMemoryReceived(final int[] data) {
        cordova.getActivity().runOnUiThread(new Runnable() {
            public void run(){
                String dataText = RcpLib.int2str(data);

                    PluginResult result = new PluginResult(PluginResult.Status.OK, dataText);
                    result.setKeepCallback(false);
                    callbackContext.sendPluginResult(result);
            }
        });
    }
}

Upvotes: 1

Related Questions