我正在编写一个Android应用程序来接收正在网络广播地址上广播的数据包(这已经过测试,数据包也会被广播并在“UDP发送器/接收器”应用程序中被接收。我无法让我的应用程序接收并告诉我它存在。这些设备位于同一网络上,并且发送设备的代码正在运行且专有。这是应用程序的基本DatagramSocket代码。
package com.ti.cc3x.android;
import java.io.IOException;
import java.net.*;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class buttonListener extends Activity {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.listener);
final TextView txt = (TextView)findViewById(R.id.txt1);
new Thread( new Runnable(){
public void run(){
try {
String text = null;
int server_port = 12356;
byte[] message = new byte[66];
DatagramPacket p = new DatagramPacket(message, message.length);
DatagramSocket s = new DatagramSocket(server_port);
while(text == null){
s.receive(p);
text = new String(message, 0, p.getLength());
txt.setText("Messed up.");
}
if(text != null){
Toast.makeText(buttonListener.this, text, Toast.LENGTH_LONG).show();
txt.setText("Received");
s.close();
}
}
catch (SocketException se) {
se.printStackTrace();
Toast.makeText(buttonListener.this, "Socket Error", Toast.LENGTH_LONG).show();
txt.setText("Socket Error");
}
catch (IOException ioe) {
ioe.printStackTrace();
Toast.makeText(buttonListener.this, "Network Error", Toast.LENGTH_LONG).show();
txt.setText("Network Error");
}
}
}).start();
}}
感谢任何帮助,谢谢!!
更新的代码:
package com.ti.cc3x.android;
import java.io.IOException;
import java.net.*;
import java.util.Arrays;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.MulticastLock;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
public class buttonListener extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.listener);
WifiManager wifi = (WifiManager)
getSystemService(Context.WIFI_SERVICE);
WifiManager.MulticastLock lock = wifi.createMulticastLock("Log_Tag");
final TextView txt = (TextView) findViewById(R.id.txt1);
lock.acquire();
new Thread( new Runnable(){
public void run(){
try {
String text = null;
int server_port = 12356;
byte[] message = new byte[66];
DatagramPacket p = new DatagramPacket(message, message.length);
DatagramSocket s = new DatagramSocket(server_port);
//while(text == null){
s.receive(p);
text = new String(message, 0, p.getLength());
txt.setText("Messed up.");
//}
//if(text != null){
Toast.makeText(buttonListener.this, text, Toast.LENGTH_LONG).show();
txt.setText("Received");
s.close();
//}
}
catch (SocketException se) {
se.printStackTrace();
Toast.makeText(buttonListener.this, "Socket Error", Toast.LENGTH_LONG).show();
txt.setText("Socket Error");
}
catch (IOException ioe) {
ioe.printStackTrace();
Toast.makeText(buttonListener.this, "Network Error", Toast.LENGTH_LONG).show();
txt.setText("Network Error");
}
}
}).start();
lock.release();
}
}
答案 0 :(得分:2)
在你的回答中,你提到正在播放你正在发送的测试数据包。如果您将数据包直接发送到设备的IP地址而不是广播,您是否尝试过查看是否可以收到数据包?您的套接字可能正常工作但只是没有接收广播数据包。默认情况下,Android Wi-Fi堆栈会过滤掉多播数据包以节省电量。如果您可以收到直接发送到您的IP地址的数据包,那么这意味着您只需通过获取MulticastLock
启用接收多播数据包,您可以在此处找到更多信息:Android device not receiving multicast package
如果您仍然无法收到直接信息包,那么可能还有其他问题,但我会先检查一下。