PhoneGap setKeepCallback - What is it?

I did a project where I made a successful connection with my bluetooth plugin to my javascript. From my Javascript, I register my plugin callback this way in java:

if (action.equals(ACTION_REGISTER_CALLBACK)) { if(mJSCallback != null) { pluginResult = new PluginResult(PluginResult.Status.ERROR, "An event callback has already been registered."); } else { mJSCallback = callbackId; pluginResult = new PluginResult(PluginResult.Status.NO_RESULT); pluginResult.setKeepCallback(true); } } 

this, of course, is done in the exec function. due to the fact that bluetooth events do not arrive at certain times, I registered a broadcast receiver that sends messages to my javascript when, for example, a device is found, for example.

 if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { sendMessagetoJSCallback(new PluginResult(PluginResult.Status.OK, "Discovery finished.")); } 

Accessory function for sending:

 public void sendMessagetoJSCallback(PluginResult res) { res.setKeepCallback(true); success(res, mJSCallback); } 

In this context, I really don't understand what setKeepCallback does in these different functions. I thought there would be documentation, but not there.

Can anyone tell me? I oriented the development to https://github.com/phonegap/phonegap-plugins/tree/master/Android/PhoneListener

+8
cordova phonegap-plugins
source share
1 answer

This means that the callback on the JS side is retained for further calls from the native (Java) side to the JS side. It is managed somewhere in the cordova.js code.

If you look at an example on the Network-Information plugin: https://github.com/apache/cordova-plugin-network-information/blob/master/src/android/NetworkManager.java#L221 you see that in every state of the network ( WIFI, 3G, 4G, offline, ...) the plugin sends the result to JS and with the callback saved, each call is received in the same callback with success on the JS website.

+3
source share

All Articles