A little late, but it might help someone else. You kind of do it wrong. You add a network (one that has not been configured before), and then try to connect to the configured network (the other), both with the same SSID. Instead, try this:
netId = wifiManager.addNetwork(conf);
if (netId == -1) {
netId =
}
wifiManager.enableNetwork(netId, true);
In Kotlin, it looks something like this:
var netId = wifiManager.addNetwork(conf)
if (netId == -1) netId = getExistingNetworkId(ssid)
if (!wifiManager.enableNetwork(netId, true)) {
}
...
private fun getExistingNetworkId(ssid: String): Int {
wifiManager.configuredNetworks?.let {
return it.firstOrNull { it.SSID.trim('"') == ssid.trim('"') }?.networkId ?: -1
}
return -1
}
. / .