Android wifimanager enable network 似乎启用了错误的网络
Android wifimanager enable network seems to enable wrong network
我制作了一个 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
而禁用了其余部分。
我是不是做错了什么?
我在调试时添加了几张手表的图片。
可以看到这里旧网络启用,其余网络禁用
这里的问题是应用 运行 所在的设备是 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.
您只需将这一行添加到您的代码中。我有同样的问题,添加此行 help me.conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
//After sleep a little milliseconds then check exist connection with this code:
try {
Thread.sleep(3000);
Toast.makeText(MyActivity.this, "You are connected to " +
mainWifimanagerObject.getConnectionInfo().getSSID(), Toast.LENGTH_LONG).show();
}
catch (InterruptedException e) {e.printStackTrace();}
我制作了一个 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
而禁用了其余部分。
我是不是做错了什么?
我在调试时添加了几张手表的图片。 可以看到这里旧网络启用,其余网络禁用
这里的问题是应用 运行 所在的设备是 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.
您只需将这一行添加到您的代码中。我有同样的问题,添加此行 help me.conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
//After sleep a little milliseconds then check exist connection with this code:
try {
Thread.sleep(3000);
Toast.makeText(MyActivity.this, "You are connected to " +
mainWifimanagerObject.getConnectionInfo().getSSID(), Toast.LENGTH_LONG).show();
}
catch (InterruptedException e) {e.printStackTrace();}