Receiving UDP broadcast packet at Pixel 2 and Pixel 2 XL

不问归期 提交于 2020-01-02 04:05:22

问题


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.

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 ).

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.

I have tried to search the similar problems and I found some such as UDP 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.

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();
}

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.


回答1:


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;
}

And the additional manifest permissions.

<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"/>



回答2:


I've encountered the same issue with a Pixel 2 XL. I've added acquiring a Wifi Multicast lock, to be able to listen to UDP broadcasted messages.

WifiManager wm = (WifiManager)getSystemService(Context.WIFI_SERVICE);
WifiManager.MulticastLock multicastLock = wm.createMulticastLock("mydebuginfo");
multicastLock.acquire();

And added this permission in the Android Manifest:

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

But UDP broadcast receiving on Pixel 2 (XL) seems to work only occasionally. I haven't been able to find a pattern yet, it seems random. I know that UDP isn't meant to be reliable, but all other devices on the same Wifi LAN receive the UDP broadcast packets perfectly, without loss.



来源:https://stackoverflow.com/questions/49102743/receiving-udp-broadcast-packet-at-pixel-2-and-pixel-2-xl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!