title: Cannot detect service using Network Service Discovery (NSD) on Android
body: <p>I'm a newbie trying to use NSD in an application I'm building to find peers running the same service on the same wifi network and connect to the it. </p>
  
  <p>[Refer to code] I have one device running <strong>HostActivity.java</strong> and one running <strong>GuestActivity.java</strong>. Clicking the floating button in Host registers a service on port 9000 ( preset for now ) and clicking it in Guest starts running a service discovery for the same. Both are connected to the same wifi network. </p>
  
  <p>In my logcat, I observe that registration from <strong>HostActivity.java</strong> works fine - I get the requisite log messages from <strong>NsdHelper.java</strong>. However, running <strong>GuestActivity.java</strong> just logs 'Service Discovery started' and never ends up resolving to the service that exists on the network. </p>
  
  <p>What am I doing wrong? Please help.</p>
  
  <p>Relevant code - </p>
  
  <p><strong>HostActivity.java</strong></p>
  
  <pre><code>NsdHelper mNsdHelper;
  
  public static final String TAG = "ABC";
  private final int ABC_PORT = 9000;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_shared_list);
  
  
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
  
  
      mNsdHelper = new NsdHelper(this);
  
      FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
      fab.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Snackbar.make(view, "Started Registration", Snackbar.LENGTH_LONG)
                      .setAction("CLOSE", null).show();
              mNsdHelper.initializeNsd();
              mNsdHelper.registerService(ABC_PORT);
              Snackbar.make(view, "Completed Registration", Snackbar.LENGTH_LONG)
                      .setAction("Action", null).show();
  
          }
      });
  }
  
  @Override
  protected void onPause() {
      super.onPause();
  }
  
  @Override
  protected void onResume() {
      super.onResume();
      if (mNsdHelper != null) {
          mNsdHelper.initializeNsd();
          mNsdHelper.registerService(ABC_PORT);
      }
  }
  
  @Override
  protected void onDestroy() {
      mNsdHelper.tearDown();
      super.onDestroy();
  }
  </code></pre>
  
  <p><strong>GuestActivity.java</strong></p>
  
  <pre><code>NsdHelper mNsdHelper;
  
  public static final String TAG = "ABC";
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_shared_list);
  
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
  
      mNsdHelper = new NsdHelper(this);
  
      FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
      fab.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Snackbar.make(view, "Searching for available services", Snackbar.LENGTH_LONG)
                      .setAction("Action", null).show();
              mNsdHelper.initializeNsd();
              mNsdHelper.discoverServices();
          }
      });
  }
  
  @Override
  protected void onPause() {
      super.onPause();
  }
  
  @Override
  protected void onResume() {
      super.onResume();
      if(mNsdHelper != null) {
          mNsdHelper.initializeNsd();
          mNsdHelper.discoverServices();
      }
  }
  
  @Override
  protected void onDestroy() {
      mNsdHelper.tearDown();
      super.onDestroy();
  }
  </code></pre>
  
  <p><strong>NsdHelper.java</strong> - This contains all the NSD implementation details</p>
  
  <pre><code>public class NsdHelper {
      Context mContext;
  
      NsdManager mNsdManager;
      NsdManager.ResolveListener mResolveListener;
      NsdManager.DiscoveryListener mDiscoveryListener;
      NsdManager.RegistrationListener mRegistrationListener;
  
      private static final String SERVICE_TYPE = "_http._tcp.";
  
      private static final String TAG = "NsdHelper";
      private String mServiceName = "ABC";
  
      // First step : register a NsdServiceInfo object to advertise your service
      NsdServiceInfo mService;
  
      public NsdHelper(Context context) {
      mContext = context;
      mNsdManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE);
  }
  
  
  public void initializeNsd() {
      initializeRegistrationListener();
      initializeDiscoveryListener();
      initializeResolveListener();
  
  }
  
  
  // Check success of service registration
  public void initializeRegistrationListener() {
      mRegistrationListener = new NsdManager.RegistrationListener() {
  
          @Override
          public void onServiceRegistered(NsdServiceInfo nsdServiceInfo) {
              mServiceName = nsdServiceInfo.getServiceName();
              Log.d(TAG, "Registered name : " + mServiceName);
          }
  
          @Override
          public void onRegistrationFailed(NsdServiceInfo nsdServiceInfo, int arg1) {
              Log.d(TAG, "Registration Failed");
          }
  
          @Override
          public void onServiceUnregistered(NsdServiceInfo nsdServiceInfo) {
              Log.d(TAG, "Unregistered");
          }
  
          @Override
          public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
          }
  
      };
  }
  
  public void registerService(int port) {
      NsdServiceInfo serviceInfo  = new NsdServiceInfo();
      serviceInfo.setPort(port);
      serviceInfo.setServiceName(mServiceName);
      serviceInfo.setServiceType(SERVICE_TYPE);
  
      mNsdManager.registerService(
              serviceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);
  
  }
  
  // To discover services of the type you're looking for on the network
  public void initializeDiscoveryListener() {
      mDiscoveryListener = new NsdManager.DiscoveryListener() {
  
          @Override
          public void onDiscoveryStarted(String regType) {
              Log.d(TAG, "Service discovery started");
          }
  
          @Override
          public void onServiceFound(NsdServiceInfo service) {
              Log.d(TAG, "Service discovery success : " + service);
              Log.d(TAG, "Host = "+ service.getServiceName());
              Log.d(TAG, "port = " + String.valueOf(service.getPort()));
  
              if (!service.getServiceType().equals(SERVICE_TYPE)) {
                  Log.d(TAG, "Unknown Service Type: " + service.getServiceType());
              } else if (service.getServiceName().equals(mServiceName)) {
                  Log.d(TAG, "Same machine: " + mServiceName);
              } else if (service.getServiceName().contains(mServiceName)){
                  mNsdManager.resolveService(service, mResolveListener);
              }
          }
  
          @Override
          public void onServiceLost(NsdServiceInfo service) {
              Log.e(TAG, "service lost" + service);
              if (mService == service) {
                  mService = null;
                  Log.e(TAG, "service lost" + service);
              }
          }
  
          @Override
          public void onDiscoveryStopped(String serviceType) {
              Log.i(TAG, "Discovery stopped: " + serviceType);
          }
  
          @Override
          public void onStartDiscoveryFailed(String serviceType, int errorCode) {
              Log.e(TAG, "Discovery failed: Error code:" + errorCode);
              mNsdManager.stopServiceDiscovery(this);
          }
  
          @Override
          public void onStopDiscoveryFailed(String serviceType, int errorCode) {
              Log.e(TAG, "Discovery failed: Error code:" + errorCode);
              mNsdManager.stopServiceDiscovery(this);
          }
      };
  }
  
  public void discoverServices() {
      mNsdManager.discoverServices(
              SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
  }
  
  
  // Connection handshake
  public void initializeResolveListener() {
      mResolveListener = new NsdManager.ResolveListener() {
  
          @Override
          public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
              Log.e(TAG, "Resolve failed" + errorCode);
          }
  
          @Override
          public void onServiceResolved(NsdServiceInfo serviceInfo) {
              Log.e(TAG, "Resolve Succeeded. " + serviceInfo);
  
              if (serviceInfo.getServiceName().equals(mServiceName)) {
                  Log.d(TAG, "Same IP.");
                  return;
              }
              mService = serviceInfo;
              int port = mService.getPort();
              InetAddress host = mService.getHost();
          }
      };
  }
  
  public void stopDiscovery() {
      if (mNsdManager != null) {
          mNsdManager.stopServiceDiscovery(mDiscoveryListener);
      }
  }
  
  public NsdServiceInfo getChosenServiceInfo() {
      return mService;
  }
  
  public void tearDown() {
      if (mNsdManager != null) {
          if (mRegistrationListener != null) {
              mNsdManager.unregisterService(mRegistrationListener);
          }
          if (mDiscoveryListener != null) {
              mNsdManager.stopServiceDiscovery(mDiscoveryListener);
          }
      }
  }
  </code></pre>
  
  <p>}</p>