AtlBeacons BeaconManager多次调用startMonitoring仅保留最后一个监控区域问题
问题成因
你遇到的监控区域被覆盖的核心原因是创建regionA和regionB时使用了相同的唯一标识符(uniqueId)。
AltBeacon框架中Region类构造方法的第一个参数为区域的唯一标识字符串,BeaconManager内部以该uniqueId作为键存储所有处于监控状态的区域:
- 调用
startMonitoring(Region)时,如果传入Region的uniqueId已存在于监控列表中,框架会直接用新的Region覆盖同id的旧条目,而非新增监控区域 - 仅当传入Region的uniqueId不存在于列表时,才会作为新的监控区域添加,实现多区域同时监控
修复方案
创建每个Region时分配不同的uniqueId即可,参考代码如下:
// 两个Region的第一个参数uniqueId分别设置为不同的值:regionA、regionB val regionA = Region("regionA", Identifier.parse("58857b49-3560-4152-ad6d-779ec1137f66"), null, null) val regionB = Region("regionB", Identifier.parse("9e3464fd-33e8-45b7-85c2-73d8e159dcd3"), null, null) 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}")
修改后再次运行,日志会输出两个监控区域,说明多区域同时监控已生效。
内容的提问来源于stack exchange,提问作者Nic Backshall
相关产品推荐
相关产品推荐

