Michael Williams
Michael Williams

Reputation: 391

Send interim results from PhoneGap Plugin to JavaScript code

I'm writing a PhoneGap application that will be communicating with a custom hardware device. This will be done through a native library supplied by a third party.

To call the native library I'll write a PhoneGap Plugin. I'm not sure how to send interim results back to my JavaScript code from my Android code though. Is there a way to do this (for example through cordova.getActivity()) or will I need to fudge this with some kind of polling from the JavaScript side?

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    //Start long running process
    //Send feedback to PhoneGap Javascript code
    //Do more stuff
    callbackContext.success("Final result");
    return true;
}

Upvotes: 1

Views: 981

Answers (1)

Michael Williams
Michael Williams

Reputation: 391

By using a PluginResult and setting KeepCallback to true, it's possible to send multiple results to your success JavaScript callback.

PluginResult progressResult = new PluginResult(PluginResult.Status.OK, "Interim 1");
progressResult.setKeepCallback(true);
callbackContext.sendPluginResult(progressResult);

Upvotes: 2

Related Questions