gpt4 book ai didi

java - Android wifimanager启用网络似乎启用了错误的网络

转载 作者:太空宇宙 更新时间:2023-11-04 11:51:42 25 4
gpt4 key购买 nike

我制作了一个 Android 应用程序,它在启动时获取当前的 wifi 网络并连接到另一个网络。至少这是它应该做的:

wifiInfo = wifiManager.getConnectionInfo();
OldNetworkID = wifiInfo.getNetworkId(); //save current network
WDTNetworkID = wifiManager.addNetwork(wificonfiguration); //add new network
wifiManager.disconnect();
wifiManager.enableNetwork(WDTNetworkID, true); //enable new network and disable all others
wifiManager.reconnect();

当我调试时,我可以看到 wificonfiguration 包含正确的 SSID(新网络的 SSID)。

addNetwork() 之后,我看到 wifiManager.getConfiguredNetworks() 包含这个新网络,该网络具有正确的 SSID 以及与 WDTNetworkID 相同的网络 ID。此时网络已启用。

但是在启用 enableNetwork() 而不是启用 WDTNetworkID 并禁用其余部分后,我看到 OldNetworkID 已启用,其余部分已禁用。

我做错了什么吗?

This is what it looks like

我在调试时添加了几 block watch 的图片。您可以在这里看到旧网络已启用,其余网络已禁用。

最佳答案

这里的问题是应用程序运行的设备是android 5.1.1。正如 enableNetwork() 文档中所述:

Note: If an application's target SDK version is LOLLIPOP or newer, network communication may not use Wi-Fi even if Wi-Fi is connected; traffic may instead be sent through another network, such as cellular data, Bluetooth tethering, or Ethernet. For example, traffic will never use a Wi-Fi network that does not provide Internet access (e.g. a wireless printer), if another network that does offer Internet access (e.g. cellular data) is available. Applications that need to ensure that their network traffic uses Wi-Fi should use APIs such as bindSocket(java.net.Socket), openConnection(java.net.URL), or bindProcessToNetwork(Network) to do so.

关于java - Android wifimanager启用网络似乎启用了错误的网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41759604/

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