title: NsdManager service not found after registered android
body: <p>I recently started working on Network Service Discovery which is provided by Android.</p>
  
  <p>I made two applications connected on the same network. One will register a service with this class :</p>
  
  <pre><code>public class NsdRegister {
  
  private Context                             mContext;
  
  private NsdManager                          mNsdManager;
  private NsdManager.RegistrationListener     mRegistrationListener;
  
  public String                               mServiceName = "NsdServer1";
  public static final String                  SERVICE_TYPE = "_http._tcp.";
  
  public NsdHelper(Context context) {
      mContext = context;
      mNsdManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE);
  }
  
  
  public void initializeRegistrationListener() {
      mRegistrationListener = new NsdManager.RegistrationListener() {
  
          @Override
          public void onServiceRegistered(NsdServiceInfo NsdServiceInfo) {
              mServiceName = NsdServiceInfo.getServiceName();
              System.out.println(mServiceName);
          }
  
          @Override
          public void onRegistrationFailed(NsdServiceInfo arg0, int arg1) {
              System.out.println(arg0.toString());
              System.out.println("Failed " + arg1);
          }
  
          @Override
          public void onServiceUnregistered(NsdServiceInfo arg0) {
          }
  
          @Override
          public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
          }
      };
  }
  
  public void registerService() {
      initializeRegistrationListener();
      NsdServiceInfo serviceInfo  = new NsdServiceInfo();
      serviceInfo.setPort(5906);
      serviceInfo.setServiceName(mServiceName);
      serviceInfo.setServiceType(SERVICE_TYPE);
      mNsdManager.registerService(serviceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);
  }
  
  public void tearDown() {
      mNsdManager.unregisterService(mRegistrationListener);
  }
  }
  </code></pre>
  
  <p>The other application will discover all services on the network with this class :</p>
  
  <pre><code>public class NsdHelper {
  
  public static final String                  SERVICE_TYPE = "_http._tcp.";
  public static final String                  TAG = "NsdHelper";
  public static final String                  SERVER_AIO = "NsdServer1";
  
  private Context                             mContext;
  
  private NsdManager                          mNsdManager;
  private NsdManager.DiscoveryListener        mDiscoveryListener;
  
  private NsdServiceInfo                      mService;
  
  public NsdHelper(Context context) {
      mContext = context;
      mNsdManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE);
  }
  
  private void initializeDiscoveryListener() {
      mDiscoveryListener = new NsdManager.DiscoveryListener() {
  
          @Override
          public void onDiscoveryStarted(String regType) {
              Log.d(TAG, "Service discovery started");
          }
  
          @Override
          public void onServiceFound(NsdServiceInfo service) {
              if (service.getServiceName().equals(SERVER_AIO)) {
                  mNsdManager.resolveService(service, new NsdManager.ResolveListener() {
                      @Override
                      public void onResolveFailed(NsdServiceInfo nsdServiceInfo, int i) {
                          System.out.println("Failed to resolve");
                      }
  
                      @Override
                      public void onServiceResolved(NsdServiceInfo nsdServiceInfo) {
                          stopDiscovery();
                          System.out.println(nsdServiceInfo.getHost().getHostAddress());
                      }
                  });
              }
          }
  
          @Override
          public void onServiceLost(NsdServiceInfo service) {
              Log.e(TAG, "service lost" + service);
              if (mService == service) {
                  mService = null;
              }
          }
  
          @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() {
      initializeDiscoveryListener();
      mNsdManager.discoverServices(SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
  }
  
  public void stopDiscovery() {
      mNsdManager.stopServiceDiscovery(mDiscoveryListener);
  }
  }
  </code></pre>
  
  <p>The register goes fine but the discovery does not find my service. I'm connected on the same network for the two apps.</p>
  
  <p>I don't know where is the problem.</p>
  
  <p>Please help me,</p>
  
  <p>Thank you</p>