Device discovery in local network
Asked Answered
M

2

8

I'm currently developing an android app using SDK >= 16 which should be able to discover different android devices (later also iOS devices) in a local area network using the WiFi radio.

My first guess was to use multicast which turned out to be non functional on my Samsung Galaxy S2: packets are only received when sent from the same device.

My second guess is to actively scan the network using a limited IP address range and wait for a proper response. Unfortunately, this implies that the network uses DHCP to address the IP addresses.

None of the above solutions seem to be the perfect solution.

My current solution for my first guess:

public class MulticastReceiver extends AsyncTask<Activity, Integer, String> {

    private static final String host = "224.1.1.1";
    private static final int port = 5007;
    private static final String TAG = "MulticastReceiver";

    protected String doInBackground(Activity... activities) {
        WifiManager wm = (WifiManager)activities[0].getSystemService(Context.WIFI_SERVICE);
        WifiManager.MulticastLock multicastLock = wm.createMulticastLock("mydebuginfo");
        multicastLock.acquire();
        String message = "Nothing";

        if (multicastLock.isHeld()) {
            Log.i(TAG, "held multicast lock");
        }

        try {
            InetAddress addr = InetAddress.getByName(host);
            MulticastSocket socket = new MulticastSocket(port);
            socket.setTimeToLive(4);
            socket.setReuseAddress(true);
            socket.joinGroup(addr);

            byte[] buf = new byte[5];
            DatagramPacket recv = new DatagramPacket(buf, buf.length, addr, port);
            socket.receive(recv);
            message = new String(recv.getData());
            socket.leaveGroup(addr);
            socket.close();
        } catch (Exception e) {
            message = "ERROR " + e.toString();
        }

        multicastLock.release();

        return message;
    }
}

This code results in blocking on line socket.receive(recv); If I specify a timeout, I get a timeout exception.

Magulac answered 30/3, 2013 at 16:3 Comment(0)
B
2

Check my answer in very similar question Android Network Discovery Service (ish) before API 14

I do not belive that multicast is not working on Galaxy S2, some time ago when I was coding some network application, I made several test on many devices, some older like G1 but also on S2, S3 and Galaxy Tab 10.

But to be able to use multicast you must enable it programatically.

Have you used this piece of code?

WifiManager wifi = (WifiManager)getSystemService( Context.WIFI_SERVICE );
if(wifi != null){
WifiManager.MulticastLock lock = wifi.createMulticastLock("Log_Tag");
lock.acquire();
}
Bullheaded answered 30/3, 2013 at 16:21 Comment(2)
Yes, I have the above code in my sample app. I will post my complete code in my original comment. I defiantly can confirm that multicast works on different machines on my network, but my Galaxy S2 fails miserably :(Magulac
So, if you have two android devices you can try running pttdroid, which is opensourced code.google.com/p/pttdroid and has option to send/receive data (voice) to multicast groups.Bullheaded
B
1

Check out http://developer.android.com/training/connect-devices-wirelessly/index.html It mentions two ways of finding local services- NSD and wifi direct.

Breunig answered 30/3, 2013 at 16:6 Comment(3)
But wifi direct implies that you lose the connection to your local network, doesn't it? I still want to be able to access the internet while discovering other devices. Also for testing purposes, it would be nice to be able to also discover a local machine running a non android os.Magulac
Then try NSD. Scanning is not the way to go, to a network admin it looks like an attack.Breunig
Doesn't NSD use multicast under the hood?Magulac

© 2022 - 2024 — McMap. All rights reserved.