Class BonjourBrowser
java.lang.Object
com.codename1.io.bonjour.BonjourBrowser
public final class BonjourBrowser
extends java.lang.Object
Browses the local network for Bonjour / mDNS services.
Bonjour ("zeroconf" / "mDNS-SD") lets clients find network services -- a
printer, a music server, another instance of your app -- without knowing
their IP address in advance. Services are advertised under a type such as
_http._tcp. or your own private _myapp._tcp.. A BonjourBrowser watches
for services of one type and notifies the listener whenever a service comes
online or goes away.
Platform support
- Android:
android.net.nsd.NsdManager. - iOS:
NSNetServiceBrowser+NSNetService. The build pipeline injectsNSLocalNetworkUsageDescriptionand the service type intoNSBonjourServicesso iOS 14+ doesn't block discovery. - Simulator: JmDNS is used when present on the classpath; otherwise discovery is a no-op and the listener is told the platform is unsupported.
Example
BonjourBrowser browser = BonjourBrowser.browse("_http._tcp.", new BonjourServiceListener() {
public void onServiceResolved(BonjourService s) {
Log.p("Found " + s.getName() + " at " + s.getHost() + ":" + s.getPort());
}
public void onServiceLost(BonjourService s) { /* ... */ }
public void onBrowseError(Throwable t) { Log.e(t); }
});
// when finished
browser.stop();
-
Method Summary
Modifier and TypeMethodDescriptionstatic BonjourBrowserbrowse(String type, BonjourServiceListener listener) Starts browsing fortypeand returns a handle to stop the search.getType()The service type passed tobrowse(...).static booleantrueif the platform implements Bonjour at all.voidstop()Stops this browser.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Method Details
-
browse
Starts browsing fortypeand returns a handle to stop the search.typemust be in mDNS form, e.g._http._tcp.(trailing dot optional).listeneris invoked on the EDT. -
isSupported
public static boolean isSupported()trueif the platform implements Bonjour at all. -
getType
The service type passed tobrowse(...). -
stop
public void stop()Stops this browser. Idempotent.
-