temp_
temp_

Reputation: 1317

Android 8.0 background services for bluetooth device

I am building a bluetooth app that tracks temperatures in the background assuming the app has not been destroyed. I noticed that in Android Oreo we have background execution limits. I need to know if I am missing anything from my project. Here is how I have the project laid out.

I have a MainActivity that has a bluetooth class BluetoothObject that takes care of communication between the main activity, BluetoothGattCallback() class, and my local Realm database.

I have now implemented a Service() in BluetoothObject, that is a bridge between the BluetoothObject and my Realm database. So far everything seems to work. I used this example to set everything up.

Since this is a bound service, do I need to call anything else to keep the service alive in the background? After I set it up, I bind the service by calling:

    val intent = Intent(context, BoundService::class.java)
    context.bindService(intent, myConnection, Context.BIND_AUTO_CREATE)

I also have the service set in my manifest file as instructed by the example.

The issue I am seeing is the bluetooth connection stays connected while the app is open and if the screen is asleep. I have issues is when my app goes to the back of the app stack and then the phone is asleep. The connection maybe last an hour. What can I do to keep this connection alive? Is there any permissions I need to enable? Is there something in Android 8+ that causes issues?

Upvotes: 1

Views: 840

Answers (1)

Pawel
Pawel

Reputation: 17248

There is no issue. That limitation was put specifically to kill background services that could be heavy on battery (ongoing sockets, blue tooth connections, etc.) and not directly visible to the user.

If you want your service to run unstopped, You will need to settle for Foreground service with a notification.

Upvotes: 1

Related Questions