Added in API level 16

NsdManager


class NsdManager
kotlin.Any
   ↳ android.net.nsd.NsdManager

The Network Service Discovery Manager class provides the API to discover services on a network. As an example, if device A and device B are connected over a Wi-Fi network, a game registered on device A can be discovered by a game on device B. Another example use case is an application discovering printers on the network.

The API currently supports DNS based service discovery and discovery is currently limited to a local network over Multicast DNS. DNS service discovery is described at http://files.dns-sd.org/draft-cheshire-dnsext-dns-sd.txt

The API is asynchronous, and responses to requests from an application are on listener callbacks on a separate internal thread.

There are three main operations the API supports - registration, discovery and resolution.

Application start
                                  |
                                  |
                                  |                  onServiceRegistered()
                      Register any local services  /
                       to be advertised with       \
                        registerService()            onRegistrationFailed()
                                  |
                                  |
                           discoverServices()
                                  |
                       Maintain a list to track
                         discovered services
                                  |
                                  |--------->
                                  |          |
                                  |      onServiceFound()
                                  |          |
                                  |     add service to list
                                  |          |
                                  |<----------
                                  |
                                  |--------->
                                  |          |
                                  |      onServiceLost()
                                  |          |
                                  |   remove service from list
                                  |          |
                                  |<----------
                                  |
                                  |
                                  | Connect to a service
                                  | from list ?
                                  |
                           resolveService()
                                  |
                          onServiceResolved()
                                  |
                      Establish connection to service
                      with the host and port information
 
  
An application that needs to advertise itself over a network for other applications to discover it can do so with a call to #registerService. If Example is a http based application that can provide HTML data to peer services, it can register a name "Example" with service type "_http._tcp". A successful registration is notified with a callback to RegistrationListener#onServiceRegistered and a failure to register is notified over RegistrationListener#onRegistrationFailed

A peer application looking for http services can initiate a discovery for "_http._tcp" with a call to #discoverServices. A service found is notified with a callback to DiscoveryListener#onServiceFound and a service lost is notified on DiscoveryListener#onServiceLost.

Once the peer application discovers the "Example" http service, and either needs to read the attributes of the service or wants to receive data from the "Example" application, it can initiate a resolve with #resolveService to resolve the attributes, host, and port details. A successful resolve is notified on ResolveListener#onServiceResolved and a failure is notified on ResolveListener#onResolveFailed. Applications can reserve for a service type at http://www.iana.org/form/ports-service. Existing services can be found at http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml

Summary

Nested classes
abstract

Interface for callback invocation for service discovery

abstract

Interface for callback invocation for service registration

abstract

Callback for use with android.

abstract

Callback to listen to service info updates.

Constants
static String

Broadcast intent action to indicate whether network service discovery is enabled or disabled.

static String

The lookup key for an int that indicates whether network service discovery is enabled or disabled.

static Int

Indicates that the operation failed because it is already active.

static Int

Indicates that the service has failed to resolve because of bad parameters.

static Int

Failures are passed with RegistrationListener#onRegistrationFailed, RegistrationListener#onUnregistrationFailed, DiscoveryListener#onStartDiscoveryFailed, DiscoveryListener#onStopDiscoveryFailed or ResolveListener#onResolveFailed.

static Int

Indicates that the operation failed because the maximum outstanding requests from the applications have reached.

static Int

Indicates that the stop operation failed because it is not running.

static Int

Network service discovery is disabled

static Int

Network service discovery is enabled

static Int

Dns based service discovery protocol

Public methods
Unit
discoverServices(serviceType: String!, protocolType: Int, listener: NsdManager.DiscoveryListener!)

Initiate service discovery to browse for instances of a service type.

Unit
discoverServices(serviceType: String, protocolType: Int, network: Network?, executor: Executor, listener: NsdManager.DiscoveryListener)

Initiate service discovery to browse for instances of a service type.

Unit
discoverServices(discoveryRequest: DiscoveryRequest, executor: Executor, listener: NsdManager.DiscoveryListener)

Initiates service discovery to browse for instances of a service type.

