Android unicast UDP works, but broadcast is not

For the purpose, we are trying to configure several Android devices to receive UDP broadcast from a laptop via a local WiFi network.

What works:

  • The Python terminal on two laptops is capable of sending and receiving UDP broadcasts or unicast transmissions to each other.
  • Android devices can receive a UDP message sent directly to their IP address

What does not work:

  • Android device (LG / Google Nexus 4) does not receive UDP broadcast message (another laptop running Python)

Any tips? Here is our source for each component (shamelessly borrowed from the Internet)

Python gets broadcast

import select, socket

port = 50000
bufferSize = 1024

ip = '0.0.0.0'

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((ip, port))

while True:
    msg, result = s.recvfrom(bufferSize)
    pr

int "Received message: ", msg

Python sends broadcast

# Send UDP broadcast packets

port = 50000

import sys, time
from socket import *

s = socket(AF_INET, SOCK_DGRAM)
s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)

data = repr(time.time()) + '\n'
s.sendto(data, ('192.168.1.255', port))

Android

private void receiveUDP() {
    final String[] text = new String[1];
    final int server_port = 50000;
    // create buffer
    final byte[] message = new byte[1500];
    final DatagramPacket p = new DatagramPacket(message, message.length);

    // listen for message, put in buffer
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                while (true) {
                    Log.d(Main.class.getName(), "Created socket");
                    DatagramSocket s = new DatagramSocket(server_port);
                    s.receive(p);
                    text[0] = new String(message, 0, p.getLength());
                    Log.d(Main.class.getName(), "message:" + text[0]);
                    s.close();
                }
            } catch (IOException e) {
                Log.e(this.getClass().getName(), "An unexpected error occurred", e);
            }
        }
    }).start();
}

, , , - UDP- Android - UDP IP-, Python UDP ....

?

UPDATE

, MulticastLock - , . Android:

private void receiveUDP() {
    final String[] text = new String[1];
    final int server_port = 50001;
    // create buffer
    final byte[] message = new byte[1500];
    final DatagramPacket p = new DatagramPacket(message, message.length);

    // listen for message, put in buffer
    new Thread(new Runnable() {
        @Override
        public void run() {
            DatagramSocket s = null;
                while (true) {
                    try{
                        WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                        WifiManager.MulticastLock lock = wifi.createMulticastLock("Main");
                        lock.acquire();
                        s = new DatagramSocket(server_port, InetAddress.getByName("192.168.1.13"));
                        Log.d(Main.class.getName(), "Created socket");
                            s.receive(p);
                        lock.release();
                        text[0] = new String(message, 0, p.getLength());
                        Log.d(Main.class.getName(), "message:" + text[0]);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(context, "Received message: "+text[0], Toast.LENGTH_SHORT).show();
                            }
                        });
                    } catch (SocketException e) {
                        Log.e(Main.class.getName(), "An unexpected error occurred", e);
                    } catch (IOException e) {
                        Log.e(Main.class.getName(), "An unexpected error occurred", e);
                    } finally {
                        if (s != null) s.close();
                        Log.d(Main.class.getName(), "Closing socket");
                    }
                }
        }
    }).start();
}
+4
1

, , Nexus 4, UDP. . , Huawei G300

+1

All Articles