title: Continuous and responsive Beacons detection on Android
body: <p>In our project we need very fast (ideally under few seconds), reliable and continuous beacon detection, most importantly in the background. Right now we are using the AltBeacon library, BeaconManager is initiated with a context of a foreground service (code provided).</p>
  
  <p>In the foreground it works perfectly on almost any device, the problem is with working in the background. Some devices in the background don't work after some time (for example, Huawei P9 2017, I know it is problem with the device). In general we need better reliability.</p>
  
  <p>The biggest problem is that detection on Oreo in the background is slow - from seconds to long minutes until it will report a first beacon. I have read the entry on AltBeacon web about changes for Oreo, so i know there are limitations from the system.
  So here is my question, is there any way to improve the detection responsiveness (ideally on all android versions)? Even at a price of a bigger battery consumption or some hacks? I've tried to at least get an exception from battery optimizations in the system, but the results are probably the same.</p>
  
  <p>BeaconManager configuration in Background service:</p>
  
  <pre><code>@Override
  public void onCreate() {
      super.onCreate();
      beaconManager = BeaconManager.getInstanceForApplication(this);
      beaconManager.setForegroundScanPeriod(1500);
      beaconManager.setForegroundBetweenScanPeriod(0);
      beaconManager.setBackgroundScanPeriod(1500);
      beaconManager.setBackgroundBetweenScanPeriod(0);
      beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT));
      beaconManager.bind(this);
      if (CommConstants.sdkIsAtLeastSDK(21)) {
          BluetoothMedic medic = BluetoothMedic.getInstance();
          medic.enablePowerCycleOnFailures(this);
          medic.enablePeriodicTests(this, BluetoothMedic.SCAN_TEST);
      }
      goForeground();
  }
  </code></pre>
  
  <p>Starting of monitoring:</p>
  
  <pre><code>private static final Region REGION = new Region(BuildConfig.APPLICATION_ID, null, null, null);
  
  protected void startBeaconMonitoring() {
      if (beaconManager.isBound(this)) {
          findingEnabled = true;
          beaconManager.addRangeNotifier(
                  (collection, region) -&gt; {
                      for (Beacon beacon : collection) {
                          // beacon processing
                      }
                  });
          try {
              mLogger.debug("Started beacon monitoring");
              beaconManager.startRangingBeaconsInRegion(REGION);
              beaconManager.startMonitoringBeaconsInRegion(REGION);
          } catch (RemoteException e) {
              LoggerFactory.getLogger(App.class).error("Monitoring start exception", e);
          }
          regionBootstrap = new RegionBootstrap(this, REGION);
          startLocationUpdates();
      }
  }
  </code></pre>
  
  <p>I've noticed there is a PR for the beacon service to be in the foreground in android-beacon-library, is it possible that this will improve reliability?  </p>