在本地设备 AP(热点)上使用 Zeroconf 和 JmDNS 定位服务
将我的 Android 设备设置为热点/接入点 (AP), 如何使用 JmDNS(3.4.0) 查找连接到该 AP 的其他设备?
使用常规 AP 以正常方式创建 JmDNS 实例时一切正常;
jmdns = JmDNS.create();
但是使用我的设备作为 AP 会引发异常。
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)
我还尝试使用本地 IP 创建 JmDNS 实例
jmdns = JmDNS.create( InetAddress.getByName("192.168.1.1") );
,但它只会抛出相同的异常。
如何让JmDNS找到连接到本地AP的服务?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
无法在本地热点上使用多播,因为无法获取多播锁。因此 JmDNS 将不起作用。
对于遇到此问题的其他人,查找我最终使用的连接设备的替代解决方案是基于解析 /proc/net/arp
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
现在,借助 Android 网络服务发现 (NSD),这已成为可能(自 API 16 起)。
NSD 支持手机作为热点/AP 以及常规 AP 连接(即连接到路由器)。因此您不再需要 JmDns 来实现您的目标。
请参阅:https://developer.android.com/training/connect-devices-无线/nsd
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
我不认为这直接回答了你的问题,但我发现除非我有活动的 wifi,否则 jmDNS 将无法工作。如果我的 wifi 关闭,我必须禁用发现功能。
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.