JmDNS: cannot enable service

I am trying to implement JmDNS discovery for communication between an Android application and a desktop application. I completed the following tutorial: http://home.heeere.com/tech-androidjmdns.html

The Android application registers the service, and the desktop application adds a listener to the service. It works fine for me with three of my four devices, but with the fourth (Samsung Galaxy Tab 10.1 PT7500 running Android 3.2). I can not allow this service. My handler receives the serviceAdded event, but not the serviceResolved event. I also tried calling jmdns.requestServiceInfo and jmdns.getServiceInfo , but the first one does nothing and the last time expires and returns null.

However, the jmdns browser is able to enable the service just fine, so this is not a device. There is no firewall on any of these devices. A service always uses an IPv4 address.

Does anyone have any ideas what might cause this problem?

Code to start the service:

 jmdns = JmDNS.create(wifiAddress); ServiceInfo serviceInfo = ServiceInfo.create(Constants.SERVICE_TYPE, Constants.SERVICE_NAME, Constants.ZEROCONF_PORT, "my service"); HashMap<String, String> deviceInfoMap = new HashMap<String, String>(); deviceInfoMap.put(Constants.KEY_DEVICE_NAME, getDeviceName()); deviceInfoMap.put(Constants.KEY_DEVICE_ID, getDeviceId()); // ... serviceInfo.setText(deviceInfoMap); jmdns.registerService(serviceInfo); 

Code for client / listener:

 jmdns = JmDNS.create(); jmdns.addServiceListener(Constants.SERVICE_TYPE, serviceListener = new ServiceListener() { @Override public void serviceResolved(ServiceEvent event) { System.out.println("Service resolved: " + event.getName() + " of type " + event.getType()); } @Override public void serviceRemoved(ServiceEvent event) { System.out.println("Service removed: " + event.getName() + " of type " + event.getType()); } @Override public void serviceAdded(ServiceEvent event) { System.out.println("Service added: " + event.getName() + " of type " + event.getType()); ServiceInfo info = jmdns.getServiceInfo(event.getType(), event.getName()); System.out.println("Service info: " + info); // --> null } }); 

Output:

 Service added: my service @ GT-P7500 of type _mytype._tcp.local. Service info: null Service added: my service @ Galaxy Nexus of type _mytype._tcp.local. Service info: [ ServiceInfoImpl@183779345 name: 'my service @ Galaxy Nexus ._mytype._tcp.local.' address: '/192.168.1.154:4242 ' status: 'DNS: myhost.local. state: probing 1 task: null', has data deviceName: Galaxy Nexus deviceId: <id> displayDensity: XHDPI ] 
+7
source share
2 answers

Checking the latest version of the source code from the github repository did the trick. Service Discovery now works great on all devices. (Note: I used JmDNS version 3.4.1, which I previously downloaded from sourceforge )

Looking through the history of SVN, there seem to be a couple of trade-offs related to issues with enabling services since the release of 3.4.1.

Edit: replace svn with github storage, as jmdns has moved there.

+8
source

Use JmDNS.create(InetAddress addr, String name) instead of JmDNS.create() . In later versions of Android JmDNS, you need to learn more about the local device and the network interface that it is listening on.

 JmDNS jmdns; // somewhere global // In a background thread public void buildJmDNS() { InetAddress addr = InetAddress.getLocalHost(); String hostname = InetAddress.getByName(addr.getHostName()).toString(); try { jmdns = JmDNS.create(addr, hostname); // throws IOException } catch (IOException e) { e.printStackTrace(); // handle this if you want } } 

I had the same problem you encountered and this solved it for me. I tried to find an example that I found that shows why this is necessary, but I cannot.

+1
source

All Articles