Example usage for android.net.nsd NsdServiceInfo getServiceName

List of usage examples for android.net.nsd NsdServiceInfo getServiceName

Introduction

In this page you can find the example usage for android.net.nsd NsdServiceInfo getServiceName.

Prototype

public String getServiceName() 

Source Link

Document

Get the service name

Usage

From source file:ibp.plugin.nsd.NSDHelper.java

private JSONObject NsdServiceInfoToJSON(NsdServiceInfo info) {
    String name = info.getServiceName();
    String type = info.getServiceType();
    InetAddress host = info.getHost();
    int port = info.getPort();
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("name", name);
    map.put("type", type);
    map.put("address", (host == null) ? "null" : host.getHostAddress());
    map.put("port", (host == null) ? 0 : port);
    JSONObject jsonObj = new JSONObject(map);
    return jsonObj;
}

From source file:ibp.plugin.nsd.NSDHelper.java

public void addServiceInfo(NsdServiceInfo info) {
    Iterator<NsdServiceInfo> iter = mServiceInfoList.iterator();
    NsdServiceInfo element;
    boolean isExist = false;
    while (iter.hasNext()) {
        element = (NsdServiceInfo) iter.next();
        if (element.getServiceName().equals(info.getServiceName())) {
            isExist = true;/*  w  w  w . j a v a2 s .  com*/
            break;
        }
    }
    if (!isExist) {
        mServiceInfoList.add(info);
        sendNotification("onServiceFound", NsdServiceInfoToJSON(info).toString());
    }
}

From source file:org.drulabs.localdash.nsddiscovery.NsdHelper.java

public void initializeDiscoveryListener() {
    mDiscoveryListener = new NsdManager.DiscoveryListener() {
        @Override//from   w ww  .j  a  va  2  s . co  m
        public void onDiscoveryStarted(String regType) {
            Log.d(TAG, "Service discovery started");
        }

        @Override
        public void onServiceFound(NsdServiceInfo service) {
            Log.d(TAG, "Service discovery success" + service);
            String serviceType = service.getServiceType();
            Log.d(TAG, "Service discovery success: " + service.getServiceName());
            // For some reason the service type received has an extra dot with it, hence
            // handling that case
            boolean isOurService = serviceType.equals(SERVICE_TYPE)
                    || serviceType.equals(SERVICE_TYPE_PLUS_DOT);
            if (!isOurService) {
                Log.d(TAG, "Unknown Service Type: " + service.getServiceType());
            } else if (service.getServiceName().equals(mServiceName)) {
                Log.d(TAG, "Same machine: " + mServiceName);
            } else if (service.getServiceName().contains(mServiceName)) {
                Log.d(TAG, "different machines. (" + service.getServiceName() + "-" + mServiceName + ")");
                mNsdManager.resolveService(service, mResolveListener);
            }
        }

        @Override
        public void onServiceLost(NsdServiceInfo service) {
            Log.e(TAG, "service lost" + service);
            if (mService == service) {
                mService = null;
            }
        }

        @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);
        }

        @Override
        public void onStopDiscoveryFailed(String serviceType, int errorCode) {
            Log.e(TAG, "Discovery failed: Error code:" + errorCode);
        }
    };
}

From source file:ibp.plugin.nsd.NSDHelper.java

public void removeServiceInfo(NsdServiceInfo info) {
    Iterator<NsdServiceInfo> iter = mServiceInfoList.iterator();
    NsdServiceInfo element = null;
    boolean isExist = false;
    while (iter.hasNext()) {
        element = (NsdServiceInfo) iter.next();
        if (element.getServiceName().equals(info.getServiceName())) {
            isExist = true;//from   w w  w  . jav a2  s.  co m
            break;
        }
    }
    if (isExist) {
        mServiceInfoList.remove(element);
        sendNotification("onServiceLost", NsdServiceInfoToJSON(info).toString());
    }
}

From source file:ibp.plugin.nsd.NSDHelper.java

public void reWriteServiceInfo(NsdServiceInfo info) {
    int index = 0;
    boolean isExist = false;
    while (index < mServiceInfoList.size()) {
        if (mServiceInfoList.get(index).getServiceName().equals(info.getServiceName())) {
            isExist = true;// www. j a va2 s .  c  om
            break;
        }
        index++;
    }
    if (isExist) {
        mServiceInfoList.set(index, info);
    } else {
        mServiceInfoList.add(info);
    }
}

From source file:org.drulabs.localdash.nsddiscovery.NsdHelper.java

public void initializeResolveListener() {
    mResolveListener = new NsdManager.ResolveListener() {
        @Override//from  ww w  .j a  va  2s .  c  om
        public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
            Log.e(TAG, "Resolve failed" + errorCode);
        }

        @Override
        public void onServiceResolved(NsdServiceInfo serviceInfo) {
            Log.v(TAG, "Resolve Succeeded. " + serviceInfo);
            if (serviceInfo.getServiceName().equals(mServiceName)) {
                Log.d(TAG, "Same IP.");
                return;
            }
            mService = serviceInfo;

            Intent intent = new Intent(BROADCAST_TAG);
            //                intent.putExtra(KEY_SERVICE_INFO, mService);
            broadcaster.sendBroadcast(intent);
        }
    };
}

From source file:ibp.plugin.nsd.NSDHelper.java

public void initializeResolveListener() {
    mResolveListener = new NsdManager.ResolveListener() {
        @Override//from   w w  w  .jav  a  2s .c  o  m
        public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
            resolveServiceCB.error("onResolveFailed: " + errorCode);
        }

        @Override
        public void onServiceResolved(NsdServiceInfo serviceInfo) {
            String oldName = serviceInfo.getServiceName();
            String newName = oldName.replace("\\032", " ");
            serviceInfo.setServiceName(newName);
            reWriteServiceInfo(serviceInfo);
            resolveServiceCB.success(NsdServiceInfoToJSON(serviceInfo));
        }
    };
}

From source file:org.drulabs.localdash.nsddiscovery.NsdHelper.java

public void initializeRegistrationListener() {
    mRegistrationListener = new NsdManager.RegistrationListener() {
        @Override/*w  ww  .j a  va  2 s  . c  om*/
        public void onServiceRegistered(NsdServiceInfo NsdServiceInfo) {
            mServiceName = NsdServiceInfo.getServiceName();
            Log.d(TAG, "Service registered: " + NsdServiceInfo);
            NotificationToast.showToast(mContext, "Service registered");
        }

        @Override
        public void onRegistrationFailed(NsdServiceInfo arg0, int arg1) {
            Log.d(TAG, "Service registration failed: " + arg1);
            NotificationToast.showToast(mContext, "Service registration failed");
        }

        @Override
        public void onServiceUnregistered(NsdServiceInfo arg0) {
            Log.d(TAG, "Service unregistered: " + arg0.getServiceName());
            NotificationToast.showToast(mContext, "Service unregistered");
        }

        @Override
        public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
            Log.d(TAG, "Service unregistration failed: " + errorCode);
            NotificationToast.showToast(mContext, "Service un-registration failed");
        }
    };
}