【问题标题】:WifiP2pGroup - Restart a persistent group?WifiP2pGroup - 重新启动持久组?
【发布时间】:2016-08-10 15:29:32
【问题描述】:

我注意到,每当我使用WifiP2pManager.createGroup() (Reference) 创建 WiFi 热点时,都会创建一个完全新网络(新 SSID 和密码)。这个新组被保存为一个持久组,尽管在我的例子中它只使用一次。这导致许多“一次性组”乱扔 WiFi Direct/WiFi P2P 设置。

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

我只是不知道如何以编程方式使用它...但也许有些可以;)

感谢您的帮助!

【问题讨论】:

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


    【解决方案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();
        }
    }
    

    【讨论】:

    • 感谢您的回答,但我尝试重新启动一个现有组,所以它的 SSID/密码保持不变...
    猜你喜欢
    • 1970-01-01
    • 2018-02-13
    • 2021-10-29
    • 1970-01-01
    • 2020-03-13
    • 2014-06-09
    • 1970-01-01
    • 1970-01-01
    • 2014-04-16
    相关资源
    最近更新 更多