title: What is the best way to communicate with Bonjour?
body: <p>There is a nice method provided by Bonjour: <code>DNSSD.browse("_killerapp._tcp", this);</code>. As the first argument of this method I give type of service which potentially can be available in the network, and as the second argument I give a "callback object". The considered method "browse" for the services of the indicated type (first argument).</p>
  
  <p>During the browsing it can "find" and then "lose" a service. If service is found (lost) bonjour call serviceFound (serviceLost) method of the callback object. The serviceFound is called with some parameters of the found service. In more details:</p>
  
  <pre><code>serviceFound(DNSSDService browser, int flags, int ifIndex, String serviceName, String regType, String domain)
  </code></pre>
  
  <p>But to get the IP address and port of the service we need to do additional operation (people call it "to resolve a service"). This is logic is kind of strange to me. Why this information cannot be given by serviceFound? I mean why Bonjour cannot resolve the service automatically whenever it finds a service.</p>
  
  <p>Anyway, I just accept the given logic and try to use it. From the <code>serviceFound</code> I call <code>DNSSD.resolve(0, ifIndex, serviceName, regType, domain, this)</code>.</p>
  
  <p>As before I give a callback object to the <code>resolve</code> (the last argument). Unfortunately I need to use different classes to provide the callback objects for <code>browse</code> and <code>resolve</code>. The reason for that is that <code>browse</code> and <code>resolve</code> can call a <code>operationFailed</code> method of the callback object and, if I use callback objects from the same class I will not know which method is calling the <code>operationFailed</code> (<code>browse</code> or <code>resolve</code>).</p>
  
  <p>So, I create a separate class to instantiate a callback object for the <code>resolve</code> method. In this class I have a method called <code>serviceResolved</code> which is called by Bonjour with IP address and port of the resolved service:</p>
  
  <pre><code>serviceResolved(DNSSDService resolver, int flags, int ifIndex, String fullname, String hostname, int port, TXTRecord txtRecord)
  </code></pre>
  
  <p>I think that the IP address and port should be fields of the objects which perform browsing. So, in the <code>serviceResolved</code> I have IP and port and I want to set these values to the corresponding field of the instance which <code>browse</code> the service. But how can I do it? This instance is not given as an argument of the <code>serviceResolved</code> method. So, it is invisible!</p>
  
  <p>Moreover, I see that serviceResolved and serviceFound take, as a first argument, <code>DNSSDService resolver</code>. Does anybody know what is it? May be this object can be used to set parameters of the service? I know that an object of this type is returned by the <code>browse</code>.</p>