2

I'm trying to implement that my app is able to receive some broadcast messages. One device sends messages to 192.168.2.255. I can see on Wireshark that this works. My app should receive these messages but it doesn't work yet. This is my code:

int port = 3123;

        // Create a socket to listen on the port.
        DatagramSocket dsocket = new DatagramSocket(port);
        dsocket.setBroadcast(true);

        // Create a buffer to read datagrams into. If a
        // packet is larger than this buffer, the
        // excess will simply be discarded!
        byte[] buffer = new byte[1024];

        // Create a packet to receive data into the buffer
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

        // Now loop forever, waiting to receive packets and printing them.
        while (true) {
        // Wait to receive a datagram
            dsocket.receive(packet);

            // Convert the contents to a string, and display them
            String msg = new String(buffer, 0, packet.getLength());
            System.out.println(packet.getAddress().getHostName() + ": " + msg);

            // Reset the length of the packet before reusing it.
            packet.setLength(buffer.length);

            res = msg;

            Log.d("mainGate_bc", res);
        }
    } catch (Exception e) {
          System.err.println(e);
    }

The programme is stuck on the line dsocket.receive(packet). Nothing arrives in my app while the other device is sending its packets.

These permissions I tried:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

Any idea why it doesn't work? Some further permissions? Settings on the Android device?

user3137385
  • 315
  • 5
  • 14
  • Thanks but same result. – user3137385 Oct 23 '16 at 14:19
  • Does the code ever overcome the blocking call to `receive()`? If not, look [here](http://stackoverflow.com/questions/40055175/how-to-interrupt-a-blocking-call-to-udp-sockets-receive/40055466#40055466) in order to make periodic interrupts on the blocking call. – Onik Oct 23 '16 at 14:55
  • It doesn't. But I also don't want to interrupt receiving. I want to receive existing messages which don't seem to arrive at my app. – user3137385 Oct 23 '16 at 15:03
  • Try `DatagramSocket dsocket = new DatagramSocket(null); dsocket.setReuseAddress(true); dsocket.setBroadcast(true); dsocket.bind(new InetSocketAddress(port));` – Onik Oct 23 '16 at 15:09
  • Unfortunately, no changes. – user3137385 Oct 23 '16 at 15:35
  • Those packages sent never reach your device, that's why the code never overcomes the blocking call to `receive()`...Try sending packages and receiving them on the same device. – Onik Oct 23 '16 at 15:58

1 Answers1

6

This question is duplicate of this other question. Apparently broadcast packets are all filtered out unless you acquire a multicast lock.

So you need something like the following code:

WifiManager wm = null;
MulticastLock multicastLock = null;

@Override
protected void onResume() {
    super.onResume();
    if(wm == null)wm = (WifiManager)getSystemService(Context.WIFI_SERVICE);
    if(multicastLock == null){
        multicastLock = wm.createMulticastLock("stackoverflow for the win");
        multicastLock.setReferenceCounted(true);
    }
    if(multicastLock != null && !multicastLock.isHeld()) multicastLock.acquire();
}

@Override
protected void onPause() {
    super.onPause();
    if(multicastLock != null && multicastLock.isHeld()) multicastLock.release();
}


<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
Community
  • 1
  • 1
Joshua
  • 1,185
  • 14
  • 23