问题描述
我正在调用函数 WifiManager.addNetwork(WifiConfiguration)尝试将自组织wifi网络添加到设备的wifi配置中.但是我假设在Android M上此函数返回-1,因为它无法访问互联网.它在大多数其他设备上都可以正常工作.下面是我正在使用的代码段.
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网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!