UPD packets doesn't arrive on a Samsung Galaxy Tab 7.7 but they arrive on a HTC Desire
Asked Answered
U

3

1

I'm developing an Android 3.1 Tablet application.

I'm going to use this app to listen to UDP packets send by a device which is sending UDP packets to 255.255.255.255:8001 every 5 seconds.

Using desktop program Docklight scripting v1.9 I see that this device sends a 11 bytes packet every 5 seconds, by my app doesn't receive every packet: sometimes it receives one, and sometimes it receives 5 or 6 packets at the same time.

This is my Activity:

public class UDPSocketActivity extends Activity
{
    private UDPServerThread myThread;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        int ipAddress = wifiInfo.getIpAddress();

        TextView txt = (TextView)findViewById(R.id.deviceIP);
        txt.setText(Integer.toString(ipAddress));
    }

    public void onStartClick(View view)
    {
        Log.v("UDPSocketActivity", "onClick");

        try
        {
            myThread = new UDPServerThread("X", 8001);
            myThread.start();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

This is UDP socket thread:

public class UDPServerThread extends Thread
{
    private static final int MESSAGE_SIZE = 11;
    protected DatagramSocket socket = null;
    protected boolean end = false;

    public UDPServerThread(String serverName, int port) throws IOException
    {
        super(serverName);

        Log.v("UDPServerThread", "constructor");

        socket = new DatagramSocket(port);
        socket.setBroadcast(true);
    }

    public void run()
    {
        while (!end)
        {
            try
            {
                byte[] buf = new byte[MESSAGE_SIZE];

                // Wait an incoming message.
                DatagramPacket packet = new DatagramPacket(buf, buf.length);

                Log.v("UDServerThread", "Listenning...");

                socket.receive(packet);

                Log.v("UDServerThread", "Mensaje recibido.");
            }
            catch (IOException e)
            {
                if (!socket.isClosed())
                    e.printStackTrace();
            }
        }
    }

    public void stopServer()
    {
        Log.v("UDPServerThread", "stopServer");
        if (socket != null)
            socket.close();
        end = true;
    }
}

This is AndroidManifest.xml permissions:

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

UPDATE:
If I send a UDP packet to Tablet's IP, e.g. UDP:192.168.1.135:8001, sometimes it receives the packet. And sometimes it receives three or four at the same time.

But if I send direct UDP packet to an HTC Desire 2.2.2 it receives all of them, but my HTC doesn't receive broadcast packets. And I'm using the same code.

This how I am receiving UDP broadcast packets (look at the time):

07-06 12:08:56.580: V/UDServerThread(6449): Mensaje recibido.
07-06 12:08:59.655: V/UDServerThread(6449): Mensaje recibido.
07-06 12:09:02.410: V/UDServerThread(6449): Mensaje recibido.
07-06 12:09:03.230: V/UDServerThread(6449): Mensaje recibido.
07-06 12:09:03.435: V/UDServerThread(6449): Mensaje recibido.
07-06 12:09:03.745: V/UDServerThread(6449): Mensaje recibido.
07-06 12:09:03.945: V/UDServerThread(6449): Mensaje recibido.
07-06 12:09:04.460: V/UDServerThread(6449): Mensaje recibido.
07-06 12:09:04.770: V/UDServerThread(6449): Mensaje recibido.
07-06 12:09:04.975: V/UDServerThread(6449): Mensaje recibido.
07-06 12:09:46.855: V/UDServerThread(6449): Mensaje recibido.
07-06 12:10:06.005: V/UDServerThread(6449): Mensaje recibido.
07-06 12:10:06.310: V/UDServerThread(6449): Mensaje recibido.
07-06 12:10:06.515: V/UDServerThread(6449): Mensaje recibido.
07-06 12:10:06.825: V/UDServerThread(6449): Mensaje recibido.
07-06 12:10:07.335: V/UDServerThread(6449): Mensaje recibido.
07-06 12:10:07.640: V/UDServerThread(6449): Mensaje recibido.
07-06 12:10:07.845: V/UDServerThread(6449): Mensaje recibido.
07-06 12:10:10.415: V/UDServerThread(6449): Mensaje recibido.
07-06 12:10:17.065: V/UDServerThread(6449): Mensaje recibido.

What am I doing wrong? Maybe I need some custom configuration.

By the way, I am testing it on a Samsung Galaxy Tab 7.7 with Android 3.1.

Upstart answered 6/7, 2012 at 8:26 Comment(7)
I don't know if there is an issue in your code or not, but when you use the UDP protocol, you have no guarantee on message arrival (some datagrams might get lost), on time of arrival or on content... See this explanation for example.Velda
While that is the nature of UDP you shouldn't be loosing packets if you are just in a local network. If you have a rooted tablet you can install Wireshark(Google Play) on the tablet and check if the UDP packets actually arrive at the tablet, this can help you in your diagnosis.Accumulation
@Accumulation Searching on Google Play I've found these ones: play.google.com/store/search?q=Wireshark+&c=apps Which is the correct one? And no, I don't have a rooted tablet.Upstart
I have updated my question with more details and data.Upstart
@Upstart it was the first one "Shark for Root" but without a rooted tablet it wont work, you can try tPacketCapture it says you dont need root, but I have never used it myself.Accumulation
Try sending the packets directly to the device instead of using broadcast, there was an android issue with broadcast a while back, see code.google.com/p/android/issues/detail?id=8407Accumulation
@Accumulation That is what I did. If I send packets directly to HTC Desire it receives all, but Samsung galaxy not.Upstart
U
0

There was a problem with my Router.

Note: I answer my own question because I think it's useful for other people with the same problem (I was dealing with it by four days).

Upstart answered 6/7, 2012 at 8:26 Comment(2)
Can you elaborate more on you fix, as i am facing same issue. and not able to receive M-SEARCH broadcast requests on my android device which is on same wifi network of the sender device.Transcend
My fix was to change my router. Try to test it with another router.Upstart
U
0

If you want to make sure that you will receive all send packets you should use TCP protocol instead of UDP.

Ulyanovsk answered 6/7, 2012 at 8:35 Comment(0)
A
0

To receive broadcasts on the HTC you need to get a Wifi Multicast lock. Basically some manufacturers are disabling receiving multicast by default. Still no idea what is happening on your Tablet, maybe some sort of buffering or slow piece of networking equipment in the middle?

WifiManager wifi;
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
MulticastLock ml = wifi.createMulticastLock("just some tag text");
ml.acquire();

When the asynctask stops I do a 
ml.release();

Code from Android Issues Tracker

Accumulation answered 6/7, 2012 at 11:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.