【问题标题】:Forgetting old WiFi Direct connections忘记旧的 WiFi Direct 连接
【发布时间】:2014-07-02 10:51:54
【问题描述】:

有没有办法忘记旧的 WiFi Direct 连接(在代码中)?我需要这个来改变谁成为组所有者。我正在设置 groupOwnerIntent = 15,但尚未成为组所有者。

【问题讨论】:

    标签: android android-wifi wifi-direct


    【解决方案1】:

    如果您只想断开现有的WiFiP2p 连接,则只需致电WiFiP2pManager#removeGroup。设备 GO 或 peer 无关紧要。

    如果您正在谈论忘记持久组 - 您也可以删除它们。但它只能通过反射来实现。而且无论是设备GO还是peer。

        manager.removeGroup(channel, new WifiP2pManager.ActionListener() {
            @Override
            public void onSuccess() {
                Log.d(TAG, "removeGroup success");
                deletePersistentGroup(group);
            }
    
            @Override
            public void onFailure(int reason) {
                Log.d(TAG, "removeGroup fail: " + reason);
            }
        });
    

    其中managerWiFip2pManager 的一个实例。而deletePersistanteGroup(WiFiP2pGroup group) 是:

        private void deletePersistentGroup(WifiP2pGroup wifiP2pGroup) {
            try {
    
                Method getNetworkId = WifiP2pGroup.class.getMethod("getNetworkId");
                Integer networkId = (Integer) getNetworkId.invoke(wifiP2pGroup);
                Method deletePersistentGroup = WifiP2pManager.class.getMethod("deletePersistentGroup",
                        WifiP2pManager.Channel.class, int.class, WifiP2pManager.ActionListener.class);
                deletePersistentGroup.invoke(manager, channel, networkId, new WifiP2pManager.ActionListener() {
                    @Override
                    public void onSuccess() {
                        Log.e(TAG, "deletePersistentGroup onSuccess");
                    }
    
                    @Override
                    public void onFailure(int reason) {
                        Log.e(TAG, "deletePersistentGroup failure: " + reason);
                    }
                });
            } 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);
            }
        }
    

    UPD

    要成为 GO,您应该在向同伴发送邀请之前致电 WiFiP2pManager#createGroup()

    【讨论】:

      猜你喜欢
      • 2012-11-03
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多