Friday 27 January 2017

Android JMDNS Search Tutorial.

A simple example to getting start with Zero configuration service integration with your android app. Lets start with some simple steps below:


1) First of all create a simple JmdnsUtil class:


public class JmdnsUtil {

    public static final String SERVICE_TYPE="_http._tcp.local.";
    public static final String SERVICE_NAME="AppName";
    public static final int PORT=1234;


}



2) In step second create a helper class that will contain all method stuffs:


public class JmdnsHelper {

    private String[] serviceNames = null;

    /**
     * This method registers jmdns service
     */

    public void registerJmdnsService() {
        try {
            // Create a JmDNS instance
            JmDNS jmdns = JmDNS.create(InetAddress.getLocalHost());

            // Register a service
            ServiceInfo serviceInfo = ServiceInfo.create(JmdnsUtil.SERVICE_TYPE,
                    JmdnsUtil.SERVICE_NAME,
                    JmdnsUtil.PORT, "path=index.html");
            jmdns.registerService(serviceInfo);

            // Wait a bit
            Thread.sleep(100);//25000

            //Call discover services method
            discoverJmdnsService();

        } catch (IOException e) {
            System.out.println(e.getMessage());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * This method discovers jmdns services or
     * connected devices in local network
     */

    public String[] discoverJmdnsService() {
        JmDNS jmdns = null;
        try {
            jmdns = JmDNS.create();

            //while (true) {

            ServiceInfo[] infos = jmdns.list(JmdnsUtil.SERVICE_TYPE);
            serviceNames = new String[infos.length];
        
            for (int i = 0; i < infos.length; i++) {
                serviceNames[i] = infos[i].getName();
                // Here we can fetch all required details of device.
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (jmdns != null) try {
                jmdns.close();
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }

        if (serviceNames != null && serviceNames.length > 0) {
            return serviceNames;
        } else {
            return null;
        }
    }
}



3) Now create a service which runs in background to search for connected devices or registered services in local network.

public class JmdnsService extends Service {

    /**
     * Jmdns Constructor
     */

    public JmdnsService() {
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        startSearchTcpIpLocalServices();
        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * This method starts searching of devices in local network
     */

    private void startSearchTcpIpLocalServices() {
        if (NetworkAvailablity.checkNetworkStatus(this)) {
            try {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //Do network search operation
                        JmdnsHelper jmdnsHelper = new JmdnsHelper();
                        final String serviceName[] = jmdnsHelper.discoverJmdnsService();

                        if (serviceName != null && serviceName.length > 0) {
                            JmdnsService.this.stopSelf();
                        } else {

                        }
                    }
                }).start();
            } catch (NullPointerException n) {
                n.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else{
            JmdnsService.this.stopSelf();
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}


Here we done with simple JMDNS search integration. You can find jmdns jar at the path below.

Download JMDNS JAR

 

Thanks...!!!















2 comments:

Andorid marshmallow custom permission code

As we all know android has put some restrictions for permission check for security reasons from android M (6.0) Marshmallow. Here we gon...