0

Problem: Short Description: I'm attempting to register a service & 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.

Longer Description: 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.

Requested Help: 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.

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.
    }


   };

}

Thanks so much for your time and help!

Eweb
  • 71
  • 1
  • 12
  • I do not remember how `NSD` acts when register the service twice, cause this is what you do. First you call `registerService(9000)` in `onCreate`, then in `onResume()`. This could be the reason. Anyway, what logcat says? – Onik Jan 23 '16 at 23:02
  • I removed the second call to register the service and it still tells me that it fails through the Log.d function. It gives me "Registration failed" with error code "0" in Logcat the NsdServiceInfo object in the onRegistrationFailed method. – Eweb Feb 03 '16 at 19:49
  • Which one did you remove, `onCreate()`'s or`onResume()`'s one? Because if you left the one that's in `onResume()` it may be called multiple times with respect to `ServerActivity` lifecycle. Also make sure you're using INTERNET permission for the app. – Onik Feb 03 '16 at 19:56
  • I removed the one that is in the onResume method, the app does have the INTERNET permission declared. – Eweb Feb 04 '16 at 21:27
  • Well...everything seems fine. Try another port and...there is a typo in the docs so as in your code, namely the signature of the `onServiceRegistered(NsdServiceInfo NsdServiceInfo)`. In fact, it shouldn't compile... Anyway, use `onServiceRegistered(NsdServiceInfo serviceInfo)` instead. And browse the sources on what the error code 0 means. – Onik Feb 04 '16 at 21:51
  • @Eweb I tried your code. It works for me. Can you post the logs? – Zaartha Jun 29 '16 at 11:26

0 Answers0