How can I connect to a WIFI Network by clicking an item on ListView of configured network results.?

243 views Asked by At

I have got a list of configured Wifi Networks by wifiManager.getConfiguredNetworks() method. I displayed them in a Listview. Now whichever listItem I click , it connects to the same single Network only. I want to connect to the network that was clicked.

Here is my Java code

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String networkSSID = list.get(position).SSID;
                String networkPass = list.get(position).preSharedKey;
               WifiConfiguration conf = new WifiConfiguration();
                conf.SSID = "\"" + networkSSID + "\"";
                conf.preSharedKey = "\""+ networkPass +"\"";
                int netId = wifiManager.addNetwork(conf);
                wifiManager.disconnect();
                wifiManager.enableNetwork(netId,true);
                wifiManager.reconnect();

            }
        });
1

There are 1 answers

0
Rahul Chaudhary On BEST ANSWER

I use below block of code

 private void connectToAP(String ssid, String passkey) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(WIFI_SERVICE);
    Log.i(TAG, "* connectToAP");

    WifiConfiguration wifiConfiguration = new WifiConfiguration();
    List<ScanResult> scanResultList = wifiManager.getScanResults();



    for (ScanResult result : scanResultList) {
        if (result.SSID.equals(ssid)) {

            String securityMode = getScanResultSecurity(result);

            if (securityMode.equalsIgnoreCase("OPEN")) {

                wifiConfiguration.SSID = "\"" + ssid + "\"";
                wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                int res = wifiManager.addNetwork(wifiConfiguration);
                Log.d(TAG, "# add Network returned " + res);

                boolean b = wifiManager.enableNetwork(res, true);
                Log.d(TAG, "# enableNetwork returned " + b);

                wifiManager.setWifiEnabled(true);

            } else if (securityMode.equalsIgnoreCase("WEP")) {

                wifiConfiguration.SSID = "\"" + ssid + "\"";
                wifiConfiguration.wepKeys[0] = "\"" + passkey + "\"";
                wifiConfiguration.wepTxKeyIndex = 0;
                wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
                int res = wifiManager.addNetwork(wifiConfiguration);
                Log.d(TAG, "### 1 ### add Network returned " + res);

                boolean b = wifiManager.enableNetwork(res, true);
                Log.d(TAG, "# enableNetwork returned " + b);

                wifiManager.setWifiEnabled(true);
            }

            wifiConfiguration.SSID = "\"" + ssid + "\"";
            wifiConfiguration.preSharedKey = "\"" + passkey + "\"";
            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);

            int res = wifiManager.addNetwork(wifiConfiguration);
            Log.d(TAG, "### 2 ### add Network returned " + res);

            wifiManager.enableNetwork(res, true);

            boolean changeHappen = wifiManager.saveConfiguration();

            if (res != -1 && changeHappen) {
                Log.d(TAG, "### Change happen");


            } else {
                Log.d(TAG, "*** Change NOT happen");
            }

            wifiManager.setWifiEnabled(true);
        }
    }
}