为什么Android Beacon Library didRangeBeaconsInRegion有时会触发两次?
Why Android Beacon Library didRangeBeaconsInRegion is sometimes triggered twice?
这是关于 Android Beacon Library 的问题。
设置
该应用使用 BootstrapNotifier
持续进行后台扫描。
一旦检测到匹配的信标(即触发 didEnterRegion
),
此后台扫描将被禁用,并启动开始对信标进行测距的前台服务。
(更多背景可以参考)
这个原子测试项目也可以参考this repository
BeaconApplication负责持续后台扫描。
SelfBeaconService 负责主动前景测距。
(如果手头有beacon想搭建测试,需要将identifier filter改为null或者自己的identifier。)
问题
有时(我说有时是因为我不知道如何100%重现),在SelfBeaconService
中,didRangeBeaconsInRegion
同时触发两次,但我只配置了一个区域.
下面是记录双触发的日志
请注意,我将扫描间隔设置为4秒。
03-07 10:46:31.811 (...) I/selfBeacon: Bootstrap didEnterRegion
03-07 10:46:31.812 (...) I/BeaconService: stop monitoring received
03-07 10:46:31.820 (...) I/selfBeacon: Service start commanded
03-07 10:46:31.831 (...) I/BeaconService: unbinding
03-07 10:46:31.832 (...) E/BeaconService: onDestroy()
03-07 10:46:31.834 (...) I/BeaconService: onDestroy called. stopping scanning
03-07 10:46:31.838 (...) I/selfBeaconService: onCreate
03-07 10:46:31.839 (...) I/selfBeaconService: onStartCommand
03-07 10:46:31.929 (...) I/CycledLeScanner: This is Android 5.0. We are using new scanning APIs
03-07 10:46:31.951 (...) I/BeaconService: beaconService version 2.13.1 is starting up on the main process
03-07 10:46:31.954 (...) W/ModelSpecificDistanceCalculator: App has no android.permission.INTERNET permission. Cannot check for distance model updates
03-07 10:46:31.958 (...) W/ModelSpecificDistanceCalculator: Cannot find match for this device. Using default
03-07 10:46:31.959 (...) W/ModelSpecificDistanceCalculator: Cannot find match for this device. Using default
03-07 10:46:31.960 (...) I/BeaconService: binding
03-07 10:46:31.979 (...) I/selfBeaconService: onBeaconServiceConnect
03-07 10:46:31.980 (...) I/BeaconService: start ranging received
03-07 10:46:32.168 (...) I/art: Do partial code cache collection, code=10KB, data=24KB
03-07 10:46:32.169 (...) I/art: After code cache collection, code=10KB, data=24KB
03-07 10:46:32.169 (...) I/art: Increasing code cache capacity to 128KB
03-07 10:46:33.065 (...) I/ScanHelper: Non-distinct packets detected in a single scan. Restarting scans unecessary.
03-07 10:46:33.106 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 3.4581850922412523 meters away.
03-07 10:46:36.726 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 3.4581850922412523 meters away.
03-07 10:46:40.726 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 3.6056118574381157 meters away.
03-07 10:46:41.285 (...) I/art: Debugger is no longer active
03-07 10:46:41.285 (...) I/art: Starting a blocking GC Instrumentation
03-07 10:46:42.392 (...) I/CycledLeScanner: This is Android 5.0. We are using new scanning APIs
03-07 10:46:42.401 (...) I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-07 10:46:42.401 (...) I/ScanJob: Running immediate scan job: instance is org.altbeacon.beacon.service.ScanJob@398f59c
03-07 10:46:42.402 (...) I/ScanJob: scanJob version 2.13.1 is starting up on the main process
03-07 10:46:42.402 (...) W/ModelSpecificDistanceCalculator: App has no android.permission.INTERNET permission. Cannot check for distance model updates
03-07 10:46:42.404 (...) W/ModelSpecificDistanceCalculator: Cannot find match for this device. Using default
03-07 10:46:42.404 (...) W/ModelSpecificDistanceCalculator: Cannot find match for this device. Using default
03-07 10:46:42.409 (...) I/ScanJob: Scan job running for 300000 millis
03-07 10:46:43.528 (...) I/ScanHelper: Non-distinct packets detected in a single scan. Restarting scans unecessary.
03-07 10:46:44.434 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 2.92768959654115 meters away.
03-07 10:46:44.727 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 3.4996718323492226 meters away.
03-07 10:46:46.451 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 3.181513412080198 meters away.
03-07 10:46:48.737 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 4.135957968665044 meters away.
03-07 10:46:48.743 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 3.4581850922412523 meters away.
03-07 10:46:52.732 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 7.408179588037119 meters away.
03-07 10:46:52.735 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 11.306160864298167 meters away.
03-07 10:46:56.736 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 5.632619096507348 meters away.
03-07 10:46:56.740 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 9.280517536034091 meters away.
03-07 10:47:00.728 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 9.280517536034091 meters away.
03-07 10:47:00.733 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 9.280517536034091 meters away.
03-07 10:47:04.729 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 8.917299181373306 meters away.
03-07 10:47:04.733 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 8.917299181373306 meters away.
如您所见,从 10:46:33
到 10:46:40
,它以 4 秒的间隔正常工作。之后,突然一堆日志消息踢了进来,看起来像Android Beacon Library 的东西(比如ScanJob
)被重启了。
从那时起,来自 10:46:44
的回调每 4 秒触发两次。
所以问题是...发生了什么,或者为什么?
快速上下文摘要:在这个项目中,作者试图通过在检测到信标时启动前台服务进行测距来优化 Android 8 上的不间断测距。当未检测到信标时,将在 Android 8.
上使用默认作业调度程序扫描
根据日志,似乎是在前台服务开始持续测距后截取的摘录。然后作业调度程序启动并在服务已经扫描的同时启动 ScanJob。这可能会产生双重回调,因为两个不同的线程同时进行循环扫描并将测距结果报告给同一个委托。因此,应用程序获得双重回调。
这里的根本问题是,如果计划的作业用于扫描,调用 beaconManager.unbind(...)
不会取消之前计划的作业。该库应该这样做,但从 2.31.1 版开始它不会这样做。这将需要与库一起解决一个新问题。我记录了一个问题 here
在此期间,可以手动取消作业。 (注意:这使用私有库 API,因此可能会在未来的版本中中断。最好暂时使用此类代码,直到库中的问题得到修复。)
有了上述警告,您应该可以在解除绑定后手动取消作业:
JobScheduler jobScheduler = (JobScheduler)
context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
jobScheduler.cancel(ScanJob.getImmediateScanJobId(context));
jobScheduler.cancel(ScanJob.getPeriodicScanJobId(context));
这是关于 Android Beacon Library 的问题。
设置
该应用使用 BootstrapNotifier
持续进行后台扫描。
一旦检测到匹配的信标(即触发 didEnterRegion
),
此后台扫描将被禁用,并启动开始对信标进行测距的前台服务。
(更多背景可以参考
这个原子测试项目也可以参考this repository
BeaconApplication负责持续后台扫描。
SelfBeaconService 负责主动前景测距。
(如果手头有beacon想搭建测试,需要将identifier filter改为null或者自己的identifier。)
问题
有时(我说有时是因为我不知道如何100%重现),在SelfBeaconService
中,didRangeBeaconsInRegion
同时触发两次,但我只配置了一个区域.
下面是记录双触发的日志
请注意,我将扫描间隔设置为4秒。
03-07 10:46:31.811 (...) I/selfBeacon: Bootstrap didEnterRegion
03-07 10:46:31.812 (...) I/BeaconService: stop monitoring received
03-07 10:46:31.820 (...) I/selfBeacon: Service start commanded
03-07 10:46:31.831 (...) I/BeaconService: unbinding
03-07 10:46:31.832 (...) E/BeaconService: onDestroy()
03-07 10:46:31.834 (...) I/BeaconService: onDestroy called. stopping scanning
03-07 10:46:31.838 (...) I/selfBeaconService: onCreate
03-07 10:46:31.839 (...) I/selfBeaconService: onStartCommand
03-07 10:46:31.929 (...) I/CycledLeScanner: This is Android 5.0. We are using new scanning APIs
03-07 10:46:31.951 (...) I/BeaconService: beaconService version 2.13.1 is starting up on the main process
03-07 10:46:31.954 (...) W/ModelSpecificDistanceCalculator: App has no android.permission.INTERNET permission. Cannot check for distance model updates
03-07 10:46:31.958 (...) W/ModelSpecificDistanceCalculator: Cannot find match for this device. Using default
03-07 10:46:31.959 (...) W/ModelSpecificDistanceCalculator: Cannot find match for this device. Using default
03-07 10:46:31.960 (...) I/BeaconService: binding
03-07 10:46:31.979 (...) I/selfBeaconService: onBeaconServiceConnect
03-07 10:46:31.980 (...) I/BeaconService: start ranging received
03-07 10:46:32.168 (...) I/art: Do partial code cache collection, code=10KB, data=24KB
03-07 10:46:32.169 (...) I/art: After code cache collection, code=10KB, data=24KB
03-07 10:46:32.169 (...) I/art: Increasing code cache capacity to 128KB
03-07 10:46:33.065 (...) I/ScanHelper: Non-distinct packets detected in a single scan. Restarting scans unecessary.
03-07 10:46:33.106 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 3.4581850922412523 meters away.
03-07 10:46:36.726 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 3.4581850922412523 meters away.
03-07 10:46:40.726 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 3.6056118574381157 meters away.
03-07 10:46:41.285 (...) I/art: Debugger is no longer active
03-07 10:46:41.285 (...) I/art: Starting a blocking GC Instrumentation
03-07 10:46:42.392 (...) I/CycledLeScanner: This is Android 5.0. We are using new scanning APIs
03-07 10:46:42.401 (...) I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-07 10:46:42.401 (...) I/ScanJob: Running immediate scan job: instance is org.altbeacon.beacon.service.ScanJob@398f59c
03-07 10:46:42.402 (...) I/ScanJob: scanJob version 2.13.1 is starting up on the main process
03-07 10:46:42.402 (...) W/ModelSpecificDistanceCalculator: App has no android.permission.INTERNET permission. Cannot check for distance model updates
03-07 10:46:42.404 (...) W/ModelSpecificDistanceCalculator: Cannot find match for this device. Using default
03-07 10:46:42.404 (...) W/ModelSpecificDistanceCalculator: Cannot find match for this device. Using default
03-07 10:46:42.409 (...) I/ScanJob: Scan job running for 300000 millis
03-07 10:46:43.528 (...) I/ScanHelper: Non-distinct packets detected in a single scan. Restarting scans unecessary.
03-07 10:46:44.434 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 2.92768959654115 meters away.
03-07 10:46:44.727 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 3.4996718323492226 meters away.
03-07 10:46:46.451 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 3.181513412080198 meters away.
03-07 10:46:48.737 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 4.135957968665044 meters away.
03-07 10:46:48.743 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 3.4581850922412523 meters away.
03-07 10:46:52.732 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 7.408179588037119 meters away.
03-07 10:46:52.735 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 11.306160864298167 meters away.
03-07 10:46:56.736 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 5.632619096507348 meters away.
03-07 10:46:56.740 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 9.280517536034091 meters away.
03-07 10:47:00.728 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 9.280517536034091 meters away.
03-07 10:47:00.733 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 9.280517536034091 meters away.
03-07 10:47:04.729 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 8.917299181373306 meters away.
03-07 10:47:04.733 (...) I/selfBeaconService: didRangeBeaconsInRegion, the closest beacon is about 8.917299181373306 meters away.
如您所见,从 10:46:33
到 10:46:40
,它以 4 秒的间隔正常工作。之后,突然一堆日志消息踢了进来,看起来像Android Beacon Library 的东西(比如ScanJob
)被重启了。
从那时起,来自 10:46:44
的回调每 4 秒触发两次。
所以问题是...发生了什么,或者为什么?
快速上下文摘要:在这个项目中,作者试图通过在检测到信标时启动前台服务进行测距来优化 Android 8 上的不间断测距。当未检测到信标时,将在 Android 8.
上使用默认作业调度程序扫描根据日志,似乎是在前台服务开始持续测距后截取的摘录。然后作业调度程序启动并在服务已经扫描的同时启动 ScanJob。这可能会产生双重回调,因为两个不同的线程同时进行循环扫描并将测距结果报告给同一个委托。因此,应用程序获得双重回调。
这里的根本问题是,如果计划的作业用于扫描,调用 beaconManager.unbind(...)
不会取消之前计划的作业。该库应该这样做,但从 2.31.1 版开始它不会这样做。这将需要与库一起解决一个新问题。我记录了一个问题 here
在此期间,可以手动取消作业。 (注意:这使用私有库 API,因此可能会在未来的版本中中断。最好暂时使用此类代码,直到库中的问题得到修复。)
有了上述警告,您应该可以在解除绑定后手动取消作业:
JobScheduler jobScheduler = (JobScheduler)
context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
jobScheduler.cancel(ScanJob.getImmediateScanJobId(context));
jobScheduler.cancel(ScanJob.getPeriodicScanJobId(context));