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
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With