Unit
discoverServices(serviceType: String, protocolType: Int, networkRequest: NetworkRequest, executor: Executor, listener: NsdManager.DiscoveryListener)

Initiate service discovery to browse for instances of a service type.

Unit
registerService(serviceInfo: NsdServiceInfo!, protocolType: Int, listener: NsdManager.RegistrationListener!)

Register a service to be discovered by other services.

Unit
registerService(serviceInfo: NsdServiceInfo, protocolType: Int, executor: Executor, listener: NsdManager.RegistrationListener)

Register a service to be discovered by other services.

Unit

Register a callback to listen for updates to a service.

Unit

Resolve a discovered service.

Unit
resolveService(serviceInfo: NsdServiceInfo, executor: Executor, listener: NsdManager.ResolveListener)

Resolve a discovered service.

Unit

Stop service discovery initiated with #discoverServices.

Unit

Stop service resolution initiated with #resolveService.

Unit

Unregister a service registered through #registerService.

Unit

Unregister a callback registered with registerServiceInfoCallback.

Constants

ACTION_NSD_STATE_CHANGED

Added in API level 16
static val ACTION_NSD_STATE_CHANGED: String

Broadcast intent action to indicate whether network service discovery is enabled or disabled. An extra EXTRA_NSD_STATE provides the state information as int.

Value: "android.net.nsd.STATE_CHANGED"

See Also

EXTRA_NSD_STATE

Added in API level 16
static val EXTRA_NSD_STATE: String

The lookup key for an int that indicates whether network service discovery is enabled or disabled. Retrieve it with android.content.Intent#getIntExtra(String,int).

Value: "nsd_state"

FAILURE_ALREADY_ACTIVE

Added in API level 16
static val FAILURE_ALREADY_ACTIVE: Int

Indicates that the operation failed because it is already active.

Value: 3

FAILURE_BAD_PARAMETERS

static val FAILURE_BAD_PARAMETERS: Int

Indicates that the service has failed to resolve because of bad parameters. This failure is passed with ServiceInfoCallback#onServiceInfoCallbackRegistrationFailed.

Value: 6

FAILURE_INTERNAL_ERROR

Added in API level 16
static val FAILURE_INTERNAL_ERROR: Int

Failures are passed with RegistrationListener#onRegistrationFailed, RegistrationListener#onUnregistrationFailed, DiscoveryListener#onStartDiscoveryFailed, DiscoveryListener#onStopDiscoveryFailed or ResolveListener#onResolveFailed. Indicates that the operation failed due to an internal error.

Value: 0

FAILURE_MAX_LIMIT

Added in API level 16
static val FAILURE_MAX_LIMIT: Int

Indicates that the operation failed because the maximum outstanding requests from the applications have reached.

Value: 4

FAILURE_OPERATION_NOT_RUNNING

static val FAILURE_OPERATION_NOT_RUNNING: Int

Indicates that the stop operation failed because it is not running. This failure is passed with ResolveListener#onStopResolutionFailed.

Value: 5

NSD_STATE_DISABLED

Added in API level 16
static val NSD_STATE_DISABLED: Int

Network service discovery is disabled

Value: 1

NSD_STATE_ENABLED

Added in API level 16
static val NSD_STATE_ENABLED: Int

Network service discovery is enabled

Value: 2

PROTOCOL_DNS_SD

Added in API level 16
static val PROTOCOL_DNS_SD: Int

Dns based service discovery protocol

Value: 1

Public methods

discoverServices

fun discoverServices(
    serviceType: String!,
    protocolType: Int,
    listener: NsdManager.DiscoveryListener!
): Unit

Initiate service discovery to browse for instances of a service type. Service discovery consumes network bandwidth and will continue until the application calls stopServiceDiscovery.

The function call immediately returns after sending a request to start service discovery to the framework. The application is notified of a success to initiate discovery through the callback DiscoveryListener#onDiscoveryStarted or a failure through DiscoveryListener#onStartDiscoveryFailed.

Upon successful start, application is notified when a service is found with DiscoveryListener#onServiceFound or when a service is lost with DiscoveryListener#onServiceLost.

