English 中文(简体)
How can I discover zeroconf (Bonjour) services on Android? I m having trouble with jmDNS
原标题:

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.)

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

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.)

I have tested their code, but without any luck.

Has anyone successfully accomplished zeroconf service discovery on Android, using jmDNS or another library?

Is it possible my discovery issue is related to the IPv6 multicast problem?

最佳回答

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.

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.

问题回答

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. Here is the build script.

Here is 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.

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 patch to include the correct header files, fix the typos and get logging via the Android APIs.

One more thing: You need to acquire and hold the MultiCast Lock from within your Java code, otherwise you won t find anybody else. See the example here.

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.

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.

Good Luck!

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 Discovery but appears to be Bonjour underneath. There also seems to be a lower-level API exposed via android.net.wifi.p2p.WifiP2pManager.

While it doesn t seem to be used for system-wide DNS lookups (e.g. I can t simply browse to http://machine.local via Chrome) it appears that this would be the way to do zeroconf/Bonjour lookups for native Android apps going forward.

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?

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

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.





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签