忘记旧的WiFi直连连接

时间:2014-05-14 11:42:04

标签: android android-wifi wifi-direct

有没有办法忘记旧的WiFi Direct连接(代码中)?我需要这样才能改变谁成为集团所有者。我正在设置groupOwnerIntent = 15但尚未成为Group owner。

1 个答案:

答案 0 :(得分:2)

如果您只想从现有的WiFiP2p连接中取消,请致电WiFiP2pManager#removeGroup。设备GO或同行无关紧要。

如果你在谈论伪造持久群体 - 你也可以删除它。但它只能通过反思来实现。而且无论是设备GO还是同行。

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

其中manager是WiFip2pManager的实例。 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()。