如何在 Android 上发现 zeroconf (Bonjour) 服务?我在使用 jmDNS 时遇到问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2474143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I discover zeroconf (Bonjour) services on Android? I'm having trouble with jmDNS
提问by Peter Kirn
I'm working with a Droid / Android 2.0.1 and encountering an issue apparently many people have: I'm unable to discover services using the one pure-Java zeroconf library I know of, jmDNS. (Apple's Bonjour, while it works on Linux and Windows Java, I believe would be harder to port to Android because of reliance on native code.)
我正在使用 Droid / Android 2.0.1 并且遇到一个问题,显然很多人都有:我无法使用我知道的一个纯 Java zeroconf 库 jmDNS 来发现服务。(Apple 的 Bonjour,虽然它适用于 Linux 和 Windows Java,但我相信由于依赖本机代码,移植到 Android 会更困难。)
I can create services, but not discover them. I'm trying to make sense of what's going on.
我可以创建服务,但不能发现它们。我试图弄清楚发生了什么。
There is an ongoing issue report here; related to multicast and IPv6, but seems to be throwing users of jmDNS, too: http://code.google.com/p/android/issues/detail?id=2323
这里有一个正在进行的问题报告;与多播和 IPv6 相关,但似乎也抛弃了 jmDNS 的用户:http: //code.google.com/p/android/issues/detail?id=2323
Any idea why this person might be having success? See comment 22 in the bug report. (I'm new to SO, so can't post more than one URL.)
知道为什么这个人可能会成功吗?请参阅错误报告中的评论 22。(我是新来的,所以不能发布多个 URL。)
I have tested their code, but without any luck.
我已经测试了他们的代码,但没有任何运气。
Has anyone successfully accomplished zeroconf service discovery on Android, using jmDNS or another library?
有没有人使用 jmDNS 或其他库在 Android 上成功完成 zeroconf 服务发现?
Is it possible my discovery issue is related to the IPv6 multicast problem?
我的发现问题是否可能与 IPv6 多播问题有关?
采纳答案by Churlbong
I'm new as well otherwise I would have just left a comment on smountcastle's answer which is mostly correct. I have just been dealing with the exact same issue on a Droid running Android 2.1. I found that I needed to set the MulticastLock to reference-counted otherwise it seemed to be released automatically.
我也是新手,否则我只会对 smountcastle 的答案发表评论,这基本上是正确的。我刚刚在运行 Android 2.1 的 Droid 上处理完全相同的问题。我发现我需要将 MulticastLock 设置为引用计数,否则它似乎会自动释放。
AndroidManifest.xml:
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
// Networking code:
WifiManager wifi = getSystemService( Context.WIFI_SERVICE );
MulticastLock lock = wifi.createMulticastLock("fliing_lock");
lock.setReferenceCounted(true);
lock.acquire();
Just make sure to call lock.release()
when you're done with it. This may only be necessary for Android 2.0+, The Droid is my only test device currently, so I can't say for sure.
只要确保lock.release()
在完成后打电话。这可能只适用于 Android 2.0+,Droid 是我目前唯一的测试设备,所以我不能肯定。
回答by sradomski
I managed to cross-compile Bonjour for Android and get it running much the same way Apple intends it to run on embedded devices like printers. Hereis the build script.
我设法为 Android 交叉编译 Bonjour,并让它以与 Apple 打算在打印机等嵌入式设备上运行的方式大致相同的方式运行。这是构建脚本。
Hereis a small convenience wrapper to get it working as you'd expect.
这是一个方便的小型包装器,可以按您的预期工作。
We are using the client_shim layer from the Bonjour distribution to wrap all access to the embedded mDNS implementation via the usual dns_sd.h API. You do not use the idiom with the filehandles and the select with the shim layer.
我们正在使用 Bonjour 发行版中的 client_shim 层,通过通常的 dns_sd.h API 来包装对嵌入式 mDNS 实现的所有访问。您不要将习惯用法与文件句柄一起使用,将选择与填充层一起使用。
The client_shim layer is not exactly good supported by Apple - in fact I found typos in variable names, but it is working nevertheless. You will need to apply this patchto include the correct header files, fix the typos and get logging via the Android APIs.
Apple 对 client_shim 层的支持并不完全 - 事实上,我发现变量名称中有拼写错误,但它仍然有效。您将需要应用此补丁以包含正确的头文件、修复拼写错误并通过 Android API 获取日志记录。
One more thing: You needto acquire and hold the MultiCast Lock from within your Java code, otherwise you won't find anybody else. See the example here.
还有一件事:您需要从 Java 代码中获取并持有 MultiCast Lock,否则您将找不到其他任何人。请参阅此处的示例。
Other than that, I have it working on Android API Level 8 and we are maintaining a prebuilt library of Bonjour for Android, though I am not sure whether this is ok as per license.
除此之外,我让它在 Android API Level 8 上工作,我们正在维护一个预建的Bonjour for Android库,但我不确定这是否符合许可证。
Edited:
编辑:
The version in the prebuilts is 330.10, newer ones with client_shim as static libraries fail to compile with MSVC2010 on windows, so we kept this one.
预编译版本中的版本是 330.10,使用 client_shim 作为静态库的较新版本无法在 Windows 上使用 MSVC2010 进行编译,因此我们保留了这个版本。
Good Luck!
祝你好运!
回答by natevw
Android 4.1 adds native Bonjour support (looks like its implemented via mDNSResponder) for applications. The high-level API seems to be called Network Service Discoverybut appears to be Bonjour underneath. There also seems to be a lower-level API exposed via android.net.wifi.p2p.WifiP2pManager.
Android 4.1 为应用程序添加了原生 Bonjour 支持(看起来像是通过 mDNSResponder 实现的)。高级 API 似乎被称为网络服务发现,但在底层似乎是 Bonjour。似乎还通过android.net.wifi.p2p.WifiP2pManager公开了一个较低级别的 API 。
While it doesn't seem to be used for system-wide DNS lookups (e.g. I can't simply browse to http://machine.localvia Chrome) it appears that this would be the way to do zeroconf/Bonjour lookups for native Android apps going forward.
虽然它似乎没有用于系统范围的 DNS 查找(例如,我不能简单地通过 Chrome浏览到http://machine.local),但这似乎是对本机进行 zeroconf/Bonjour 查找的方法未来的 Android 应用程序。
回答by svoisen
If you haven't seen it already, I suggest checking out this project on github (disclaimer - I'm not the author): https://github.com/twitwi/AndroidDnssdDemo
如果你还没有看过,我建议你在 github 上查看这个项目(免责声明 - 我不是作者):https: //github.com/twitwi/AndroidDnssdDemo
It is a sample project that pretty much shows how to get everything up and running. It also includes a custom jmdns.jar that solves some issues I was having with the out-of-the-box jmdns.jar having a duplicate class file.
这是一个示例项目,几乎展示了如何启动和运行所有内容。它还包括一个自定义 jmdns.jar,它解决了我在使用具有重复类文件的开箱即用 jmdns.jar 时遇到的一些问题。
回答by smountcastle
Have you explicitly acquired the multicast lock so that you can receive the multicast packets?
您是否明确获取了多播锁定以便您可以接收多播数据包?
AndroidManifest.xml: <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" /> // Networking code: WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE); MulticastLock lock = wifi.createMulticastLock("mylock"); lock.acquire();
According to that Android Issue thread it looks like 2.0.1 doesn't have the fix. Perhaps you should transition to a later release?
根据那个 Android 问题线程,看起来 2.0.1 没有修复。也许您应该过渡到更高版本?