为什么Android Beacon Library didRangeBeaconsInRegion有时会被触发两次?

时间:2018-03-07 03:29:06

标签: android service ibeacon ibeacon-android

这是关于Android Beacon Library的问题。

设置

该应用使用BootstrapNotifier连续进行背景扫描 一旦检测到匹配的信标(即,didEnterRegion被触发),则 此背景扫描将被禁用,并启动一个开始范围信标的前台服务。

(您可以参考this post获取更多背景信息)

您也可以参考this repository进行此原子测试项目。

BeaconApplication负责连续背景扫描 SelfBeaconService负责主动前景测距。

(如果您手边有一个信标并且想要构建和测试,则需要将标识符过滤器更改为null或您自己的标识符。)

问题

有时(我说有时因为我不知道如何100%重现),在SelfBeaconServicedidRangeBeaconsInRegion同时触发了两次,但我只配置了一个区域。

以下是记录双重触发的日志 请注意,我已将扫描间隔设置为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:3310:46:40可以看到,它以4秒的间隔正常工作。在那之后,突然出现了一堆日志消息,并且看起来像Android Beacon Library的某些内容(例如ScanJob)重新启动。
从那时起,10:46:44的回调每4秒触发一次。

所以问题是......发生了什么,或者为什么?

1 个答案:

答案 0 :(得分:1)

快速上下文摘要:此项目是作者通过启动前台服务在检测到信标时进行测距来寻求优化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));