'How to create a non-persistent WiFi direct group on android?

When WifiP2pManager.connect is called for the first time, there is a confirmation dialog on the target device to accept the connection.

On subsequent connections this confirmation is not asked anymore, the group is persisted on the device.

I don't want this behaviour because I want the device that initiates the connection to always be the group owner. When the group is persisted, the group owner cannot be changed.

Is there a way to create a temporary group instead of a persistent group? Or can I 'forget' the persistent group when I'm done with it?



Solution 1:[1]

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);
    }
}

Solution 2:[2]

Doesn't removeGroup in Wifip2pManager do what you need? http://developer.android.com/reference/android/net/wifi/p2p/WifiP2pManager.html#removeGroup(android.net.wifi.p2p.WifiP2pManager.Channel, android.net.wifi.p2p.WifiP2pManager.ActionListener)

I would think calling that when you are done with your connection would cause it to ask to reconnect everytime.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jimmy Praet
Solution 2 Ben