Back to project page Android-Apps.
The source code is released under:
Apache License
If you think the Android project Android-Apps listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.kniezrec.xbmcgear.connection; //w w w. j av a2 s . c o m import android.content.Context; import android.net.nsd.NsdManager; import android.net.nsd.NsdServiceInfo; import android.os.Handler; import android.util.Log; import java.util.HashSet; import java.util.Set; public class NSDSearch extends Thread { public final static String UNRESOLVED = "HostParcel"; public final static String RESOLVED = "RESOLVEDParcel"; public final static int ADD_TO_RESOLVED = 101; private static final String SERVICE_TYPE = "_xbmc-jsonrpc-h._tcp"; private static final String TAG = "NSDSearch"; private final Set<String> helpSet = new HashSet<String>(); private final Handler handler; private NsdManager mNsdManager; private NsdManager.DiscoveryListener mDiscoveryListener; private Thread resolverThread; public NSDSearch(Handler handler) { this.handler = handler; } public void stopDiscovery() { if (mNsdManager != null) { mNsdManager.stopServiceDiscovery(mDiscoveryListener); } } @Override public void run() { Context context = AndroidApplication.getInstance() .getApplicationContext(); mNsdManager = (NsdManager) context .getSystemService(Context.NSD_SERVICE); mDiscoveryListener = new NsdManager.DiscoveryListener() { @Override public void onDiscoveryStarted(String regType) { Log.d(TAG, "Service discovery started"); } @Override public void onServiceFound(NsdServiceInfo service) { Log.d(TAG, "Service discovery success" + service.getServiceName()); if (!helpSet.contains(service.getServiceName())) { helpSet.add(service.getServiceName()); NSDResolve resolver = new NSDResolve(handler, service); resolverThread = new Thread(resolver); resolverThread.start(); try { Thread.sleep(700); } catch (InterruptedException e) { e.printStackTrace(); } } } @Override public void onServiceLost(NsdServiceInfo service) { Log.e(TAG, "service lost" + service); } @Override public void onDiscoveryStopped(String serviceType) { Log.i(TAG, "Discovery stopped: " + serviceType); if (resolverThread != null && resolverThread.isAlive()) { resolverThread.interrupt(); } } @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); } }; mNsdManager.discoverServices(SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener); } }