Receiving UDP broadcast packet at Pixel 2 and Pixel 2 XL

淺唱寂寞╮ 提交于 2019-12-05 10:08:55

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

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.

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