Say "Hi!" to RxBonjour, a wrapper around Android's network service discovery functionalities with a support implementation for devices below Jelly Bean, going down all the way to API level 9.
RxBonjour
is available on jcenter()
:
compile "com.github.aurae:rxbonjour:0.4.0"
Create a network service discovery request using RxBonjour.newDiscovery(Context, String)
and subscribe to the returned Observable
:
RxBonjour.newDiscovery(this, "_http._tcp")
.subscribe(bonjourEvent -> {
BonjourService item = bonjourEvent.getService();
switch (bonjourEvent.getType()) {
case ADDED:
// Called when a service was discovered
break;
case REMOVED:
// Called when a service is no longer visible
break;
}
}, error -> {
// Service discovery failed, for instance
});
RxBonjour pre-configures the returned Observables to run on an I/O thread, but return their callbacks on the main thread. The discovery will be stopped automatically upon unsubscribing from the Observable.
Create a service to broadcast using RxBonjour.newBroadcast(Context, String)
and subscribe to the returned Observable
of the broadcast object:
BonjourBroadcast<?> broadcast = RxBonjour.newBroadcast("_http._tcp")
.name("My Broadcast")
.port(65335)
.build();
broadcast.start(this)
.subscribe(bonjourEvent -> {
// Same as above
});
RxBonjour pre-configures the returned Observables to run on an I/O thread, but return their callbacks on the main thread. The broadcast will be stopped automatically upon unsubscribing from the Observable.
RxBonjour comes with two implementations for network service discovery. By default, the support implementation is used because of the unreliable state of the NsdManager
APIs and known bugs with that. If you really want to use NsdManager
on devices running Jelly Bean and up though, you can specify this when creating service discovery Observables:
// If you're feeling real and ready to reboot your device once NsdManager breaks, pass in "true" to use it for supported devices
RxBonjour.newDiscovery(this, "_http._tcp", true)
.subscribe(bonjourEvent -> {
// ...
}, error -> {
// ...
});
On devices running Jelly Bean and up, Android's native Network Service Discovery API, centered around NsdManager
, can be used.
The support implementation utilizes the latest available version of jmDNS (a snapshot of version 3.4.2) and a WifiManager
multicast lock as its service discovery backbone; because of this, including this library in your application's dependencies automatically adds the following permissions to your AndroidManifest.xml
, in order to allow jmDNS to do its thing:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
Copyright 2016 Marcel Schnelle
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.