Example usage for android.net.wifi.p2p.nsd WifiP2pDnsSdServiceRequest newInstance

List of usage examples for android.net.wifi.p2p.nsd WifiP2pDnsSdServiceRequest newInstance

Introduction

In this page you can find the example usage for android.net.wifi.p2p.nsd WifiP2pDnsSdServiceRequest newInstance.

Prototype

public static WifiP2pDnsSdServiceRequest newInstance(String serviceType) 

Source Link

Document

Create a service discovery to search for Bonjour services with the specified service type.

Usage

From source file:org.thaliproject.p2p.btconnectorlib.internal.wifi.WifiServiceWatcher.java

/**
 * Starts the service discovery./*w  ww  . j  a  va2  s.c  o  m*/
 */
public synchronized void start() {
    mIsRestarting = false;
    WifiP2pDnsSdServiceRequest request = WifiP2pDnsSdServiceRequest.newInstance(mServiceType);
    final WifiServiceWatcher thisInstance = this;
    final Handler handler = new Handler();

    mP2pManager.addServiceRequest(mP2pChannel, request, new WifiP2pManager.ActionListener() {
        @Override
        public void onSuccess() {
            Log.i(TAG, "Service request added successfully");

            // There is supposedly a possible race-condition bug with the service discovery.
            // Thus, to avoid it, we are delaying the service discovery initialization here.
            handler.postDelayed(new Runnable() {
                public void run() {
                    thisInstance.mP2pManager.discoverServices(thisInstance.mP2pChannel,
                            new WifiP2pManager.ActionListener() {
                                @Override
                                public void onSuccess() {
                                    Log.d(TAG, "Service discovery started successfully");
                                }

                                @Override
                                public void onFailure(int reason) {
                                    Log.e(TAG,
                                            "Failed to start the service discovery, got error code: " + reason);

                                    // Uncomment the following to auto-restart
                                    //thisInstance.stop(true); // Restart
                                }
                            });
                }
            }, START_SERVICE_DISCOVERY_DELAY_IN_MILLISECONDS);
        }

        @Override
        public void onFailure(int reason) {
            Log.e(TAG, "Failed to add a service request, got error code: " + reason);

            // Uncomment the following to auto-restart
            //thisInstance.stop(true); // Restart
        }
    });
}