I have a java application on my host that sends UDP packets to a port: e.g. 8888.
And I have an Android application that listens to this port and simply displays the data.
This works fine with a real device (which is connected via WiFi to the same network), but I cannot get this working in the Emulator.
Some info:
telnet localhost 5554
redir add udp:8888:8888
the code in the Android app to connect to the port:
final DatagramSocket udpSocket = new DatagramSocket(8888);
here's the (pseudo) code of the server that sends the broadcast:
String data = "test";
InetAddress broadcastAddress = Inet4Address.getByName("255.255.255.255");
DatagramSocket udpSocket = new DatagramSocket();
udpSocket.setBroadcast(true);
byte[] dataBytes = data.getBytes();
DatagramPacket datagramPacket = new DatagramPacket(dataBytes, dataBytes.length, broadcastAddress, 8888);
udpSocket.send(datagramPacket);
What am I missing?
This does not work because of a bug in Android: Issue#207602: Emulator does not redirect UDP packets
Workaround:
emulator -list-avds
-engine classic
parameter:emulator -avd Nexus_6_API_25_GER -engine classic
Run
- Run 'app'
and connect to the emulator that you have just startedtelnet localhost 5554
redir add udp:8888:8888
-engine classic
(pixel launcher crashes), but my application worked and it received the UDP packets from the host