Question

I have to provide support for API-14 and above and so I am not able to use network discovery service provided by Android.

So, I have tried Jmdns Library for this purpose.

I have two questions regardnig Jmdns implementation. QUESTION# 1

I have implemented the following code but I am not able to discover any service. When i use NSD for the same purpose and on the same network, then i am able to discover the respective service. So i am noyt sure what is wrong with my code. Could someone please help?

public void onDiscoveryRequested(View v) {

        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                setUp();

            }});

        thread.start();

    }


     private String type = "_http._tcp.";
        private JmDNS jmdns = null;
        private ServiceInfo serviceInfo;
        android.net.wifi.WifiManager.MulticastLock lock;
        android.os.Handler handler = new android.os.Handler();

    private void setUp() {

        android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) getSystemService(android.content.Context.WIFI_SERVICE);

        WifiInfo wifiinfo = wifi.getConnectionInfo();
          int intaddr = wifiinfo.getIpAddress();
          byte[] byteaddr = new byte[] { (byte) (intaddr & 0xff), (byte) (intaddr >> 8 & 0xff),
                  (byte) (intaddr >> 16 & 0xff), (byte) (intaddr >> 24 & 0xff) };
         InetAddress addr = null;
        try {
            addr = InetAddress.getByAddress(byteaddr);
        } catch (UnknownHostException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }


        lock = wifi.createMulticastLock("mylockthereturn");
        lock.setReferenceCounted(true);
        lock.acquire();
        try {
            jmdns = JmDNS.create(addr);
            jmdns.addServiceListener(type, new ServiceListener() {

                @Override
                public void serviceResolved(ServiceEvent ev) {
                    Log.e("tag", "Service resolved: " + ev.getInfo().getQualifiedName() + " port:" + ev.getInfo().getPort()
                            + " ip:" + ev.getInfo().getInetAddresses().toString());
                }

                @Override
                public void serviceRemoved(ServiceEvent ev) {
                    Log.e("tag", "Service removed: " + ev.getName());
                }

                @Override
                public void serviceAdded(ServiceEvent ev) {
                    // Required to force serviceResolved to be called again (after the first search)
                    jmdns.requestServiceInfo(ev.getType(), ev.getName(), 1);
                    Log.e("tag", "Service added: " + ev.getInfo().getQualifiedName() + " port:" + ev.getInfo().getPort()
                            + " ip:" + ev.getInfo().getInetAddresses());
                }
            });

        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
    }

QUESTION# 2 Is there a way to discover all running services on a network via Jmdns?

Could someone please help me with the above two questions?

Thanks and regards, Sunny

Was it helpful?

Solution

Try "_http._tcp.local." as your service type. I saw a similar problem when using jmdns with AirPlay.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top