title: Android App not scanning for beacons after the first run
body: <p>I am trying to build an app where the strength of the nearest beacon is detected  (by the signal strength) and the corresponding API must be called (with the beacon ID as a parameter). The app asks for location the first time and runs very well (detects the nearest beacon). But after I close the app and reopen it, the beacon ID will be empty always (string2 variable is being set with beacon ID). Why is it running the first time and not subsequent times?</p>
  
  <p>Whenever I clear the data of the app, the app asks for location and runs fine. I have coded the entire segment again three times and yet I encounter the same problem.</p>
  
  <p>I have tried to trace the flow through log statements. I observed that onBeaconServiceConnect() method isn't executing the second time (unless the app's data is cleared).</p>
  
  <p><strong>AsyncTask:</strong></p>
  
  <pre><code>public class Navigate extends AsyncTask&lt;String, String, String&gt; {
  
          protected String doInBackground(String... strings) {
              //REST Query
              String cs, ps = null;
  
      //Here bid is a field and it will be set to Beacon ID of the nearest beacon.
  
                  while (!bid.equals(strings[1])) {
                      try {
                          imageURL=callAPI("bid1","bid3");
                           Log.e("BeaconID", bid); //The default value of bid will be printed and not the beacon ID.
  
  
                      } catch (IOException e) {
                          e.printStackTrace();
                      } catch (JSONException e) {
                          e.printStackTrace();
                      }
  
  
                  }
  
  
  
              Log.e("Debug", "Step3");
  
              return "none";
          }
  
          @Override
          protected void onPreExecute() {
              super.onPreExecute();
  
          }
  
  
          @Override
          protected void onProgressUpdate(String... values) {
              super.onProgressUpdate(values);
  
              Toast.makeText(getApplicationContext(), "onProgressUpdate", Toast.LENGTH_SHORT).show();
              mNetworkImageView = (NetworkImageView) findViewById(R.id.mapImage);
              mImageLoader = CustomVolleyRequestQueue.getInstance(getApplicationContext()).getImageLoader();
              mImageLoader.get(values[0], ImageLoader.getImageListener(mNetworkImageView, R.mipmap.ic_launcher_round, android.R.drawable.ic_menu_report_image));
              Log.e("Image Published", values[0]);
              mNetworkImageView.setImageUrl(values[0], mImageLoader);
  
          }
  
          @Override
          protected void onPostExecute(String s) {
              super.onPostExecute(s);
              progressDialog.dismiss();
              tv.setText(s);
  
          }
      }
  </code></pre>
  
  <p><strong>OnBeaconServiceConnect:-</strong></p>
  
  <pre><code>public void onBeaconServiceConnect() {
  
          //Constructing a new Region object to be used for Ranging or Monitoring
          region = new Region("myBeaons", Identifier.parse("23542266-18D1-4FE4-B4A1-23F8195B9D39"), null, null);
  
          //Specifies a class that should be called each time the BeaconService sees or stops seeing a Region of beacons.
          beaconManager.addMonitorNotifier(new MonitorNotifier() {
  
              /*
                  This override method is runned when some beacon will come under the range of device.
              */
  
              @Override
              public void didEnterRegion(Region region) {
                  System.out.println("ENTER -------------------&gt;");
                  try {
  
                      //Tells the BeaconService to start looking for beacons that match the passed Region object
                      // , and providing updates on the estimated mDistance every seconds while beacons in the Region
                      // are visible.
                      beaconManager.startRangingBeaconsInRegion(region);
                  } catch (RemoteException e) {
                      e.printStackTrace();
                  }
                  Log.e("Debug", "Step10");
              }
  
  
              /*
                   This override method is runned when beacon that comes in the range of device
                   ,now been exited from the range of device.
               */
              @Override
              public void didExitRegion(Region region) {
                  System.out.println("EXIT-----------------------&gt;");
                  try {
  
                      //Tells the BeaconService to stop looking for beacons
                      // that match the passed Region object and providing mDistance
                      // information for them.
                      beaconManager.stopRangingBeaconsInRegion(region);
                  } catch (RemoteException e) {
                      e.printStackTrace();
                  }
                  Log.e("Debug", "Step11");
              }
  
  
              /*
                   This override method will Determine the state for the device , whether device is in range
                 of beacon or not , if yes then i = 1 and if no then i = 0
              */
              @Override
              public void didDetermineStateForRegion(int state, Region region) {
                  System.out.println("I have just switched from seeing/not seeing beacons: " + state);
              }
          });
  
  
          //Specifies a class that should be called each time the BeaconService gets ranging data,
          // which is nominally once per second when beacons are detected.
          beaconManager.addRangeNotifier(new RangeNotifier() {
  
              /*
                 This Override method tells us all the collections of beacons and their details that
                 are detected within the range by device
               */
              @Override
              public void didRangeBeaconsInRegion(Collection&lt;Beacon&gt; beacons, Region region) {
  
                  // Checking if the Beacon inside the collection (ex. list) is there or not
  
                  // if Beacon is detected then size of collection is &gt; 0
  
                  if (beacons.size() &gt; 0) {
  
                      final ArrayList&lt;ArrayList&lt;String&gt;&gt; arrayList = new ArrayList&lt;ArrayList&lt;String&gt;&gt;();
                      BeaconManager.setRssiFilterImplClass(ArmaRssiFilter.class);
                      // Iterating through all Beacons from Collection of Beacons
                      ArrayList&lt;Integer&gt; arr = new ArrayList&lt;Integer&gt;();
                      int count = 0;
                      for (Beacon b : beacons) {
                          //RSSI
                          count += 1;
                          System.out.println(b.getBluetoothAddress()+" : "+b.getBluetoothName());
                          System.out.println("Count : " + count);
                          Integer rssi = Integer.valueOf(b.getRssi());
                          arr.add(rssi);
  
                          Integer maxRssi = Collections.max(arr);
                          for(Beacon b1 : beacons){
  
                              if(b1.getRssi() == maxRssi)
                              {
                                  start = b1.getId1().toString();
                                  maxid = start;
                                  Log.e("Debug", "Step12");
                                  System.out.println("uuid :"+func());
                              }
                          }
                      }
                  }
              }
          });
          try {
  
              //Tells the BeaconService to start looking for beacons that match the passed Region object.
              beaconManager.startMonitoringBeaconsInRegion(region);
          } catch (RemoteException e) {
          }
      }
  
      @Override
      public void onDestroy() {
          super.onDestroy();
          //Unbinds an Android Activity or Service to the BeaconService to avoid leak.
          beaconManager.unbind(this);
          finish();
      }
  </code></pre>
  
  <p>The bid is set on the first go. Whenever I exit the app and reopen it again, the bid will not change at all. I have to clear the data of the app and re-run it to work properly.</p>