我使用以下代码创建新的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。
WifiManager
或WifiConfiguration
中的任何API都是这样做的吗?
感谢。
答案 0 :(得分:2)
我们必须通过调用保存当前创建的wifi配置的WifiManager.saveConfiguration()
保存创建的wifi配置,我们还需要设置最高优先级来创建wifi配置,以便在下次重启时安卓wi-fi管理器给出偏好这个网络。
答案 1 :(得分:0)
为每个启动时间写一个广播接收器设置用户名和密码。那时不要写任何UI。
答案 2 :(得分: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);