Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WiFiDirect discoverServices keeps failing with error 3 (NO_SERVICE_REQUESTS)

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.

like image 999
Cypress Frankenfeld Avatar asked Feb 16 '14 21:02

Cypress Frankenfeld


1 Answers

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.

like image 157
Darrell Avatar answered Nov 14 '22 23:11

Darrell