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

你离开我真会死。 提交于 2019-12-06 03:56:13

问题


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.


回答1:


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.




回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/9756601/how-to-remember-wifi-configuration-and-connected-network-through-out-the-reboots

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!