Is there any way to end the connection to a peer over Wifi-Direct? I tried cancelConnect and removeGroup. Both of them returned Busy? thanks.
Wi-Fi Direct (also known as peer-to-peer or P2P) allows your application to quickly find and interact with nearby devices, at a range beyond the capabilities of Bluetooth. The Wi-Fi peer-to-peer (P2P) APIs allow applications to connect to nearby devices without needing to connect to a network or hotspot.
Android P2P is now called WiFi Direct. On the Settings panel select Wi-Fi. You'll see a list of WiFi networks but you want to click the menu button on the lower right (the vertical ...). This opens up a submenu with Wi-Fi Direct as one of the options, select it.
Wi-Fi Direct is a peer-to-peer Wi-Fi connection between devices that enables faster data transfer than Bluetooth with lower latency than Wi-Fi through a router. You can use it on Android with the Cast Screen feature on certain devices and the Nearby Share file-sharing feature.
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
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