BLE扫描如何在Oreo上用于altbeacon库? [英] How does BLE scanning work for altbeacon library on Oreo?

查看:84
本文介绍了BLE扫描如何在Oreo上用于altbeacon库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Android L之前,Android Altbeacon库使用后台运行服务来扫描BLE信标.默认扫描时间是在前台循环中执行1.1秒,在后台每5分钟执行一次10秒.同样,对于后台任务,警报管理器也用于唤醒应用程序.

Before Android L Android Altbeacon library used background running services to scan BLE beacons. Default scan times are 1.1 seconds in a loop in the foreground and 10 seconds every 5 mins in the background. Also for background tasks alarm manager is used to wakeup the app.

鉴于不允许长期运行的后台服务,我一直在研究类似的设置在Android Oreo中的工作方式.我正在经历 http://www.davidgyoungtech.com/2017/08/07/beacon-detection-with-android-8 它确实清除了一些东西,但是我仍然不完全了解蓝牙扫描API和作业计划程序的组合如何进行扫描.

I was looking at how similar setup works in Android Oreo given that long-running background services are not allowed. I was going through http://www.davidgyoungtech.com/2017/08/07/beacon-detection-with-android-8 and it did clear something but I am still not completely clear on how scan works with the combination of bluetooth scanning API and job scheduler.

那么如何将低功耗扫描与基于作业调度程序的定期调度结合使用来监视和定位信标?如果有人可以提供一个例子,那就太好了.

So how is low power scan is used in combination of job scheduler based periodic scheduling to monitor and range beacons? If someone could provide an example that would be great.

更具体一点-

  1. 我们什么时候要通过常规背景扫描进行被动新信标检测?
  2. 为什么被动扫描仅适用于新检测?为什么我们不能循环运行呢?为什么只在作业计划程序扫描之间运行它?
  3. 让我们说我将扫描周期设置为10秒,并将扫描间隔设置为2分钟.在什么时间间隔使用什么技术扫描信标?
  4. 如果作业计划程序可以安排频率超过15分钟的作业,并且可以最多运行10分钟,那么我们可以将扫描间隔设置为10分钟,将扫描间隔设置为5分钟吗?由于操作系统的限制,我们将仅损失5分钟的扫描时间?另外,我们可以在这5分钟内进行被动扫描吗?所以我们没有任何时间不扫描信标吗?

推荐答案

了解低功率扫描如何在低水平下工作至关重要:

It is critical to understand how low power scans work at a low level:

低功耗扫描取决于将匹配的蓝牙LE模式卸载到蓝牙芯片上的硬件过滤器.如果它与传入的数据包匹配,则芯片会通知操作系统,操作系统会将意图发送给应用程序,唤醒应用程序处理该数据包.

A low power scan relies on offloading the Bluetooth LE pattern matching to a hardware filter on the Bluetooth chip. If it is matched by an incoming packet, the chip notifies the operating system, which sends an intent to the app that wakes it up to process the packet.

以上内容依赖于由意图(Android 8中的新API)提供的过滤扫描的组合.

The above relies on a combination on of filtered scans delivered by intents (a new API in Android 8).

当计划的扫描作业以Android Beacon库结束时,此基于意图的传递将设置为由操作系统和蓝牙芯片处理.最终结果是,如果检测到新的信标,广播的接收器将非常迅速地对其进行处理.这样可以在几秒钟内向应用程序发送监视回调,而无需等待15分钟才能完成下一个计划的作业.

When a scheduled scan job ends with the Android Beacon Library, this intent based delivery is set up to be handled by the operating system and Bluetooth chip. The end result is that if a new beacon is detected, it will be handled by a broadcasted receiver very quickly. This can send a monitoring callback to the app within a few seconds instead of waiting 15 minutes for the next scheduled job.

当过滤器与附近的蓝牙设备不匹配时,所有这些都可以使用.如果已经在附近,则Intent将在100毫秒左右发送,这是获取持续更新的CPU效率非常低的方式,这破坏了低功耗API的全部观点,并会导致该应用持续运行并符合通过Android 8终止.

This is all designed to work when the filter will not match a Bluetooth device that is already nearby. If one is already nearby, the Intent would be sent within 100ms or so, which is a very CPU inefficient way of getting constant updates, which defeats the whole point of low power APIs, and would cause the app to be running constantly and eligible for termination by Android 8.

使用的过滤器与任何信标匹配.这样做是为了节省筛选器,因为它们是有限的硬件资源.因此,如果已知已经存在信标,则不使用这些扫描API,因为它会立即匹配数据包并破坏该技术的目的.底线:如果扫描作业以可见的信标结束,则不执行低功耗扫描.

The filter used matches any beacon. This is done to conserve filters as they are a limited hardware resource. For this reason, these scanning APIs are not used if a beacon is known to be present already, because it would immediately match a packet and defeat the purpose of the technique. Bottom line: if a scan job ends with a beacon visible, the low power scanning is not done.

您不能将计划的作业设置为超过15分钟执行一次.这是操作系统的限制.在扫描周期之间间隔10分钟是可以的,但是如果周期结束,磁带库只会在最后发送扫描结果,因此这可能无济于事.可以将库修改为仅在每个扫描作业上最多扫描10分钟,但是当前它不执行此操作,而当您单击时,它会保留5分钟的时间.

You cannot set a scheduled job to execute any more often than 15 minutes. This is an operating system restriction. A 10 minute between scan period would work, but the library only delivers scan results at the end if the cycle, so this probably isn't not helpful. It would be possible to modify the library to simply scan for the max 10 minutes on each scan job, but currently it does not do this, and as you nite it would leave 5 minutes uncovered.

如果您真的想在Android 8+上在后台进行恒定BLE扫描,则OS允许的方法是使用前台服务,该服务显示图标,以便用户知道它正在运行.不过,请注意在消费类应用程序中执行此操作,因为这会消耗大量电量,并且用户可能会在将其归咎于电池耗尽后将其卸载.但是,对于特殊用例,这可能是可以接受的.

If you really want to do constant BLE scanning in the background on Android 8+, the OS-permitted way to do so is with a foreground service that displays an icon so the user knows it is running. Be careful about doing this in consumer apps, though, as battery drain will be very significant and users will probably uninstall the app after blaming it for dead batteries. For special use cases, however, this may be acceptable.

这篇关于BLE扫描如何在Oreo上用于altbeacon库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