连接 WEP 加密
此示例使用 SSID 和密码连接到具有 WEP 加密的 Wi-Fi 接入点。
public boolean ConnectToNetworkWEP(String networkSSID, String password)
{
try {
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\""; // Please note the quotes. String should contain SSID in quotes
conf.wepKeys[0] = "\"" + password + "\""; //Try it with quotes first
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedGroupCiphers.set(WifiConfiguration.AuthAlgorithm.OPEN);
conf.allowedGroupCiphers.set(WifiConfiguration.AuthAlgorithm.SHARED);
WifiManager wifiManager = (WifiManager) this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
int networkId = wifiManager.addNetwork(conf);
if (networkId == -1){
//Try it again with no quotes in case of hex password
conf.wepKeys[0] = password;
networkId = wifiManager.addNetwork(conf);
}
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
break;
}
}
//WiFi Connection success, return true
return true;
} catch (Exception ex) {
System.out.println(Arrays.toString(ex.getStackTrace()));
return false;
}
}