Code example for NsdManager

Methods: discoverServices, registerService

0
 
        initializeDiscoveryListener(); 
        initializeRegistrationListener(); 
        initializeResolveListener(); 
 
        mNsdManager.discoverServices(SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, 
                mDiscoveryListener); 
 
        registerService(intent.getIntExtra("PORT", 0)); 
 
        return super.onStartCommand(intent, flags, startId); 
    } 
 
    @Override 
    public void onDestroy() { 
        running = false;
        mNsdManager.unregisterService(mRegistrationListener);
        mNsdManager.stopServiceDiscovery(mDiscoveryListener);
        super.onDestroy(); 
    } 
 
    private void registerService(int port) {
        NsdServiceInfo serviceInfo = new NsdServiceInfo();
 
        serviceInfo.setServiceName(Build.SERIAL + "-" + new Date().getTime());
        serviceInfo.setServiceType(SERVICE_TYPE);
        serviceInfo.setPort(port);
 
        mNsdManager.registerService(serviceInfo, NsdManager.PROTOCOL_DNS_SD,
                mRegistrationListener);
    } 
 
    @Override 
    public IBinder onBind(Intent intent) {