在Android蓝牙低功耗创建后台服务蓝牙、后台、低功耗、Android

2023-09-05 05:32:30 作者:轻念っ

背景

我非常希望我的Andr​​oid设备将所有的时间用于扫描蓝牙低功耗设备的,只要具有特定属性的新设备出现启动一个应用程序的能力。

Ideally I would like my Android device to be scanning for Bluetooth Low Energy devices all the time an the ability to start an application whenever a new device with specific properties appears.

因此​​,在BLE广播包会如列举了一组由广播装置所提供的服务。那么应用程序将可以注册一个兴趣某些服务,并自动启动时,使用此服务的设备进入的范围。

So the broadcast packet in BLE will for instance enumerate a set of services provided by the broadcasting device. An app would then be able to register an interest for certain services and automatically be started when a device with this services comes into range.

据我了解,这是不如何在Android BLE API的工作?所以,我怎么能得到类似的东西?

From what I understand this is not how the Android BLE API works? So how can I get something similar?

最简单的例子

我有一个BLE传感器,记录环境温度随着时间的推移。每当我的Andr​​oid手机是足够接近我想要连接并下载所有的数据,将其发送到一些云存储解决方案。这个应用程序不需要任何GUI(至少不能完成配置后)。但究竟应该如何在后台运行,而不会耗尽电池,但还是给我的设备快速连接,一旦它在范围内的一个相当不错的机会吗?

I have a BLE sensor that logs ambient temperature over time. Whenever my Android phone is close enough I want to connect and download all the data, sending it to some cloud storage solution. This app would not need any GUI (at least not after configuration is done). But how should it run in the background without draining the battery, but still give me a fairly good chance of connecting the device quickly once it is in range?

问题

我是否需要设置一个计时器和唤醒应用程序每过一段时间,然后手动启动扫描?那时候我该选择什么样的时间间隔。我多久可以离开扫描仪的运行没有电池造成负面影响?

Do I need to set a timer and wake the app every once in a while and then manually start scanning? What kind of intervals should I then choose. How long can I leave the scanner running without adversely affecting the battery?

可能的解决方法

这是我想出来为止。

系统配置活动设置的时间间隔和设备扫描 在配置活动将设立类似的WakefulBroadcastReceiver Scheduler例如 当接收器得到的中的onReceive事件中,我开始BLE的扫描服务(我写)作为一个清醒的服务。 在扫描服务开始扫描(以注册的回调)。 在该服务可能会得到高级的报告,它可以在采取行动 在超时后,该服务将停止扫描仪和结束清醒的服务。 A configuration activity to set the intervals and devices to scan for The configuration activity will set up an WakefulBroadcastReceiver similar to the Scheduler example When the receiver get's the onReceive event I start a BLE scan service (that I've written) as a wakeful service. The scan service starts scanning (with a registered callback). The service might get adv reports that it can act upon After a timeout the service will stop the scanner and end the wakeful service.

这工作,但我不知道这是最好的办法。我也不知道我是多么小的时间间隔可以有,仍然避免破坏电池的使用寿命。我想是开始扫描每两分钟,扫描10-20秒。但我怕这将是相当频繁,以唤醒设备?

This works, but I'm not sure it's the best way. I also don't know how small intervals I can have and still avoid destroying the battery life. What I would want is to start scanning every two minutes, scanning for 10-20 seconds. But I'm afraid that would be rather frequently to wake up the device?

推荐答案

本功能已经全部被转移到了开源 Android的灯塔图书馆,以:

This functionality has all been moved to the open source Android Beacon Library which will:

唤醒/启动应用程序检测iBeacons匹配所需的图案时

wake up/launch your app when iBeacons matching a desired pattern are detected

在后台执行信标扫描,即使用户没有启动您的应用程序还没有

perform beacon scanning in the background even if the user has not launched your app yet

自动降低扫描速度的背景下为30秒,每五分钟,以节省电池。 (时序配置。)

reduce the scan rate automatically in the background to 30 seconds every five minutes to save battery. (Timing configurable.)

code例子在这里显示

Code examples are show here

如果您的BLE设备不是一盏明灯,你仍然可以使用这个库通过让您的传感器还发送信标则检测连接到主售后服务做到这一点。

If your BLE device is not a beacon you could still use this library to accomplish this by having your sensor also transmit as a beacon then after it is detected connect to the main service.