Java Code Examples for android.net.nsd.NsdServiceInfo#setPort()

The following examples show how to use android.net.nsd.NsdServiceInfo#setPort() . 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: Device.java    From nitroshare-android with MIT License 5 votes vote down vote up
public NsdServiceInfo toServiceInfo() {
    NsdServiceInfo serviceInfo = new NsdServiceInfo();
    serviceInfo.setServiceType(SERVICE_TYPE);
    serviceInfo.setServiceName(mName);
    //serviceInfo.setAttribute(UUID, mUuid);
    serviceInfo.setPort(mPort);
    return serviceInfo;
}
 
Example 2
Source File: NsdHelper.java    From NsdHelper with Apache License 2.0 5 votes vote down vote up
/**
 * Register new service with given serivce name, type and port.
 *
 * @param desiredServiceName Desired service name. If the name already exists in network it will be change to something like 'AppChat (1)'.
 * @param serviceType        Service type.
 * @param port               Service port.
 */
public void registerService(String desiredServiceName, String serviceType, int port) {
    if (port == 0) return;

    mRegisteredServiceInfo = new NsdServiceInfo();
    mRegisteredServiceInfo.setServiceName(desiredServiceName);
    mRegisteredServiceInfo.setServiceType(serviceType);
    mRegisteredServiceInfo.setPort(port);

    mRegistrationListener = new NsdListenerRegistration(this);
    mNsdManager.registerService(mRegisteredServiceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);
}
 
Example 3
Source File: ServiceDiscovery.java    From PHONK with GNU General Public License v3.0 4 votes vote down vote up
Create(Context a, String name, String serviceType, int port) {

            // Create the NsdServiceInfo object, and populate it.
            serviceInfo = new NsdServiceInfo();

            // The name is subject to change based on conflicts
            // with other services advertised on the same network.
            serviceInfo.setServiceName(name);
            serviceInfo.setServiceType(serviceType);
            serviceInfo.setPort(port);

            String ip = (String) mAppRunner.pNetwork.networkInfo().get("ip");
            try {
                InetAddress p = InetAddress.getByName(ip);
                serviceInfo.setHost(p);
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }

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

            mRegistrationListener = new NsdManager.RegistrationListener() {
                @Override
                public void onServiceRegistered(NsdServiceInfo serviceInfo) {
                    // Save the service name.  Android may have changed it in order to
                    // resolve mContext conflict, so update the name you initially requested
                    // with the name Android actually used.

                    ReturnObject ret = new ReturnObject();
                    ret.put("status", "registered");
                    ret.put("host", serviceInfo.getHost());
                    ret.put("port", serviceInfo.getPort());
                    ret.put("type", serviceInfo.getServiceType());
                    ret.put("name", serviceInfo.getServiceName());
                    if (mCallback != null) mCallback.event(ret);
                }

                @Override
                public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
                    // Registration failed!  Put debugging code here to determine why.
                    ReturnObject ret = new ReturnObject();
                    ret.put("name", serviceInfo.getServiceName());
                    ret.put("status", "registration_failed");
                    if (mCallback != null) mCallback.event(ret);
                }

                @Override
                public void onServiceUnregistered(NsdServiceInfo arg0) {
                    // Service has been unregistered.  This only happens when you call
                    // NsdManager.unregisterService() and pass in this listener.
                    ReturnObject ret = new ReturnObject();
                    ret.put("name", serviceInfo.getServiceName());
                    ret.put("status", "unregistered");
                    if (mCallback != null) mCallback.event(ret);
                }

                @Override
                public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
                    // Unregistration failed.  Put debugging code here to determine why.
                    ReturnObject ret = new ReturnObject();
                    ret.put("name", serviceInfo.getServiceName());
                    ret.put("status", "unregistration_failed");
                    if (mCallback != null) mCallback.event(ret);
                }
            };
        }
 
Example 4
Source File: MonitorActivity.java    From protect-baby-monitor with GNU General Public License v3.0 4 votes vote down vote up
private void registerService(final int port)
{
    final NsdServiceInfo serviceInfo  = new NsdServiceInfo();
    serviceInfo.setServiceName("ProtectBabyMonitor");
    serviceInfo.setServiceType("_babymonitor._tcp.");
    serviceInfo.setPort(port);

    _registrationListener = new NsdManager.RegistrationListener()
    {
        @Override
        public void onServiceRegistered(NsdServiceInfo nsdServiceInfo) {
            // Save the service name.  Android may have changed it in order to
            // resolve a conflict, so update the name you initially requested
            // with the name Android actually used.
            final String serviceName = nsdServiceInfo.getServiceName();

            Log.i(TAG, "Service name: " + serviceName);

            MonitorActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run()
                {
                    final TextView statusText = (TextView) findViewById(R.id.textStatus);
                    statusText.setText(R.string.waitingForParent);

                    final TextView serviceText = (TextView) findViewById(R.id.textService);
                    serviceText.setText(serviceName);

                    final TextView portText = (TextView) findViewById(R.id.port);
                    portText.setText(Integer.toString(port));
                }
            });
        }

        @Override
        public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode)
        {
            // Registration failed!  Put debugging code here to determine why.
            Log.e(TAG, "Registration failed: " + errorCode);
        }

        @Override
        public void onServiceUnregistered(NsdServiceInfo arg0)
        {
            // Service has been unregistered.  This only happens when you call
            // NsdManager.unregisterService() and pass in this listener.

            Log.i(TAG, "Unregistering service");
        }

        @Override
        public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode)
        {
            // Unregistration failed.  Put debugging code here to determine why.

            Log.e(TAG, "Unregistration failed: " + errorCode);
        }
    };

    _nsdManager.registerService(
            serviceInfo, NsdManager.PROTOCOL_DNS_SD, _registrationListener);
}