How do I connect to a WiFi Network with an unknown encryption algorithm in Android?
Asked Answered
T

1

3

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();
    }

}
Transliterate answered 6/2, 2014 at 15:59 Comment(3)
by any chance did Eric Woodruff hit the spot, or was it something else?Florance
@Florance it has been too long - I don't remember. I did have to do something similar in another project and found it to be way easier than this example using some newer Android APIs.Transliterate
thanks for the feedback. I'm working on this now, and recent API doesn't seem to have changed much - very basic documentation and no tutorial at all, as they like to do for things they want the OS UI to handle, not user apps :/Florance
S
2

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.

Sanches answered 6/2, 2014 at 16:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.