android - WifiManager enableNetwork 几秒钟后恢复到以前的网络

标签 android wifimanager android-connectivitymanager

在 Android 8.1.0 及更高版本上,我遇到了一个主要问题。 当使用 enableNetwork 连接到没有互联网的网络时,Android 会在几秒钟后决定将设备放回之前的网络。

我有意连接到所需的网络,并计划使用 bindToNetwork 来确保所有流量都通过网络,但是 Android 似乎忽略了绑定(bind),之后很快就断开了连接。

不幸的是,我在这里看到了这个问题的一些排列组合,但都没有得到答复。

我正在使用以下代码创建到网络的连接。我能够看到网络连接。

WifiConfiguration conf = new WifiConfiguration();
conf.SSID = desiredSSID;
if (desiredNetwork.has("password")) {
    conf.preSharedKey = String.format("\"%s\"", desiredNetwork.get("password"));
}
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);

int networkID = wifiManager.addNetwork(conf);
wifiManager.enableNetwork(networkID, true);

是否有禁用此行为的可用选项,因为我有意连接到没有可用互联网的网络。

谢谢。

最佳答案

我能够通过从已配置的 wifi 列表中删除现有配置来解决此问题。网络不会断开与新配置的连接。

List<WifiConfiguration> wifiConfigurationList = wifiManager.getConfiguredNetworks();

for (WifiConfiguration removeConfig : wifiConfigurationList) {
    if (removeConfig.SSID.equals(desiredSSID)) {
        wifiManager.removeNetwork(removeConfig.networkId);
        break;
    }
}

只需再次添加网络即可解决问题。

关于android - WifiManager enableNetwork 几秒钟后恢复到以前的网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52879760/

相关文章:

Android快速切换到已知wifi网络

android - wifi peer-2-peer(wifi direct)组自动认证

android - WifiInfo 返回 null

android - 如何根据接收到的数据更改数据类型

Android Studio 失败并显示 : "File was deleted, but previous version not found in cache"

java - 发送短信之前启动应用程序

java - 如何处理 com.android.volley.NoConnectionError : java.net.UnknownHostException

android - 如何在 AOSP 构建中添加 APK?

java - WifiManager.setWifiEnabled() 究竟返回什么?

android - 互联网关闭时无法获得回调,然后应用程序在 NetworkCallback 内启动