Search code examples
androidandroid-networkingservice-discovery

What would cause NsdManager.discoverServices() to stop finding services on a particular device?


Here's my code:

  private val nsdManager = (context.getSystemService(Context.NSD_SERVICE) as NsdManager)

  fun go() {
    val discoveryListener = object : NsdManager.DiscoveryListener { ... }

    nsdManager.discoverServices("_sonos._tcp.", NsdManager.PROTOCOL_DNS_SD, discoveryListener)
  }

This code has been working great for many months, but yesterday it just stopped working on a Pixel 3 (Android 11, Jan 5th security patch). discoveryListener.onDiscoverStarted and discoveryListener.onDiscoveryStopped get called back as expected. There's no discoveryListener.onStartDiscoveryFailed failure. But discoveryListener.onServiceFound just never gets called back anymore. The same code works fine on a Galaxy S9 (Android 10) on the same wifi network. The same code worked fine (until yesterday) on the Pixel 3.

Is NsdManager sensitive to coroutines and threading or to Activity vs Application context? I thought perhaps some work I've been doing with coroutines may have caused it. But now even the production app does not return any onServiceFound results on this device only.

Is it possible that NsdManager caches results and got into a bad state? If so, how do I reset this state? Here are some things I tried:

  • uninstall/reinstall
  • clear app storage
  • forget wifi network and reconnect
  • wait 24 hours

Solution

  • Waiting even longer fixed this. Perhaps 48 hours or perhaps 24 hours with no calls to discoverServices in the mean time.