vidulaJ
vidulaJ

Reputation: 1260

Connect to a WPA/ WPA2 PSK WiFi network

I have been through several threads on stackoverflow itself and other blogs and sites about connecting to a WiFi network programmatically. I have got the guide of them and developed a code snippet also, which is published below. I have a secured WPA/WPA2 PSK secured WiFi network at my place and what I want to know is, would I be able to connect to the WiFi network through this snippet? I can't connect for the time being. What have I done wrong here? What can I do to go ahead? Any suggestions and help is highly appreciated.
Thank you.

Here is my code snippet.

try {
            WifiConfiguration conf = new WifiConfiguration();
            conf.SSID           = "\"" + SSID + "\"";
            conf.hiddenSSID     = false;
            conf.wepKeys[0]     = "\\" + networkPass + "\\";
            conf.wepTxKeyIndex  = 0;

            conf.preSharedKey = "\\" + networkPass + "\\";
                conf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
            conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
            conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
            conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
            conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);

            conf.status = WifiConfiguration.Status.ENABLED;
            conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
            conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
            conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
            conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);              

            mainWifi.addNetwork(conf);
            mainWifi.saveConfiguration();

            List<WifiConfiguration> list = mainWifi.getConfiguredNetworks();
            for( WifiConfiguration i : list ) {
                if(i.SSID != null && i.SSID.equals("\"" + SSID + "\"")) {
                    mainWifi.disconnect();

                    Log.d("RSSI_VALUE", "NET_ID " + String.valueOf(i.networkId));

                    boolean enable = mainWifi.enableNetwork(i.networkId, true);
                    Log.d("RSSI_VALUE", "ENABLE_WIFI " + String.valueOf(enable));

                    boolean recon = mainWifi.reconnect();         
                    Log.d("RSSI_VALUE", "RECONN_WIFI " + String.valueOf(recon));

                    break;
                }           
             }
        } catch (Exception e) {
            e.printStackTrace();
        }            }

Upvotes: 2

Views: 4331

Answers (1)

Namrata Bagerwal
Namrata Bagerwal

Reputation: 1210

You have mixed the code for WEP and PSK security modes, you should try below code:

    if(scanResultSecurity.equals(Constants.WEP)){
                                            wifiConfiguration.SSID = "\"" + networkSSID + "\""; 
                                            wifiConfiguration.wepKeys[0] = "\"" + password + "\"";
                                            wifiConfiguration.wepTxKeyIndex = 0;
                                            wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                                            wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
                                            //Adds to the list of network and returns the network id which can be used to enable it later.
                                            networkId = wifiManager.addNetwork(wifiConfiguration);
                                            if(networkId != -1){
                                                connectWifi(wifiConfiguration);
                                            }

                                        }
                                        else if(scanResultSecurity.equals(Constants.PSK)){
                                            wifiConfiguration.SSID = "\"" + networkSSID + "\""; 
                                            wifiConfiguration.preSharedKey = "\"" + password + "\"";
                                            wifiConfiguration.hiddenSSID = true;
                                            wifiConfiguration.status = WifiConfiguration.Status.ENABLED;
                                            wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
                                            wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                                            wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                                            wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                                            wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                                            wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                                            wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
                                          //Adds to the list of network and returns the network id which can be used to enable it later.
                                            networkId = wifiManager.addNetwork(wifiConfiguration); 
                                            if(networkId != -1){
                                                connectWifi(wifiConfiguration);
                                            }
                                        }


private void connectWifi(WifiConfiguration wifiConfiguration){
        wifiManager.disconnect();
        wifiManager.setWifiEnabled(true);
        boolean enableNetworkBoolean = wifiManager.enableNetwork(wifiConfiguration.networkId, true);
        Log.i(Constants.LOG_TAG, "networkId "+wifiConfiguration.networkId);
        Log.i(Constants.LOG_TAG, "SSID "+wifiConfiguration.SSID);
        Log.i(Constants.LOG_TAG, enableNetworkBoolean+" enableNetworkBoolean");
        boolean reconnectBoolean = wifiManager.reconnect();
        Log.i(Constants.LOG_TAG, reconnectBoolean+" reconnectBoolean");
        boolean changeHappen = wifiManager.saveConfiguration();
        Log.i(Constants.LOG_TAG, changeHappen+" changeHappen");
        if(enableNetworkBoolean && reconnectBoolean && changeHappen){
            Log.i(Constants.LOG_TAG, "Change Happen" );     
            Utility.showToast(MainActivity.this, Constants.CONNECTED);
        }
        else{
            Log.i(Constants.LOG_TAG, "Change not Happen" );
            Utility.showToast(MainActivity.this, Constants.CONNECTING_ERROR);
        }                   
    }




  Hope this works for you :-)

Upvotes: 2

Related Questions