5

I'm a beginner in app development and would appreciate any help!

I have an app that connects to a BLE device and is connected to a foreground service which collects the data given by the BLE Device. The foreground service is bound to the activity.

The problem is when a user minimizes the app/press home and locks the phone, then after a while(5-10 minutes), the service always pauses or sleeps/doze? (maybe the whole app?).

From Android Profiler: CPU was at a consistent ~20-30%, Memory at 160MB and then drops to 0 after pause/sleep.

I have logs on BluetoothGattCallback.onCharacteristicChanged which is called everytime the BLE device sends data to check if the service is still active. This logging halts after a couple of minutes the app is in background. When I resume the app, the data that would have had been sent during the "idle time" comes flooding all at the same time (also missing some I think). This is a problem since I need to record the time when the data arrives and the flow of data should not be interrupted. (Called a handler.postDelayed method that runs in a separate thread that counts indefinitely to check if Service is being stopped or App istelf. This method also stops logging.)

I have tried below solutions but none of it worked:

  1. PARTIAL_WAKE_LOCK WakeLock.acquire() in both Activity or Application. (No call of release for testing). Noticed that when logging stops, WakeLock is being removed at maybe the same time(?). Verified in adb using command "adb shell dumpsys power | findStr -i wake"
  2. REQUEST_IGNORE_BATTERY_OPTIMIZATIONS in Manifest. (for testing)
  3. startService() and bindService(..., BIND_AUTO_CREATE | BIND_IMPORTANT ) and calling startForeground().
  4. getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); on Activity.

Tested using a Vivo 1811 and a Huawei phone only.

I'm completely lost in this, any help is greatly appreciated! Thank you very much!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
gmatcat
  • 155
  • 7
  • 1
    https://developer.android.com/reference/android/provider/Settings.html#ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS did you send this intent to really put the app on the whitelist? – Emil Nov 29 '19 at 08:52
  • Hi Emil, I didn't. I had assumed that adding REQUEST_IGNORE_BATTERY_OPTIMIZATIONS in Manifest automatically granted that permission without user interaction. I started the intent then manually changed battery saving option on my phone and I no longer encountered it. It works as expected now. Thanks so much! – gmatcat Nov 29 '19 at 13:37

0 Answers0