OnCapabilityChanged working on phone but not on wearable
Asked Answered
G

1

8

After carefully following the instructions in this post:

How to detect when android wear device gets disconnected? I finally managed to get my phone to detect when the wearable is connected using onCapabilityChanged.

I have followed the exact same steps on the wearable side and I am trying to detect when the phone is connected to the wearable but onCapabilityChanged is never triggered.

wear.xml (on the phone)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="android_wear_capabilities">
        <item>track_phone</item>
    </string-array>
</resources>

In AndroidManifest (wearable)

<service android:name=".BackgroundService.WearableService"
            android:enabled="true"
            android:exported="true" >
            <intent-filter>
                <action android:name="com.google.android.gms.wearable.CAPABILITY_CHANGED" />
                <data android:scheme="wear" android:host="*"/>
            </intent-filter>
        </service>

In WearableService (on wearable)

override fun onCapabilityChanged(p0: CapabilityInfo?) {
    super.onCapabilityChanged(p0)
    Log.i("WearableService", "Capability changed: {${p0?.nodes?.size ?: "null"}")
}

I am sure the service is started as I have another process running from it

I am doing the same thing on the phone side. When I dissconnect/connect the bluetooth onCapabilityChanged is correctly called in the phone's service, but not on the wearable's service.

Any ideas?

Gad answered 18/1, 2019 at 14:50 Comment(6)
What does your code look like that doesn't work? Please provide a Minimal, Complete, and Verifiable example that demonstrates the issue.Futch
You mention that you have verified that your service is started. Have you also verified that your service is still running when it's supposed to receive the onCapabilityChanged call?Nary
@Nary Thanks, yes I haveGad
@Futch Ok, will doGad
@Futch I have done as you suggested, and in that simple project it is working.. So somewhere something is conflicting or not working in my main project.. Struggling to find out what..Gad
Same issue. It doesn't help that the docs for Wear is frustratingly incomplete. For example, I'm wondering if <data android:scheme="wear" android:host="*"/> is needed for capability changes to be detected, or if there is a path prefix, or what - zero documentation on that.Groupie
G
1

After trying everything to get it to work, I opted to use a standard Service instead of a WearableListenerService.

I implemented CapabilityClient.OnCapabilityChangedListener on the Service:

override fun onCapabilityChanged(p0: CapabilityInfo) {
        Log.i("WearService", "Wearable Capability changed: {${p0?.nodes?.size ?: "null"}")
    }

And registered the listener:

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Wearable.getCapabilityClient(this).addListener(this, CAPABILITY)
        return super.onStartCommand(intent, flags, startId)
}
Gad answered 15/2, 2019 at 7:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.