I am trying to detect an iBeacon with AltBeacon library but it won't work.
iBeacon layout is registered:
BeaconManager.getInstanceForApplication(applicationContext).beaconParsers.add(BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"))
Ranging code
val beaconManager: BeaconManager = BeaconManager.getInstanceForApplication(App.appContext)
beaconManager.addRangeNotifier { beacons, region ->
Log.d("BeaconScanner", "Beacons detected: ${beacons.size}")
}
// Start ranging my beacon
val id = "test"
val uuid = Identifier.parse("30168DC2-8CA6-4283-8169-B309262C4561")
val major = Identifier.parse("12345")
val minor = Identifier.parse("54321")
val region = Region(id, uuid, major, minor)
Log.d("BeaconScanner", "Started ranging $region")
beaconManager.startRangingBeacons(region)
The beacon seems to be detected, but the library thinks the Regions does not match
Beacon packet detected for: id1: 30168dc2-8ca6-4283-8169-b309262c4561 id2: 12345 id3: 54321 with rssi -42
2022-01-24 16:31:43.578 14239-14276/com.mhcin.beacontest D/ScanHelper: beacon detected : id1: 30168dc2-8ca6-4283-8169-b309262c4561 id2: 12345 id3: 54321
2022-01-24 16:31:43.578 14239-14275/com.mhcin.beacontest D/ScanHelper: Beacon packet detected for: id1: 30168dc2-8ca6-4283-8169-b309262c4561 id2: 12345 id3: 54321 with rssi -42
2022-01-24 16:31:43.578 14239-14275/com.mhcin.beacontest D/ScanHelper: beacon detected : id1: 30168dc2-8ca6-4283-8169-b309262c4561 id2: 12345 id3: 54321
2022-01-24 16:31:43.579 14239-14275/com.mhcin.beacontest D/ScanHelper: looking for ranging region matches for this beacon
2022-01-24 16:31:43.579 14239-14275/com.mhcin.beacontest D/ScanHelper: This region (id1: 80ddda3d-66b7-4e35-bf6a-8a647f43126c id2: 12345 id3: 54321) does not match beacon: id1: 30168dc2-8ca6-4283-8169-b309262c4561 id2: 12345 id3: 54321
2022-01-24 16:31:43.579 14239-14276/com.mhcin.beacontest D/ScanHelper: looking for ranging region matches for this beacon
2022-01-24 16:31:43.580 14239-14276/com.mhcin.beacontest D/ScanHelper: This region (id1: 80ddda3d-66b7-4e35-bf6a-8a647f43126c id2: 12345 id3: 54321) does not match beacon: id1: 30168dc2-8ca6-4283-8169-b309262c4561 id2: 12345 id3: 54321
2022-01-24 16:31:43.597 14239-14239/com.mhcin.beacontest D/CycledLeScannerForLollipop: got record
2022-01-24 16:31:43.598 14239-14273/com.mhcin.beacontest D/BeaconParser: Processing pdu type FF: 1eff060001092002784495beaac2372a367ef2729772d9728b120dcf6284c200000000000000000000000000000000000000000000000000000000000000 with startIndex: 2, endIndex: 30
2022-01-24 16:31:43.599 14239-14273/com.mhcin.beacontest D/BeaconParser: This is not a matching Beacon advertisement. (Was expecting be ac. The bytes I see are: 1eff060001092002784495beaac2372a367ef2729772d9728b120dcf6284c200000000000000000000000000000000000000000000000000000000000000
2022-01-24 16:31:43.599 14239-14273/com.mhcin.beacontest D/BeaconParser: Processing pdu type FF: 1eff060001092002784495beaac2372a367ef2729772d9728b120dcf6284c200000000000000000000000000000000000000000000000000000000000000 with startIndex: 2, endIndex: 30
2022-01-24 16:31:43.599 14239-14273/com.mhcin.beacontest D/BeaconParser: This is not a matching Beacon advertisement. (Was expecting 02 15. The bytes I see are: 1eff060001092002784495beaac2372a367ef2729772d9728b120dcf6284c200000000000000000000000000000000000000000000000000000000000000
2022-01-24 16:31:43.607 14239-14239/com.mhcin.beacontest D/CycledLeScannerForLollipop: got record
2022-01-24 16:31:43.607 14239-14278/com.mhcin.beacontest D/BeaconParser: Ignoring pdu type 01
Looking at the logs, it seems to me like AltBeacon is looking for the wrong Region? It clearly says it finds my iBeacon, but then it's looking to match a region with a completely different UUID even though my code clearly defines the correct UUID.
Whole LogCat dump here: https://pastebin.com/VHCkd6Kx
The problem is caused by the way beacon scanning Regions are persisted in the Android Beacon Library. The problem generally only applies to monitoring regions, but when using the job scheduler for scanning (
beaconManager.setEnableScheduledScanJobs(true)) it applies to ranged regions as well.The solution is to do one of three things:
stopMonitoringorstopRangingon the old region definition.This is admittedly awkward. You can see a detailed description of the problem in the library's open source repo along with proposals on making it better:
https://github.com/AltBeacon/android-beacon-library/issues/1075
Any ideas on making this better are appreciated. Please comment on the above issue if you have thoughts.