android resolve .local (mDNS)

后端 未结 2 1383
情歌与酒
情歌与酒 2021-01-05 19:38

I\'m looking for a solution to resolve .local host names with Android 4.0.4 (no NSD, due to API level 15). On the device I don\'t have any service to discover, just the host

相关标签:
2条回答
  • 2021-01-05 19:48

    You should be able to use the InetAddress class to resolve the hostname for a given IP address. For example, using the IP address provided in the original question, try the following:

    try
    {
        String hostname = InetAddress.getByName("10.202.0.29").getHostName();
    }
    catch (UnknownHostException e)
    {
        Log.e("MyApplication", "Attempt to resolve host name failed");
    }
    

    Since this is a network operation, make sure that it is not performed on the UI thread.

    EDIT

    You should be able to resolve a local hostname with jmDNS as follows:

    InetAddress localHost = InetAddress.getByName("10.202.0.29");
    JmDNS jmdns = JmDNS.create(localHost);
    String localHostName = jmdns.getHostName();
    
    0 讨论(0)
  • 2021-01-05 19:49

    I had almost the same requirements as your question, apart from the requirement to use jmDNS, so I solved it with NSD. I realize this doesn't address your question exactly, but thought it might still be somewhat helpful for yourself and others to see how I solved it.

    I setup an NSD discovery listener and an NSD resolve listener, and within the discovery listener code, added a filter for the target host name (e.g. "kcmeasurement", or in my case, "garagedoor").

    There is a blog post here which explains in detail how to do that. Refer to steps 3-4, which are dealing with the Android App code required.

    http://www.dodgycoder.net/2015/02/setting-up-bonjourzeroconfmdnsnsd.html

    For your case, I would imagine you would have to do the equivalent process but just using the jmDNS library instead of NSD.

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