1

The result of the registration shouldn't be empty, this is what I get from logcat and the callback of successful registration.

registerService 46518
onServiceRegistered name: mytest, type: null, host: null, port: 0, txtRecord:    

Everything is empty, the port 46518 which was generated by the system, the type, the txtrecord.

The following code is from the official guide

private String mServiceName = "mytest";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try {
        // Initialize a server socket on the next available port.
        ServerSocket mServerSocket = new ServerSocket(0);
        int mLocalPort = mServerSocket.getLocalPort();
        registerService(mLocalPort);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void registerService(int port) {
    Log.i(tag, "registerService " + port);
    // Create the NsdServiceInfo object, and populate it.
    NsdServiceInfo serviceInfo = new NsdServiceInfo();
    // The name is subject to change based on conflicts with other services advertised on the same network.
    serviceInfo.setServiceName(mServiceName);
    serviceInfo.setServiceType("_mytest._tcp");
    serviceInfo.setPort(port);
    serviceInfo.setAttribute("info", android.os.Build.MODEL);

    NsdManager mNsdManager = (NsdManager) getSystemService(Context.NSD_SERVICE);
    mNsdManager.registerService(serviceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);
}

NsdManager.RegistrationListener mRegistrationListener = new NsdManager.RegistrationListener() {

    @Override
    public void onServiceRegistered(NsdServiceInfo nsdServiceInfo) {
        // Save the service name.  Android may have changed it in order to
        // resolve a conflict, so update the name you initially requested
        // with the name Android actually used.
        mServiceName = nsdServiceInfo.getServiceName();
        Log.i(tag, "onServiceRegistered " + nsdServiceInfo);
    }

    @Override
    public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
        // Registration failed!  Put debugging code here to determine why.
        Log.i(tag, "onRegistrationFailed code " + errorCode + "\n" + serviceInfo);
    }

    @Override
    public void onServiceUnregistered(NsdServiceInfo arg0) {
        // Service has been unregistered.  This only happens when you call
        // NsdManager.unregisterService() and pass in this listener.
        Log.i(tag, "onServiceUnregistered " + arg0);
    }

    @Override
    public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
        // Unregistration failed.  Put debugging code here to determine why.
        Log.i(tag, "onRegistrationFailed code " + errorCode + "\n" + serviceInfo);
    }
};
user3290180
  • 4,260
  • 9
  • 42
  • 77

1 Answers1

1

Got the same on my LG G5 (Android 7.0)

Checked if the MDNS-Packets show up in Wireshark and they did! It seems that the service is announced with the right ip and port, even if the NsdServiceInfo in onServiceRegistered() says something different.

roplacebo
  • 2,837
  • 2
  • 17
  • 19