Search code examples
androidibeaconaltbeaconandroid-ibeacon

Android - unable to stop ranging beacons


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 mainactivity, the app shows the previous instance of ranging also.

For Example:

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.

MAINACTIVITY

@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;
}

Variable Distance in oncreate

SharedPreferences sharedPref = getApplication().getSharedPreferences("Settings",Context.MODE_PRIVATE);
distance = sharedPref.getFloat("distance",10.0f);

Ranging function

  @Override
public void onBeaconServiceConnect() {

    //beacon ranging


    beaconManager.addRangeNotifier(new RangeNotifier() {

        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {

            if (beacons.size() > 0) {
                for (Beacon beacon : beacons) {
                    if(beacon.getDistance()<= distance) {
                        Log.i(TAG,beacon.getBluetoothName());
                        Log.i(TAG,String.valueOf(distance));                      
                    }

                }
            }

        }

    });


    try {
        beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
    } catch (RemoteException e) {    }
}

Settings

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));

            }
        }
    });

LOG

before

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

after

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

proxiteeMini_00037 is within 10m range

proxiteeMini_00035 is within 5m range


Solution

  • I suspect you have multiple instances of MainActivity running at the same time. The old instance of MainActivity has the old value of distance and the new one has the new value. Creating an intent to go back to the MainActivity from SettingsActivity will cause this. You can fix this by exiting the SettingsActivity with this.finish();