First you need to check if the Wi-Fi device is turned on or not if it is not turned on. Then use WifiManager and getScanResults to get a list of available wifi. Here is a snippet of code that 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 this worked or not.
source share