7

I'm looking for a solution to resolve .local host names with Android 4.0.4 (no NSD, due to API level 15). On the device I don't have any service to discover, just the hostname. Could you please provide an example how to resolve? I integrated the jmDNS, but don't know how to use for host name resolving.

win-cmd:

ping kcmeasurement.local

Pinging kcmeasurement.local [10.202.0.29] with 32 bytes of data:
Reply from 10.202.0.29: bytes=32 time<1ms TTL=64
...

Thank you,

Daniel

MartoniczD
  • 178
  • 1
  • 1
  • 9
  • 1
    This is a shortcoming of Android. If you want it fixed in the OS, please drop by and *star* (upvote) [this android buganizer issue](https://issuetracker.google.com/issues/140786115). – Wyck Sep 26 '21 at 13:19

2 Answers2

7

I had almost the same requirements as your question, apart from the requirement to use jmDNS, so I solved it with NSD. I realize this doesn't address your question exactly, but thought it might still be somewhat helpful for yourself and others to see how I solved it.

I setup an NSD discovery listener and an NSD resolve listener, and within the discovery listener code, added a filter for the target host name (e.g. "kcmeasurement", or in my case, "garagedoor").

There is a blog post here which explains in detail how to do that. Refer to steps 3-4, which are dealing with the Android App code required.

http://www.dodgycoder.net/2015/02/setting-up-bonjourzeroconfmdnsnsd.html

For your case, I would imagine you would have to do the equivalent process but just using the jmDNS library instead of NSD.

dodgy_coder
  • 12,407
  • 10
  • 54
  • 67
  • This appears to be about resolving an mDNS *service* rather than resolving a *name* - in particular, the writeup proposes changing the configuration of the raspberry pi, rather than merely recognizing the rapsberrypi.local mDNS name it should already have out of the box. – Chris Stratton Aug 12 '17 at 00:17
  • I just managed to reach RPi from PC, - only after I installed samba and restarted avahi-daemon. No luck from Android – jaromrax Sep 13 '20 at 07:33
  • the issue with android is that it doesn't ship with an mdns service. .local addresses are handled by avahi in Linux and bonjour in ios/macos. jmdns or the nsd app should enable that – Fuseteam Feb 22 '21 at 22:42
-5

You should be able to use the InetAddress class to resolve the hostname for a given IP address. For example, using the IP address provided in the original question, try the following:

try
{
    String hostname = InetAddress.getByName("10.202.0.29").getHostName();
}
catch (UnknownHostException e)
{
    Log.e("MyApplication", "Attempt to resolve host name failed");
}

Since this is a network operation, make sure that it is not performed on the UI thread.

EDIT

You should be able to resolve a local hostname with jmDNS as follows:

InetAddress localHost = InetAddress.getByName("10.202.0.29");
JmDNS jmdns = JmDNS.create(localHost);
String localHostName = jmdns.getHostName();
Willis
  • 5,308
  • 3
  • 32
  • 61
  • 1
    Thank you for your reply! Unfortunately It's not working, because the "kcmeasurement" is an mDNS host name and my android version (4.0.4) doesn't have a built-in mDNS resolver. So if I call the InetAddress.getByName I got a not found exception. – MartoniczD Jan 05 '15 at 11:46
  • 1
    Moreover I included the jmDNS package into my Apk, so the advertising works, but I don't find how to resolve .local names. – MartoniczD Jan 05 '15 at 11:47
  • -1 Neither the original nor amended form of this answer address the actual question **at all**. It's likely that you did not understand what was being asked - the so called local "name" you claim to be resolving in the second part is neither an mDNS name nor any sort of name at all, it's a numeric IP address, and there's no need to "resolve" that at all. – Chris Stratton Aug 12 '17 at 00:16
  • I am just using the IP address to create a 'InetAddress' object. The host name is found by calling 'getHostName'. – Willis Aug 12 '17 at 00:19
  • 2
    That's not what was asked. The question is about turning a name like "kcmeasurement.local" into an IP address. Once you have a numeric IP address, you're done. You *started* with a numeric IP address, which makes your response completely inapplicable to the problem of the question asked - you're assuming prior knowledge of the very thing the question is about discovering! – Chris Stratton Aug 12 '17 at 00:37