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")
    }
}
Maragues
  • 37,861
  • 14
  • 95
  • 96
  • Maybe you could post your code? – Emil Nov 23 '21 at 17:07
  • Thanks for taking a look, I added some code – Maragues Nov 24 '21 at 06:41
  • Is it possible that something else is causing your app to exit (e.g. a crash?) That would cause the behavior you describe. – davidgyoung Mar 23 '22 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 '22 at 15:58
  • @Maragues Did you ever resolve this issue? I am having the same concerns, but I think it is being destroyed because there are no ongoing tasks / references to the service. I'm not exactly certain, but in other words, bound services are destoryed as soon as there is nothing else depending on the service, so it is dropped immediately. If there is some component that is dependent - or some ongoing async operations tied to the service, then it will continue? I am exploring this myself still. – AlwaysTalkingAboutMyDog Mar 27 '22 at 06:26
  • 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 https://issuetracker.google.com/issues/207485313 ? – Maragues Mar 28 '22 at 07:36
  • @Maragues Done. I'm probably going to try pulling android's source if possible and scour how REQUEST_COMPANION_RUN_IN_BACKGROUND is used, if anywhere, and if we're missing anything. Keep us updated if you make any progress. Unfortunately this functionality is pretty new, so there are roughly 0 working examples available. – AlwaysTalkingAboutMyDog Apr 09 '22 at 07:13

2 Answers2

0

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

samygj
  • 1
  • 2
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.

Keith Loughnane
  • 475
  • 1
  • 5
  • 21
  • That's not what the documentation says https://developer.android.com/reference/android/companion/CompanionDeviceService 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 '23 at 08: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. – Keith Loughnane Aug 23 '23 at 12:50