@Override
public void onBeaconServiceConnect() {
BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
Beacon.setHardwareEqualityEnforced(true);
List<Beacon> beaconList = new ArrayList<Beacon>(beacons);
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个ibeacons时,beaconList.size()
应该为2,但是beaconList
的大小为1,并且在日志中打印的输出为>
03-24 172233.292 27699-27699net.simplifiedlearning.firebaseauth IMainActivity您在2号会议室
03月24日172234.412 27699-27699net.simplifiedlearning.firebaseauth IMainActivity您在2号会议室
03-24 172235.552 27699-27699net.simplifiedlearning.firebaseauth IMainActivity您在2号会议室
03-24 172236.737 27699-27699net.simplifiedlearning.firebaseauth IMainActivity检测到少于2个信标
03-24 172237.857 27699-27699net.simplifiedlearning.firebaseauth IMainActivity检测到少于2个信标
03-24 172239.012 27699-27699net.simplifiedlearning.firebaseauth IMainActivity检测到少于2个信标
03-24 172241.292 27699-27699net.simplifiedlearning.firebaseauth IMainActivity您在2号会议室
beaconList.size()
的大小应为2,因为我在线有2个ibeacons,而我确实有Beacon.setHardwareEqualityEnforced(true);
但是相反,大小是1,并且打印的信标少于2个,而您却在2号房间。这里似乎是什么问题?