问题描述
我开发一个Android 3.1平板电脑的应用程序。
I'm developing an Android 3.1 Tablet application.
我要使用这个程序来听的UDP数据包发送由发送UDP数据包255.255.255.255:8001每5秒的设备。
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.
使用桌面程序 Docklight脚本V1.9 我看到这个设备发送一个11字节的数据包每5秒通过我的应用程序不会收到的每个数据包:有时收到一个,有时它接收5或6的数据包在同一时间
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.
这是我的活动:
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" />
更新:
如果我发送一个UDP包,平板电脑的IP,如UDP:192.168.1.135:8001,有时它接收到的数据包。有时它接收三个或四个同时
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.
但是,如果我直接发送UDP数据包到 HTC Desire的2.2.2 接收所有的人,但我的HTC不接收广播数据包。而我使用的是相同的code。
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.
这怎么我收到UDP广播数据包(看时间):
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.
顺便说一句,我对三星Galaxy Tab 7.7测试它与Android 3.1。
By the way, I am testing it on a Samsung Galaxy Tab 7.7 with Android 3.1.
推荐答案
有我的路由器有问题。
注:我回答我的问题,因为我认为这是其他人同样的问题(我正在处理这四天)
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).
这篇关于UPD数据包没有到达上三星Galaxy Tab 7.7,但他们上的HTC Desire到货的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!