我想要的只是我的UDP广播工作:
@Override
protected String doInBackground(String[] params) {
try {
DatagramSocket datagramSocket = new DatagramSocket();
datagramSocket.setBroadcast(true);
byte[] buffer = "test".getBytes();
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName("192.168.0.255"), 8899);
datagramSocket.send(packet);
System.out.println("Package sent.");
System.out.println("Data: " + buffer);
System.out.println("Broadcast address: " + this.broadcastAddress);
System.out.println("--------------------------");
} catch (Exception e) {
System.out.println("Package not sent: " + e.getMessage());
e.printStackTrace();
}
return "complete";
}
此代码通过按钮在onClick事件上执行,并且位于AsyncTask内。
2次点击的日志是:
System.out﹕ Package sent.
System.out﹕ Data: [B@3b9b0914
System.out﹕ Broadcast address: /192.168.0.255
System.out﹕ --------------------------
System.out﹕ Package sent.
System.out﹕ Data: [B@3090d7b2
System.out﹕ Broadcast address: /192.168.0.255
System.out﹕ --------------------------
所以看起来它正在发挥作用。但事实并非如此,因为我无法用Wireshark来抓住它。
我尝试过在手机上使用UDP包发件人,这样做有效。
也许是因为我无法在手表上发送这样的UDP包?有没有人知道要搜索什么?