network.onConnect() on Android?


#1

I’m using the following code to watch for reconnect after disconnect.

 this.network.onConnect().subscribe(() => {
            this.error = [];
            this.getNewsPosts();
        });

In the browser on desktop this works. But if i run the app on android it doesn’t? Any idea why? Or is that actually the expected behaviour for Android? On the ios emulator it also works (with a little delay).


#2

You will need to wait a few seconds before the connection is fully initialized and ready. From the plugin doc:

// watch network for a connection
let connectSubscription = this.network.onConnect().subscribe(() => {
  console.log('network connected!');
  // We just got a connection but we need to wait briefly
   // before we determine the connection type. Might need to wait.
  // prior to doing any api requests as well.
  setTimeout(() => {
    if (this.network.type === 'wifi') {
      console.log('we got a wifi connection, woohoo!');
    }
  }, 3000);
});

#3

I tried that and it doesn’t make any difference sadly. On the iOS emulator the code in my first posts also works. Just not on Android sadly.