如何在android上创建一个非持久的WiFi直接组?
作者:互联网
当第一次调用WifiP2pManager.connect时,目标设备上有一个确认对话框来接受连接.
在后续连接中,不再询问此确认,该组将保留在设备上.
我不想要这种行为,因为我希望启动连接的设备始终是组所有者.保持组时,无法更改组所有者.
有没有办法创建一个临时组而不是持久组?或者,当我完成它时,我可以“忘记”持久性群体吗?
解决方法:
WifiP2pManager.removeGroup没有做我想要的.当您重新连接到同一设备时,它仍会记住该组.
但我发现有一个隐藏方法WifiP2pManager.deletePersistentGroup,我现在用反射调用它.这不好,但现在需要做.
private void removeAndDeleteGroup(WifiP2pGroup wifiP2pGroup) {
wifiP2PManager.removeGroup(wifiChannel, new SimpleLoggingActionListener("removeGroup"));
try {
Method getNetworkId = WifiP2pGroup.class.getMethod("getNetworkId");
Integer networkId = (Integer) getNetworkId.invoke(wifiP2pGroup);
Method deletePersistentGroup = WifiP2pManager.class.getMethod("deletePersistentGroup",
WifiP2pManager.Channel.class, Integer.class, WifiP2pManager.ActionListener.class);
deletePersistentGroup.invoke(wifiP2PManager, wifiChannel, networkId, new SimpleLoggingActionListener("deletePersistentGroup"));
} 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);
}
}
标签:wifi-direct,android,wifip2p 来源: https://codeday.me/bug/20190830/1765731.html