I have to show list of access points (with no internet) and then connect to a selected network. I was able to get all available network network with wifiManager.getScanResults() then i have filtered my preferred open network and configured with wifiManager.addNetwork(conf);then i am trying to connect that preferred network with wifiManager.enableNetwork(j.networkId, true) It connecting to That network and again switching back to previous network.
1) the selected network gets connected 2) then its disconnected after couple of seconds 3 )wifi connects to the previously connected network The behavior is only observed in Android devices with OS above 7.0. Marsh mellow and below device it is not switching network
here is my code 1 ) scanning and configuring
if (netType == ConnectivityManager.TYPE_WIFI) {
//wifiManager =
(WifiManager)this.getSystemService(Context.WIFI_SERVICE);
List<ScanResult> results = wifiManager.getScanResults();
if (!results.equals(null)) {
for (int i = 0; i < results.size(); i++) {
ScanResult result = results.get(i);
if(result.SSID.toLowerCase().startsWith("XXXX.")) {
list.add(result.SSID);
conf =new WifiConfiguration();
conf.SSID = "\"" + result.SSID + "\"";
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiManager.addNetwork(conf);
}
}
}
2) to connect preferred network
if (j.SSID != null && j.SSID.equals("\"" + XXXXXX+ "\"")) {
Log.d("Config", "" + j.SSID);
wifiManager.disconnect();
wifiManager.enableNetwork(j.networkId, true);
wifiManager.reconnect();
break;
}