当前位置: 首页 > 知识库问答 >
问题:

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

皮自明
2023-03-14

这是一个关于Android Beacon库的问题。

应用程序使用BootstrapNotifier连续进行后台扫描
一旦检测到匹配的信标(即触发didEnterRegion),此后台扫描将被禁用,并启动开始对信标进行测距的前台服务。

(你可以参考这篇文章了解更多背景)

对于这个原子测试项目,您也可以参考这个存储库。

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

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

有时(我说有时是因为我不知道如何100%复制),在selfbeacervice中,didrangebeaconregion会同时触发两次,但我只配置了一个区域。

下面是记录双触发器的日志
请注意,我已将扫描间隔设置为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个答案

汪晨
2023-03-14

快速上下文总结:本项目是一个案例,作者试图通过在检测到信标时启动前台服务来优化Android 8上的不间断测距。当未检测到信标时,将在Android 8上使用默认的作业调度程序扫描。

根据日志,似乎是在前台服务启动常量范围后进行的摘录。然后,作业调度器启动,并在服务已经扫描的同时启动了一个ScanJob。这可能会产生双重回调,因为两个不同的线程同时执行循环扫描,并向同一委托报告范围结果。因此,该应用程序会得到两次回调。

这里的根本问题是,如果计划的作业用于扫描,则调用beaconManager。解除绑定(…) 不会取消计划以前计划的作业。库应该这样做,但从版本2.31起它就不这样做了。1.这将是图书馆需要解决的一个新问题。我在这里记录了一个问题

在此期间,可以手动取消作业。(注意:这使用私有库API,因此可能会在将来的版本中中断。在库中修复问题之前,最好暂时使用此类代码。)

有了上述警告,您应该能够在使用以下命令解除绑定后手动取消作业:

JobScheduler jobScheduler = (JobScheduler) 
  context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
jobScheduler.cancel(ScanJob.getImmediateScanJobId(context));
jobScheduler.cancel(ScanJob.getPeriodicScanJobId(context));

 类似资料:
  • 本文向大家介绍render在什么时候会被触发?相关面试题,主要包含被问及render在什么时候会被触发?时的应答技巧和注意事项,需要的朋友参考一下 在 React 中,只要执行了 setState 方法,就一定会触发 render 函数执行; 组件的 props 改变了,不一定触发 render 函数的执行,除非 props 的值来自于父组件或者祖先组件的 state

  • 我是前端的新手,我正在尝试实现一个简单的功能:检索城市列表,为每个城市渲染一个按钮,并单独捕获它们的点击动作 我得到的列表如下: 并像这样渲染按钮 但是,这些按钮在呈现时将被单击两次,并且如果我之后单击它们,则不会记录任何内容

  • 我正在使用应用服务计划进行 azure 函数,并添加了 blob 触发器,但是当任何文件上传到 blob 容器时,函数没有触发.或者有时它花了太多时间,然后在它开始触发之后。任何建议将不胜感激 它应该在新文件上传到 Blob 容器时触发该函数。

  • 问题内容: window.onload = function(){ 这是我的代码,当我单击“文本”时,它将向您提示两次,但是当我单击该框时,该元素将仅触发一次,为什么? 问题答案: 当您单击标签时,它会触发单击处理程序,并且您会收到警报。 但是单击标签还会自动将click事件发送到关联的输入元素,因此将其视为对复选框的单击。然后事件冒泡会导致在包含元素(即标签)上触发click事件,因此您的处理程

  • 在JavaScript中,我按住两个键,并且被完美触发。当我释放其中一个键时,被触发。到目前为止一切都很好。但是我仍然按住一个键,那么为什么没有被触发呢?我需要在我的游戏中发生这种情况。我做错了什么吗?这是预期的反应吗?有什么解决办法吗?

  • 我正在学习Java,正在使用java 8,spring 5.3.9和Apache Tomcat 9。我已经将我的jar文件添加到我的构建路径中的类路径中,将Apache Tomcat添加到我的服务器中,我的项目运行得非常好。现在我开始使用beans和xml文件,我遇到了一个问题。我的代码的一部分被触发,另一部分被忽略。 我有以下界面 FortuneService.java: 和一个快乐财富服务类: