This is the method I am using to disconnect from peer. I noticed from logs that the android built in app also uses the same method to disconnect the peers.
public static void disconnect() {
if (mManager != null && mChannel != null) {
mManager.requestGroupInfo(mChannel, new GroupInfoListener() {
@Override
public void onGroupInfoAvailable(WifiP2pGroup group) {
if (group != null && mManager != null && mChannel != null) {
mManager.removeGroup(mChannel, new ActionListener() {
@Override
public void onSuccess() {
Log.d(TAG, "removeGroup onSuccess -");
}
@Override
public void onFailure(int reason) {
Log.d(TAG, "removeGroup onFailure -" + reason);
}
});
}
}
});
}
}
Edit 1/12/2020: Removed isGroupOwner() from the if-statement. This allows a non-owner device to disconnect using this same method.
When a non-owner disconnects, the new WifiP2pInfo will show:
groupFormed: false isGroupOwner: false groupOwnerAddress: null
You can find your WifiP2pInfo from:
(1) Registering a broadcast receiver listening to WIFI_P2P_CONNECTION_CHANGED_ACTION
and getting WifiP2pInfo from the extra EXTRA_WIFI_P2P_INFO
(2) By calling WifiP2pManager#requestConnectionInfo