WifiP2pGroup - 重新启动持久组?

时间:2016-08-10 15:29:33

标签: java android android-wifi wifi-direct wifip2p

我注意到每当我使用WifiP2pManager.createGroup()Reference)创建WiFi热点时,都会创建一个完全新网络(新的SSID和密码)。这个新组保存为持久组,但在我的情况下只使用一次。这导致许多一次性团体'乱丢WiFi Direct / WiFi P2P设置。

There is a way 删除所有持久性WiFi P2P组,但一直重用一个组将是一个更清洁的解决方案。 实际上,用户可以使用此选项:

Image

我只是无法弄清楚如何以编程方式使用它...但也许可能会有一些;)

感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

WiFi-Buddy是一个可能对您有用的库。有一个removePersistentGroups()方法,它使用Java反射来调用Wi-Fi Direct API中的removePersistentGroup()方法。

/**
 * 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();
    }
}