Host is NULL in NsdServiceInfo of NsdManager.DiscoveryListener.onServiceFound

I am trying to get mHost from the NsdServiceInfo passed as a parameter in NsdManager.DiscoveryListener.onServiceFound (), but this value is null. I have two Android devices, where device 1 is the server and device 2 is the client.

This is how I register the server in device 1

public void registerService(int port, InetAddress myIp) { NsdServiceInfo serviceInfo = new NsdServiceInfo(); serviceInfo.setPort(port); serviceInfo.setServiceName(this.serviceName); serviceInfo.setServiceType(SERVICE_TYPE); serviceInfo.setHost(myIp); this.nsdManager.registerService( serviceInfo, NsdManager.PROTOCOL_DNS_SD, registrationListener); } 

And this is how I initialize the DiscoveryListener

 public void initializeDiscoveryListener() { discoveryListener = new NsdManager.DiscoveryListener() { @Override public void onServiceFound(NsdServiceInfo service) { Log.d(TAG, "Service discovery success" + service); if (!service.getServiceType().equals(SERVICE_TYPE)) { Log.d(TAG, "Unknown Service Type: " + service.getServiceType()); } else if (service.getHost() == myIp) { Log.d(TAG, "Same machine: " + service.getHost()); } else if (service.getServiceName().contains(serviceName)){ nsdManager.resolveService(service, resolveListener); } } ... } } 

But service.getHost () returns null.
Any suggestion?

+6
source share
1 answer

I just ran into this problem and managed to solve it with a little help on the Google page when a network was discovered.

http://developer.android.com/training/connect-devices-wirelessly/nsd.html

The problem is that the connection information is unknown when the service is discovered. You must solve this first before getHost () will work.

You already have a line:

  nsdManager.resolveService(service, resolveListener); 

The resolListener variable contains callbacks for success and failure. You want to use getHost () when the connection information has been successfully determined. Here's the solution listener from Google:

  public void initializeResolveListener() { resolveListener = new NsdManager.ResolveListener() { @Override public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) { // Called when the resolve fails. Use the error code to debug. 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; } service = serviceInfo; int port = service.getPort(); InetAddress host = service.getHost(); // getHost() will work now } }; } 
+13
source

Source: https://habr.com/ru/post/951183/


All Articles