4

I have created wi-fi scanner. It continually scans for available wi-fi networks. But my question is why would exactly broadcast receiver is needed if i can actually run scanning (invoke startScan() with timer every x seconds) and receive the same results without creating broadcast receiver?

This is broadcast receiver code in onCreate() method:

i = new IntentFilter();
i.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
receiver = new BroadcastReceiver(){
    public void onReceive(Context c, Intent i){
      WifiManager w = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);
      List<ScanResult> l = w.getScanResults();
      for (ScanResult r : l) {
         // do smth with results
      }
      // log results
    }
};

In scanning method, which is invoked after scan button is pressed I have:

timer = new Timer(true);
timer.schedule(new WifiTimerTask(), 0, scanningInterval);
registerReceiver(receiver, i );

where WifiTimerTask is

publlic class WifiTimerTask extends TimerTask{
    @Override
     public void run(){
         wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
         if (wifi.isWifiEnabled()) {
            wifi.startScan();
            List<ScanResult> sc = wifi.getScanResults(); 
            for (ScanResult r : l) {
               // do smth with results
            }
           // log results
          }
     }
}

And the point is that scanning can be performed without registerReceiver(receiver,i). However only if scanningInterval is lower than 2s, then receiver scanresults and startScan() are not synchronized. By that I mean startScan() results do not change until receiver will get new results. Meanwhile in logCat I get ERROR/wpa_supplicant(5837): Ongoing Scan action.... Seems like 2s is the lowest scanning interval though. Please correct me if my assumptions are wrong.

Jørgen R
  • 10,568
  • 7
  • 42
  • 59
Artur
  • 75
  • 1
  • 8

1 Answers1

2

When you call startScan() you don't know how long the actual scan will take (generally speaking it can be 1 ms or 5 hours). So you cannot reliably call getScanResults() as you don't know when the scan is completed.

To track the event when when getScanResults() will return update scan results you need to subscribe to SCAN_RESULTS_AVAILABLE_ACTION.

inazaruk
  • 74,247
  • 24
  • 188
  • 156
  • Thanks! I got your point. But if i use startScan() with interval higher than 2s, can it be reliable without receiver? What can be the obstacle to receive result with fixed time interval? Please check my updated question. – Artur Jun 10 '11 at 13:29
  • Ok, first you create an unnecessary thread just to initiate wifi scans. Second, you can't be sure whether those intervals are 2s or 5min. There are different devices, vendors, network drivers, wi-fi layer implementations can differ drastically between devices and so on. You shouldn't use hardcoded interval. That's just bad (even for non-android things), especially when you have a easy to use built-in alternative. – inazaruk Jun 10 '11 at 13:37
  • Thanks. So your suggestion is just use broadcast receiver without timer thread? In that case only one scan is performed. I initiate `startScan()` and then register receiver in the scanning method. – Artur Jun 10 '11 at 14:56
  • With SCAN_RESULTS_AVAILABLE_ACTION only one result after button press is returned. However with RSSI_CHANGED_ACTION I return updates from time to time. – Artur Jun 10 '11 at 15:10
  • you could always use SCAN_RESULTS_AVAILABLE_ACTION and call startScan at the end of the broadcast receiver, that way you kind of have a loop – LDomagala Jun 22 '11 at 16:04