title: Android - unable to stop ranging beacons
body: <p>I am trying to create an android application that ranges for beacons in an area given by the user.
  The problem is when I add the distance constraint and come back to <strong>mainactivity</strong>, the app shows the previous instance of ranging also.</p>
  
  <p><strong>For Example</strong>:</p>
  
  <p>The default value of the distance is 10m when I change the distance to 5m, it still shows the beacon that is 10m apart. If I change the value of the distance to 2m, I will be shown the 5m instance of ranging and 10m instance of ranging also.</p>
  
  <p><strong>MAINACTIVITY</strong></p>
  
  <pre><code>@Override
  public boolean onOptionsItemSelected(MenuItem item) {
      int id = item.getItemId();
  
      if(id == R.id.settings) {
          try {
              beaconManager.stopRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
              beaconManager.unbind(this);
              Log.w(TAG,"Stopped ranging");
          } catch (RemoteException e) {
              e.printStackTrace();
          }
          startActivity(new Intent(MainActivity.this, Settings.class));
  
      }
  
      return true;
  }
  </code></pre>
  
  <p><strong>Variable</strong>  <em>Distance</em> in oncreate</p>
  
  <pre><code>SharedPreferences sharedPref = getApplication().getSharedPreferences("Settings",Context.MODE_PRIVATE);
  distance = sharedPref.getFloat("distance",10.0f);
  </code></pre>
  
  <p><strong>Ranging function</strong></p>
  
  <pre><code>  @Override
  public void onBeaconServiceConnect() {
  
      //beacon ranging
  
  
      beaconManager.addRangeNotifier(new RangeNotifier() {
  
          @Override
          public void didRangeBeaconsInRegion(Collection&lt;Beacon&gt; beacons, Region region) {
  
              if (beacons.size() &gt; 0) {
                  for (Beacon beacon : beacons) {
                      if(beacon.getDistance()&lt;= distance) {
                          Log.i(TAG,beacon.getBluetoothName());
                          Log.i(TAG,String.valueOf(distance));                      
                      }
  
                  }
              }
  
          }
  
      });
  
  
      try {
          beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
      } catch (RemoteException e) {    }
  }
  </code></pre>
  
  <p><strong>Settings</strong></p>
  
  <pre><code>Button button;
  EditText distance;
  Float distanceValue;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_settings);
      setTitle("Settings");
      SharedPreferences sharedPref = getApplication().getSharedPreferences("Settings",Context.MODE_PRIVATE);
      final SharedPreferences.Editor editor = sharedPref.edit();
      button = (Button) findViewById(R.id.button);
      distance = (EditText) findViewById(R.id.distanceValue);
      button.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
  
              if(distance.getText().toString().isEmpty()){
                  distance.requestFocus();
                  distance.setError("Required");
              }
              else{
                  distanceValue = Float.valueOf(distance.getText().toString());
                  editor.putFloat("distance",distanceValue);
                  editor.commit();
                  startActivity(new Intent(Settings.this,MainActivity.class));
  
              }
          }
      });
  </code></pre>
  
  <p><strong>LOG</strong></p>
  
  <p><em>before</em></p>
  
  <pre><code>06-28 14:54:21.724 13866-18103/com.example.beaconscanner I/MonitoringActivity: proxiteeMini_00037
  06-28 14:54:21.724 13866-18103/com.example.beaconscanner I/MonitoringActivity: 10.0
  06-28 14:54:21.732 13866-18103/com.example.beaconscanner I/MonitoringActivity: proxiteeMini_00035
  06-28 14:54:21.732 13866-18103/com.example.beaconscanner I/MonitoringActivity: 10.0
  </code></pre>
  
  <p><em>after</em></p>
  
  <pre><code>06-28 14:56:37.778 13866-18103/com.example.beaconscanner I/MonitoringActivity: proxiteeMini_00035
  06-28 14:56:37.778 13866-18103/com.example.beaconscanner I/MonitoringActivity: 5.0
  06-28 14:56:37.788 13866-18103/com.example.beaconscanner I/MonitoringActivity: proxiteeMini_00035
  06-28 14:56:37.788 13866-18103/com.example.beaconscanner I/MonitoringActivity: 5.0
  06-28 14:56:37.801 13866-18103/com.example.beaconscanner I/MonitoringActivity: proxiteeMini_00037
  06-28 14:56:37.801 13866-18103/com.example.beaconscanner I/MonitoringActivity: 10.0
  </code></pre>
  
  <p><strong>proxiteeMini_00037 is within 10m range</strong></p>
  
  <p><strong>proxiteeMini_00035 is within 5m range</strong></p>