How to make a list with the wifis available

0

I would like to know how to get all the wifis available and put them in a list, I searched the internet, but I did not get it right, I would like a well-coded code, thank you.

public class WiFiDemo extends Activity implements OnClickListener
{      
WifiManager wifi;       
ListView lv;
TextView textStatus;
Button buttonScan;
int size = 0;
List<ScanResult> results;

String ITEM_KEY = "key";
ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
SimpleAdapter adapter;

/* Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    textStatus = (TextView) findViewById(R.id.textStatus);
    buttonScan = (Button) findViewById(R.id.buttonScan);
    buttonScan.setOnClickListener(this);
    lv = (ListView)findViewById(R.id.list);

    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)
    { }         
}    
}
    
asked by anonymous 09.09.2015 / 21:45

1 answer

1

registerReceiver (BroadcastReceiver receiver , IntentFilter filter) registers a BroadcastReceiver for a given IntentFilter . This means that when the event (in your case) WifiManager.SCAN_RESULTS_AVAILABLE_ACTION happens, the BroadcastReceiver that you declared in:

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

You will receive it and treat it.

In short, whenever the SCAN_RESULTS_AVAILABLE_ACTION broadcast happens, its BroadcastReceiver implementation with its reimplementation of the onReceive method will be responsible for handling the broadcast. It is within onReceive that you get the number of wifis available, as you are already doing with:

results = wifi.getScanResults();
size = results.size();
    
09.09.2015 / 22:09