title: Asynchronous communication between Javascript and Phonegap Plugin
body: <p>So, everybody knows that we make a Class extending <code>CordovaPlugin</code> and override the  <code>execute()</code> and then creates a bridge between the JS and native Java (for Android). Further we use <code>PluginResult</code> to return the result back to the JS.
  <p>So, all of this happens when there is a request fired from the JS to the Java Plugin. My question is, <strong>how to send a result back to JS</strong> (and therefore to HTML) <strong>asynchronously ?</strong> <p>I don't know if the word <strong>asynchronous</strong> 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).
  <p>I have already researched on this but haven't got anything which suits to my case.
  <p>
  The thing I've tried is -</p>
  
  <ul>
  <li> Created a <Code>BroadcastReceiver</code> listening to the <code>WiFi</code> events using the <code>WifiManager</code> class.
  <li> Registered the receiver.
  <li> And finally, popping a <code>Toast</code> when <code>WiFi</code> is enabled/disabled, and sending the result using  <code>CallbackContext</code><br><br><code>callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, "Wifi 
  Connected"))</code> and for disconnected with a different message.<br/><br> </ul>
  
  <p><p><strong>MyPlugin.java</strong><br></p>
  
  <pre><code>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"));
                  }
              }           
          }
  
  }
  </code></pre>
  
  <p><p>The <code>Toast</code> pops but the <code>PluginResult</code> isn't sent to the JS.</p>
  
  <hr>
  
  <p><strong>PS :</strong> Listening to WiFi events isn't my actual problem, I want to replicate the <code>Android Bluetooth Chat</code> app in Phonegap. So, it has to be asynchronous in nature.</p>