3

I have devlope project to list out all wifi connection available near to my device.Now, I want to connect any one specific network connection. SO what can I do? I have got all networks in listview. If any example available then post comment link.

private void getWifiNetworksList() {
    IntentFilter filter = new IntentFilter();
    filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
    final WifiManager wifiManager = (WifiManager) getApplicationContext()
            .getSystemService(Context.WIFI_SERVICE);
    ;
    registerReceiver(new BroadcastReceiver() {

        @SuppressLint("UseValueOf")
        @Override
        public void onReceive(Context context, Intent intent) {
            sb = new StringBuilder();
            scanList = wifiManager.getScanResults();
            sb.append("\n  Number Of Wifi connections :" + " "
                    + scanList.size() + "\n\n");
            adapter = new ArrayAdapter<String>(DraginoActivity.this,
                    android.R.layout.simple_list_item_1, list);
            adapter.clear();
            for (int i = 0; i < scanList.size(); i++) {

                listWifiDevice.setAdapter(adapter);

                // list.add(new Integer(i + 1).toString() + ". ");
                list.add((new Integer(i + 1).toString() + ". ")
                        + (scanList.get(i)).toString().substring(5,
                                (scanList.get(i)).toString().indexOf(",")));
                // list.add("\n\n");

                listWifiDevice.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                listWifiDevice
                        .setOnItemClickListener(new OnItemClickListener() {

                            @Override
                            public void onItemClick(AdapterView<?> arg0,
                                    View arg1, int arg2, long arg3) {
                                TextView tv = (TextView) arg1;

                                // TODO Auto-generated method stub
                                Toast.makeText(DraginoActivity.this,
                                        "" + tv.getText().toString(), 1000)
                                        .show();

                            }
                        });
            }

            // listWifiDevice.setAdapter(adapter);
        }

    }, filter);
    wifiManager.startScan();

}

1 Answer 1

2
 private void setNetworksList() {
    networksList = (ListView) findViewById(R.id.networkList);
    networksList.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int position,
                                long id) {
            final int chosenPosition = position;
            WiFiItem wifiItem = new WiFiItem();
            wifiItem = (WiFiItem) parent.getItemAtPosition(position);
            networkSSID = wifiItem.getSSID();
            networkPass = "";
            showPasswordDialog(ConnectActivity.this);
            setPasswordListener(new PasswordTypedListener() {

                @Override
                public void onPasswordTyped() {
                    connectHelper = new ConnectHelper();
                    if(scanResult.get(chosenPosition).capabilities.contains("WEP")) {
                        connectHelper.setupConnection(networkSSID, networkPass, "WEP");
                    }
                    //jezeli siec WPA
                    else if(scanResult.get(chosenPosition).capabilities.contains("WPA")) {
                        connectHelper.setupConnection(networkSSID, networkPass, "WPA");
                    }

                    //inne przypadki (open network)
                    else {
                        connectHelper.setupConnection(networkSSID, networkPass, "ELSE");
                    }

                    wifiManager.addNetwork(connectHelper.getConfiguration());

                    List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
                    for( WifiConfiguration i : list ) {
                        if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
                            wifiManager.disconnect();
                            wifiManager.enableNetwork(i.networkId, true);
                            if(wifiManager.reconnect()) {
                                for(int j=0;j<listWifi.size();j++){
                                    listWifi.get(j).setConnected(false);
                                }
                                runnableCounter=0;
                                handler.postDelayed(ifConnected,1000);
                            }
                            break;
                        }
                    }
                }

            });

        }

    });
}

and also dialog

 private void showPasswordDialog(Context context) {
    final EditText input = new EditText(context);
    input.setSingleLine();
    AlertDialog.Builder alert = new AlertDialog.Builder(context);
    alert.setTitle("PASSWORD");
    alert.setView(input);
    alert.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            networkPass = input.getText().toString();
            if(ConnectActivity.this.passwordListener != null)
                ConnectActivity.this.passwordListener.onPasswordTyped();
        }
    });
    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
        }
    });
    alert.show();

}

and in onCreate but you could skip creating WifiItems and just store SSIDs

    ArrayList<WiFiItem> listWifi = new ArrayList<WiFiItem>();
    wifiManager.startScan();
    scanResult = wifiManager.getScanResults();        
    for(ScanResult sc : scanResult) {
        WiFiItem wifiItem = new WiFiItem();
        wifiItem.setSSID(sc.SSID);
        wifiItem.setBSSID(sc.BSSID);           
        listWifi.add(wifiItem);
    }
4
  • Thanx for solution. What is "WiFiItem"? WHen I tried to declare this it's give an error. Aug 5, 2015 at 12:13
  • WiFiItem is a class. Where it is declared? Aug 5, 2015 at 12:22
  • Hey, You can easily create that class by your own, it cointains only Strings SSID and BSSID. As I said you don't have to create that, only need to store the SSIDs.
    – maciej
    Aug 5, 2015 at 13:05
  • Ok. Thanks. I will try it. :-) Aug 6, 2015 at 5:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.