I am trying to get the hostname from a service that is advertising itself on my local network using mDNS/Zeroconf. I am using Android's Network Service Discovery API, but it seems as if Android doesn't use the advertised hostname but tries to do a reverse DNS lookup and if that fails only returns the IP address.
On my Macbook I do can do the following:
$ dns-sd -B _myservice._tcp
DATE: ---Mon 29 Oct 2018---
17:35:25.332 ...STARTING...
Timestamp A/R Flags if Domain Service Type Instance Name
17:35:25.333 Add 2 10 local. _myservice._tcp. My Service Name
$ dns-sd -L "My Service Name" _myservice._tcp
DATE: ---Mon 29 Oct 2018---
17:38:35.423 ...STARTING...
17:38:35.423 My\032Service\032Name._myservice._tcp.local. can be reached at test.my.service.local.:443 (interface 10)
I can then use the returned hostname (test.my.service.local
) to find the corresponding IP. I've got this working correctly on iOS using NSNetServiceBrowser
.
On Android in my onServiceDiscovered
if I do the following:
private static Map<String, Object> OnServiceDiscovered(NsdServiceInfo info) {
Log.d(TAG, info.getHost().getHostAddress());
Log.d(TAG, info.getHost().getHostName());
}
I do get the correct IP address, but getHostName returns "raspberrypi.local" (from /etc/hostname) or on another network only the IP address. My suspicion is that on Android getHost().getHostName() is doing a reverse DNS lookup and ignores the hostname being advertised with mDNS altogether.
Is there any way to get the hostname from mDNS on Android?