So, everybody knows that we make a Class extending CordovaPlugin
and override the execute()
and then creates a bridge between the JS and native Java (for Android). Further we use PluginResult
to return the result back to the JS.
So, all of this happens when there is a request fired from the JS to the Java Plugin. My question is, how to send a result back to JS (and therefore to HTML) asynchronously ?
I don't know if the word asynchronous is right here. The thing is I want to send something back to the JS out of the blue (say, when wifi becomes enable/disable).
I have already researched on this but haven't got anything which suits to my case.
The thing I've tried is -
- Created a
BroadcastReceiver
listening to theWiFi
events using theWifiManager
class. - Registered the receiver.
- And finally, popping a
Toast
whenWiFi
is enabled/disabled, and sending the result usingCallbackContext
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, "Wifi Connected"))
and for disconnected with a different message.
MyPlugin.java
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
...
public class MyPlugin extends CordovaPlugin {
private WifiReceiver wifiBroadcastReceiver = null;
private CallbackContext callbackContext = null;
...
public MyPlugin() {
wifiBroadcastReceiver = new WifiReceiver();
...
}
...
public boolean execute(String action, final JSONArray args,
final CallbackContext callbackId) throws JSONException {
IntentFilter wifiFilter = new IntentFilter(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
cordova.getActivity().registerReceiver(wifiBroadcastReceiver, wifiFilter);
this.callbackContext = callbackId;
...
}
public class WifiReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
if (intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)) {
Toast.makeText(cordova.getActivity(), "Wifi Connected", Toast.LENGTH_SHORT).show();
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, "Wifi Connected"));
} else {
Toast.makeText(cordova.getActivity(), "Wifi Disconnected", Toast.LENGTH_SHORT).show();
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "Wifi Disconnected"));
}
}
}
}
The Toast
pops but the PluginResult
isn't sent to the JS.
PS : Listening to WiFi events isn't my actual problem, I want to replicate the Android Bluetooth Chat
app in Phonegap. So, it has to be asynchronous in nature.
W/CordovaPlugin(6976): Result was: "Hello"`. So, now the question is, **How to handle these Second Callbacks** in JS or in HTML? – Anas Azeem Oct 07 '13 at 12:19