Danny Milman
Danny Milman

Reputation: 21

Receive notifications from companion device while in the background

We have a medical watch which continuously produces measurements. The measurements need to be analyzed offline in the cloud, and are uploaded from an application which communicates with the watch using BLE. What we do today is we run a service as a short foreground job every few minutes, extract as much data from the watch and then go back to sleep.

We’d like to speed up the process, and also receive alerts from the watch in real time. Is using Android’s CompanionDeviceManager the solution to this? Will it allow the application to receive notification while it’s in the background?

Upvotes: 0

Views: 752

Answers (1)

Emil
Emil

Reputation: 18497

CompanionDeviceManager does not directly affect GATT notifications in any way.

Whenever any device has been paired through the CompanionDeviceManager by the app however, the app gains these permissions:

  1. The app can start activities, turning on the screen, while the screen is turned off. See https://developer.android.com/guide/components/activities/background-starts. This was always allowed up to Android 9, but for Android 10 and later this is now restricted and having a paired device this way is one way to lift this restriction.

  2. https://developer.android.com/reference/android/Manifest.permission#REQUEST_COMPANION_USE_DATA_IN_BACKGROUND. I guess this relates to the use of mobile data traffic in the background.

  3. https://developer.android.com/reference/android/Manifest.permission#REQUEST_COMPANION_RUN_IN_BACKGROUND. It's a bit unclear to me exactly what this permission does, but at least this can help you start a foreground service while in background.

The CompanionDeviceManager doesn't really affect the Bluetooth API or Bluetooth communication in any way. The important thing in order to have working GATT notifications is to not get the app process killed, which can example be done by making sure a Foreground Service is running in the app process.

Upvotes: 3

Related Questions