STEP ONE: Register Your Service on the Network

1) Define your service as a NsdServiceInfo object
2) Init socket server if using one
3) Create a NsdManager.RegistrationListener object
4) Call NsdManager.serviceInfo:NsdServiceInfo, protocolType:int, listener:NsdManager.RegistrationListener

STEP TWO: Discover Services on the Network

1) Create a NsdManager.DiscoveryListener object
2) Call NsdManager.discoverServices (serviceType:String, protocolType:int, listner:NsdManager.DiscoveryListener)

When defining this listener, it needs to handle:

– Verification the type of service
– If it has found it’s own service by picking up it’s own broadcast
– Verification the name of service

STEP THREE: Connect to Services on The Network

Resolve Phase – Obtain connection information prior to connection

) Create a NsdManager.ResolveListener object
) Provided that the discover found the correct service, call NsdManager.resolveService(serviceInfo:NsdServiceInfo, listener:NsdManager.ResolveListener)

When defining this listener, it needs to handle:
– If the service names match, then it’s on the same IP
– If it finds the correct service is found, then keep a reference to that service’s NsdServiceInfo object, port number and IP address represented as an java.net.InetAddress object

STEP FOUR: Unregister Your Service on Application Close

– Availability of NSD should be tied to app lifecycle
– Unregister when app is closed so that other apps stop trying to connect to it
– NSD is expensive so invoke it only when the user needs it

Source – http://developer.android.com/training/connect-devices-wirelessly/nsd.html

Share

Google+ Comments