Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receiving UDP broadcast packet at Pixel 2 and Pixel 2 XL

Tags:

udp

broadcast

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.

like image 460
Henry Zo Avatar asked Oct 17 '22 21:10

Henry Zo


1 Answers

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"/>
like image 183
IrishHayZeus Avatar answered Oct 21 '22 08:10

IrishHayZeus