androidzeroconfmdnsdns-sd

Unregistering a zeroconf service using jmDNS


I'm tryng to make my Android app register a service via zeroconf while the app is active, and then unregister it after the app exits. In my onResume method I have the following code:

    // register ourselves with zero conf
    try {
        JmDNS jmdns = JmDNS.create();

        serviceInfo = ServiceInfo.create("_http._tcp.local.", "My App Service", SERVER_PORT, 0, 0, false, "path=/crazycatapp");
        jmdns.registerService(serviceInfo);
    } catch (Throwable t) {
        Debug.e("Error registering service with zeroconf", t);
    }

and then subsequently in my onPause method:

    if(serviceInfo != null) {

            // unregister ourselves with zero conf
            JmDNS jmdns = JmDNS.create();
            jmdns.unregisterService(serviceInfo);
        }
    } catch (Throwable t) {
        Debug.e("Error unregistering service with zeroconf", t);
    }

I've elimintated the code that does multicast enabling, etc. The point is, I can register the service just fine, and can subsequently see it using a zeroconf browser on another device. However, once registered, the service never disappears unless I actually disable the wifi on the device that registered the service. The call to unregisterService doesn't seem to actually affect a service registration. unregisterAllServices doesn't seem to have any effect either. Anyone have any ideas?


Solution

  • From looking at the relevant code in JmDNS I think you should unregister your ServiceInfo from the same JmDNS instance you registered it with. So you'll have to keep the jmdns reference from your registration snippet around and call unregister on that one, instead of the new instance you create in your unregister snippet.