Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remember wifi configuration and connected network through-out the reboots

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.

like image 478
User7723337 Avatar asked Nov 04 '22 03:11

User7723337


1 Answers

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.

like image 154
User7723337 Avatar answered Nov 15 '22 01:11

User7723337