Skip to content

ToxicBakery/Android-Nsd-Rx

Repository files navigation

Android Network Service Discovery Rx

CircleCI Central

An Rx wrapper around the Android NsdManager api.

Install

You can find this library on MavenCentral

implementation 'com.ToxicBakery.library.nsd.rx:android-nsd-rx:3.+'

Creating a Manager

The NsdManagerRx can be created with a context instance.

NsdManagerRx.fromContext(context)

Discovery

The service name used is defined in rfc6763 and finds all services without filtering. ietf.org/rfc/rfc6763.txt

NsdManagerRx(context)
    .discoverServices(DiscoveryConfiguration("_services._dns-sd._udp"))
    .subscribe { event: DiscoveryEvent -> Log.d("Discovery", "${event.service.serviceName}") }

Register

NsdManagerRx(context)
    .registerService(RegistrationConfiguration(port = 12345))
    .subscribe { event: RegistrationEvent ->
        Log.d("Registration", "Registered ${event.nsdServiceInfo.serviceName}")
    }

Resolve

NsdManagerRx(context)
    .resolveService(serviceInfo)
    .subscribe { event: ResolveEvent ->
        Log.d("Resolve", "Resolved ${event.nsdServiceInfo.serviceName}")
    }

Binders

Discovery and registration return binders in their events. The binders allow the internal listener to be unregistered without disposing the observable. This is important if you want to receive the unregister and stop events from the Android NsdManager. Alternately, disposing of the observables will unregister the internal listeners.

Demo App

The demo app provides a quick way to run the NSD service against your connected network.

Demo Application Screenshot