wifiManager.startScan not returning any results (need some guidance please)

android, eclipse, wifimanager

Solution

That's probably because the `startScan` method only triggers the discovery of the nearby access points, and doesn't make them available to you immediatly.

The Android documentation states about this method that it :

Request a scan for access points. Returns immediately. The availability of the results is made known later by means of an asynchronous event sent on completion of the scan.

So, you'd have to register a `BroadcastReceiver` to the `SCAN_RESULTS_AVAILABLE_ACTION` in order to get the list of the available access points as soon as the scan completes.

At this time, you can call the `getScanResults()` method which will return the list of the found access points.

EDIT : Also make sure that the Wi-Fi is enabled on the device before launching a scan.

Problem

At this moment in time I am trying to build an Android application that will be able to locate a wifi enabled device indoors. So I have broken it down into various steps in order and 1 of the steps is to scan for available wireless networks and return the signal strength etc associated with each access point that it discovers (it doesn't have to connect to the access points but just has to ping them for information). Below shows the snippet of code that I have created and when the code is debugged it isn't identifying an wifi access points so could someone tell me where the problem is or point me in the right direction. ``` myWifiMan.startScan(); List<ScanResult> wifiList = myWifiMan.getScanResults(); if (wifiList != null) { //Construct Clue for(int i = 0; i < wifiList.size(); i++) { message = message + "'" + wifiList.get(i).SSID +"':" + Integer.toString(wifiList.get(i).level); if((i+1) < wifiList.size()) message = message + ","; } message = message + "}]"; ``` due to the answer given below would the following code give me the desired results? ``` private void initializeWiFiListener(){ System.out.println("executing initializeWiFiListener"); String connectivity_context = Context.WIFI_SERVICE; final WifiManager wifi = (WifiManager)getSystemService(connectivity_context); if(!wifi.isWifiEnabled()){ if(wifi.getWifiState() != WifiManager.WIFI_STATE_ENABLING){ wifi.setWifiEnabled(true); } } registerReceiver(new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); boolean a= wifiManager.startScan();//request a scan for access points final List<ScanResult> results= wifiManager.getScanResults();//list of access points from the last scan for(final ScanResult result : results){ System.out.println("ScanResult level: "+ result.level); } } }, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); } ```

Original source