Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating background services for Bluetooth low energy on Android

Background:

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.

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.

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

Simplest possible example:

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?

Question:

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?

Possible solution:

This is what I've come up with so far.

  • 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.

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?

like image 590
Vegar Westerlund Avatar asked Nov 11 '13 19:11

Vegar Westerlund


People also ask

What is Bluetooth Low Energy in Android?

Android provides built-in platform support for Bluetooth Low Energy (BLE) in the central role and provides APIs that apps can use to discover devices, query for services, and transmit information. Common use cases include the following: Transferring small amounts of data between nearby devices.

What is Bluetooth Low Energy mode?

Bluetooth Low Energy is a wireless, low-power personal area network that operates in the 2.4 GHz ISM band. Its goal is to connect devices over a relatively short range. BLE was created with IoT applications in mind, which has particular implications for its design.

How do I turn off Bluetooth Low Energy Android?

Users can disable system-level Bluetooth background scanning by going to Settings > Security & Location > Location > Scanning and disabling the toggle for Bluetooth scanning. This does not affect BLE scanning for location or local devices.


1 Answers

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

  • 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

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

Code examples are show here

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.

like image 79
davidgyoung Avatar answered Oct 19 '22 13:10

davidgyoung