Android NSD not discovering all services

后端 未结 6 610
慢半拍i
慢半拍i 2021-02-02 08:24

I\'m trying to run an application using Android Native Service Discovery but sometimes when I run the application, it doesn\'t discover all services from my network. I\'m runnin

6条回答
  •  孤街浪徒
    2021-02-02 08:47

    I was seeing the same issue with devices not being discovered and was able to fix it by acquiring the WifiManager Multicast lock prior to discovering services:

    val multicastLock: WifiManager.MulticastLock = (mContext.getSystemService(Context.WIFI_SERVICE) as WifiManager).createMulticastLock(TAG)
    multicastLock.setReferenceCounted(true)
    
    multicastLock.acquire()
    
    mNsdManager.discoverServices(SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener)
    

    And later releasing the lock when done discovering services:

    mNsdManager.stopServiceDiscovery(mDiscoveryListener)
    
    multicastLock.release()
    

提交回复
热议问题