I am using following code to create new wifi access point and to connect to it.
This code is working fine and i am able to connect to wifi access point, but the problem i am facing that is the wifi connection which i am creating is not getting remembered through out the reboots of the device.
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = "\"SSIDName\"";
wc.preSharedKey = "\"password\"";
wc.hiddenSSID = true;
wc.status = WifiConfiguration.Status.ENABLED;
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
int res = wifi.addNetwork(wc);
Log.d("WifiPreference", "add Network returned " + res );
boolean b = wifi.enableNetwork(res, true);
Log.d("WifiPreference", "enableNetwork returned " + b );
What i want to archive is when i successfully connect to SSID i want to remember that network and on next reboot of the device Android should automatically connect to that SSID which was previously connected to.
Is that any API in WifiManager
or WifiConfiguration
to do so?
Thanks.
We have to save the created wifi configuration with call to WifiManager.saveConfiguration()
which saves the currently created wifi configuration, also we need to set the highest priority to created wifi configuration so that on next reboot android wi-fi manager gives preference to this network.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With