i made a project, where i established a successfull communication from my bluetooth plugin to my javascript. From my Javascript i register a callback to my plugin that 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 is of course done in the exec function. because of the fact, that bluetooth events come not at certain points of time, i registered a broadcast receiver, that sends messages to my javascript, when theres is found an device for example.
if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
sendMessagetoJSCallback(new PluginResult(PluginResult.Status.OK, "Discovery finished."));
}
The belonging function for sending:
public void sendMessagetoJSCallback(PluginResult res) {
res.setKeepCallback(true);
success(res, mJSCallback);
}
What i did not really understand in that context is, what setKeepCallback does in those different functions. I thought there would be a documentation, but there isnt.
Can someone tell me? I orientated the development on https://github.com/phonegap/phonegap-plugins/tree/master/Android/PhoneListener
This means that the callback on JS side is kept for further calls from native (Java) side to JS side. It is somewhere managed in the cordova.js code.
If you look for example to the Network-Information plugin: https://github.com/apache/cordova-plugin-network-information/blob/master/src/android/NetworkManager.java#L221 you see that on every network state (WIFI,3G,4G,offline,...) the plugin send the result to JS and with keeping the callback every call is received in the same success callback in JS site.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With