How to stop peer detection in WifiDirect Android

I am trying to stop peer open in wifi directly using the below code.

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 can see the Success message (Stopped peer discovery) in the logs. But other devices can still view it when peers are detected. Am I doing something wrong here? Please advise. Thanks to

+3


source to share


1 answer


It's not a problem. Actually you misinterpreted this one onSuccess

. This onSuccess

does not tell you that peer discovery has stopped, but it does mean that your request was successfully sent to the hardware / framework in charge of the above call, it will stop the discovery asynchronously and notify you (as explained above). Likewise, its failure simply means that your call has not reached the frame.

You need to register a BroadcastReceiver for WIFI_P2P_DISCOVERY_CHANGED_ACTION . As you can read in the documentation, an additional aspect of EXTRA_DISCOVERY_STATE lets you know if peer discovery detection has 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
    }
}

      

+5


source







All Articles