2012-03-18 39 views
5

Tôi đang sử dụng mã sau để tạo điểm truy cập wifi mới và kết nối với nó.
Mã này hoạt động tốt và tôi có thể kết nối với điểm truy cập wifi, nhưng vấn đề tôi đang gặp phải là kết nối wifi mà tôi đang tạo không nhận được thông qua quá trình khởi động lại thiết bị.Cách nhớ cấu hình wifi và kết nối mạng qua lại khi khởi động lại

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

gì tôi muốn lưu trữ là khi tôi kết nối thành công tới SSID tôi muốn nhớ mạng đó và khởi chạy tiếp theo của thiết bị Android sẽ tự động kết nối với SSID mà trước đây đã được kết nối tới.

Có phải API nào trong số WifiManager hoặc WifiConfiguration để làm như vậy không?

Cảm ơn.

+0

là nó 'WifiManager.saveConfiguration()' để lưu cấu hình wifi hiện được tạo. – User7723337

Trả lời

2

Chúng ta phải lưu lại cấu hình wifi tạo ra với cuộc gọi đến WifiManager.saveConfiguration() giúp tiết kiệm cấu hình wifi hiện tạo ra, chúng tôi cũng cần phải đặt ưu tiên cao nhất để cấu hình wifi tạo nên rằng khi khởi động lại trình quản lý wi-fi android tiếp theo sẽ ưu tiên cho mạng này.

+0

'WifiManager.saveConfiguration()' không được dùng nữa –

0

Viết bộ thu phát sóng cho mỗi Thời gian khởi động đặt tên người dùng và mật khẩu. Không viết bất kỳ giao diện người dùng nào tại thời điểm đó.

0

Hãy thử mã này cho 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); 
Các vấn đề liên quan