Upon failure to start, service discovery is not active and application does not need to invoke stopServiceDiscovery

The application should call stopServiceDiscovery when discovery of this service type is no longer required, and/or whenever the application is paused or stopped.

Parameters
serviceType String!: The service type being discovered. Examples include "_http._tcp" for http services or "_ipp._tcp" for printers
protocolType Int: The service discovery protocol
listener NsdManager.DiscoveryListener!: The listener notifies of a successful discovery and is used to stop discovery on this serviceType through a call on stopServiceDiscovery. Cannot be null. Cannot be in use for an active service discovery.

discoverServices

fun discoverServices(
    serviceType: String,
    protocolType: Int,
    network: Network?,
    executor: Executor,
    listener: NsdManager.DiscoveryListener
): Unit

Initiate service discovery to browse for instances of a service type. Service discovery consumes network bandwidth and will continue until the application calls stopServiceDiscovery.

The function call immediately returns after sending a request to start service discovery to the framework. The application is notified of a success to initiate discovery through the callback DiscoveryListener#onDiscoveryStarted or a failure through DiscoveryListener#onStartDiscoveryFailed.

Upon successful start, application is notified when a service is found with DiscoveryListener#onServiceFound or when a service is lost with DiscoveryListener#onServiceLost.

Upon failure to start, service discovery is not active and application does not need to invoke stopServiceDiscovery

The application should call stopServiceDiscovery when discovery of this service type is no longer required, and/or whenever the application is paused or stopped.

Parameters
serviceType String: The service type being discovered. Examples include "_http._tcp" for http services or "_ipp._tcp" for printers This value cannot be null.
protocolType Int: The service discovery protocol
network Network?: Network to discover services on, or null to discover on all available networks
executor Executor: Executor to run listener callbacks with This value cannot be null.
listener NsdManager.DiscoveryListener: The listener notifies of a successful discovery and is used to stop discovery on this serviceType through a call on stopServiceDiscovery. This value cannot be null.

discoverServices

fun discoverServices(
    discoveryRequest: DiscoveryRequest,
    executor: Executor,
    listener: NsdManager.DiscoveryListener
): Unit

Initiates service discovery to browse for instances of a service type. Service discovery consumes network bandwidth and will continue until the application calls stopServiceDiscovery.

The function call immediately returns after sending a request to start service discovery to the framework. The application is notified of a success to initiate discovery through the callback DiscoveryListener#onDiscoveryStarted or a failure through DiscoveryListener#onStartDiscoveryFailed.

Upon successful start, application is notified when a service is found with DiscoveryListener#onServiceFound or when a service is lost with DiscoveryListener#onServiceLost.

Upon failure to start, service discovery is not active and application does not need to invoke stopServiceDiscovery

The application should call stopServiceDiscovery when discovery of this service type is no longer required, and/or whenever the application is paused or stopped.

Parameters
discoveryRequest DiscoveryRequest: the DiscoveryRequest object which specifies the discovery parameters such as service type, subtype and network This value cannot be null.
executor Executor: Executor to run listener callbacks with This value cannot be null.
listener NsdManager.DiscoveryListener: The listener notifies of a successful discovery and is used to stop discovery on this serviceType through a call on stopServiceDiscovery. This value cannot be null.

discoverServices

fun discoverServices(
    serviceType: String,
    protocolType: Int,
    networkRequest: NetworkRequest,
    executor: Executor,
    listener: NsdManager.DiscoveryListener
): Unit

Initiate service discovery to browse for instances of a service type. Service discovery consumes network bandwidth and will continue until the application calls stopServiceDiscovery.

The function call immediately returns after sending a request to start service discovery to the framework. The application is notified of a success to initiate discovery through the callback DiscoveryListener#onDiscoveryStarted or a failure through DiscoveryListener#onStartDiscoveryFailed.

Upon successful start, application is notified when a service is found with DiscoveryListener#onServiceFound or when a service is lost with DiscoveryListener#onServiceLost.

Upon failure to start, service discovery is not active and application does not need to invoke stopServiceDiscovery

The application should call stopServiceDiscovery when discovery of this service type is no longer required, and/or whenever the application is paused or stopped.

