So I've been having some trouble figuring this stuff out for myself and I'm rather new at Android so would really appreciate some help on this.
I have taken a look at answers such as these on SO: How do I connect to a specific Wi-Fi network in Android programmatically?
and I see that you have to configure a network, manually put in the password, and add it to the list of configured networks, etc.
I was wondering what one would do in the case of multiple wifi already being configured/saved by the user, and choosing a specific one to connect to programmatically? Does this mean that they already appear in the getConfiguredNetworks() method call? If so, how would one go about executing .enableNetwork(int netId, boolean disableOthers)?
As a side note, both of those functions are part of the WifiManager class. Hopefully this all made sense!
Just change the string "Your SSID" in the 3rd line to the SSID you want to connect.
And add this permissions in the manifest:
My first contribution to StackOverflow, hope it helps someone :D
wifiManager.enableNetwork(wifiConfiguration.networkId, true);I took the answer by racuevji and elaborated on it. Some more permissions are required and if the wifi is off, the solution below also switches it on:
where activity_main.xml:
and AndroidManifest.xml:
The main problem with connecting to existing configuration is, that you look for existing network with SSID key, which is not a unique key of the network. You can have at home 2 different networks with the same SSID, or it can happend that network password is changed from the last time, your device was connected to that network. If your app is targeted below 6.0, then you can update saved configuration, if not, then you have a problem and you have to create new WifiConfiguration and save it.
Android Developer on changes in Android 6.0:
The best scenario that I found it was that I try to connect to every saved network with the same SSID and if every configuration failed to connect, then I have added and saved new Configuration to the device.