本文介绍了在Pixel 2和Pixel 2 XL接收UDP广播数据包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个从Wi-Fi摄像机接收UPD广播数据包的应用程序。
在我发现Google Pixel 2 / Pixel 2 XL接收到UPD广播数据包之前,感觉很好。

I am developing an app receiving UPD broadcast packet from Wi-Fi camera.It used to be good before I found the problem in receiving UPD broadcast packet at Google Pixel 2 / Pixel 2 XL.

要弄清楚原因,我制作了2个测试应用:一个是UPD广播发件人(),另一个是UDP广播接收器()。

To figure out the reason, I made 2 test apps: one is UPD broadcast sender( https://senatech.box.com/s/gmhr391pbl32lqai0mhkffyk6j0ckle5 ), the other is UDP broadcast receiver( https://senatech.box.com/s/abamuor47nlafocs035nfuj90d0uvx0m ).

我已经在一些android设备上对其进行了测试,发现Google Pixel 2 / Pixel 2 XL无法接收UDP广播数据包。除Pixel 2 / Pixel 2 XL以外的Android设备都可以正常工作。 Android 8.1上的Nexus也可以正常使用。

I have tested them on some android devices and found that Google Pixel 2 / Pixel 2 XL cannot revceive UDP broadcast packet. Android devices except Pixel 2 / Pixel 2 XL work well. Nexus on Android 8.1 works well, too.

我尝试搜索类似的问题,但发现一些问题,例如Android 8.0上未收到的
UDP广播数据包()。
我认为这可能是由于同样的问题导致的,尽管它是用QT编写的。

I have tried to search the similar problems and I found some such asUDP broadcast packets not received on Android 8.0 ( https://bugreports.qt.io/browse/QTBUG-64233 ).I think that this may result from same problem though it is written in QT.

以下是有关UDP广播发送方的简短代码

Here is brief code on UDP broadcast sender

public void sendUPDBroadcast() {
   Thread thread = new Thread() {
      @Override
      public void run() {
         DatagramSocket ds = null;
         int port = 0;
         String udpData = "";
         try {
            port = Integer.parseInt(etPort.getText().toString());
            udpData = etUDPData.getText().toString();
            InetAddress ia = InetAddress.getByName("192.168.255.255");
            ds = new DatagramSocket(port);
            DatagramPacket data = new DatagramPacket(udpData.getBytes(), udpData.getBytes().length, ia, port);
            ds.send(data);
         } catch(Exception e) {
         } finally {
            if (ds != null) {
               ds.close();
               ds = null;
            }
         }
      }
   };
   thread.start();
}

以下是UDP广播发送方的简短代码

Here is brief code on UDP broadcast sender

   packet = new DatagramPacket(buffer, buffer.length);
   socket = new DatagramSocket(port);
   socket.setBroadcast(true);

   @Override
   public void run() {
      try {
         while (alive) {
            try {
               packet.setLength(buffer.length);
               socket.receive(packet);
               String s = stringFromPacket(packet);
            } catch (java.io.InterruptedIOException e) {
            } catch (java.io.IOException ex) {
            } catch (Exception allException) {
            } finally {
               if (socket != null)
                  socket.close();
                  socket = null;
               }
            }
         }
      }
   }

有人遇到此问题并解决吗?
谢谢您。

Is there anybody who experienced this problem and fix it?Thank you in advanced.

推荐答案

尝试[Ruud van Reenen]的解决方案,结果好坏参半。但是,在添加了一些其他权限并启用了引用计数之后,它对我来说更加可靠。这是我的代码:

Trying [Ruud van Reenen]'s solution, I had mixed results. However, after adding some additional permissions, and enabling reference counts, it is working much more reliably for me. Here is my code:

WifiManager wm = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiManager.MulticastLock multicastLock = wm.createMulticastLock("RavnApplication");
multicastLock.setReferenceCounted(true);
multicastLock.acquire();

...

// don't forget to release when you're done...
if (multicastLock != null) {
    multicastLock.release();
    multicastLock = null;
}

以及其他清单权限。

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

这篇关于在Pixel 2和Pixel 2 XL接收UDP广播数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 03:50