0
public void onBeaconServiceConnect() {
    BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
    beaconManager.setRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {

            List<Beacon> beaconList = new ArrayList<Beacon>(beacons);

            for (Beacon beacon : beacons) {


                Beacon.setHardwareEqualityEnforced(true);

                beaconList.add(beacon);

                if (beaconList.size() >= 2) {

                    if (beaconList.get(0).getBluetoothAddress().equals("20:91:48:4C:5A:34") && beaconList.get(1).getBluetoothAddress().equals("20:91:48:4C:5B:AF")) {

                        if (beaconList.get(0).getDistance() < beaconList.get(1).getDistance()) {

                            Log.i("MainActivity", "You're in room 1");

                        }


                    }

                    if (beaconList.get(1).getBluetoothAddress().equals("20:91:48:4C:5A:34") && beaconList.get(0).getBluetoothAddress().equals("20:91:48:4C:5B:AF")) {

                        if (beaconList.get(1).getDistance() < beaconList.get(0).getDistance()) {

                            Log.i("MainActivity", "You're in room 1");

                        }


                    }
                    if (beaconList.get(0).getBluetoothAddress().equals("20:91:48:4C:5B:AF") && beaconList.get(1).getBluetoothAddress().equals("20:91:48:4C:5A:34")) {

                        if (beaconList.get(0).getDistance() < beaconList.get(1).getDistance()) {

                            Log.i("MainActivity", "You're in room 2");

                        }
                    }


                    if (beaconList.get(1).getBluetoothAddress().equals("20:91:48:4C:5B:AF") && beaconList.get(0).getBluetoothAddress().equals("20:91:48:4C:5A:34")) {

                        if (beaconList.get(1).getDistance() < beaconList.get(0).getDistance()) {

                            Log.i("MainActivity", "You're in room 2");

                        }
                    }


                } else {
                    Log.i("MainActivity", "Less than 2 beacons detected");
                }

            }
        }
    });

So this code is actually fine and works, but whenever I go out of region from one of the beacons, the list size remains 2 and the else { Log.i("MainActivity", "Less than 2 beacons detected");

part of the code is never executed, how can I remove or refresh the beacons whenever they're out of range so whenever there are 2 beacons added into the list but one of the beacons is out of range, less than 2 beacons detected would be printed.

1 Answers1

0

Just get rid of the for loop. You already add all the beacons to the list when the ArrayList is initialized.

Then take the if block out of the for loop and leave it right after the list is initialized.

This way, when the beacon list goes to a length of zero, the if block is still executed..

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • My problem is that the beaconlist length will never go back to zero, once there is a beacon detected in region, it's added to the beaconlist, and if this same beacon is out of region the beaconlist size stays the same, I want the beaconlist size to decrease whenever a beacon is out of the range. – Anas Shaaban Mar 24 '19 at 03:37
  • I don't think that is correct. The `didRangeBeaconsInRegion` notifier is called one per second with a list of beacons detected in the past second. If no beacons are visible in the past second, the beacons collection parameter will have size zero. If you think you are seeing differently. please check your premises. Something must not be as you suspect. – davidgyoung Mar 24 '19 at 13:35