2012-03-18 119 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);