我正在尝试创建自己的wifi屏幕,其中向用户显示可点击的AP列表。问题是,当用户点击AP时,设备断开连接,然后重新连接到同一AP而不是所选AP。这是代码:(只有开放网络部分,我正试图从安全网络转移到开放网络)
public void onClick(View v) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
for (WifiConfiguration configuredNetwork : configuredNetworks) {
if (currSSID().equals(configuredNetwork.SSID)) { // I took care of the removel of the "" in configuredNetwork.SSID in my code
configuredNetwork .allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiManager.disconnect();
wifiManager.enableNetwork(configuredNetwork.networkId, true);
wifiManager.reconnect();
break;
}
}
答案 0 :(得分:0)
我正试图从安全网络转移到开放网络
此处必须先更改配置,因此最好创建一个新配置:
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + your_net_name + "\"";
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
并且您还需要先检查是否启用了wifi:
if(!wifiManager.isWifiEnabled()){
wifiManager.setWifiEnabled(true);
}
您还必须在wifiManager
之后使用saveConfiguration()
将当前更改保存到reconnect()
:
wifiManager.saveConfiguration();
所以,整个方式必须是:
public void onClick(View v) {
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + your_net_name + "\"";
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if(!wifiManager.isWifiEnabled()){
wifiManager.setWifiEnabled(true);
}
List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
for (WifiConfiguration configuredNetwork : configuredNetworks) {
if (currSSID().equals(configuredNetwork.SSID)) { // I took care of the removel of the "" in configuredNetwork.SSID in my code
//configuredNetwork .allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); // you don't need it here
wifiManager.disconnect();
wifiManager.enableNetwork(configuredNetwork.networkId, true);
wifiManager.reconnect();
wifiManager.saveConfiguration(); // to save your new changes to the network
break;
}
}
答案 1 :(得分:0)
我最终使用ScanResult
代替WifiConfiguration
并创建了一个新配置 - 配置了所需的扫描结果,如下所示:
conf.SSID = "\"" + scanResult.SSID + "\"";
conf.BSSID = scanResult.BSSID;
conf.hiddenSSID = true;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
int id = wifiManager.addNetwork(conf);
wifiManager.disconnect();
wifiManager.enableNetwork(id, true);
wifiManager.reconnect();
wifiManager.saveConfiguration();