问题
I have researched this question on StackOverflow, but all answers specify how to connect to a network with a known encryption algorithm (mostly WEP). In my application, I retrieve a list of available wifi networks, and display them in a ListView
(using WifiManager
). When the user clicks one of the items in the list, I want to connect to the network.
My current implementation attempts to get the WifiConfiguration
data from a ScanResult
's capabilities String. For example, these are all actual capability strings retrieved:
[WPA2-PSK-CCMP][ESS]
[WPA2-PSK-CCMP+TKIP][ESS]
[WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][ESS]
I have assumed, based on some research, that these are bracket-separated capabilities, and the first item for each of these is a -
separated String showing:
[Authentication Algorithm - Key Management Algorithm - Pairwise Cipher]
I parse this data, then create a WifiConfiguration
Object, then attempt to connect to it, but it always fails (addNetwork
returns -1
). What am I doing wrong? Here is my code:
@Override
public void onItemClick(AdapterView<?> adapter, View parent, int position, long id) {
ScanResult result = (ScanResult) adapter.getItem(position);
WifiConfiguration config = new WifiConfiguration();
String currentNetwork = mWifiManager.getConnectionInfo().getSSID();
if (currentNetwork != null && currentNetwork.equals(result.SSID))
{
Toast.makeText(this, "Already connected", Toast.LENGTH_SHORT).show();
return;
}
config.BSSID = result.BSSID;
config.SSID = result.SSID;
String firstCapabilities = result.capabilities.substring(1, result.capabilities.indexOf("]")-1);
String[] capabilities = firstCapabilities.split("-");
String auth = capabilities[0];
String keyMgmt = capabilities[1];
String pairwiseCipher = capabilities[2];
int a = 0;
if (auth.contains("EAP"))
a |= WifiConfiguration.AuthAlgorithm.LEAP;
else if (auth.contains("WPA"))
a |= WifiConfiguration.AuthAlgorithm.OPEN;
else if (auth.contains("WEP"))
a |= WifiConfiguration.AuthAlgorithm.SHARED;
config.allowedAuthAlgorithms.set(a);
int k = WifiConfiguration.KeyMgmt.NONE;
if (keyMgmt.contains("IEEE802.1X"))
k |= WifiConfiguration.KeyMgmt.IEEE8021X;
else if (auth.contains("WPA") && keyMgmt.contains("EAP"))
k |= WifiConfiguration.KeyMgmt.WPA_EAP;
else if (auth.contains("WPA") && keyMgmt.contains("PSK"))
k |= WifiConfiguration.KeyMgmt.WPA_PSK;
config.allowedKeyManagement.set(k);
int c = WifiConfiguration.PairwiseCipher.NONE;
if (pairwiseCipher.contains("CCMP"))
c |= WifiConfiguration.PairwiseCipher.CCMP;
if (pairwiseCipher.contains("TKIP"))
c |= WifiConfiguration.PairwiseCipher.TKIP;
config.allowedPairwiseCiphers.set(c);
int networkId = mWifiManager.addNetwork(config);
if (networkId == -1)
{
//always hits this line!
Toast.makeText(this, "Failed to create network configuration", Toast.LENGTH_SHORT).show();
}
else
{
//Never reaches here!
mWifiManager.disconnect();
mWifiManager.enableNetwork(networkId, true);
mWifiManager.reconnect();
}
}
回答1:
For WPA*, if you don't set the preSharedKey to 8 or more characters this will fail with -1. I don't see you setting it at all.
来源:https://stackoverflow.com/questions/21607815/how-do-i-connect-to-a-wifi-network-with-an-unknown-encryption-algorithm-in-andro