How can I connect to a specific wifi network?

user3510984

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

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I get WiFi Network information (SSID) in a Phonegap app?

From Dev

Unable to connect to a specific Wifi network programmatically on Android

From Dev

How to connect to hidden wifi network using nmcli

From Dev

Wifi card can see network but cannot connect

From Dev

How can I connect to a specific BSSID?

From Dev

How do I connect to a WiFi network using wicd?

From Dev

Wifi card can see network but cannot connect

From Dev

Can I automatically connect to the strongest wifi network under Windows 7?

From Dev

How can I connect to a specific BSSID?

From Dev

How do I connect to a WiFi network using wicd?

From Dev

How can I prefer one wifi network over another?

From Dev

Can't connect to a specific WiFi network (Ubuntu 14.04)

From Dev

How can I bypass dns for one hostname on one wifi network

From Dev

How can I connect 2 windows 8 machines with WiFi Direct?

From Dev

Can I connect to a router's wired network via WiFi for gaming purposes?

From Dev

Can a laptop connect to an ethernet network printer with WIFI?

From Dev

How can I connect to a specific wifi network?

From Dev

How can I force a connection to a specific WiFi network using nmcli?

From Dev

How do I connect to a WPA wifi network using the command line?

From Dev

Can I instruct gnome network manager NOT to connect to a specific network

From Dev

Unable to connect to specific wifi network

From Dev

How I can connect a bridge but use the wifi of the router?

From Dev

How can I identify my Wifi network device?

From Dev

How can I have iwd connect automatically to know available WiFi?

From Dev

How to connect 3 wifi routers in a home network

From Dev

How can I automatically connect to wifi + openvpn on startup/resume when "Automatically connect to wifi" is enabled?

From Dev

Can't connect to a specific WiFi network (Ubuntu 20.04)

From Dev

How do I connect to a hidden WiFi network on Ubuntu 20.04

From Dev

Can I use easy connect library with the Roving Network XV Wifi module?

Related Related

  1. 1

    How can I get WiFi Network information (SSID) in a Phonegap app?

  2. 2

    Unable to connect to a specific Wifi network programmatically on Android

  3. 3

    How to connect to hidden wifi network using nmcli

  4. 4

    Wifi card can see network but cannot connect

  5. 5

    How can I connect to a specific BSSID?

  6. 6

    How do I connect to a WiFi network using wicd?

  7. 7

    Wifi card can see network but cannot connect

  8. 8

    Can I automatically connect to the strongest wifi network under Windows 7?

  9. 9

    How can I connect to a specific BSSID?

  10. 10

    How do I connect to a WiFi network using wicd?

  11. 11

    How can I prefer one wifi network over another?

  12. 12

    Can't connect to a specific WiFi network (Ubuntu 14.04)

  13. 13

    How can I bypass dns for one hostname on one wifi network

  14. 14

    How can I connect 2 windows 8 machines with WiFi Direct?

  15. 15

    Can I connect to a router's wired network via WiFi for gaming purposes?

  16. 16

    Can a laptop connect to an ethernet network printer with WIFI?

  17. 17

    How can I connect to a specific wifi network?

  18. 18

    How can I force a connection to a specific WiFi network using nmcli?

  19. 19

    How do I connect to a WPA wifi network using the command line?

  20. 20

    Can I instruct gnome network manager NOT to connect to a specific network

  21. 21

    Unable to connect to specific wifi network

  22. 22

    How I can connect a bridge but use the wifi of the router?

  23. 23

    How can I identify my Wifi network device?

  24. 24

    How can I have iwd connect automatically to know available WiFi?

  25. 25

    How to connect 3 wifi routers in a home network

  26. 26

    How can I automatically connect to wifi + openvpn on startup/resume when "Automatically connect to wifi" is enabled?

  27. 27

    Can't connect to a specific WiFi network (Ubuntu 20.04)

  28. 28

    How do I connect to a hidden WiFi network on Ubuntu 20.04

  29. 29

    Can I use easy connect library with the Roving Network XV Wifi module?

HotTag

Archive