How to remember Wi-Fi configuration and connected network through reboots

I use the following code to create a new Wi-Fi access point and connect to it.
This code works fine, and I can connect to a Wi-Fi access point, but the problem I am having is that the Wi-Fi connection that I create is not remembered when the device reboots.

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, when I successfully connect to the SSID, I want to remember this network and the next time I reboot the Android device, it should automatically connect to this SSID that was previously connected.

Is this any API in WifiManageror WifiConfigurationfor this?

Thank.

+5
source share
3 answers

Wi-Fi WifiManager.saveConfiguration(), Wi-Fi, Wi-Fi, Wi- .

+2

, . .

0

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);
0
source

All Articles