忘记旧的WiFi Direct连接 [英] Forgetting old WiFi Direct connections

查看:140
本文介绍了忘记旧的WiFi Direct连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法忘记旧的WiFi Direct连接(以代码形式)?我需要这样做,以便更改谁成为组所有者.我将groupOwnerIntent设置为15,但尚未成为组所有者.

Is there a way to forget old WiFi Direct connections(in code)? I need this so as to change who becomes Group owner. I am setting groupOwnerIntent = 15 and yet not becoming Group owner.

推荐答案

如果您只想与现有的WiFiP2p连接区别开,则不只是调用WiFiP2pManager#removeGroup.与设备GO或对等设备无关.

If you want just to discinnect from existant WiFiP2p connection, than just call WiFiP2pManager#removeGroup. Doesnt matter is device GO or peer.

如果您正在谈论忘记持久性组-您也可以将其删除.但这只能通过反思来实现.而且无论设备GO还是对等设备.

If you are talking about forgeting persistant groups - u can also remove it. But it can only be achieved via reflection. And also no matter is device GO or 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);
    }
});

其中manager是WiFip2pManager的实例.而deletePersistanteGroup(WiFiP2pGroup group)是:

Where manager is an instance of WiFip2pManager. And deletePersistanteGroup(WiFiP2pGroup group) is:

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().

To became a GO you should call WiFiP2pManager#createGroup() before sending invites to peers.

这篇关于忘记旧的WiFi Direct连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