7

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?

HPage
  • 1,412
  • 20
  • 27
  • What does your code look like that doesn't work? Please provide a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) that demonstrates the issue. – Mr.Rebot Jan 22 '19 at 05:36
  • 1
    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? – TofferJ Jan 22 '19 at 15:53
  • @TofferJ Thanks, yes I have – HPage Jan 22 '19 at 17:53
  • @Mr.Rebot Ok, will do – HPage Jan 22 '19 at 17:54
  • @Mr.Rebot 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.. – HPage Feb 13 '19 at 07:43
  • Same issue. It doesn't help that the docs for Wear is frustratingly incomplete. For example, I'm wondering if `` is needed for capability changes to be detected, or if there is a path prefix, or what - zero documentation on that. – James Allen Mar 04 '19 at 12:33

1 Answers1

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)
}
HPage
  • 1,412
  • 20
  • 27