Forgetting old WiFi Direct connections

♀尐吖头ヾ 提交于 2019-12-09 12:46:36

问题


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.


回答1:


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

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

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

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



来源:https://stackoverflow.com/questions/23653707/forgetting-old-wifi-direct-connections

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!