title: on App resume beaconmanager fails to detect beacon using Android Beacon Library
body: <p>First time I run the app beacon monitoring and ranging is working fine. But the second time I lanuch the app i.e after calling onResume it is only showing following and no more calls to didRangeBeaconsInRegion : I am guessing it is because of beaconManager instance is not properly destroyed or something in calls to onDestroy. However according to the library example I tried unbinding in onPause() with no success. Any idea ?</p>
  <pre><code>I/MonitoringActivity: I have just switched from seeing/not seeing beacons: 0
  2020-07-15 18:41:40.776 9657-9657/com.something.beacon I/MonitoringActivity: I have just switched from seeing/not seeing beacons: 0
  2020-07-15 18:41:40.824 9657-9657/com.something.beacon I/MonitoringActivity: I have just switched from seeing/not seeing beacons: 1
  
  
  public class MainActivity extends AppCompatActivity implements BeaconConsumer {
  
  private static final int PERMISSION_REQUEST_FINE_LOCATION = 1;
  private static final int PERMISSION_REQUEST_BACKGROUND_LOCATION = 2;
  
  ListView beaconList;
  protected static final String TAG = &quot;MonitoringActivity&quot;;
  private BeaconManager beaconManager;
  
  private FirebaseDatabase database;
  private DatabaseReference beaconDBRef;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      //Firebase database setup
      database = FirebaseDatabase.getInstance();
      beaconDBRef = database.getReference().child(&quot;messages&quot;);
  
      beaconList = (ListView) findViewById(R.id.beacon_list);
      Log.d(TAG, &quot;onCreate: is called&quot;);
  
      beaconManager = BeaconManager.getInstanceForApplication(this);
  
      beaconManager.getBeaconParsers().add(new BeaconParser().
              setBeaconLayout(&quot;m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25&quot;));
  
      // Detect the main identifier (UID) frame:
      beaconManager.getBeaconParsers().add(new BeaconParser().
              setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT));
  
      // Detect the telemetry (TLM) frame:
      beaconManager.getBeaconParsers().add(new BeaconParser().
              setBeaconLayout(BeaconParser.EDDYSTONE_TLM_LAYOUT));
  
      // Detect the URL frame:
      beaconManager.getBeaconParsers().add(new BeaconParser().
              setBeaconLayout(BeaconParser.EDDYSTONE_URL_LAYOUT));
  
      beaconManager.bind(this);
  
      verifyBluetooth();
      if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
          if (this.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
                  == PackageManager.PERMISSION_GRANTED) {
              if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.Q) {
                  if (this.checkSelfPermission(Manifest.permission.ACCESS_BACKGROUND_LOCATION)
                          != PackageManager.PERMISSION_GRANTED) {
                      if (!this.shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_BACKGROUND_LOCATION)) {
                          final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                          builder.setTitle(&quot;This app needs background location access&quot;);
                          builder.setMessage(&quot;Please grant location access so this app can detect beacons in the background.&quot;);
                          builder.setPositiveButton(android.R.string.ok, null);
                          builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
  
                              @TargetApi(23)
                              @Override
                              public void onDismiss(DialogInterface dialog) {
                                  requestPermissions(new String[]{Manifest.permission.ACCESS_BACKGROUND_LOCATION},
                                          PERMISSION_REQUEST_BACKGROUND_LOCATION);
                              }
  
                          });
                          builder.show();
                      }
                      else {
                          final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                          builder.setTitle(&quot;Functionality limited&quot;);
                          builder.setMessage(&quot;Since background location access has not been granted, this app will not be able to discover beacons in the background.  Please go to Settings -&gt; Applications -&gt; Permissions and grant background location access to this app.&quot;);
                          builder.setPositiveButton(android.R.string.ok, null);
                          builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
  
                              @Override
                              public void onDismiss(DialogInterface dialog) {
                              }
  
                          });
                          builder.show();
                      }
                  }
              }
          } else {
              if (!this.shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)) {
                  requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
                                  Manifest.permission.ACCESS_BACKGROUND_LOCATION},
                          PERMISSION_REQUEST_FINE_LOCATION);
              }
              else {
                  final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                  builder.setTitle(&quot;Functionality limited&quot;);
                  builder.setMessage(&quot;Since location access has not been granted, this app will not be able to discover beacons.  Please go to Settings -&gt; Applications -&gt; Permissions and grant location access to this app.&quot;);
                  builder.setPositiveButton(android.R.string.ok, null);
                  builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
  
                      @Override
                      public void onDismiss(DialogInterface dialog) {
                      }
  
                  });
                  builder.show();
              }
  
          }
      }
  }
  
  @Override
  protected void onResume() {
      super.onResume();
  }
  
  @Override
  protected void onDestroy() {
      super.onDestroy();
      beaconManager.unbind(this);
  }
  
  long tempTime = Integer.MIN_VALUE;
  long callInterval = 10000;
  
  @Override
  public void onRequestPermissionsResult(int requestCode,
                                         String permissions[], int[] grantResults) {
      switch (requestCode) {
          case PERMISSION_REQUEST_FINE_LOCATION: {
              if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                  Log.d(TAG, &quot;fine location permission granted&quot;);
              } else {
                  final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                  builder.setTitle(&quot;Functionality limited&quot;);
                  builder.setMessage(&quot;Since location access has not been granted, this app will not be able to discover nearby devices.&quot;);
                  builder.setPositiveButton(android.R.string.ok, null);
                  builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
  
                      @Override
                      public void onDismiss(DialogInterface dialog) {
                      }
  
                  });
                  builder.show();
              }
              return;
          }
          case PERMISSION_REQUEST_BACKGROUND_LOCATION: {
              if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                  Log.d(TAG, &quot;background location permission granted&quot;);
              } else {
                  final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                  builder.setTitle(&quot;Functionality limited&quot;);
                  builder.setMessage(&quot;Since background location access has not been granted, this app will not be able to discover devices when in the background.&quot;);
                  builder.setPositiveButton(android.R.string.ok, null);
                  builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
  
                      @Override
                      public void onDismiss(DialogInterface dialog) {
                      }
  
                  });
                  builder.show();
              }
              return;
          }
      }
  }
  private void verifyBluetooth() {
  
      try {
          if (!BeaconManager.getInstanceForApplication(this).checkAvailability()) {
              final AlertDialog.Builder builder = new AlertDialog.Builder(this);
              builder.setTitle(&quot;Bluetooth not enabled&quot;);
              builder.setMessage(&quot;Please enable bluetooth in settings and restart this application.&quot;);
              builder.setPositiveButton(android.R.string.ok, null);
              builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
                  @Override
                  public void onDismiss(DialogInterface dialog) {
                      //finish();
                      //System.exit(0);
                  }
              });
              builder.show();
          }
      }
      catch (RuntimeException e) {
          final AlertDialog.Builder builder = new AlertDialog.Builder(this);
          builder.setTitle(&quot;Bluetooth LE not available&quot;);
          builder.setMessage(&quot;Sorry, this device does not support Bluetooth LE.&quot;);
          builder.setPositiveButton(android.R.string.ok, null);
          builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
  
              @Override
              public void onDismiss(DialogInterface dialog) {
                  //finish();
                  //System.exit(0);
              }
  
          });
          builder.show();
  
      }
  
  }
  
  @Override
  public void onBeaconServiceConnect() {
      // Set the two identifiers below to null to detect any beacon regardless of identifiers
      Identifier myBeaconNamespaceId = Identifier.parse(&quot;0x2F234454F4911BA9FFA6&quot;);
      final Identifier myBeaconInstanceId = Identifier.parse(&quot;0x000000000001&quot;);
  
      beaconManager.removeAllMonitorNotifiers();
      beaconManager.removeAllRangeNotifiers();
  
      beaconManager.addMonitorNotifier(new MonitorNotifier() {
          @Override
          public void didEnterRegion(Region region) {
              Log.i(TAG, &quot;I just saw an beacon for the first time!&quot;);
  
              try {
                  Log.d(TAG, &quot;didEnterRegion&quot;);
                  beaconManager.startRangingBeaconsInRegion(region);
              } catch (RemoteException e) {
                  e.printStackTrace();
              }
          }
  
          @Override
          public void didExitRegion(Region region) {
              Log.i(TAG, &quot;I no longer see an beacon&quot;);
  
              try {
                  Log.d(TAG, &quot;didExitRegion&quot;);
                  beaconManager.stopRangingBeaconsInRegion(region);
              } catch (RemoteException e) {
                  e.printStackTrace();
              }
          }
  
          @Override
          public void didDetermineStateForRegion(int state, Region region) {
              Log.i(TAG, &quot;I have just switched from seeing/not seeing beacons: &quot;+state);
          }
      });
  
  
      beaconManager.addRangeNotifier(new RangeNotifier() {
          @Override
          public void didRangeBeaconsInRegion(Collection&lt;Beacon&gt; beacons, Region region) {
  
              for(Beacon oneBeacon : beacons) {
                  String beaconInfo = &quot;Namespace id: &quot; + oneBeacon.getId1() + &quot;\nInstance Id: &quot; + oneBeacon.getId2();
                  Log.d(TAG, beaconInfo);
                  Toast.makeText(MainActivity.this, beaconInfo, Toast.LENGTH_SHORT).show();
  
                  //Write to database every 10 seconds
                  long currentTime = System.currentTimeMillis();
                  if(currentTime &gt; tempTime + callInterval){
  
                      BeaconModel beaconModel = new BeaconModel();
  
                      beaconModel.setBeaconName(oneBeacon.getId2().toString());
                      beaconModel.setDistance(oneBeacon.getDistance());
                      beaconModel.setMessage(&quot;this is a nice message&quot;);
  
                      beaconDBRef.child(beaconModel.getBeaconName()).setValue(beaconModel);
                      //beaconDBRef.push().setValue(&quot;beacon detected! - &quot; + oneBeacon.getId2());
                      tempTime = currentTime;
                  }
              }
          }
      });
  
      try {
  
          Identifier myBeaconNamespaceId1 = Identifier.parse(&quot;0xedd1ebeac04e5defa017&quot;);
          Identifier myBeaconInstanceId1 = Identifier.parse(&quot;0x0e32fea91b13&quot;);
          Identifier myBeaconInstanceId2 = Identifier.parse(&quot;0xa9937b420872&quot;);
  
          beaconManager.startMonitoringBeaconsInRegion(new Region(&quot;myMonitoringUniqueId&quot;, myBeaconNamespaceId1, myBeaconInstanceId1, null));
          beaconManager.startMonitoringBeaconsInRegion(new Region(&quot;myMonitoringUniqueId2&quot;, myBeaconNamespaceId1, myBeaconInstanceId2, null));
          beaconManager.startMonitoringBeaconsInRegion(new Region(&quot;myMonitoringUniqueId3&quot;, null, null, null));
  
      } catch (RemoteException e) {
  
          Log.d(TAG, e.getMessage());
      }
  }
  </code></pre>
  <p>}</p>