title: Android Beacon Library - BLE beacons detecting is not working
body: <p>I am trying to detecting beacons with <a href="http://altbeacon.github.io/android-beacon-library/index.html" rel="nofollow">Android Beacon Library</a> in Android. I created a service which is running in background and detecting beacons.</p>
  
  <p>The problem is that app is not detecting beacons when bluetooth is turned off. But if I turn on bluetooth is working properly. There is a really weird thing as well. If I turn off bluetooth again while app is running, it still continue detecting. It means that BLE detecting is working but only if I turn on bluetooth and turn it off again.</p>
  
  <p>How can I enable BLE detecting? There is my implementation below. Do I miss something?</p>
  
  <p><strong>Beacon Service class</strong></p>
  
  <pre><code>public class BeaconDiscoverer extends Service implements BeaconConsumer {
      private static final String TAG = BeaconDiscoverer.class.getSimpleName();
      private static BeaconManager beaconManager;
      private static Region region;
      private BackgroundPowerSaver backgroundPowerSaver;
  
      public BeaconDiscoverer() {
      }
  
      @Nullable
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
  
      @Override
      public void onCreate() {
          region = new Region("myRangingUniqueId", null, null, null);
          beaconManager = BeaconManager.getInstanceForApplication(this);
  
          beaconManager.getBeaconParsers().add(new BeaconParser().
                  setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
  
          configureBatterySaverMode();
  
          beaconManager.bind(this);
      }
  
      @Override
      public void onDestroy() {
          beaconManager.unbind(this);
          super.onDestroy();
      }
  
      private void configureBatterySaverMode() {
          BeaconManager.setAndroidLScanningDisabled(true);
          backgroundPowerSaver = new BackgroundPowerSaver(getApplicationContext());
  
          // set the duration of the scan to be 5 seconds
          beaconManager.setBackgroundScanPeriod(Utility.convertToMilliseconds(2));
          // set the time between each scan to be 1 min (60 seconds)
          beaconManager.setBackgroundBetweenScanPeriod(Utility.convertToMilliseconds(25));
      }
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          Log.d(TAG, "BeaconDiscoverer started up");
  
          return super.onStartCommand(intent, flags, startId);
      }
  
      @Override
      public void onBeaconServiceConnect() {
          Log.d(TAG, "onBeaconServiceConnect");
          beaconManager.setRangeNotifier(new RangeNotifier() {
              @Override
              public void didRangeBeaconsInRegion(Collection&lt;Beacon&gt; beacons, Region region) {
                  if (beacons.size() &gt; 0) {
                      Beacon firstBeacon = beacons.iterator().next();
                      Log.i(TAG, "Beacon detected: " + firstBeacon.getDistance() + " m. - " + firstBeacon.getBluetoothAddress());
  
                  }
              }
          });
  
          startRanging();
      }
  
      public void stopRanging() {
          try {
              beaconManager.stopRangingBeaconsInRegion(region);
          } catch (RemoteException e) {
              e.printStackTrace();
          }
      }
  
      public void startRanging() {
          if (User.currentUser() == null)
              return;
  
          try {
              beaconManager.startRangingBeaconsInRegion(region);
          } catch (RemoteException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p><strong>Application class</strong></p>
  
  <pre><code>public class App extends Application {
      private static final String TAG = App.class.getSimpleName();
  
      @Override
      public void onCreate() {
          super.onCreate();
  
          startService(new Intent(this, BeaconDiscoverer.class));
      }
  }
  </code></pre>