Api Demo | 接口原型

NsdManager

2022-01-11T07:53:20
--------------------------------------------------------------------------------------------------------

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(NsdServiceInfo, int, NsdManager.RegistrationListener). 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(String, int, NsdManager.DiscoveryListener). 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(NsdServiceInfo, NsdManager.ResolveListener) 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

See also:



discoverServices(String serviceType, int protocolType, NsdManager.DiscoveryListener listener)

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


registerService(NsdServiceInfo serviceInfo, int protocolType, NsdManager.RegistrationListener listener)

Register a service to be discovered by other services.


resolveService(NsdServiceInfo serviceInfo, NsdManager.ResolveListener listener)

Resolve a discovered service.


stopServiceDiscovery(NsdManager.DiscoveryListener listener)

Stop service discovery initiated with discoverServices(String, int, NsdManager.DiscoveryListener).


unregisterService(NsdManager.RegistrationListener listener)

Unregister a service registered through registerService(NsdServiceInfo, int, NsdManager.RegistrationListener).