title: Bonjour implementation on Android
body: <p>I am trying to implement bonjour/zero conf on my android app. I am using jmDns library for searching the all the available devices. Here is the code that I am using for searching the devices in the same network:</p>
  
  <pre><code>public class ListDevices extends ListActivity {
      JmDNS jmdns;
      JmDNSImpl impl;
      MulticastLock lock;
      protected ServiceListener listener;
      protected ServiceInfo info;
      public ListView lv;
      public ArrayList&lt;String&gt; deviceList;
      public int cancel = 0;
      public final static String TAG = "ListDevices";
  
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          deviceList = new ArrayList&lt;String&gt;();
          showAllPrinters();
  
          setListAdapter(new ArrayAdapter&lt;String&gt;(this,
                  android.R.layout.simple_list_item_1, deviceList));
  
          lv = getListView();
          lv.setTextFilterEnabled(true);
  
          lv.setOnItemClickListener(new OnItemClickListener() {
              public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                      int position, long id) {
                  // When clicked, show a toast with the TextView text
                  Toast.makeText(getApplicationContext(),
                         ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
              }
          });
          this.listener = new ServiceListener() {
              public void serviceAdded(ServiceEvent event) {
                  deviceList.add("Service added   : " + event.getName() + "."
                          + event.getType());
                  Log.v(TAG, "Service added   : " + event.getName() + "."
                          + event.getType());
              }
  
              public void serviceRemoved(ServiceEvent event) {
                  deviceList.add("Service removed : " + event.getName() + "."
                          + event.getType());
                  Log.v(TAG, "Service removed : " + event.getName() + "."
                          + event.getType());
              }
  
              public void serviceResolved(ServiceEvent event) {
                  deviceList.add("Service resolved: " + event.getInfo());
                  Log.v(TAG, "Service resolved: " + event.getInfo());
              }
          };
      }
  
      public void showAllPrinters() {
          Log.d("ListDevices", "in showAllPrinters");
          try {
  
              WifiManager wifi = (WifiManager)
                                 getSystemService(Context.WIFI_SERVICE);
              lock = wifi.createMulticastLock("fliing_lock");
              lock.setReferenceCounted(true);
              lock.acquire();
  
              InetAddress inetAddress = getLocalIpAddress();
              jmdns = JmDNS.create(inetAddress, "TEST");
  
              ServiceInfo[] infos = jmdns.list("_http._tcp.local.");
  
              if (infos != null &amp;&amp; infos.length &gt; 0) {
                  for (int i = 0; i &lt; infos.length; i++) {
                      deviceList.add(infos[i].getName());
                  }
              } else {
                  deviceList.add("No device found.");
              }
              impl = (JmDNSImpl) jmdns;
  
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
      public InetAddress getLocalIpAddress() {
          try {
              for (Enumeration&lt;NetworkInterface&gt; en = NetworkInterface
                      .getNetworkInterfaces(); en.hasMoreElements();) {
                  NetworkInterface intf = (NetworkInterface) en.nextElement();
                  for (Enumeration&lt;InetAddress&gt; enumIpAddr = intf
                          .getInetAddresses(); enumIpAddr.hasMoreElements();) {
                      InetAddress inetAddress = (InetAddress) enumIpAddr
                              .nextElement();
                      if (!inetAddress.isLoopbackAddress()) {
                          return inetAddress;
                      }
                  }
              }
          } catch (SocketException ex) {
              Log.e("ListDevices", ex.toString());
          }
          return null;
      }
  
      @Override
      protected void onPause() {
          super.onPause();
          if (isFinishing()) {
              lock.release();
          }
      }
  }
  </code></pre>
  
  <p>Basically, I am adding them in a list so that I can display a list of all available devices. Now when I am running this code, I am getting no exception/nothing like error. But on the other hand, nothing is added to my list [PS: there atleast 5-6 PCs and Macs are there in the network.</p>
  
  <p>I also tried to get the list from this code:</p>
  
  <pre><code>jmdns.addServiceListener("_http._tcp.local.", listener);
  </code></pre>
  
  <p><code>listener</code> is defined in the <code>onCreate</code> of the activity. But this also did not returned any device.</p>
  
  <p>Please help, suggest what I am doing wrong here. Any help is appreciated!</p>