2

I am using following code to connect with WPA2 in android (I can connect with WEP and WPA). But I am getting only 'Scanning' status. And I am unable to connect with WPA2 network. Can you tell me what changes I need to make this code relevant with wpa2 WiFi.

private boolean saveWepConfigAndEnableNetwork(String ssid, String pass) {
    isAlreadyPresend = false;
    WifiConfiguration wc = new WifiConfiguration();
    wc.SSID = "\"" + ssid + "\""; // IMP! This should be in Quotes!!
    wc = checkPreviousConfiguration(wc);
    wc.hiddenSSID = true;
    wc.status = WifiConfiguration.Status.DISABLED;
    wc.priority = 40;
    wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
    wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
    wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
    wc.preSharedKey = "\"" + pass + "\"";

    wc.wepKeys[0] = "\"" + pass + "\""; // This is the WEP Password
    wc.wepTxKeyIndex = 0;

    boolean res1 = wifi.setWifiEnabled(true);
    int res = 0;
    if(isAlreadyPresend){
        res = wifi.addNetwork(wc);
    }else{
        res = wifi.updateNetwork(wc);
    }

    Log.d("WifiPreference", "add Network returned " + res);
    boolean es = wifi.saveConfiguration();
    Log.d("WifiPreference", "saveConfiguration returned " + es);
    boolean b = wifi.enableNetwork(res, true);
    Log.d("WifiPreference", "enableNetwork returned " + b);
    return b;
}

// Check if this SSID is already stored. If it is, return that
// configuration.
// If not, return the configuration being tested.
public WifiConfiguration checkPreviousConfiguration(WifiConfiguration wc) {
    List<WifiConfiguration> configs = wifi.getConfiguredNetworks();
    for (WifiConfiguration config : configs) {
        if (config.SSID.equals(wc.SSID)){
            isAlreadyPresend = true;
            return config;
        }
    }
    return wc;
}
Nanne
  • 64,065
  • 16
  • 119
  • 163
Muhammad Nabeel Arif
  • 19,140
  • 8
  • 51
  • 70

2 Answers2

3

Here is the code which worked for me to connect with WPA2

// Adding a WPA or WPA2 network
public static void changeNetworkWPA(WifiManager wifiManager, String ssid, String password) {
    WifiConfiguration config = changeNetworkCommon(ssid);
    // Hex passwords that are 64 bits long are not to be quoted.
    config.preSharedKey = quoteNonHex(password, 64);
    config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
    config.allowedProtocols.set(WifiConfiguration.Protocol.WPA); // For WPA
    config.allowedProtocols.set(WifiConfiguration.Protocol.RSN); // For WPA2
    config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
    config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
    config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
    config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
    updateNetwork(wifiManager, config);
}

Code: From Zxing library

Muhammad Nabeel Arif
  • 19,140
  • 8
  • 51
  • 70
  • 1
    The code is taken from: com.google.zxing.client.android.wifi.WifiConfigManager.java – Ben Mar 11 '14 at 23:54
0

just to enrich the excellent answer above,

Android supports different WIFI security mechanism such as WEP and WPA/WPA2 which are described under IEEE 802.11 norm. The Android WifiManager API lets you set up the WIFI network parameters of your application in order to connect to a particular access point (your wifi box) using a particular protocol.

for example, in case of WPA/WPA2, by performing the key challenge handshake that authorizes connection on a WPA/WPA2 networks. Now, to successfully connect to a WPA or WPA2 network you just need to add the supported protocols, cipher methods and key management methods that your device will try to use when connecting to it otherwise they won’t have a chance to understand each other.

If you are using a PreShared key as authentication with WPA/WPA2 (which is used in most part of the real world wifi hotspots) then you are basically using a symmetric key phrase that’s know in advance by your device and your access point, Assuming you use a WPA/WPA2 network (Not WEP nor Enterprise WPA2 server crt authentication)

Remember WPA and WPA2 differ in the type of cipher for the packages the first on uses generally TKIP and the other one uses AES ( similar CCMP) which is more secure. that say, you can have confusing combinations of cipher methods using the same WIFI protection standard as for example

WPA2:
NETWORK USE KEY CCMP=[WPA2-PSK-CCMP][ESS]
WPA:
NETWORK USE KEY CCMP=[WPA-PSK-CCMP][ESS]
NETWORK USE KEY TKIP=[WPA-PSK-TKIP+CCMP][ESS]
NETWORK USE KEY TKIP=[WPA-PSK-TKIP][ESS]

So the best approach in case you are interested in access to both WPA and WPA2 networks with a keyphrase as mentioned above, resumes on explicitly adding the corresponding supported config options to be as open as possible for the type of network you want to potentially connect and once config is done, you just need to add the network to use by your app and start the connection challenge.

you can use this code , it took me a while but it works great! for WPA and WPA2 using same wifi configuration.

public static boolean addNetwork8(Context context, String ssid, String password) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    // setup a wifi configuration
    WifiConfiguration wc = new WifiConfiguration();
    wc.SSID = "\"" + ssid + "\"";
    wc.preSharedKey = "\""+ password +"\"";
    wc.status = WifiConfiguration.Status.ENABLED;
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
    wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
    // connect to and enable the connection
    int netId = wifiManager.addNetwork(wc);
    wifiManager.enableNetwork(netId, true);
    wifiManager.setWifiEnabled(true);


    return true;
}

and then you can call the method wherever you need to connect to a WPA/WPA2 network

boolean added = Connectivity.addNetwork8(this, network, password);
    Log.d(TAG, "Network added " + added);

Enjoy, working code!!

PS: i did not have to specify authalgorithm config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN) ; for WAP/WAP2 i assume that it's because it uses x0 , luckily .

/** Open System authentication (required for WPA/WPA2) */public static 
final int OPEN = 0;