becvert / cordova-plugin-zeroconf

Cordova ZeroConf Plugin
MIT License
81 stars 57 forks source link

[Android] zeroconf.watch event with service without ipv4Addresses #49

Closed EnricoGiordano1992 closed 5 years ago

EnricoGiordano1992 commented 6 years ago

Hi becvert, I've a problem using your plugin with Android device. In particular, I receive, on "watch", event without ipv4Addresses; at first call, I can simply check if service does not contain ipv4Addresses (because I receive duplicate service, one with ipv4Addresses, one without ipv4Addresses). When I go out and return in a app page that use zeroconf, it returns only service without ipv4Addresses. How can I resolve this? This is my code:

  ionViewDidLoad(){
    var self = this;
    this.platform.ready().then(() =>{
      if((<any>window).cordova && (<any>window).cordova.plugins){
        var zeroconf = (<any>window).cordova.plugins.zeroconf;
        zeroconf.registerAddressFamily = 'ipv4'; // or 'ipv6' ('any' by default)
        zeroconf.watchAddressFamily = 'ipv4'; // or 'ipv6' ('any' by default)
        zeroconf.watch('_myservice._tcp.', 'local.',
          function(result:any) {
            var exists_name = false;
            for(var i = 0; i < self.List.length; i++){
              if(self.List[i].name == result.service.name)
                exists_name = true;
            }
            if(!exists_name && result.service.ipv4Addresses.length > 0){
              self.List.push(result.service);
              self.zone.run(() => { }); // update ngFor externally
            }
          },
          function(err:any){
              console.log("err: "+err)
            }
          );
        }
    })

      var self = this;
      var foo_timeout2 = true;
      setTimeout(
        function(){
          if(foo_timeout2){
            self.timeElapsed = true;
          }
      },10000);
  }

I'm using Ionic (3.13.0 but same problem with previous versions), tested on Samsung Galaxy S4 (Android 5.0.1) and Wiko Fever (Android 6.0). Thanks for help!

becvert commented 6 years ago

check that result.action == 'resolved'?

EnricoGiordano1992 commented 6 years ago

At first time, when plugin find a device, it gives me action == "resolved", but second time that I use it, it gives me only action == "added".

becvert commented 6 years ago

by second time does it mean you 'paused' the app and then you resumed it? or you left if for good and then reopened it? does ionViewDidLoad is call again on resume?

EnricoGiordano1992 commented 6 years ago

No, I mean that I change page the I return in the page that I call zeroconf plugin. Yes, ionViewDidLoad is called again.

antweb commented 6 years ago

Just ran into the same problem. action == "resolved" is only returned on the first watch attempt. Any consecutive watch just returns action == "added". zeroconf.unwatch() does not seem to help. But after zeroconf.stop() I once again receive another action == "resolved".

(Android 7.1.2 / cordova-android 6.3.0)

emcniece commented 6 years ago

We recently introduced a reInit() method to help with situations where watches may potentially be duplicated. This method reverts the plugin almost completely to its initial state. Any chance you could try this and report the outcome?

shader commented 6 years ago

I'm running into this problem, I think.

reInit() doesn't seem to help, and sometimes even reloading the app doesn't seem to change anything either... In fact, it seems that if I start my service and register it before launching the app, I only receive the "added" events without the IP addresses.

Interestingly, I only had this problem when running on the ad-hoc wifi of my android device; when both the device and my service are running on a local wifi network it seems to work just fine.

emcniece commented 6 years ago

@shader can you cast some light on your network topologies for both working and non-working scenarios? I'd ask if you can discover your services with a third-party Bonjour browser tool, but it sounds like you can as "running on a local wifi network" works fine. I might suggest ad-hoc-ing your phone to a computer and running the Bonjour browser tool to test.

@becvert I wonder if the OS is having trouble resolving IP addresses when using a particular network adapter configuration. Could this be caused by lack or presence of DHCP?

becvert commented 6 years ago

@emcniece I have no idea :confused:

becvert commented 5 years ago

Closing old issue. Feel free to reopen.