Android 7和8丢失UDP广播包,而ANDROID 5不会丢失。
我有一些PCB在端口7112上每秒传输小型UDP包(100字节)。
我们可以同时在网络上激活一个或多个pcb。同样的问题。
我所看到的是,使用Android 5的设备会收到所有这些软件包,但是如果我在装有Android 7或8的任何设备上运行相同的APP,则软件包损失会很大。
在运行Android 5的旧平板电脑上,会收到每个软件包,但在其他设备上(如Samsung S7或Lenovo TAB 7 Essential),聆听相同的本地wifi发射器会造成巨大的丢包。
在任何已连接的Windows PC或Mac上,软件包都已接收到应用程序,因此我知道wifi /网络正在传输软件包。
从Android Play商店下载的其他UDP广播侦听APPS的行为相同(丢失的包(如果安装在Android 7和8上)。
我试图设置wifi-lock在服务中使用packagereceiver,并且只将包打印到控制台,而不进行任何解析。 Android 7和8仍在丢包。更改缓冲区大小也无济于事。
运行包侦听器的线程的源:
public void run(){
final byte[] receiveData = new byte[1000];
DatagramSocket serverSocketUDP;
WifiManager wifi = (WifiManager)theContext.getSystemService(Context.WIFI_SERVICE);
if (wifi != null){
wl = wifi.createWifiLock("mylock_wl");
wl.setReferenceCounted(true);
wl.acquire();
ml = wifi.createMulticastLock("mylock_ml");
ml.setReferenceCounted(true);
ml.acquire();
}
try {
serverSocketUDP = new DatagramSocket(null);
serverSocketUDP.setSoTimeout(5000);
serverSocketUDP.setBroadcast(true);
serverSocketUDP.setReuseAddress(true);
serverSocketUDP.setReceiveBufferSize(1000);
serverSocketUDP.bind(new InetSocketAddress(7112));
while(true) {
try {
while (true) {
Log.d(LOGTAG, "Waiting for Broadcast packet.");
final DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocketUDP.receive(receivePacket);
byte[] sendData = new byte[1024];
InetAddress address = receivePacket.getAddress();
int port = receivePacket.getPort();
String req = new String(receivePacket.getData(), 0, receivePacket.getLength());
Log.d(LOGTAG, "Received UDP message : " + req + " from: " + receivePacket.getAddress().getHostAddress());
String[] sa=req.split("\r\n");
for (int lv=0;lv<sa.length;lv++){
req=sa[lv];
try {
if (StatePacket.isStateUDPPacket(new String(req.getBytes("ISO-8859-1")))) {
StatePacket sp = new StatePacket(0, req);
if (macFilter.equals(sp.getMACinStringFormat(""))) {
// Log.d(LOGTAG, "--> Received Machine State UDP message : " + req + " from: " + receivePacket.getAddress().getHostAddress());
for (IStateBroadcastListner theStateBroadcastListner : theStateBroadcastListners.values()) {
theStateBroadcastListner.gotStateBroadcast(sp);
}
if (sp.getState()==4) {
if (theLastState != 4) {
theStateLog.clear();
}
theStateLog.add(sp);
}
theLastState=sp.getState();
}
if (sp.getCurrentMacro()==998){
if (newThermosTimerListener!=null){
newThermosTimerListener.gotBrewPing(sp);
}
}
} else if (Pcb.isPCBPacket(req)) {
Pcb aPcb = new Pcb(req);
PcbList.getInstance().add(aPcb);
Log.d(LOGTAG, "--> Received PCB message : " + req + " from: " + receivePacket.getAddress().getHostAddress());
} else if (MachineObject.isMachineStatePacket(new String(req.getBytes("ISO-8859-1")))) {
MachineObject.getInstance().parse(req);
Log.d(LOGTAG, "--> Received State message : " + req + " from: " + receivePacket.getAddress().getHostAddress());
} else if (BatchStartPacket.isBatchStartPacket(new String(req.getBytes("ISO-8859-1")))) {
if (newThermosTimerListener!=null){
newThermosTimerListener.gotStartRecipe(new BatchStartPacket(new String(req.getBytes("ISO-8859-1"))));
}
} else if (BatchEndPacket.isBatchEndPacket(new String(req.getBytes("ISO-8859-1")))) {
if (newThermosTimerListener!=null){
newThermosTimerListener.gotEndRecipe(new BatchEndPacket(new String(req.getBytes("ISO-8859-1"))));
}
} else if (NewRecipesPacket.isNewRecipesUDPPacket(new String(req.getBytes("ISO-8859-1")))) {
NewRecipesPacket nrp = new NewRecipesPacket(0, req);
if (macFilter.equals(nrp.getMACinStringFormat(""))) {
if (newRecipesListner != null) {
newRecipesListner.gotNewrecipes();
}
}
} else if (RedipeEnabledDisabledPacket.isRedipeEnabledDisabledPacket(new String(req.getBytes("ISO-8859-1")))) {
RedipeEnabledDisabledPacket nrp = new RedipeEnabledDisabledPacket(0, req);
if (macFilter.equals(nrp.getMACinStringFormat(""))) {
if (newRecipesListner != null) {
newRecipesListner.gotRedipeEnabledDisabledPacket(nrp.getFlag());
}
}
}
} catch (Exception e) {
}
}
}
}
catch (Exception e) {
e.printStackTrace();
try {
//serverSocketUDP = new DatagramSocket(thePort);
if (wifi != null){
wl.release();
ml.release();
wl = wifi.createWifiLock("mylock_wl");
wl.setReferenceCounted(true);
ml = wifi.createMulticastLock("mylock_ml");
ml.setReferenceCounted(true);
wl.acquire();
ml.acquire();
}
serverSocketUDP.close();
serverSocketUDP=null;
serverSocketUDP = new DatagramSocket(null);
serverSocketUDP.setSoTimeout(5000);
serverSocketUDP.setBroadcast(true);
serverSocketUDP.setReuseAddress(true);
serverSocketUDP.setReceiveBufferSize(1000);
serverSocketUDP.bind(new InetSocketAddress(7112));
Log.i(LOGTAG, "Recive Buffer Size:" + serverSocketUDP.getReceiveBufferSize());
Log.i(LOGTAG, "Recive Buffer Size:" + serverSocketUDP.getBroadcast());
} catch (Exception j){
}
Log.i(LOGTAG, "Exception in UDPClientThread" + e.toString());
}
}// while ends
} catch (Exception e){
}
}