NsdManager发现在Android 9上不起作用 [英] NsdManager discovery does not work on Android 9

查看:669
本文介绍了NsdManager发现在Android 9上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了很长时间,让NsdManager发现功能可以在Android 9上运行.它可以在此之前的任何Android版本上正常运行.

I have tried for a good time now to get the NsdManager discovery function to work on Android 9. It works on any Android version prior to that without any problems.

出于测试目的,我使用此简单代码段,清单中有"INTERNET"权限.

For testing purposes I use this simple code snippet and there is a permission for "INTERNET" in the manifest.

var nsdManager = context.getSystemService(Context.NSD_SERVICE) as NsdManager

nsdManager.discoverServices("_https._tcp", NsdManager.PROTOCOL_DNS_SD, object: NsdManager.DiscoveryListener {
    override fun onDiscoveryStarted(serviceType: String?) {
        println("DEBUG: onDiscoveryStarted $serviceType") 
    }
    override fun onDiscoveryStopped(serviceType: String?) {
        println("DEBUG: onDiscoveryStopped $serviceType") 
    }
    override fun onServiceFound(serviceInfo: NsdServiceInfo?) {
        println("DEBUG: onServiceFound $serviceInfo") 
    }
    override fun onServiceLost(serviceInfo: NsdServiceInfo?) {
        println("DEBUG: onServiceLost $serviceInfo") 
    }
    override fun onStartDiscoveryFailed(serviceType: String?, errorCode: Int) {
        println("DEBUG: onStartDiscoveryFailed $serviceType $errorCode") 
    }
    override fun onStopDiscoveryFailed(serviceType: String?, errorCode: Int) {
        println("DEBUG: onStopDiscoveryFailed $serviceType $errorCode") 
    }
})

执行此代码时,我在日志中看不到任何调试或错误消息.

When this code executes i don't see any debug or error messages in the logs.

使用Wireshark,我可以看到广播的mDNS Standard查询"_https._tcp.local"以及来自所有预期设备的所有相应响应.由于从未调用过函数"onServiceFound",因此似乎在Android上似乎未收到响应.实际上,除"onDiscoveryStared"外,没有其他函数被调用过.

With Wireshark I can see the broadcasted mDNS Standard query for "_https._tcp.local" and all corresponding responses from all expected devices. The responses just don't seem to be received on Android because the function "onServiceFound" is never called. In fact no function except "onDiscoveryStared" is ever called.

我尚未在Android 9上看到任何关于为什么不起作用的更改,我们非常感谢您提供的任何帮助.也许我做错了什么,或者错过了当前Android版本中的任何权限更改.

I have not seen any changes on Android 9 on why this should not be working and any help is gladly appreciated. Maybe i have done something wrong or missed any permission changes in the current Android version.

更新:在具有当前Android 9 beta的Samsung S9 +上,服务发现可以正常运行.另一方面,在我的Pixel 3XL上它不起作用.

UPDATE: On an Samsung S9+ with the current Android 9 beta the service discovery works without any problems. On my Pixel 3XL on the other side it does not work.

推荐答案

好吧,我已经在旧的 MulticastLock 接收多播消息.

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.

我尚未找到有关此文件的任何文档,这似乎不是Android 9的特定问题,而是Google Pixel的默认设置,可禁止所有对多播包的侦听以节省电池电量.

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:

AndroidManifest.xml:

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

浏览器:

WifiManager wifi = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
WifiManager.MulticastLock multicastLock = wifi.createMulticastLock("multicastLock");
multicastLock.setReferenceCounted(true);
multicastLock.acquire();
.
.
multicastLock.release(); // release after browsing

这篇关于NsdManager发现在Android 9上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