Last active
September 8, 2016 14:18
-
-
Save maydin/5a6c3684819c1893c3773fb8e78081a2 to your computer and use it in GitHub Desktop.
Discovery Listener Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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