I am building an Android app that needs to be in constant Bluetooth (Classic) communication with a peripheral device. As long as the app is running in the foreground, everything is fine. But when the app is put in the background, the system kills the app after a indeterminate amount of time. The app can hang in there anywhere from 2 - 15 minutes. This happens even when the app doesn't have any battery optimisation. I hoped by turning the Battery Optimization OFF, it would prevent Android from putting the app into Standby Mode/Killing it.

If the app cannot run forever when the device is running on Battery, how long can one expect the App to exist in the background?

I wish the duration of the app's existance in the background was determinable! I appreciate any help on the matter.

Tested on Motorola G5S Plus running Android 8.1.0

The manifest file has background_running turned ON: <meta-data android:name="android.app.background_running" android:value="true"/>

App's Target SDK is API 29: Android 10

1

There are 1 answers

0
Roshan Ghadiyali On

You need to implement background service with WakeLock/Foreground Service, In service you need to write all bluetooth code that you want to run in background, Code that you wrote in activity/fragment place in service, for bluetooth callbacks need to implement Broadcast Receiver.