title: Decreased BLE startScan detected devices on Android 5.0 Lollipop
body: <p>Short version:</p>
  
  <p>In my tests with Android 5.0 Lollipop I have noticed <a href="https://developer.android.com/reference/android/bluetooth/le/BluetoothLeScanner.html" rel="nofollow noreferrer"><code>android.bluetooth.le.BluetoothLeScanner</code></a> detects BLE devices less frequently than Android 4.4 KitKat. Why is this and is there an alternative?</p>
  
  <p>Long version:</p>
  
  <p>I am developing an Android application, specifically for the Nexus 7 tablet, that focuses on detecting Bluetooth Low Energy (BLE) devices. The app is mainly interested in the RSSI value of the beacons, to determine their proximity to the tablet. This means I won't need to connect to the BLE device, since the RSSI value is passed to the scan callback when the device is detected.</p>
  
  <p>In Android 4.4 KitKat, when I call <code>BluetoothAdapter.startLeScan(LeScanCallback)</code>, my callback gets called only ONCE for every detected BLE device. (I have seen <a href="https://stackoverflow.com/questions/20058450/how-to-continuously-get-rssi-without-connecting-to-the-ble-device#comment29883084_20058450">some discussions</a> claim that this behaviour can differ per device) However, I am interested in the constantly changing RSSI value, so the currently recommended way is to continuously do startLeScan and stopLeScan with a set interval (250ms in my case):</p>
  
  <pre><code>public class TheOldWay {
  
      private static final int SCAN_INTERVAL_MS = 250;
  
      private Handler scanHandler = new Handler();
      private boolean isScanning = false;
  
      public void beginScanning() {
          scanHandler.post(scanRunnable);
      }
  
      private Runnable scanRunnable = new Runnable() {
          @Override
          public void run() {
              BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
  
              if (isScanning) {
                  adapter.stopLeScan(leScanCallback);
              } else if (!adapter.startLeScan(leScanCallback)) {
                  // an error occurred during startLeScan
              }
  
              isScanning = !isScanning;
  
              scanHandler.postDelayed(this, SCAN_INTERVAL_MS);
          }
      };
  
      private BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
          @Override
          public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
              // use the RSSI value
          }
      };
  
  }
  </code></pre>
  
  <p>Essentially this gives me the required results, but this process is very resource intensive and eventually leads to an unresponsive bluetooth adapter.</p>
  
  <p>For these reasons I upgraded my Nexus 7 to Android 5.0 Lollipop, to see whether my BLE issues would be fixed. In Lollipop  BluetoothAdapter.startLeScan(LeScanCallback) is deprecated and replaced with <a href="https://developer.android.com/reference/android/bluetooth/le/BluetoothLeScanner.html" rel="nofollow noreferrer">a new API</a> that allows for some more control over the scanning process. From my first tests, it appears startScan does not continuously call my callback (on my Nexus 7) when the RSSI values change, so I still need to use the startScan / stopScan implementation:</p>
  
  <pre><code>@TargetApi(21)
  public class TheNewWay {
  
      private static final int SCAN_INTERVAL_MS = 250;
  
      private Handler scanHandler = new Handler();
      private List&lt;ScanFilter&gt; scanFilters = new ArrayList&lt;ScanFilter&gt;();
      private ScanSettings scanSettings;
      private boolean isScanning = false;
  
      public void beginScanning() {
          ScanSettings.Builder scanSettingsBuilder = new ScanSettings.Builder();
          scanSettingsBuilder.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY);
          scanSettings = scanSettingsBuilder.build();
  
          scanHandler.post(scanRunnable);
      }
  
      private Runnable scanRunnable = new Runnable() {
          @Override
          public void run() {
              BluetoothLeScanner scanner = BluetoothAdapter.getDefaultAdapter().getBluetoothLeScanner();
  
              if (isScanning) {
                  scanner.stopScan(scanCallback);
              } else {
                  scanner.startScan(scanFilters, scanSettings, scanCallback);
              }
  
              isScanning = !isScanning;
  
              scanHandler.postDelayed(this, SCAN_INTERVAL_MS);
          }
      };
  
      private ScanCallback scanCallback = new ScanCallback() {
          @Override
          public void onScanResult(int callbackType, ScanResult result) {
              super.onScanResult(callbackType, result);
  
              int rssi = result.getRssi();
  
              // do something with RSSI value
          }
  
          @Override
          public void onScanFailed(int errorCode) {
              super.onScanFailed(errorCode);
  
              // a scan error occurred
          }
      };
  
  }
  </code></pre>
  
  <p>As you can see, I have configured the scanner using the ScanSettings class, which allows you to set the <code>scanMode</code>. I use <code>ScanSettings.SCAN_MODE_LOW_LATENCY</code>, which has the following documentation: "Scan using highest duty cycle. It's recommended to only use this mode when the application is running in the foreground." Sounds exactly like what I want, but unfortunately I only get a beacon detect every 15 - 30 seconds, where the KitKat version shows me the same beacon every 1 - 2 seconds on this scan interval.</p>
  
  <p>Do you have any idea what could be the reason for this difference? Am I missing something, maybe some new settings? Are there alternative ways of doing the above?</p>
  
  <p>Thanks a lot in advance!</p>
  
  <p>Abel</p>
  
  <p>PS: I wanted to include more links to resources I've used, but I don't have the rep points for it yet.</p>