Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter wifi connections in android?

I want to disable a specific wifi connection which is entered by the user.

How to get all the wifi connections of the device and enable/disable the specific one?

like image 764
Devu Soman Avatar asked Dec 19 '12 04:12

Devu Soman


People also ask

Can you prioritize Wi-Fi connections on Android?

Prioritize Android Wi-Fi Network Using Built-In Settings To check if your ROM has one, open Settings > Network & internet > Wi-Fi. Tap on the overflow menu, then hit Advanced Wi-Fi. If you see a Wi-Fi Priority option, you can specify the priority of Wi-Fi networks here.


1 Answers

First you should check that whether the wifi of device is enabled or not, if not enable it. Then use WifiManager and getScanResults to get the list of wifi available. Here is a snippet of code, you can use.

 wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        if (wifi.isWifiEnabled() == false)
        {
            Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled", Toast.LENGTH_LONG).show();
            wifi.setWifiEnabled(true);
        }   
        this.adapter = new SimpleAdapter(WiFiDemo.this, arraylist, R.layout.row, new String[] { ITEM_KEY }, new int[] { R.id.list_value });
        lv.setAdapter(this.adapter);

        registerReceiver(new BroadcastReceiver()
        {
            @Override
            public void onReceive(Context c, Intent intent) 
            {
               results = wifi.getScanResults();
               size = results.size();
            }
        }, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));                    
    }

    public void onClick(View view) 
    {
        arraylist.clear();          
        wifi.startScan();

        Toast.makeText(this, "Scanning...." + size, Toast.LENGTH_SHORT).show();
        try 
        {
            size = size - 1;
            while (size >= 0) 
            {   
                HashMap<String, String> item = new HashMap<String, String>();                       
                item.put(ITEM_KEY, results.get(size).SSID + "  " + results.get(size).capabilities);

                arraylist.add(item);
                size--;
                adapter.notifyDataSetChanged();                 
            } 
        }
        catch (Exception e)
        { }         
    }

Let me know if it worked or not.

like image 151
Sahil Mahajan Mj Avatar answered Oct 17 '22 18:10

Sahil Mahajan Mj