问题
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.
回答1:
If you want just to discinnect from existant WiFiP2p connection, than just call WiFiP2pManager#removeGroup
. Doesnt matter is device GO or peer.
If you are talking about forgeting persistant groups - u can also remove it. 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 became a GO you should call WiFiP2pManager#createGroup() before sending invites to peers.
来源:https://stackoverflow.com/questions/23653707/forgetting-old-wifi-direct-connections