gpt4 book ai didi

android - 忘记旧的 WiFi Direct 连接

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:16:19 28 4
gpt4 key购买 nike

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

最佳答案

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

如果您正在谈论忘记永久组 - 您也可以删除它们。但它只能通过反射(reflection)来实现。而且无论是设备 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()

关于android - 忘记旧的 WiFi Direct 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23653707/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com