我正在创建一个范围内的wifis列表并将其显示给用户。我希望用户可以选择列表中的每个项目并插入密码以连接到选定的SSID。
我为wifi连接写了这个方法:
https://www.example.com/example2/
但是当我为了连接到选定的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。
请帮助我解答你的问题。 谢谢。
答案 0 :(得分:1)
addNetwork()
失败时返回-1。
失败的一个常见原因是用户先前通过wifi设置屏幕(或通过wifi设置屏幕)或具有不同Unix用户ID(即UID)的应用以编程方式配置了网络。回想一下,每个应用程序都与一个UID相关。
如果相同的UID第二次调用addNetwork()
,则将允许其覆盖配置,并且该功能将成功。
与通过removeNetwork()
API删除配置有关的类似行为。只有添加了配置的UID的应用程序才能将其删除。
这种行为是有道理的,因为您不希望恶意(或只是有错误的)应用覆盖或删除由用户或其他应用完成的配置。
到目前为止,还不错,但是有一个复杂性-重新安装应用程序(阅读:先卸载然后再安装)时,操作系统将为其提供不同的UID。现在,重新安装后先前添加了配置的同一应用程序将无法再删除或更新它。哎呀!
允许相同的应用程序ID能够更新/删除网络真是太好了。哦,很好。