Is there a way to forget old WiFi Direct connections(in code)? I need this so as to change who becomes Group owner. I am setting groupOwnerIntent = 15 and yet not becoming Group owner.
For Android (general instruction using Google Marshmallow): Open Settings on your device, and tap on the WiFI icon to access WiFi network options. Tap and hold the WiFi network you want to delete, then select Forget Network from the menu that appears.
If you want just to disconnect from existing WiFiP2p
connections, then just call WiFiP2pManager#removeGroup
. Doesn't matter is device GO or peer.
If you are talking about forgetting persistent groups - you can also remove them. But it can only be achieved via reflection. And also no matter is device GO or peer.
manager.removeGroup(channel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Log.d(TAG, "removeGroup success");
deletePersistentGroup(group);
}
@Override
public void onFailure(int reason) {
Log.d(TAG, "removeGroup fail: " + reason);
}
});
Where manager
is an instance of WiFip2pManager
. And deletePersistanteGroup(WiFiP2pGroup group)
is:
private void deletePersistentGroup(WifiP2pGroup wifiP2pGroup) {
try {
Method getNetworkId = WifiP2pGroup.class.getMethod("getNetworkId");
Integer networkId = (Integer) getNetworkId.invoke(wifiP2pGroup);
Method deletePersistentGroup = WifiP2pManager.class.getMethod("deletePersistentGroup",
WifiP2pManager.Channel.class, int.class, WifiP2pManager.ActionListener.class);
deletePersistentGroup.invoke(manager, channel, networkId, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Log.e(TAG, "deletePersistentGroup onSuccess");
}
@Override
public void onFailure(int reason) {
Log.e(TAG, "deletePersistentGroup failure: " + reason);
}
});
} catch (NoSuchMethodException e) {
Log.e("WIFI", "Could not delete persistent group", e);
} catch (InvocationTargetException e) {
Log.e("WIFI", "Could not delete persistent group", e);
} catch (IllegalAccessException e) {
Log.e("WIFI", "Could not delete persistent group", e);
}
}
UPD
To become a GO you should call WiFiP2pManager#createGroup()
before sending invites to peers.
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