WifiP2pManager.removeGroup
does not do what I want. It still remembers the group when you reconnect to the same device.
But I found there is a hidden method WifiP2pManager.deletePersistentGroup
which I now call with reflection. It's not nice but it will need to do for now.
private void removeAndDeleteGroup(WifiP2pGroup wifiP2pGroup) {
wifiP2PManager.removeGroup(wifiChannel, new SimpleLoggingActionListener("removeGroup"));
try {
Method getNetworkId = WifiP2pGroup.class.getMethod("getNetworkId");
Integer networkId = (Integer) getNetworkId.invoke(wifiP2pGroup);
Method deletePersistentGroup = WifiP2pManager.class.getMethod("deletePersistentGroup",
WifiP2pManager.Channel.class, Integer.class, WifiP2pManager.ActionListener.class);
deletePersistentGroup.invoke(wifiP2PManager, wifiChannel, networkId, new SimpleLoggingActionListener("deletePersistentGroup"));
} 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);
}
}