Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Stop peer discovery in WifiDirect Android

I an trying to stop peer discovery in wifi direct using the code below.

public void StopPeerDiscovery(){
    manager.stopPeerDiscovery(channel, new ActionListener() {
        @Override
        public void onSuccess() {
            Log.d(WiFiDirectActivity.TAG,"Stopped Peer discovery");
        }

        @Override
        public void onFailure(int i) {
            Log.d(WiFiDirectActivity.TAG,"Not Stopped Peer discovery");
        }
    });
}

I am able to see Success message (Stopped peer discovery) in logs. But other devices are still able to view it in in peer discovery.. Am I doing something wrong here? Please advise. Thanks

like image 432
Furious Avatar asked Jul 15 '15 19:07

Furious


1 Answers

This is not an issue. Actually you interpreted this onSuccess wrong. This onSuccess does not tell you that peer discovery has stopped but rather it means that your request has been successfully sent to the hardware/framework responsible for the above call, it will stop discovery asynchronously and let you know (as explained ahead). Similarly, its failure just tells that your call did not reach successfully to the framework.

You need to register a BroadcastReceiver for WIFI_P2P_DISCOVERY_CHANGED_ACTION intent. As you can read in the documentation, the extra namely EXTRA_DISCOVERY_STATE lets you know whether the discovery of peer discovery stopped.

if (WifiP2pManager.WIFI_P2P_DISCOVERY_CHANGED_ACTION.equals(intent.getAction()))
{
    int state = intent.getIntExtra(WifiP2pManager.EXTRA_DISCOVERY_STATE, 10000);
    if (state == WifiP2pManager.WIFI_P2P_DISCOVERY_STARTED)
    {
        // Wifi P2P discovery started.
    }
    else
    {
        // Wifi P2P discovery stopped.
        // Do what you want to do when discovery stopped
    }
}
like image 85
unrealsoul007 Avatar answered Nov 03 '22 03:11

unrealsoul007