Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Background operation of BLE Library with Android 8 - Howto?

I have extended the Template app in nRF Toolbox. When my app runs in the background on an Android 8 (only) device and I try to connect to the peripheral, I get this error when trying to execute startService(service) in BleProfileServiceReadyActivity:

java.lang.IllegalStateException: Not allowed to start service Intent { ... }: app is in background
This is expected as pointed out here and elsewhere:  https://stackoverflow.com/questions/46445265/android-8-0-java-lang-illegalstateexception-not-allowed-to-start-service-inten

What is Nordic's advice:

Q1 On this topic in general?
Q2 How should the Template app be changed to support BLE Library operation in the background?
Q3 Is there a sample Android app (all versions) that will scan for BLE advertisments at all times (including when the app is in the background and/or the phone has been reset)
and then spring into life, connect to the peripheral and interact with it?

Q3 is what I am really trying to do. The Android Beacon Library addresses the problem, but is specific to beacons. IMHO this is a sufficiently important use case that it deserves
a robust example from Nordic.

Thanks
  • Hi, I remember answering to this question long time ago, but the answer must have get lost somewhere... Let's try again.

    Since Android 8 there are restrictions in background services. A background service will be killed 5 min after the application was closed (went to background). Also, they may not be started using startService() when app is not in foreground. More here: https://developer.android.com/about/versions/oreo/background

    The Template app keeps the service running only when the device is connected. I'd change the service to be a foreground service.

    If you need to scan for a device in foreground, use new method in BluetoothLeScanner with IntentFilter instead of using service at all.

    We don't have any example like you described, unfortunately. I agree that it is a good idea.

  • I would like to ask about this again...

    To set the context: I am working on a digital health sensor with a BLE interface. The user operates the sensor which collects data and saves it. It then starts advertising and will transfer the data when the sensor and the smart phone are next in radio range. The phone then transfers the reading to the cloud. The user does not need to have his phone when he operates the medical device, and he does not need to fire up an app manually in order to send the data to the cloud.

    I have an experimental Android app that combines Nordic's Android BLE Library and the Android Beacon Library. I have hacked the Android Beacon Library to listen for my custom 128-bit UUID. This works in Android background mode. When the app detects my BLE custom UUID it fires an event and then I switch over to Nordic's BLE Library to connect to my device and transfer data. (This is obviously out of scope for the Andorid Beacon Library).

    Clearly my specific use case can can be generalised: it should be possible for any BLE sensor (not just beacons) to send data to a phone that the user sees as "off".

    Most of the time this works very well. But (a) my hack of the Android Beacon Library is pretty ugly, and (b) sometimes my app does not hear my device advertising and I must reset the phone. This is probably because of (a). Also, (c) the Android Beacon Library contains much more code than my app requires.

    I would much prefer to work with one library and not two. What chance is the that I can get Nordic to work with me on this? The spec is to create an example Android app that listens for a BLE device in background mode, connects to it, transfers data and disconnects. I would work with you on this.

  • Hi Acutetech,

    Please make a new case and then refer to this case since this case is over 7 months old.

    Best Regards,

    Martin L. 

Related