刷新或删除列表中的信标

问题描述 投票:0回答:1
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");
                }

            }
        }
    });

所以这段代码实际上很好并且有效,但每当我从其中一个信标离开区域时,列表大小仍然是2和else { Log.i("MainActivity", "Less than 2 beacons detected");

部分代码永远不会执行,如何在信号超出范围时删除或刷新信标,因此每当有2个信标添加到列表中但其中一个信标超出范围时,检测到的信标少于2个打印。

java android beacon altbeacon android-ibeacon
1个回答
0
投票

摆脱for循环。初始化ArrayList时,您已将所有信标添加到列表中。

然后从for循环中取出if块,并在列表初始化后立即将其保留。

这样,当信标列表长度为零时,仍然执行if块。

© www.soinside.com 2019 - 2024. All rights reserved.