title: What interval should I use between each WiFi scan on Android?
body: <p>I need to perform Wifi scans at regular intervals. I am encountering a problem when the time interval is set to 1-2 seconds. It seems like I am not getting any <code>ScanResult</code>. Is there a minimum amount of time to set so that the <code>WifiManager</code> is able to perform a successful WiFi scan?</p>
  
  <p>Here is the code. I am using a <code>Service</code> to do the Wifi scan:</p>
  
  <pre><code>public class WifiScanning extends Service{
    private static final String TAG = "WifiScanning";
    private Timer timer;
    public int refreshRate, numberOfWifiScan, wifiScanGranularity;
    WifiReceiver receiverWifi = new WifiReceiver();
    WifiManager wifi;
    StringBuilder sb;
    List&lt;ScanResult&gt; wifiList;
    List&lt;APData&gt; apdataList;
    List&lt;List&lt;APData&gt;&gt;surveyData;
  
    private TimerTask updateTask = new TimerTask() {
      @Override
      public void run() {
        Log.i(TAG, "Timer task doing work");
        wifi.startScan();
      }
    };
    @Override
      public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
      }
    @Override
    public void onCreate() {
      super.onCreate();
      Log.i(TAG, "Service creating");
      //retrieve the mapRefreshRate from config.xml
      XMLOperations test = new XMLOperations();
      Configuration config = new Configuration();
      config = test.saxXmlParsing(this, 1);
      if(config==null)
        config = test.saxXmlParsing(this, 2);
      refreshRate = Integer.parseInt(config.getMapRefreshRate());
      numberOfWifiScan = Integer.parseInt(config.getNumberOfWifiScan_Positioning());
      wifiScanGranularity = Integer.parseInt(config.getWifiScanGranularity_Positioning());
      timer = new Timer();
      Log.i(TAG, "Refresh Rate: "+ String.valueOf(refreshRate));
      timer.schedule(updateTask, 0, refreshRate);
      wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
      registerReceiver(receiverWifi, new IntentFilter(
      WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    }
    @Override
    public void onDestroy() {
      super.onDestroy();
      Log.i(TAG, "Service destroying");
      unregisterReceiver(receiverWifi);
      if (timer != null){
        timer.cancel();
        timer.purge();
        timer = null;
      }
    }
    class WifiReceiver extends BroadcastReceiver {
      public void onReceive(Context c, Intent intent) {
        sb = new StringBuilder();
        wifiList = wifi.getScanResults();
        String ap_ssid;
        String ap_mac;
        Double ap_rssi;
        for(int i = 0; i &lt; wifiList.size(); i++){
          ap_ssid = wifiList.get(i).SSID;
          ap_mac = wifiList.get(i).BSSID;
          ap_rssi = Double.valueOf(wifiList.get(i).level);
          APData ap = new APData(ap_ssid,ap_mac,ap_rssi);
          apdataList.add(ap);
          sb.append(" " + (wifiList.get(i).SSID).toString());
          sb.append(" " + (wifiList.get(i).BSSID).toString());
          sb.append((" " + String.valueOf(wifiList.get(i).level)));
          sb.append("\n");
        }
        Log.d(TAG, sb.toString());
        for(int i=1; i&lt;=numberOfWifiScan; i++){
          surveyData.add(apdataList);
        }
      }
    }
  }
  </code></pre>
  
  <p>However, I seem to get <code>Nullpointer</code> at this line: <code>apdataList.add(ap);</code>. So I wonder whether the interval is too short, which causes <code>ScanResult</code> to be empty?</p>