NsdManager discovery does not work on Android 9

被刻印的时光 ゝ 提交于 2019-12-07 11:29:30

Ok I have found a solution for this problem in an old Github issue. Apparently you need to have the wifi multicast permission and need to acquire a MulticastLock to receive multicast messages.

I have yet to find any documentation about this and it seems to not be an specific issue with Android 9 but rather a Google Pixel default setting to disable all listening on multicast packages to preserve battery.

For anyone who comes around this problem and don't want to dive through long issues I have added following code:

AndroidManifest.xml:

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

Browser:

WifiManager wifi = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
WifiManager.MulticastLock multicastLock = wifi.createMulticastLock("multicastLock");
multicastLock.setReferenceCounted(true);
multicastLock.acquire();
.
.
multicastLock.release(); // release after browsing
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!