Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot send UDP packets from development machine to Android Emulator
    primarykey
    data
    text
    <p>Because the Android Emulator does not support Multicast as described <a href="http://developer.android.com/tools/devices/emulator.html#limitations" rel="nofollow">here</a> I've been trying to make a work around for my situation.</p> <p>To do this I wrote a small Java program which listens for Multicast packets and then repeats it to the localhost address. Because this 'Multicast repeater' is running on that same machine as the android Emulator, I was hoping I could loopback the Multicast packets into the Emulator. Unfortunately this does not seem to work.</p> <p>This is my 'Multicast repeater' method which I run outside of Eclipse and the Emulator. I have checked that it receives UDP messages from another machine elsewhere on the network that transmits UDP packets containing the time:</p> <pre><code>public static void main(String[] args) throws IOException{ boolean loop = true; MulticastSocket socket = new MulticastSocket(4446); InetAddress address = InetAddress.getByName("230.0.0.1"); socket.joinGroup(address); DatagramSocket txSocket = new DatagramSocket(); // UDP socket to re transmit on DatagramPacket packet; while (loop){ byte[] buf = new byte[256]; packet = new DatagramPacket(buf, buf.length); socket.receive(packet); String received = new String(packet.getData(), 0, packet.getLength()); System.out.println("Quote of the Moment: " + received); packet = new DatagramPacket(received.getBytes(), received.getBytes().length, InetAddress.getLocalHost, 4448); txSocket.send(packet); } socket.leaveGroup(address); socket.close(); } </code></pre> <p>And this is my 'receiver' code that runs in the android app:</p> <pre><code>ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); if (networkInfo != null &amp;&amp; networkInfo.isConnected()) { txtView.append("Network connected.\r\n"); WifiManager wm = (WifiManager)getSystemService(Context.WIFI_SERVICE); WifiManager.MulticastLock wmmc = wm.createMulticastLock("MulticastTest"); if (wmmc.isHeld()){ txtView.append("Multicast lock is held\r\n"); }else{ txtView.append("Multicast lock NOT held, attempting to acquire.\r\n"); wmmc.acquire(); } if (wmmc.isHeld()){ try{ DatagramSocket rxSocket = new DatagramSocket(4448); rxSocket.setSoTimeout(5000); DatagramPacket packet; // get a few quotes for (int i = 0; i &lt; 5; i++) { byte[] buf = new byte[256]; packet = new DatagramPacket(buf, buf.length); try{ rxSocket.receive(packet); String received = new String(packet.getData(), 0, packet.getLength()); txtView.append(received + "\r\n"); } catch (InterruptedIOException ex){ txtView.append("Socket timed out.\r\n"); } //System.out.println("Quote of the Moment: " + received); } //socket.leaveGroup(address); //socket.close(); }catch (Exception ex){ txtView.append(ex.toString() + "\r\n"); }finally{ wmmc.release(); } }else{ txtView.append("Could not acquire multicast lock.\r\n"); } }else{ txtView.append("Network NOT connected"); } </code></pre> <p>Unfortunately the receive socket just times out. I have tried binding to '10.0.2.2' (described <a href="http://developer.android.com/tools/devices/emulator.html#networkaddresses" rel="nofollow">here</a>) but this throws an exception although this appears to be useful only if I wanted to access my development machine from the Emulator whereas I want to access the emulator from my development machine. Does anyone have any suggestions?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload