Added in API level 34
Also in T Extensions 7

NsdManager.ServiceInfoCallback


public static interface NsdManager.ServiceInfoCallback

android.net.nsd.NsdManager.ServiceInfoCallback


Callback to listen to service info updates. For use with NsdManager.registerServiceInfoCallback to register, and with NsdManager.unregisterServiceInfoCallback to stop listening.

Summary

Public methods

default void onServiceInfoCallbackRegistered()

Reports that the callback was successfully registered.

abstract void onServiceInfoCallbackRegistrationFailed(int errorCode)

Reports that registering the callback failed with an error.

abstract void onServiceInfoCallbackUnregistered()

Reports that service info updates have stopped.

abstract void onServiceLost()

Reports when the service that this callback listens to becomes unavailable.

default void onServiceLost(NsdServiceInfo serviceInfo)

Reports when the service that this callback listens to becomes unavailable.

abstract void onServiceUpdated(NsdServiceInfo serviceInfo)

Reports updated service info.

Public methods

onServiceInfoCallbackRegistered

Added in API level 34
public void onServiceInfoCallbackRegistered ()

Reports that the callback was successfully registered.

Called on the executor passed to NsdManager.registerServiceInfoCallback.

This indicates that onServiceInfoCallbackRegistrationFailed will not be called, and service update callbacks will be sent.

onServiceInfoCallbackRegistrationFailed

Added in API level 34
Also in T Extensions 7
public abstract void onServiceInfoCallbackRegistrationFailed (int errorCode)

Reports that registering the callback failed with an error. Called on the executor passed to NsdManager.registerServiceInfoCallback. onServiceInfoCallbackRegistrationFailed will be called exactly once when the callback could not be registered. No other callback will be sent in that case.

Parameters
errorCode int: Value is one of the following:

onServiceInfoCallbackUnregistered

Added in API level 34
Also in T Extensions 7
public abstract void onServiceInfoCallbackUnregistered ()

Reports that service info updates have stopped. Called on the executor passed to NsdManager.registerServiceInfoCallback. A callback unregistration operation will call onServiceInfoCallbackUnregistered once. After this, the callback may be reused.

onServiceLost

Added in API level 34
Also in T Extensions 7
public abstract void onServiceLost ()

Reports when the service that this callback listens to becomes unavailable.

Called on the executor passed to NsdManager.registerServiceInfoCallback. The service may become available again, in which case onServiceUpdated(NsdServiceInfo) will be called.

This method is never called if onServiceLost(NsdServiceInfo) is implemented.

When registering through registerServiceInfoCallback(DiscoveryRequest,Executor,ServiceInfoCallback), onServiceLost(NsdServiceInfo) should be used instead, as multiple services may be found and this method does not indicate which one was lost.

Additionally, when registering through registerServiceInfoCallback(NsdServiceInfo,Executor,ServiceInfoCallback), if NsdServiceInfo.getNetwork() is null, the service may be found on multiple networks, so onServiceLost(NsdServiceInfo) should also be preferred as it allows identifying on which network the service was lost.

onServiceLost

Added in API level 34
Also in T Extensions 7
public void onServiceLost (NsdServiceInfo serviceInfo)

Reports when the service that this callback listens to becomes unavailable.

Called on the executor passed to NsdManager.registerServiceInfoCallback. The service may become available again, in which case onServiceUpdated(NsdServiceInfo) will be called.

This is called every time a service (as per NsdServiceInfo.getServiceName()) is lost on any Network (as per NsdServiceInfo.getNetwork()) on which it was previously discovered. Therefore, this method may be called multiple times for a given service name, if multiple onServiceUpdated(NsdServiceInfo) callbacks were received for that service name on different networks.

Parameters
serviceInfo NsdServiceInfo: The service that was lost.
This value cannot be null.

onServiceUpdated

Added in API level 34
Also in T Extensions 7
public abstract void onServiceUpdated (NsdServiceInfo serviceInfo)

Reports updated service info. Called on the executor passed to NsdManager.registerServiceInfoCallback. Any service updates will be notified via this callback until NsdManager.unregisterServiceInfoCallback is called. This will only be called once the service is found, so may never be called if the service is never present.

For each service (as identified by NsdServiceInfo.getServiceName()) and network (as per NsdServiceInfo.getNetwork()), this will be called when the service is first found on the Network, and then every time NsdServiceInfo is updated for that service.

Note the same service name may be found multiple times on different networks, if DiscoveryRequest.getNetwork() (when registered via registerServiceInfoCallback(DiscoveryRequest,Executor,ServiceInfoCallback)) or NsdServiceInfo.getNetwork() (when registered via registerServiceInfoCallback(NsdServiceInfo,Executor,ServiceInfoCallback)) was not specified. The NsdServiceInfo contents may differ in that case; in particular NsdServiceInfo.getHostAddresses() may depend on the network.

Parameters
serviceInfo NsdServiceInfo: This value cannot be null.