During discovery, new networks may connect or existing networks may disconnect - for example if wifi is reconnected. When a service was found on a network that disconnects, DiscoveryListener#onServiceLost will be called. If a new network connects that matches the NetworkRequest, DiscoveryListener#onServiceFound will be called for services found on that network. Applications that do not want to track networks themselves are encouraged to use this method instead of other overloads of discoverServices, as they will receive proper notifications when a service becomes available or unavailable due to network changes.
Requires android.Manifest.permission#ACCESS_NETWORK_STATE

Parameters
serviceType String: The service type being discovered. Examples include "_http._tcp" for http services or "_ipp._tcp" for printers This value cannot be null.
protocolType Int: The service discovery protocol
networkRequest NetworkRequest: Request specifying networks that should be considered when discovering This value cannot be null.
executor Executor: Executor to run listener callbacks with This value cannot be null.
listener NsdManager.DiscoveryListener: The listener notifies of a successful discovery and is used to stop discovery on this serviceType through a call on stopServiceDiscovery. This value cannot be null.

registerService

fun registerService(
    serviceInfo: NsdServiceInfo!,
    protocolType: Int,
    listener: NsdManager.RegistrationListener!
): Unit

Register a service to be discovered by other services.

The function call immediately returns after sending a request to register service to the framework. The application is notified of a successful registration through the callback RegistrationListener#onServiceRegistered or a failure through RegistrationListener#onRegistrationFailed.

The application should call unregisterService when the service registration is no longer required, and/or whenever the application is stopped.

Parameters
serviceInfo NsdServiceInfo!: The service being registered
protocolType Int: The service discovery protocol
listener NsdManager.RegistrationListener!: The listener notifies of a successful registration and is used to unregister this service through a call on unregisterService. Cannot be null. Cannot be in use for an active service registration.

registerService

fun registerService(
    serviceInfo: NsdServiceInfo,
    protocolType: Int,
    executor: Executor,
    listener: NsdManager.RegistrationListener
): Unit

Register a service to be discovered by other services.

The function call immediately returns after sending a request to register service to the framework. The application is notified of a successful registration through the callback RegistrationListener#onServiceRegistered or a failure through RegistrationListener#onRegistrationFailed.

The application should call unregisterService when the service registration is no longer required, and/or whenever the application is stopped.

Parameters
serviceInfo NsdServiceInfo: The service being registered This value cannot be null.
protocolType Int: The service discovery protocol
executor Executor: Executor to run listener callbacks with This value cannot be null.
listener NsdManager.RegistrationListener: The listener notifies of a successful registration and is used to unregister this service through a call on unregisterService. Cannot be null.

registerServiceInfoCallback

fun registerServiceInfoCallback(
    serviceInfo: NsdServiceInfo,
    executor: Executor,
    listener: NsdManager.ServiceInfoCallback
): Unit

Register a callback to listen for updates to a service. An application can listen to a service to continuously monitor availability of given service. The callback methods will be called on the passed executor. And service updates are sent with continuous calls to ServiceInfoCallback#onServiceUpdated. This is different from #resolveService which provides one shot service information.

An application can listen to a service once a time. It needs to cancel the registration before registering other callbacks. Upon failure to register a callback for example if it's a duplicated registration, the application is notified through ServiceInfoCallback#onServiceInfoCallbackRegistrationFailed with FAILURE_BAD_PARAMETERS.

Parameters
serviceInfo NsdServiceInfo: the service to receive updates for This value cannot be null.
executor Executor: Executor to run callbacks with This value cannot be null.
listener NsdManager.ServiceInfoCallback: to receive callback upon service update This value cannot be null.

resolveService

Added in API level 16
Also in T Extensions 3
Deprecated in API level 36
fun resolveService(
    serviceInfo: NsdServiceInfo!,
    listener: NsdManager.ResolveListener!
): Unit

Deprecated: the returned ServiceInfo may get stale at any time after resolution, including immediately after the callback is called, and may not contain some service information that could be delivered later, like additional host addresses. Prefer using registerServiceInfoCallback, which will keep the application up-to-date with the state of the service.

