6

I'm using WiFi P2P for network service discovery, and I'm following the instructions as outlined on the developer guide. Here's the relevant code in my service class:

public void onCreate() {
    manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
    channel = manager.initialize(this, getMainLooper(), null);
    registerP2pService();
    lookForServices();
}

private void registerP2pService() {
    WifiP2pDnsSdServiceInfo serviceInfo =
            WifiP2pDnsSdServiceInfo.newInstance("_service.name", "_presence._tcp", new HashMap<String, String>());

    manager.addLocalService(channel, serviceInfo, new WifiP2pManager.ActionListener() {
        @Override
        public void onSuccess() {
            Log.i("tag", "REGISTERED SERVICE");
        }

        @Override
        public void onFailure(int arg0) {
            Log.e("tag", "FAILED to register service");
        }
    });
}

private void setServiceListeners() {
    WifiP2pDnsSdServiceRequest serviceRequest = WifiP2pDnsSdServiceRequest.newInstance();
    manager.addServiceRequest(channel, serviceRequest,
        new WifiP2pManager.ActionListener() {
        @Override
        public void onSuccess() {
            Log.d("SCOPE", "Added a service request.");
            discoverServices();
        }

        @Override
        public void onFailure(int code) {
            Log.e("tag", "Error adding service request.");
        }
   });
}


public void discoverServices() {
    manager.discoverServices(channel, new WifiP2pManager.ActionListener() {

        @Override
        public void onSuccess() {
            Log.d("tag", "Service discovery was initiated");
        }

        @Override
        public void onFailure(int code) {
            // This is where it keeps failing with error code 3 (NO_SERVICE_REQUESTS)
            Log.d("SCOPE", "Service discovery failure code "  + code);
        }
    });
}

The first time I run my service after rebooting the phone, service discovery is initiated just fine, but if I kill the service by stopping it from the app settings page, then open it again, it always fails with error code 3. If I reboot my phone and run the app again it works just fine. I am confused because I am explicitly calling discoverServices only when the service request has been successfully added.

My hunch is that it may be due to some code that is unrelated to service discovery because the service discovery code seems extremely straightforward, but if you see anything wrong with what I've posted, let me know. I'm grasping at straws here.

I'm running this on a Nexus 5 with Android 4.4.2.

Cypress Frankenfeld
  • 2,317
  • 2
  • 28
  • 40
  • where are you receiving the error? I mean in which method? if you look at [this](http://developer.android.com/reference/android/net/wifi/p2p/WifiP2pManager.html#P2P_UNSUPPORTED), I'd say there is something wrong in your setServiceListener method. Are you stopping your service onStop() or somewhere? – Saeid Farivar Feb 25 '14 at 21:03
  • I'm receiving the error in the `onFailure()` callback of `manager.discoverServices()`. I'm not stopping my service in `onStop()` or anywhere else. – Cypress Frankenfeld Feb 26 '14 at 02:11
  • That's probably the case. look at [this](http://stackoverflow.com/questions/18679481/wifi-direct-end-connection-to-peer-on-android). this is how you should do it. – Saeid Farivar Feb 26 '14 at 02:19
  • look at what? what is the case? – Cypress Frankenfeld Feb 26 '14 at 22:12
  • I suspect the reason for seeing this error is that you're not stopping your service.... and I suggested a link in my comment. http://stackoverflow.com/questions/18679481/wifi-direct-end-connection-to-peer-on-android I hope it's useful. – Saeid Farivar Feb 26 '14 at 22:26

2 Answers2

9

I'm seeing this same problem on a second generation Nexus 7. It works fine the first time, subsequent attempts error out. Specifically, the addServiceRequest action listener reports success, but then discoverServices reports NO_SERVICE_REQUESTS.

(Teardown removeLocalService and removeServiceRequest succeed before exiting the app after the initial successful use.)

While it isn't an ideal answer, toggling wifi off then on again appears to reset whatever is stuck. That can be done programatically.

Darrell
  • 1,945
  • 15
  • 21
7

WifiDirect/NSD on Android has been a thorn in my side for the last couple of weeks.

I spent half of last week trying to figure out why my peer discovery calls would fail without any consistency with NO_SERVICE_REQUESTS and finally found a solution that works fairly well. It does seem to be a bug in the OS, and luckily the fine folks of P2Feed figured out a workaround:

if (code == WifiP2pManager.NO_SERVICE_REQUESTS) {

    // initiate a stop on service discovery
    manager.stopPeerDiscovery(channel, new WifiP2pManager.ActionListener() {
        @Override
        public void onSuccess() {
            // initiate clearing of the all service requests
            manager.clearServiceRequests(channel, new WifiP2pManager.ActionListener() {
                @Override
                public void onSuccess() {
                    // reset the service listeners, service requests, and discovery
                    setServiceListeners();
                }

                @Override
                public void onFailure(int i) {
                    Log.d(TAG, "FAILED to clear service requests ");
                }
            });

        }

        @Override
        public void onFailure(int i) {
            Log.d(TAG, "FAILED to stop discovery");
        }
    });
}
Makario
  • 2,097
  • 21
  • 19