0

I noticed that whenever I use WifiP2pManager.createGroup() (Reference) for creating a WiFi hotspot, a completely new network (new SSID and password) is created. This new group is saved as a persistent group, although it is only used once in my case. This leads to many 'throwaway groups' littering up the WiFi Direct/WiFi P2P settings.

There is a way to delete all persistent WiFi P2P groups, but reusing one group all the time would be a much cleaner solution. In fact, this option is available to the user:

Image

I just can't figure it out how to use this programmatically... but maybe some of you can ;)

Any help is appreciated!

Community
  • 1
  • 1
Candor
  • 493
  • 4
  • 12

1 Answers1

1

WiFi-Buddy is a library that may be useful for you. There is a removePersistentGroups() method that uses Java reflection to call the removePersistentGroup() method in the Wi-Fi Direct API.

/**
 * Removes persistent/remembered groups
 *
 * Source: https://android.googlesource.com/platform/cts/+/jb-mr1-dev%5E1%5E2..jb-mr1-dev%5E1/
 * Author: Nick  Kralevich <nnk@google.com>
 *
 * WifiP2pManager.java has a method deletePersistentGroup(), but it is not accessible in the
 * SDK. According to Vinit Deshpande <vinitd@google.com>, it is a common Android paradigm to
 * expose certain APIs in the SDK and hide others. This allows Android to maintain stability and
 * security. As a workaround, this removePersistentGroups() method uses Java reflection to call
 * the hidden method. We can list all the methods in WifiP2pManager and invoke "deletePersistentGroup"
 * if it exists. This is used to remove all possible persistent/remembered groups. 
 */
private void removePersistentGroups() {
    try {
        Method[] methods = WifiP2pManager.class.getMethods();
        for (int i = 0; i < methods.length; i++) {
            if (methods[i].getName().equals("deletePersistentGroup")) {
                // Remove any persistent group
                for (int netid = 0; netid < 32; netid++) {
                    methods[i].invoke(wifiP2pManager, channel, netid, null);
                }
            }
        }
        Log.i(TAG, "Persistent groups removed");
    } catch(Exception e) {
        Log.e(TAG, "Failure removing persistent groups: " + e.getMessage());
        e.printStackTrace();
    }
}
Brendan
  • 1,403
  • 4
  • 18
  • 37
  • Thanks for your answer, but I trie to restart an _existing_ group, so it's SSID/password **stays the same**... – Candor Aug 14 '16 at 14:13