我正在努力解决一个问题 - 我正试图通过以下代码连接一个SSID,即WPA2 PSK:
WifiConfiguration conf = new WifiConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
conf.SSID =bed_ssid_name;
} else {
conf.SSID = "\"" + bed_ssid_name + "\"";
}
conf.preSharedKey = "\""+ networkPass +"\"";
wifi.addNetwork(conf);
List<WifiConfiguration> list = null;
list = wifi.getConfiguredNetworks();
//wifi.disconnect();
//wifi.disconnect();
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals("\"" + bed_ssid_name + "\"")) {
System.out.println("!!!!!!!### several ssid "+i.SSID +" ssid "+"\""+bed_ssid_name+"\"");
wifi.enableNetwork(i.networkId, true);
break;
}
}
wifi.reconnect();
上面的代码可以正常工作到KitKat。但我在Lollipop面临一些不一致的问题。在Goggle nexus和摩托罗拉第二代有时候它没有连接,有些连接成功,但经过一段时间自发它会断开连接。然后在一些谷歌我发现了一些更多的许可,我们需要给予,然后我已经完成了这个
public void addWifiConfig(String ssid,String password) {
// Log.d(TAG, "Inside addWifiConfig...");
if (ssid == null) {
throw new IllegalArgumentException(
"Required parameters can not be NULL #");
}
String wifiName = ssid;
WifiConfiguration conf = new WifiConfiguration();
// On devices with version Kitkat and below, We need to send SSID name
// with double quotes. On devices with version Lollipop, We need to send
// SSID name without double quotes
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
conf.SSID = wifiName;
} else {
conf.SSID = Constants.BACKSLASH + wifiName + Constants.BACKSLASH;
}
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
// conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
conf.status = WifiConfiguration.Status.ENABLED;
// conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
//conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
int newNetworkId = wifi.addNetwork(conf);
wifi.enableNetwork(newNetworkId, true);
wifi.saveConfiguration();
wifi.setWifiEnabled(true);
}
但是同样的问题,请让我摆脱这个问题。我在这里缺少什么。