title: Failed NsdManager Registration
body: <p><strong>Problem:</strong> 
  <strong>Short Description:</strong>
  I'm attempting to register a service &amp; port visible to my PC for later connection to a PC application. This code does not register the service and I am unable to tell why, all I know is that when it runs on my phone through the android monitor I used a logger to tell me that the registration failed.</p>
  
  <p><strong>Longer Description:</strong> 
  I've been trying to build a simple app that will provide my PC with information from my Android Phone. I'm unfamiliar with network programming but from my research I've understood that if I use android's NsdManager I will be able to create a service running on my network for the PC application I am developing to connect to using "Socket Programming". I've been attempting to use NsdManager to register this service (if theirs a better way to start a service running on my network on my phone to be connected to by PC the info would be much appreciated.</p>
  
  <p><strong>Requested Help:</strong>
  It would be nice to know:
   1. What is the best way to begin developing an Android App that would "pair" with a PC app which I will develop later?
   2. Will the NsdManager method of registering the service work for this?
   3. Is there a way to catch the error code of why this service is unable to
      register?
   4. Any helpful resources, libraries, tips or tricks for beginning android network development? I am new to this subsection of
      development and looking for these kinds of things.</p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>import android.app.Activity;
      import android.content.Context;
      import android.net.nsd.NsdManager;
      import android.net.nsd.NsdServiceInfo;
      //import android.support.v7.appcompat.R;
      import android.os.Bundle;
      import android.util.Log;
  
  public class ServerActivity extends Activity {     //Possibly should be extends "AppCompatActivity"
  private String SERVICE_NAME = "meService";
  private String SERVICE_TYPE = "_http._tcp.";
  private String TAG = "ServerActivity";
  private NsdManager mNsdManager;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      setContentView(R.layout.activity_server);
  
      mMethod();
  
  }
  
  private void mMethod() {
      mNsdManager = (NsdManager) getSystemService(Context.NSD_SERVICE);
      registerService(9000);
  }
  
  @Override
  protected void onPause() {
      if (mNsdManager != null) {
          mNsdManager.unregisterService(mRegistrationListener);
      }
      super.onPause();
  }
  
  @Override
  protected void onResume() {
      super.onResume();
      if (mNsdManager != null) {
          registerService(9000);
      }
  
  }
  
  @Override
  protected void onDestroy() {
      if (mNsdManager != null) {
          mNsdManager.unregisterService(mRegistrationListener);
      }
      super.onDestroy();
  }
  
  public void registerService(int port) {
      NsdServiceInfo serviceInfo = new NsdServiceInfo();
      serviceInfo.setServiceName(SERVICE_NAME);
      serviceInfo.setServiceType(SERVICE_TYPE);
      serviceInfo.setPort(port);
  
      mNsdManager.registerService(serviceInfo,
              NsdManager.PROTOCOL_DNS_SD,
              mRegistrationListener);
  }
  
  NsdManager.RegistrationListener mRegistrationListener = new NsdManager.RegistrationListener() {
  
      @Override
      public void onServiceRegistered(NsdServiceInfo NsdServiceInfo) {
          String mServiceName = NsdServiceInfo.getServiceName();
          SERVICE_NAME = mServiceName;
          Log.d(TAG, "Registered name : " + mServiceName);
      }
  
      @Override
      public void onRegistrationFailed(NsdServiceInfo serviceInfo,
                                       int errorCode) {
          Log.d(TAG, "Registration failed: " + errorCode);
          // Registration failed! Put debugging code here to determine
          // why.
      }
  
      @Override
      public void onServiceUnregistered(NsdServiceInfo serviceInfo) {
          // Service has been unregistered. This only happens when you
          // call
          // NsdManager.unregisterService() and pass in this listener.
          Log.d(TAG,
                  "Service Unregistered : " + serviceInfo.getServiceName());
      }
  
      @Override
      public void onUnregistrationFailed(NsdServiceInfo serviceInfo,
                                         int errorCode) {
          // Unregistration failed. Put debugging code here to determine
          // why.
      }
  
  
     };
  
  }
  </code></pre>
  
  <p><strong>Thanks so much for your time and help!</strong></p>