2012-03-18 141 views
5

我正在使用以下代码来创建新的wifi接入点并连接到它。
此代码工作正常,我能够连接到WiFi接入点,但我面临的问题是我创建的无线连接并没有被记住通过设备的重新启动。如何记住WiFi配置和连接网络重新启动

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

我希望存档是什么时,我成功地连接到SSID我要记住,网络和设备的下次重新启动的Android应该自动连接到以前连接到该SSID。

请问WifiManagerWifiConfiguration中的任何API都可以这样做吗?

谢谢。

+0

它是'WifiManager.saveConfiguration()',它保存当前创建的wifi配置。 – User7723337 2012-03-18 07:20:54

回答

2

我们必须和调用创建的无线网络的配置保存到WifiManager.saveConfiguration()节省了当前创建的无线网络配置,还需要我们的最高优先级设置为创建无线网络配置,在下次重新启动时,android wi-fi管理员会优先选择此网络。

+0

'WifiManager.saveConfiguration()'已被弃用 – 2017-10-10 08:34:09

0

为每个启动时间写一个广播接收器设置用户名和密码。不要在那个时候写任何UI。

0

试试这个代码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); 
相关问题