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.