User7723337
User7723337

Reputation: 12018

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.

Upvotes: 5

Views: 6592

Answers (3)

User7723337
User7723337

Reputation: 12018

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.

Upvotes: 2

alejandrocordon
alejandrocordon

Reputation: 356

Try this code for WPA :

        WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        WifiConfiguration wc = new WifiConfiguration(); 
        wc.SSID = "\""+SSIDname+"\""; //IMP! This should be in Quotes!!
        wc.hiddenSSID = false;
        wc.status = WifiConfiguration.Status.DISABLED;     
        wc.priority = 1;
        wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        wc.preSharedKey = "\"".concat(password).concat("\"");
        int res = wifi.addNetwork(wc);

Upvotes: 0

Pavandroid
Pavandroid

Reputation: 1586

Write a broadcast receiver for every Boot time set the username and password. Don't write any UI at that moment.

Upvotes: 0

Related Questions