title: unexpected result using timer for wifi scan android
body: <p>I'm trying to schedule a wifi scan every 1 second as the current 6 secs delay between each scan result is too long for me. I think somehow it works, but I barely understand either the way it works or the result.
  Here is the basic code telling the timer to start <code>wifiMgr.startScan();</code> every 1000 ms.</p>
  
  <pre><code>private void startNetworkScan() {
      mTimer = new Timer();
      mTimer.schedule(new TimerTask() {
          @Override
          public void run() {
              TimerMethod();
          }
      }, 0, 1000);
  }
  
  private void TimerMethod() {
      this.runOnUiThread(rTimer);
  }
  
  private Runnable rTimer = new Runnable() {
      public void run() {
          wifiMgr.startScan();
          Bundle bb = wifiScanReceiver.getResultExtras(true);
          txtList.append("Result " + bb.getString("scanresult") + "\n");
      }
  };
  </code></pre>
  
  <p>Below is the code on the BroadcastReceiver class. I just display the timestamp and related info from the access point.</p>
  
  <pre><code>private void handleScanResultsAvailable() {
      List&lt;ScanResult&gt; results = wifiMgr.getScanResults();
  
      String currentTimeStr = new SimpleDateFormat("HH:mm:ss").format(new Date());
      Bundle b = new Bundle();
      StringBuilder sb = new StringBuilder();
      sb.append(i + ": " + currentTimeStr);
      for (ScanResult result : results) {
          sb.append(String.format(" SSID: %s, RSSI: %s dBm ", result.SSID, result.level));
      }
      b.putString("scanresult", sb.toString());
      setResultExtras(b);
      i++;
  }
  </code></pre>
  
  <p>And here is a snippet of the result:</p>
  
  <pre><code>Result 1: 10:04:12 SSID: XXXXX, RSSI: -88 dBm 
  Result 1: 10:04:12 SSID: XXXXX, RSSI: -88 dBm 
  Result 2: 10:04:14 SSID: XXXXX, RSSI: -87 dBm 
  Result 2: 10:04:14 SSID: XXXXX, RSSI: -87 dBm 
  </code></pre>
  
  <p>Mostly each result is displayed every 1-2 seconds, which is quite good. What I don't understand is, for each result, I always get two lines with the same value. I actually expect that there should be some overlapping results because <code>wifiMgr.startScan()</code> is not stopped at every execution by the timer, like this:</p>
  
  <pre><code>// there are ca. 6-7 lines for every record
  // as the wifi scanresult delay is 6 secs
  Result 1: 10:03:40 SSID: XXXXX, RSSI: -85 dBm 
  Result 1: 10:03:41 SSID: XXXXX, RSSI: -85 dBm 
  Result 1: 10:03:42 SSID: XXXXX, RSSI: -85 dBm 
  Result 1: 10:03:43 SSID: XXXXX, RSSI: -85 dBm 
  Result 1: 10:03:44 SSID: XXXXX, RSSI: -85 dBm 
  Result 1: 10:03:45 SSID: XXXXX, RSSI: -85 dBm 
  Result 2: 10:03:45 SSID: XXXXX, RSSI: -85 dBm 
  Result 1: 10:03:46 SSID: XXXXX, RSSI: -85 dBm 
  Result 2: 10:03:46 SSID: XXXXX, RSSI: -85 dBm 
  Result 1: 10:03:47 SSID: XXXXX, RSSI: -85 dBm 
  Result 2: 10:03:47 SSID: XXXXX, RSSI: -85 dBm 
  Result 1: 10:03:48 SSID: XXXXX, RSSI: -85 dBm 
  Result 2: 10:03:48 SSID: XXXXX, RSSI: -85 dBm 
  </code></pre>
  
  <p>Can anyone shed some light? - is the code right, is the result fine. Would appreciate any explanation on the logic behind it.</p>
  
  <p><strong>Update</strong>: Here is the rest of the BroadcastReceiver code.</p>
  
  <pre><code>@Override
  public void onReceive(Context context, Intent intent) {
      if (intent.getAction().equalsIgnoreCase(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
          handleScanResultsAvailable();
      }
  }
  </code></pre>