Skip to content

Instantly share code, notes, and snippets.

@maydin
Last active September 8, 2016 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maydin/5a6c3684819c1893c3773fb8e78081a2 to your computer and use it in GitHub Desktop.
Save maydin/5a6c3684819c1893c3773fb8e78081a2 to your computer and use it in GitHub Desktop.
Discovery Listener Class
public class MyDiscoveryListener implements NsdManager.DiscoveryListener {
private static final String TAG = "MyDiscoveryListener";
public static final String SERVICE_TYPE = "_http._tcp.";
String mServiceName;
NsdManager mNsdManager;
MainActivity activity;
public MyDiscoveryListener(String mServiceName,NsdManager mNsdManager,MainActivity activity)
{
this.mServiceName = mServiceName;
this.mNsdManager = mNsdManager;
this.activity = activity;
}
// Called as soon as service discovery begins.
@Override
public void onDiscoveryStarted(String regType) {
Log.d(TAG, "Service discovery started");
}
@Override
public void onServiceFound(NsdServiceInfo service) {
// A service was found! Do something with it.
Log.d(TAG, "Service discovery success>> " + service);
if (!service.getServiceType().equals(SERVICE_TYPE)) {
// Service type is the string containing the protocol and
// transport layer for this service.
Log.d(TAG, "Unknown Service Type: " + service.getServiceType());
} else {
this.activity.addAppliance(new Appliance(service.getServiceName()));
}
}
@Override
public void onServiceLost(NsdServiceInfo service) {
this.activity.removeAppliance(new Appliance(service.getServiceName()));
}
@Override
public void onDiscoveryStopped(String serviceType) {
Log.i(TAG, "Discovery stopped: " + serviceType);
}
@Override
public void onStartDiscoveryFailed(String serviceType, int errorCode) {
Log.e(TAG, "Discovery failed: Error code:" + errorCode);
mNsdManager.stopServiceDiscovery(this);
}
@Override
public void onStopDiscoveryFailed(String serviceType, int errorCode) {
Log.e(TAG, "Discovery failed: Error code:" + errorCode);
mNsdManager.stopServiceDiscovery(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment