Java Code Examples for android.net.nsd.NsdManager#DiscoveryListener

The following examples show how to use android.net.nsd.NsdManager#DiscoveryListener . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainActivity.java    From Rucky with GNU General Public License v3.0 5 votes vote down vote up
private void initPiDiscoveryListener() {
    piDiscoveryListener = new NsdManager.DiscoveryListener() {
        @Override
        public void onStartDiscoveryFailed(String serviceType, int errorCode) {
            mNsdManager.stopServiceDiscovery(this);
        }

        @Override
        public void onStopDiscoveryFailed(String serviceType, int errorCode) {
            mNsdManager.stopServiceDiscovery(this);
        }

        @Override
        public void onDiscoveryStarted(String serviceType) {
        }

        @Override
        public void onDiscoveryStopped(String serviceType) {
        }

        @Override
        public void onServiceFound(NsdServiceInfo serviceInfo) {
            String name = serviceInfo.getServiceName();
            String type = serviceInfo.getServiceType();
            if (type.equals(SERVICE_TYPE) && name.contains("Rucky")) {
                mNsdManager.resolveService(serviceInfo, piResolveListener);
            }
        }

        @Override
        public void onServiceLost(NsdServiceInfo serviceInfo) {
        }
    };
}
 
Example 2
Source File: NsdHelper.java    From snapdroid with GNU General Public License v3.0 4 votes vote down vote up
private void initializeDiscoveryListener() {
    mDiscoveryListener = new NsdManager.DiscoveryListener() {
        @Override
        public void onStartDiscoveryFailed(String serviceType, int errorCode) {
            Log.d(TAG, "Discovery failed");
        }

        @Override
        public void onStopDiscoveryFailed(String serviceType, int errorCode) {
            Log.d(TAG, "Stopping discovery failed");
        }

        @Override
        public void onDiscoveryStarted(String serviceType) {
            Log.d(TAG, "Discovery started");
        }

        @Override
        public void onDiscoveryStopped(String serviceType) {
            Log.d(TAG, "Discovery stopped");
        }

        @Override
        public void onServiceFound(NsdServiceInfo serviceInfo) {
            NsdServiceInfo info = serviceInfo;
            Log.d(TAG, "Service found: " + info.getServiceName());
            if (info.getServiceName().startsWith(serviceName)) {
                try {
                    mNsdManager.resolveService(info, mResolveListener);
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onServiceLost(NsdServiceInfo serviceInfo) {
            NsdServiceInfo info = serviceInfo;
            Log.d(TAG, "Service lost: " + info.getServiceName());
        }
    };
}
 
Example 3
Source File: ServiceDiscovery.java    From PHONK with GNU General Public License v3.0 4 votes vote down vote up
Discover(Context a, final String serviceType) {
    mServiceType = serviceType;
    mNsdManager = (NsdManager) a.getSystemService(Context.NSD_SERVICE);

    // Instantiate mContext new DiscoveryListener
    mDiscoveryListener = new NsdManager.DiscoveryListener() {

        //  Called as soon as service discovery begins.
        @Override
        public void onDiscoveryStarted(String regType) {
            MLog.d(TAG, "Service discovery started");
            ReturnObject ret = new ReturnObject();
            ret.put("name", regType);
            ret.put("status", "started");
            if (mCallback != null) mCallback.event(ret);
        }

        @Override
        public void onServiceFound(final NsdServiceInfo serviceInfo) {
            // A service was found!  Do something with it.

            MLog.d(TAG, "1: " + serviceInfo.getServiceType() + " 2: " + mServiceType);
            if (serviceInfo.getServiceType().equals(mServiceType)) {
                mNsdManager.resolveService(serviceInfo, new NsdManager.ResolveListener() {
                    @Override
                    public void onResolveFailed(NsdServiceInfo nsdServiceInfo, int i) {

                    }

                    @Override
                    public void onServiceResolved(NsdServiceInfo nsdServiceInfo) {
                        ReturnObject ret = new ReturnObject();
                        ret.put("status", "discovered_resolved");
                        ret.put("port", serviceInfo.getPort());
                        ret.put("serviceName", serviceInfo.getServiceName());
                        ret.put("host", serviceInfo.getHost());
                        ret.put("type", serviceInfo.getServiceType());
                        if (mCallback != null) mCallback.event(ret);
                    }
                });
            }

        }


        @Override
        public void onServiceLost(NsdServiceInfo serviceInfo) {
            // When the network service is no longer available.
            // Internal bookkeeping code goes here.
            Log.e(TAG, "service lost");
            ReturnObject ret = new ReturnObject();
            ret.put("status", "service_lost");
            ret.put("port", serviceInfo.getPort());
            ret.put("serviceName", serviceInfo.getServiceName());
            ret.put("host", serviceInfo.getHost());
            ret.put("type", serviceInfo.getServiceType());
            if (mCallback != null) mCallback.event(ret);
        }

        @Override
        public void onDiscoveryStopped(String serviceType) {
            Log.i(TAG, "Discovery stopped: " + serviceType);
            ReturnObject ret = new ReturnObject();
            ret.put("type", serviceType);
            ret.put("status", "discovery_stopped");
            if (mCallback != null) mCallback.event(ret);
        }

        @Override
        public void onStartDiscoveryFailed(String serviceType, int errorCode) {
            Log.e(TAG, "Discovery failed: Error code:" + errorCode);
            //mNsdManager.stopServiceDiscovery(this);
            ReturnObject ret = new ReturnObject();
            ret.put("type", serviceType);
            ret.put("error", errorCode);
            ret.put("status", "discovery_failed");
            if (mCallback != null) mCallback.event(ret);
        }

        @Override
        public void onStopDiscoveryFailed(String serviceType, int errorCode) {
            Log.e(TAG, "Discovery failed: Error code:" + errorCode);
            ReturnObject ret = new ReturnObject();
            ret.put("name", serviceType);
            ret.put("error", errorCode);
            ret.put("status", "stop_discovering_failed");
            if (mCallback != null) mCallback.event(ret);
        }
    };
}
 
Example 4
Source File: Mdns.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
private void initializeDiscoveryListener() {

        if (mDiscoveryListener != null) {
            try {
                mNsdManager.stopServiceDiscovery(mDiscoveryListener);
                UserError.Log.wtf(TAG, "Discovery service was active when it shouldn't be!");
            } catch (Exception e) {
                UserError.Log.d(TAG, "Could not stop service during initialization: " + e);
            }
        }
        mDiscoveryListener = new NsdManager.DiscoveryListener() {


            @Override
            public void onDiscoveryStarted(String regType) {
            }

            @Override
            public synchronized void onServiceFound(final NsdServiceInfo service) {

                final String type = service.getServiceType();

                UserError.Log.d(TAG, "onServiceFound " + type + service.getServiceName());
                if (type.equals(SERVICE_TYPE)) {
                    final String name = service.getServiceName();
                    final LookUpInfo li = iplookup.get(shortenName(name));
                    if ((li == null) || (JoH.msSince(li.received) > CACHE_REFRESH_MS)) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                singleResolveService(service);
                            }
                        }).start();
                    } else {
                        UserError.Log.d(TAG, "Already have recent data for: " + name + " => " + li.address);
                    }
                }
            }


            @Override
            public void onServiceLost(NsdServiceInfo service) {
            }

            @Override
            public void onDiscoveryStopped(String serviceType) {
            }

            @Override
            public void onStartDiscoveryFailed(String serviceType, int errorCode) {
                myStopServiceDiscovery();
            }

            @Override
            public void onStopDiscoveryFailed(String serviceType, int errorCode) {
                if (JoH.ratelimit("mdns-onStopDiscoveryFailed", 10)) {
                    myStopServiceDiscovery();
                }
            }

            private void myStopServiceDiscovery() {
                try {
                    mNsdManager.stopServiceDiscovery(this);
                } catch (IllegalArgumentException | IllegalStateException e) {
                    UserError.Log.e(TAG, "Could not stop service discovery: " + e);
                }
            }
        };
    }
 
