0

I am working on the mobile app using ionic angular cordova. I created a listener in android cordova plugin. The listener will listen to a gateway then do something every time gateway sends something to it.

Now i have a function in the ionic app to initialize the listener in cordova plugin. However, how can i get the response from the listener in the cordova plug constantly?

zifan yan
  • 125
  • 2
  • 11

1 Answers1

0

refer to this websites:

https://ourcodeworld.com/articles/read/33/how-to-execute-a-cordova-callback-more-than-once-in-java-android-

how to send data or message from java application / plugin to javascript with cordova / phonegap

public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
   // Execute an asynchronous task
   cordova.getThreadPool().execute(new Runnable() {
        public void run() {
            // Then you're allowed to execute more than twice a callback.
            PluginResult resultA = new PluginResult(PluginResult.Status.OK, "myfirstJSONResponse");
            resultA.setKeepCallback(true);
            callbacks.sendPluginResult(resultA);

            // Some more code

            Boolean something = true;

            // bla bla bla code


            PluginResult resultB = new PluginResult(PluginResult.Status.OK, "secondJSONResponse");
            resultB.setKeepCallback(true);
            callbacks.sendPluginResult(resultB);    
          // you can do multiple callbacks here
        }
   });
zifan yan
  • 125
  • 2
  • 11