Android NSD注册服务,发现服务



import android.content.Context;
import android.net.nsd.NsdManager;
import android.net.nsd.NsdServiceInfo;
import android.util.Log;


public  class NSDHelper {
    private static final String TAG = NSDHelper.class.getSimpleName();
    private NsdManager.RegistrationListener mRegistrationListener;
    private NsdManager.DiscoveryListener mDiscoveryListener;
    private NsdManager mNsdManager;
    private String mServiceName;

    public NSDHelper(Context context) {
        mNsdManager = (NsdManager) context.getApplicationContext().getSystemService(Context.NSD_SERVICE);
    }

    /**
     * 注册NSD 服务,用来广播自己的服务
     */
    public void registerService(String serviceName, int port, NsdManager.RegistrationListener listener) {
        mServiceName = (serviceName == null) ? Constants.NSD_SERVER_DEFAULT_NAME : serviceName;
        NsdServiceInfo serviceInfo = new NsdServiceInfo();
        serviceInfo.setPort(port);
        serviceInfo.setServiceName(mServiceName);
        serviceInfo.setServiceType(Constants.NSD_SERVER_TYPE);
        initRegisterationListener(listener);
        mNsdManager.registerService(serviceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);
    }

    /**
     * 取消注册服务
     */
    public void unRegisterService() {
        mNsdManager.unregisterService(mRegistrationListener);
    }

    private void initRegisterationListener(NsdManager.RegistrationListener listener) {
        if (listener == null) {
            createDefaultListener();
        } else {
            mRegistrationListener = listener;
        }
    }

    private void createDefaultListener() {
        mRegistrationListener = new NsdManager.RegistrationListener() {
            @Override
            public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
                Log.i(TAG, "onRegistrationFailed,errCode = " + errorCode);
            }

            @Override
            public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
                Log.i(TAG, "onUnregistrationFailed,errCode = " + errorCode);
            }

            @Override
            public void onServiceRegistered(NsdServiceInfo serviceInfo) {
                Log.i(TAG, "onServiceRegistered,serviceInfo=" + serviceInfo.toString());
            }

            @Override
            public void onServiceUnregistered(NsdServiceInfo serviceInfo) {
                Log.i(TAG, "onServiceUnregistered,serviceInfo=" + serviceInfo.toString());
            }
        };
    }

    /**
     * 开始设备发现服务
     */
    public void startDiscoveryService(NsdManager.DiscoveryListener discoveryListener, NsdManager.ResolveListener resolveListener) {
        initDiscoveryListener(discoveryListener);
        mNsdManager.discoverServices(Constants.NSD_SERVER_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
    }

    /**
     * 停止设备发现服务
     */
    public void stopDiscoveryService() {
        mNsdManager.stopServiceDiscovery(mDiscoveryListener);
    }

    private void initDiscoveryListener(NsdManager.DiscoveryListener discoveryListener) {
        if (discoveryListener == null) {
            createDefaultDiscoveryListener();
        } else {
            mDiscoveryListener = discoveryListener;
        }

    }

    private void createDefaultDiscoveryListener() {
        mDiscoveryListener = new NsdManager.DiscoveryListener() {
            @Override
            public void onStartDiscoveryFailed(String serviceType, int errorCode) {

            }

            @Override
            public void onStopDiscoveryFailed(String serviceType, int errorCode) {

            }

            @Override
            public void onDiscoveryStarted(String serviceType) {

            }

            @Override
            public void onDiscoveryStopped(String serviceType) {

            }

            @Override
            public void onServiceFound(NsdServiceInfo serviceInfo) {
                Log.i(TAG, "onServiceFound,serviceInfo=  " + serviceInfo.toString());
                if(serviceInfo.getServiceName().equalsIgnoreCase(mServiceName)){
                    mNsdManager.resolveService(serviceInfo, new NsdManager.ResolveListener() {
                        @Override
                        public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {

                        }

                        @Override
                        public void onServiceResolved(NsdServiceInfo serviceInfo) {
                            Log.i(TAG, "onServiceResolved,serviceInfo=  " + serviceInfo.toString());
                        }
                    });
                }
            }

            @Override
            public void onServiceLost(NsdServiceInfo serviceInfo) {
                Log.i(TAG, "onServiceLost,serviceInfo=  " + serviceInfo.toString());
            }
        };
    }


}

下面是打印的日志:

 onServiceRegistered,serviceInfo=name: dy.com.alphad.nsd, type: null, host: null, port: 0
 I/NSDHelper: onServiceFound,serviceInfo=  name: dy.com.alphad.nsd, type: _http._tcp., host: null, port: 0
 I/NSDHelper: onServiceResolved,serviceInfo=  name: dy.com.alphad.nsd, type: ._http._tcp, host: .....165, port: 1008

可以看出来,1.自己注册的服务也可以在被自己在发现服务的时候找到

                    2.serviceinfo 在解析之前只能读取到名称,解析之后才能看到全部信息。

另外(发现服务)和(向外广播自己的服务)据说比较耗电,记得开启INTENET权限。

Android NSD注册服务,发现服务_第1张图片

你可能感兴趣的:(Android NSD注册服务,发现服务)