diff --git a/src/platform/android/java/chip/platform/NsdServiceFinderAndResolver.java b/src/platform/android/java/chip/platform/NsdServiceFinderAndResolver.java index c1a187680c5c73..1663a2092a9f21 100644 --- a/src/platform/android/java/chip/platform/NsdServiceFinderAndResolver.java +++ b/src/platform/android/java/chip/platform/NsdServiceFinderAndResolver.java @@ -41,6 +41,7 @@ class NsdServiceFinderAndResolver implements NsdManager.DiscoveryListener { private final ChipMdnsCallback chipMdnsCallback; private final MulticastLock multicastLock; private final ScheduledFuture resolveTimeoutExecutor; + private NsdServiceInfo discoveredServiceInfo = null; @Nullable private final NsdManagerServiceResolver.NsdManagerResolverAvailState nsdManagerResolverAvailState; @@ -94,14 +95,13 @@ public void run() { @Override public void onServiceFound(NsdServiceInfo service) { - if (targetServiceInfo.getServiceName().equals(service.getServiceName())) { + if (discoveredServiceInfo == null && targetServiceInfo.getServiceName().equals(service.getServiceName())) { Log.d(TAG, "onServiceFound: found target service " + service); if (stopDiscoveryRunnable.cancel(false)) { nsdManager.stopServiceDiscovery(this); } - - resolveService(service, callbackHandle, contextHandle, chipMdnsCallback); + discoveredServiceInfo = service; } else { Log.d(TAG, "onServiceFound: found service not a target for resolution, ignoring " + service); } @@ -206,7 +206,10 @@ public void onServiceLost(NsdServiceInfo service) { @Override public void onDiscoveryStopped(String serviceType) { - Log.i(TAG, "Discovery stopped: " + serviceType); + Log.i(TAG, "Discovery stopped: " + serviceType + ", discoveredServiceInfo: " + discoveredServiceInfo); + if (discoveredServiceInfo != null) { + resolveService(discoveredServiceInfo, callbackHandle, contextHandle, chipMdnsCallback); + } } @Override