3

I'm trying to implement CompanionDeviceService in order to interact with our BLE device. According to the documentation

System will keep this service bound whenever an associated device is nearby, ensuring app stays alive

But that's not what I'm seeing

17:47:48.563 MyCompanionDeviceService: onDeviceAppeared FF:FF:6D:10:F1:16
17:47:48.565 MyCompanionDeviceService: onUnbind
17:47:48.568 MyCompanionDeviceService: onDestroy

Around 1 minute later, onDeviceAppeared is invoked again, with the same result.

FF:FF:6D:10:F1:16 is not bonded. createBond is never invoked on BleDevice. I haven't found whether this is relevant or not.

I'm running on a Pixel 4a on latest available Android 12 version

Edit: Adding more code for reference

Manifest

<uses-permission android:name="android.permission.REQUEST_COMPANION_RUN_IN_BACKGROUND" />
<uses-permission android:name="android.permission.REQUEST_COMPANION_USE_DATA_IN_BACKGROUND" />
<uses-permission android:name="android.permission.REQUEST_OBSERVE_COMPANION_DEVICE_PRESENCE" />


<service
  android:name="com.mycompany.MyCompanionDeviceService"
  android:exported="true"
 android:permission="android.permission.BIND_COMPANION_DEVICE_SERVICE">
      <intent-filter>
        <action android:name="android.companion.CompanionDeviceService" />
      </intent-filter>
</service>

The startObservingDevicePresence succeeds, otherwise my service wouldn't be called at all

And there's nothing relevant on the service

@RequiresApi(Build.VERSION_CODES.S)
internal class MyCompanionDeviceService : CompanionDeviceService() {   

    override fun onCreate() {
        appComponent.inject(this)
        super.onCreate()
    }

    override fun onUnbind(intent: Intent?): Boolean {
        Timber.d("onUnbind")
        return super.onUnbind(intent)
    }

    override fun onDeviceAppeared(address: String) {
        Timber.d("onDeviceAppeared $address")
    }    

    override fun onDeviceDisappeared(address: String) {
        Timber.tag("companionservice").d("onDeviceDisappeared $address")
    }

    override fun onDestroy() {
        super.onDestroy()

        Timber.d("onDestroy")
    }
}
7
  • Maybe you could post your code?
    – Emil
    Nov 23, 2021 at 17:07
  • Thanks for taking a look, I added some code
    – Maragues
    Nov 24, 2021 at 6:41
  • Is it possible that something else is causing your app to exit (e.g. a crash?) That would cause the behavior you describe. Mar 23, 2022 at 18:24
  • I don't see anything on the logs. I ended up launching a foreground service, which sucks because I believe it can be killed at any instant. In any case, for now it seems to work fine, tho we haven't launched the feature on the play store yet.
    – Maragues
    Mar 24, 2022 at 15:58
  • 1
    As I understand the documentation, the System should bound to our service, which should keep it alive. Then, we manually kill the service whenever we are no longer interested. I'm glad that you have the same issue, I no longer feel alone :-D Can you please star this issue issuetracker.google.com/issues/207485313 ?
    – Maragues
    Mar 28, 2022 at 7:36

2 Answers 2

0

MyCompanionDeviceService: onUnbind means the system unbinds your MyCompanionDeviceService. unbind here is not related to your device binding.

0

It's intended behavior for MyCompanionDeviceService to spin up call onDeviceAppeared and then be destroyed.

What you want to do is start something else that will run for longer in order to do whatever operation you are trying to do. For example you could open your main activity or start a foreground service. Then that would run the code you want to run.

2
  • That's not what the documentation says developer.android.com/reference/android/companion/… The system binds to the service and increases the priority of the process, which should prevent the service from being killed. But we all know how unreliable background services are on many android flavors from vendors. In any case, I haven't tested this in a very long time, so I can't say what's the current state.
    – Maragues
    Aug 17, 2023 at 8:47
  • 1
    @Maragues If you prove me wrong I will be very happy but I spent a couple of days testing it and that's how it seems to work. Aug 23, 2023 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.