Android,自动连接到无法访问互联网的wifi网络 [英] Android, automatically connecting to wifi networks that have no internet access
问题描述
I'm calling the function WifiManager.addNetwork(WifiConfiguration) to try and add an adhoc wifi network to the device's wifi configurations. But on Android M this function returns -1, I'm assuming because it doesn't have internet access. It works fine on most other devices. Below is code snippet I'm using.
WifiConfiguration wifiConfiguration = new WifiConfiguration();
wifiConfiguration.SSID = '\"' + ssid + '\"';
wifiConfiguration.hiddenSSID = false;
wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiManager.addNetwork(wifiConfiguration);
WifiConfiguration wifiConfiguration = new WifiConfiguration();
wifiConfiguration.SSID = '\"' + ssid + '\"';
wifiConfiguration.hiddenSSID = false;
wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiManager.addNetwork(wifiConfiguration);
有什么方法可以绕过Internet连接检查并强制添加网络?
Any way to get around the internet connectivity check and force the network to be added?
推荐答案
之后,您需要启用网络:
You need to enable the network after that :
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals("\"" + ssid + "\"")) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
enter code here
break;
}
}
这篇关于Android,自动连接到无法访问互联网的wifi网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!