How to make a list of available "wifis"

Asked

Viewed 82 times

0

I would like to know how to take all available wifis and put them on a list, searched on the internet, but did not understand right, I would like a well summarized 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)
    { }         
}    
}
  • Put the code you found there, easier to explain what you didn’t understand than to create an example from scratch.

  • @re22 edited, I did not understand very well the WifiManager and the registerReceiver

  • You said in your question that you want to know (get the available wifis), posted your code and just missed to say which or what problems specifically you are encountering.

  • As there is no explanation of the code, I have not understood almost anything(for what purpose each thing).

1 answer

1


The registerReceiver (Broadcastreceiver receiver, Intentfilter filter) registers a BroadcastReceiver for a particular IntentFilter. That means when the event(in your case) WifiManager.SCAN_RESULTS_AVAILABLE_ACTION happens, the BroadcastReceiver which you stated 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 him and treat him.

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

results = wifi.getScanResults();
size = results.size();
  • Could show me a way to just save the active "wifis" on a list, without performing any action ??

  • You already have it in List<ScanResult> results

Browser other questions tagged

You are not signed in. Login or sign up in order to post.