Bonjour implementation on Android

后端 未结 5 1174
深忆病人
深忆病人 2020-12-23 10:22

I am trying to implement bonjour/zero conf on my android app. I am using jmDns library for searching the all the available devices. Here is the code that I am using for sear

相关标签:
5条回答
  • 2020-12-23 10:28

    I can't give you any specific help with the code but I'm pretty sure that there are issues with Android and mDNS at least with some handsets and (I believe) the emulator.

    More information here:

    http://rgladwell.wordpress.com/2010/04/18/gotchas-android-and-multicast-dns/

    0 讨论(0)
  • 2020-12-23 10:42

    You may use an existing tool from Android's Play Store to scan the local network first, like "bonjour browser" to make sure there are the services you want to scan. Then you can check the jmDNS keyword to scan the network.

    But there is a known issue that jmDns does not work on some Android 4.x devices.

    0 讨论(0)
  • 2020-12-23 10:52

    Android 4.1 adds Network Service Discovery that seems to be just wrapping up the Bonjour stack in different terms. I also see a lower-level API called android.net.wifi.p2p.WifiP2pManager that exposes DNS-SD (as well as UPnP?) directly.

    Note that the underlying mDNSResponder daemon does not seem to be running all the time, and is not used for systemwide DNS lookups (e.g. from a browser) as far as I can tell right now.

    0 讨论(0)
  • 2020-12-23 10:53

    Do you know for a fact that multicast is enabled on your phone? See http://home.heeere.com/tech-androidjmdns.html.

    And you should probably looking for "_ipp._tcp.local" (or something similar) instead of "_http.tcp" services. But that's just for testing, right? :-)

    0 讨论(0)
  • 2020-12-23 10:54

    As noted in comments above, the native Android support is not working and/or not implemented completely to allow retrieval of TXT records (as of Android v5.1). I also could not get the jmDns library to work for discovery. I finally found the mdnsjava project and it worked very easily for me. Note that its sample code is not correct. Here is some sample of code I used to synchronously find all IPP printers:

        String type[] = {"_ipp._tcp.local."};
        Lookup resolve = null;
        try {
            resolve = new Lookup(type, Type.ANY, DClass.IN);
            ServiceInstance[] records = resolve.lookupServices();
            for (ServiceInstance record : records) {
                Log.d(TAG, record.toString());
                Map txtMap = record.getTextAttributes();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    

    Also note that you need to add the dnsjava library to your libs folder and your build.gradle. I used version 2.1.7.

    0 讨论(0)
提交回复
热议问题