Can Android Companion Device Pairing wake up the Companion App from background upon Bluetooth Connection
Asked Answered
T

2

10

I want my app to be woken up when the device finds a specified bluetooth device. The problem is, that after android O you can't register a broadcast receiver anymore to handle that in the background. Having a constant foreground service is also no option for me. Android Auto does this behaviour already I am trying to implement myself.

I found this: https://developer.android.com/guide/topics/connectivity/companion-device-pairing It says

After the device is paired, the device can leverage the REQUEST_COMPANION_RUN_IN_BACKGROUND and REQUEST_COMPANION_USE_DATA_IN_BACKGROUND permissions to start the app from the background.

Sadly i cant seem to find how this is used and if this even provides what im looking for. I am happy for all suggestions and hints.

Tar answered 1/3, 2020 at 15:0 Comment(4)
have you found any solution to this?Vigesimal
As it turns out the Bluetooth connection broadcast is one of the few that will still trigger an app update when an intent receiver is registered in the manifest. So you're good to goTar
@Tar Did you end up making use of REQUEST_COMPANION_RUN_IN_BACKGROUND at all? I'm unable to keep a service alive in the background when a bonded BLE device is in range. :(Mani
No I did not, but I did not do any coding on this project for quite some time. I think I simply started a foreground service when the intent receiver fired.Tar
H
3

I recently got back from this rabbit hole. The documentation about waking up when detecting a device seems to be just one sentence at the end of the companion pairing documentation and the class description for CompanionDeviceService but from the description it seemed possible.

And after testing on different devices it seems to work this way. Yes you can set up a CompanionDeviceService and that has a onDeviceAppeared method that can call if your device is detected, even if the app is closed (but not killed or in Doze mode).

BUT, and here's the deal breaker. Android doesn't periodically scan or anything like that. It only calls if some other code starts a scan. This could be your app or some other 3rd party app but something needs to start it.

I figured this out after disabling my unrelated smartwatch app, my app stopped detecting my bluetooth device. So either your need to start the scan in which case CompanionDeviceService is pointless or you need to hope some other app will start a scan which will make you app unreliable.

Hagans answered 15/8, 2023 at 11:24 Comment(1)
Wondering if the behavior changed? They seem to be recommending Companion Device Pairing instead of continuous BLE scanning. I would be even keen on launching the foreground service from onDeviceAppeared - would that be possible?Facet
P
0

There's an option to associate a device using CompanionDeviceManager and register a listener CompanionDeviceService via https://developer.android.com/reference/android/companion/CompanionDeviceManager#startObservingDevicePresence(java.lang.String), then you will receive notifications from the system https://developer.android.com/reference/android/companion/CompanionDeviceService#onDeviceAppeared(android.companion.AssociationInfo) and execute your service in the background.

Platonic answered 13/11, 2022 at 0:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.