Resolve a discovered service. An application can resolve a service right before establishing a connection to fetch the IP and port details on which to setup the connection.

Parameters
serviceInfo NsdServiceInfo!: service to be resolved
listener NsdManager.ResolveListener!: to receive callback upon success or failure. Cannot be null. Cannot be in use for an active service resolution.

resolveService

Added in API level 33
Also in T Extensions 3
Deprecated in API level 36
fun resolveService(
    serviceInfo: NsdServiceInfo,
    executor: Executor,
    listener: NsdManager.ResolveListener
): Unit

Deprecated: the returned ServiceInfo may get stale at any time after resolution, including immediately after the callback is called, and may not contain some service information that could be delivered later, like additional host addresses. Prefer using registerServiceInfoCallback, which will keep the application up-to-date with the state of the service.

Resolve a discovered service. An application can resolve a service right before establishing a connection to fetch the IP and port details on which to setup the connection.

Parameters
serviceInfo NsdServiceInfo: service to be resolved This value cannot be null.
executor Executor: Executor to run listener callbacks with This value cannot be null.
listener NsdManager.ResolveListener: to receive callback upon success or failure. This value cannot be null.

stopServiceDiscovery

Added in API level 16
fun stopServiceDiscovery(listener: NsdManager.DiscoveryListener!): Unit

Stop service discovery initiated with #discoverServices. An active service discovery is notified to the application with DiscoveryListener#onDiscoveryStarted and it stays active until the application invokes a stop service discovery. A successful stop is notified to with a call to DiscoveryListener#onDiscoveryStopped.

Upon failure to stop service discovery, application is notified through DiscoveryListener#onStopDiscoveryFailed.

Parameters
listener NsdManager.DiscoveryListener!: This should be the listener object that was passed to #discoverServices. It identifies the discovery that should be stopped and notifies of a successful or unsuccessful stop. In API versions 20 and above, the listener object may be used for another service discovery once the callback has been called. In API versions <= 19, there is no entirely reliable way to know when a listener may be re-used, and a new listener should be created for each service discovery request.

stopServiceResolution

fun stopServiceResolution(listener: NsdManager.ResolveListener): Unit

Stop service resolution initiated with #resolveService. A successful stop is notified with a call to ResolveListener#onResolutionStopped.

Upon failure to stop service resolution for example if resolution is done or the requester stops resolution repeatedly, the application is notified ResolveListener#onStopResolutionFailed with FAILURE_OPERATION_NOT_RUNNING

Parameters
listener NsdManager.ResolveListener: This should be a listener object that was passed to #resolveService. It identifies the resolution that should be stopped and notifies of a successful or unsuccessful stop. Throws IllegalArgumentException if the listener was not passed to resolveService before. This value cannot be null.

unregisterService

Added in API level 16
fun unregisterService(listener: NsdManager.RegistrationListener!): Unit

Unregister a service registered through #registerService. A successful unregister is notified to the application with a call to RegistrationListener#onServiceUnregistered.

Parameters
listener NsdManager.RegistrationListener!: This should be the listener object that was passed to #registerService. It identifies the service that should be unregistered and notifies of a successful or unsuccessful unregistration via the listener callbacks. In API versions 20 and above, the listener object may be used for another service registration once the callback has been called. In API versions <= 19, there is no entirely reliable way to know when a listener may be re-used, and a new listener should be created for each service registration request.

unregisterServiceInfoCallback

fun unregisterServiceInfoCallback(listener: NsdManager.ServiceInfoCallback): Unit

Unregister a callback registered with registerServiceInfoCallback. A successful unregistration is notified with a call to ServiceInfoCallback#onServiceInfoCallbackUnregistered. The same callback can only be reused after this is called.

If the callback is not already registered, this will throw with IllegalArgumentException.

Parameters
listener NsdManager.ServiceInfoCallback: This should be a listener object that was passed to registerServiceInfoCallback. It identifies the registration that should be unregistered and notifies of a successful or unsuccessful stop. Throws IllegalArgumentException if the listener was not passed to registerServiceInfoCallback before. This value cannot be null.