我正在创建一个范围内的wifis列表并将其显示给用户.我希望用户可以选择列表中的每个项目并插入密码以连接到所选的SSID.
我为wifi连接写了这个方法:
private WifiConfiguration wifiConf; /* WifiConfiguration object */
private WifiManager wifiMgr; /* WifiManager object */
private WifiInfo wifiInfo; /* WifiInfo object */
public boolean connectToSelectedNetwork(String networkSSID, String networkPassword, String securityType) {
int networkId;
int SecurityProtocol;
if (securityType.contains("WEP")) {
SecurityProtocol = 1;
Log.i(TAG, "Security: WEP");
} else if (securityType.contains("WPA2")) {
Log.i(TAG, "Security: WPA2");
SecurityProtocol = 2;
} else if (securityType.contains("WPA")) {
Log.i(TAG, "Security: WPA");
SecurityProtocol = 3;
} else {
Log.i(TAG, "Security: OPEN");
SecurityProtocol = 4;
}
// Clear wifi configuration variable
clearWifiConfig();
// Sets network SSID name on wifiConf
wifiConf.SSID = "\"" + networkSSID + "\"";
Log.i(TAG, "SSID Received: " + wifiConf.SSID);
switch (SecurityProtocol) {
// WEP "security".
case WEP:
wifiConf.wepKeys[0] = "\"" + networkPassword + "\"";
wifiConf.wepTxKeyIndex = 0;
wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
break;
// WAP security. We have to set preSharedKey.
case WPA2:
wifiConf.preSharedKey = "\"" + networkPassword + "\"";
wifiConf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wifiConf.allowedProtocols.set(WifiConfiguration.Protocol.RSN); // For WPA2
wifiConf.allowedProtocols.set(WifiConfiguration.Protocol.WPA); // For WPA
wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
wifiConf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wifiConf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
break;
case WPA:
wifiConf.preSharedKey = "\"" + networkPassword + "\"";
// Network without security.
case OPEN_NETWORK:
wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
break;
}
// Add WiFi configuration to list of recognizable networks
if ((networkId = wifiMgr.addNetwork(wifiConf)) == -1) {
Log.i("TAG", "Failed to add network configuration!");
return false;
}
// Disconnect from current WiFi connection
if (!disconnectFromWifi()) {
Log.i("TAG", "Failed to disconnect from network!");
return false;
}
// Enable network to be connected
if (!wifiMgr.enableNetwork(networkId, true)) {
Log.i("TAG", "Failed to enable network!");
return false;
}
// Connect to network
if (!wifiMgr.reconnect()) {
Log.i("TAG", "Failed to connect!");
return false;
}
return true;
}
但是,当我调用此功能以连接到选定的wifi时,我总是得到假!我调试了很多次,如果在这种方法中它进入第一,并且没有连接到wifi.
请帮我解答你的问题.
谢谢.
最佳答案 addNetwork()失败时返回-1.
它失败的一个常见原因是网络先前由用户(通过wifi设置屏幕)或通过具有不同Unix用户ID(即UID)的应用程序以编程方式配置.回想一下,每个应用都与UID相关联.
如果相同的UID第二次调用addNetwork(),则允许覆盖配置并且该函数将成功.
类似的相关行为是关于通过removeNetwork()API删除配置.只有具有添加配置的UID的应用才能将其删除.
这种行为是有道理的,因为您不希望恶意(或只是错误的)应用程序覆盖或删除用户或其他应用程序完成的配置.
到目前为止一切都那么好,但是有一个复杂的问题 – 当重新安装应用程序时(读取:卸载然后重新安装),操作系统将为其提供不同的UID.现在,重新安装之后添加配置的相同应用程序无法再删除或更新它.哎哟!
允许相同的应用程序ID能够更新/删除网络会很不错.那好吧.