Example 5
Source File: Mdns.java    From xDrip-plus with GNU General Public License v3.0 4 votes vote down vote up
private void initializeDiscoveryListener() {

        if (mDiscoveryListener != null) {
            try {
                mNsdManager.stopServiceDiscovery(mDiscoveryListener);
                UserError.Log.wtf(TAG, "Discovery service was active when it shouldn't be!");
            } catch (Exception e) {
                UserError.Log.d(TAG, "Could not stop service during initialization: " + e);
            }
        }
        mDiscoveryListener = new NsdManager.DiscoveryListener() {


            @Override
            public void onDiscoveryStarted(String regType) {
            }

            @Override
            public synchronized void onServiceFound(final NsdServiceInfo service) {

                final String type = service.getServiceType();

                UserError.Log.d(TAG, "onServiceFound " + type + service.getServiceName());
                if (type.equals(SERVICE_TYPE)) {
                    final String name = service.getServiceName();
                    final LookUpInfo li = iplookup.get(shortenName(name));
                    if ((li == null) || (JoH.msSince(li.received) > CACHE_REFRESH_MS)) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                singleResolveService(service);
                            }
                        }).start();
                    } else {
                        UserError.Log.d(TAG, "Already have recent data for: " + name + " => " + li.address);
                    }
                }
            }


            @Override
            public void onServiceLost(NsdServiceInfo service) {
            }

            @Override
            public void onDiscoveryStopped(String serviceType) {
            }

            @Override
            public void onStartDiscoveryFailed(String serviceType, int errorCode) {
                myStopServiceDiscovery();
            }

            @Override
            public void onStopDiscoveryFailed(String serviceType, int errorCode) {
                if (JoH.ratelimit("mdns-onStopDiscoveryFailed", 10)) {
                    myStopServiceDiscovery();
                }
            }

            private void myStopServiceDiscovery() {
                try {
                    mNsdManager.stopServiceDiscovery(this);
                } catch (IllegalArgumentException | IllegalStateException e) {
                    UserError.Log.e(TAG, "Could not stop service discovery: " + e);
                }
            }
        };
    }
 
