Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating background services for Bluetooth low energy on Android
    primarykey
    data
    text
    <p><strong>Background</strong>:</p> <p>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.</p> <p>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.</p> <p>From what I understand this is not how the Android BLE API works? So how can I get something similar?</p> <p><strong>Simplest possible example</strong>:</p> <p>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?</p> <p><strong>Question</strong>:</p> <p>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?</p> <p><strong>Possible solution</strong>:</p> <p>This is what I've come up with so far.</p> <ul> <li>A configuration activity to set the intervals and devices to scan for</li> <li>The configuration activity will set up an WakefulBroadcastReceiver similar to the <a href="https://android.googlesource.com/platform/developers/samples/android/+/d26c23e/background/alarms/Scheduler/" rel="noreferrer">Scheduler</a> example</li> <li>When the receiver get's the onReceive event I start a BLE scan service (that I've written) as a wakeful service.</li> <li>The scan service starts scanning (with a registered callback).</li> <li>The service might get adv reports that it can act upon</li> <li>After a timeout the service will stop the scanner and end the wakeful service.</li> </ul> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload