开发者

Locate services using Zeroconf with JmDNS on local device AP(hotspot)

With my android device set up as a hotspot/Access point(AP), how can I use JmDNS(3.4.0) to find other devices connected to 开发者_运维问答this AP?

Using a regular AP everything works fine when creating the JmDNS instance the normal way;

jmdns = JmDNS.create();

But using my device as an AP this throws exception.

    java.net.SocketException: No such device
    at org.apache.harmony.luni.platform.OSNetworkSystem.setSocketOption(Native Method)
    at dalvik.system.BlockGuard$WrappedNetworkSystem.setSocketOption(BlockGuard.java:382)
    at org.apache.harmony.luni.net.PlainDatagramSocketImpl.setOption(PlainDatagramSocketImpl.java:198)
    at org.apache.harmony.luni.net.PlainDatagramSocketImpl.join(PlainDatagramSocketImpl.java:137)
    at java.net.MulticastSocket.joinGroup(MulticastSocket.java:190)
    at javax.jmdns.impl.JmDNSImpl.openMulticastSocket(JmDNSImpl.java:459)
    at javax.jmdns.impl.JmDNSImpl.<init>(JmDNSImpl.java:420)
    at javax.jmdns.JmDNS.create(JmDNS.java:60)

I've also tried creating the JmDNS instance with the local IP

jmdns = JmDNS.create( InetAddress.getByName("192.168.1.1") );

which just throws the same exception.

How can I make JmDNS find services connected to the local AP?


It is not possible to use multicast on a local hotspot since it's not possible to acquire the multicast lock. Thus JmDNS will not work.

For anyone else having this problem, the alternative solution of finding connected devices I ended up using is based on parsing /proc/net/arp


This is now possible (since API 16) with a help of Android Network Service Discovery (NSD).
NSD supports phone as hotspot/AP as well as regular AP connections (i.e. connection to a router). So you don't need JmDns anymore to achieve your goal.
See: https://developer.android.com/training/connect-devices-wirelessly/nsd


I don't think this directly answers your question, but I found that unless I have active wifi jmDNS won't work. I had to disable discovery if my wifi is off.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