Example 6
Source File: DiscoverResolver.java    From tinydnssd with MIT License 4 votes vote down vote up
protected void discoverServices(String serviceType, int protocol, NsdManager.DiscoveryListener listener) {
    ((NsdManager) mContext.getSystemService(Context.NSD_SERVICE)).discoverServices(serviceType, protocol, listener);
}
 
Example 7
Source File: DiscoverResolver.java    From tinydnssd with MIT License 4 votes vote down vote up
protected void stopServiceDiscovery(NsdManager.DiscoveryListener listener) {
    ((NsdManager) mContext.getSystemService(Context.NSD_SERVICE)).stopServiceDiscovery(listener);
}
 
Example 8
Source File: NSDDiscoveryTools.java    From commcare-android with Apache License 2.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static void initializeDiscoveryListener() {

    // Instantiate a new DiscoveryListener
    mDiscoveryListener = new NsdManager.DiscoveryListener() {

        //  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 if (service.getServiceName().equals(SERVICE_NAME)) {
                Log.d(TAG, "Found CommCare Micronode");

                mNsdManager.resolveService(service, getResolveListener());
            }
        }

        @Override
        public void onServiceLost(NsdServiceInfo service) {
            // When the network service is no longer available.
            // Internal bookkeeping code goes here.
            Log.e(TAG, "service lost" + service);
        }

        @Override
        public void onDiscoveryStopped(String serviceType) {
            Log.i(TAG, "Discovery stopped: " + serviceType);
            state = NsdState.Idle;
        }

        @Override
        public void onStartDiscoveryFailed(String serviceType, int errorCode) {
            Log.e(TAG, "Discovery failed: Error code:" + errorCode);
            mNsdManager.stopServiceDiscovery(this);
            state = NsdState.Idle;
        }

        @Override
        public void onStopDiscoveryFailed(String serviceType, int errorCode) {
            Log.e(TAG, "Discovery failed: Error code:" + errorCode);
            mNsdManager.stopServiceDiscovery(this);
        }
    };
}
 
Example 9
Source File: CastMediaRouteProvider.java    From android_packages_apps_GmsCore with Apache License 2.0 4 votes vote down vote up
@SuppressLint("NewApi")
public CastMediaRouteProvider(Context context) {
    super(context);

    if (android.os.Build.VERSION.SDK_INT < 16) {
        Log.i(TAG, "Cast discovery disabled. Android SDK version 16 or higher required.");
        return;
    }

    mNsdManager = (NsdManager)context.getSystemService(Context.NSD_SERVICE);

    mDiscoveryListener = new NsdManager.DiscoveryListener() {

        @Override
        public void onDiscoveryStarted(String regType) {
            CastMediaRouteProvider.this.state = State.DISCOVERING;
        }

        @Override
        public void onServiceFound(NsdServiceInfo service) {
            mNsdManager.resolveService(service, new NsdManager.ResolveListener() {
                @Override
                public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
                    if (errorCode == NsdManager.FAILURE_ALREADY_ACTIVE) {
                        return;
                    }
                    Log.e(TAG, "DiscoveryListener Resolve failed. Error code " + errorCode);
                }

                @Override
                public void onServiceResolved(NsdServiceInfo serviceInfo) {
                    String name = serviceInfo.getServiceName();
                    InetAddress host = serviceInfo.getHost();
                    int port = serviceInfo.getPort();
                    Map<String, byte[]> attributes = serviceInfo.getAttributes();
                    if (attributes == null) {
                        Log.e(TAG, "Error getting service attributes from DNS-SD response");
                        return;
                    }
                    try {
                        String id = new String(attributes.get("id"), "UTF-8");
                        String deviceVersion = new String(attributes.get("ve"), "UTF-8");
                        String friendlyName = new String(attributes.get("fn"), "UTF-8");
                        String modelName = new String(attributes.get("md"), "UTF-8");
                        String iconPath = new String(attributes.get("ic"), "UTF-8");
                        int status = Integer.parseInt(new String(attributes.get("st"), "UTF-8"));

                        onChromeCastDiscovered(id, name, host, port, deviceVersion, friendlyName, modelName, iconPath, status);
                    } catch (UnsupportedEncodingException | NullPointerException ex) {
                        Log.e(TAG, "Error getting cast details from DNS-SD response", ex);
                        return;
                    }
                }
            });
        }

        @Override
        public void onServiceLost(NsdServiceInfo serviceInfo) {
            String name = serviceInfo.getServiceName();
            onChromeCastLost(name);
        }

        @Override
        public void onDiscoveryStopped(String serviceType) {
            CastMediaRouteProvider.this.state = State.NOT_DISCOVERING;
        }

        @Override
        public void onStartDiscoveryFailed(String serviceType, int errorCode) {
            CastMediaRouteProvider.this.state = State.NOT_DISCOVERING;
        }

        @Override
        public void onStopDiscoveryFailed(String serviceType, int errorCode) {
            CastMediaRouteProvider.this.state = State.DISCOVERING;
        }
    };
}