1

我正在开发一个 Android 3.1 平板电脑应用程序。

我将使用这个应用程序来监听设备发送的 UDP 数据包,该设备每 5 秒向 255.255.255.255:8001 发送 UDP 数据包。

使用桌面程序Docklight scripting v1.9我看到这个设备每 5 秒发送一个 11 字节的数据包,因为我的应用程序没有收到每个数据包:有时它会收到一个,有时它会同时收到 5 或 6 个数据包。

这是我的活动:

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();
        }
    }
}

这是 UDP 套接字线程:

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;
    }
}

这是 AndroidManifest.xml 权限:

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

更新:
如果我向平板电脑的 IP 发送 UDP 数据包,例如 UDP:192.168.1.135:8001,有时它会收到数据包。有时它会同时接收三个或四个。

但是,如果我将直接 UDP 数据包发送到HTC Desire 2.2.2,它会接收所有这些数据包,但我的 HTC 不会收到广播数据包。我正在使用相同的代码。

这就是我接收 UDP 广播数据包的方式(看看时间):

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.

我究竟做错了什么?也许我需要一些自定义配置。

顺便说一句,我正在使用 Android 3.1 的三星 Galaxy Tab 7.7 上对其进行测试。

4

3 回答 3

0

如果您想确保您将收到所有发送的数据包,您应该使用 TCP 协议而不是 UDP。

于 2012-07-06T08:35:00.627 回答
0

要在 HTC 上接收广播,您需要获得 Wifi 多播锁定。基本上一些制造商默认禁用接收多播。仍然不知道您的平板电脑上发生了什么,可能是中间的某种缓冲或慢速网络设备?

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();

来自 Android 问题跟踪器的代码

于 2012-07-06T11:14:32.370 回答
0

我的路由器有问题。

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).

于 2012-07-08T13:35:13.830 回答