Home > Enterprise >  AtlBeacons' BeaconManager.startMonitoring(Region) replaces the current monitored region
AtlBeacons' BeaconManager.startMonitoring(Region) replaces the current monitored region

Time:11-18

BeaconManager.monitoredRegions shows just one region is being monitored, when BeaconManager.startMonitoring() has been called twice:

beaconManager.startMonitoring(regionA)
Log.d(TAG, "Started observing beacon: ${entity.a}")
beaconManager.startMonitoring(regionB)
Log.d(TAG, "Started observing beacon: ${entity.b}")
 
Log.i(TAG, "Monitoring regions: ${beaconManager.monitoredRegions}")

The above code produces the following logs:

2021-11-17 14:25:10.804 3918-3918/com.dyson.n620f D/BeaconRepository: Started observing beacon: 58857b49-3560-4152-ad6d-779ec1137f66
2021-11-17 14:25:10.833 3918-3918/com.dyson.n620f D/BeaconRepository: Started observing beacon: 9e3464fd-33e8-45b7-85c2-73d8e159dcd3
2021-11-17 14:25:10.833 3918-3918/com.dyson.n620f I/BeaconRepository: Monitoring regions: [id1: 9e3464fd-33e8-45b7-85c2-73d8e159dcd3 id2: null id3: null]

If beaconManager.startMonitoring(regionA) and beaconManager.startMonitoring(regionB) are swapped, the following logs are given:

2021-11-17 14:29:40.211 4291-4291/com.dyson.n620f D/BeaconRepository: Started observing beacon: 9e3464fd-33e8-45b7-85c2-73d8e159dcd3
2021-11-17 14:29:40.232 4291-4291/com.dyson.n620f D/BeaconRepository: Started observing beacon: 58857b49-3560-4152-ad6d-779ec1137f66
2021-11-17 14:29:40.232 4291-4291/com.dyson.n620f I/BeaconRepository: Monitoring regions: [id1: 58857b49-3560-4152-ad6d-779ec1137f66 id2: null id3: null]

Does anyone understand why this is happening, or what I am miss-understanding? Thank you for your help in advance!

CodePudding user response:

You don't show the definition of your Region objects, but I suspect they might look something like this:

val regionA = Region("my region", "9e3464fd-33e8-45b7-85c2-73d8e159dcd3", null, null)
val regionB = Region("my region", "58857b49-3560-4152-ad6d-779ec1137f66", null, null)

In the sample above, the first parameter to the constructor is a unique id for identifying the region when calling the API. This unique id must be different for every region you define, and if it is not, calls to start monitoring for regionA followed by regionB will tell the library to replace regionA with regionB.

If you want to monitor both regions at the same time, just make sure the first parameter is unique like this:

val regionA = Region("my region A", "9e3464fd-33e8-45b7-85c2-73d8e159dcd3", null, null)
val regionB = Region("my region B", "58857b49-3560-4152-ad6d-779ec1137f66", null, null)
  • Related