onCapabilityChanged is not fired on both handheld and wearable while disabling/enabling connections

*爱你&永不变心* 提交于 2019-12-25 10:03:09

问题


<service android:name=".SMessage">
        <intent-filter>
            <action android:name="com.google.android.gms.wearable.CAPABILITY_CHANGED" />
            <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
            <data android:host="*" android:pathPrefix="/send-message" android:scheme="wear" />
        </intent-filter>
    </service>


public class SMessage extends WearableListenerService {

@Override
        public void onCapabilityChanged(CapabilityInfo capabilityInfo) {
        super.onCapabilityChanged(capabilityInfo);
        _A.toast("onCapabilityChanged");
        Set<Node> connectedNodes = capabilityInfo.getNodes();
        pickBestNodeId(connectedNodes);
    }
}

this method is not fired onCapabilityChanged if I enable aircraft mode or disable bluetooth connection.

What have I missed? _A.toast shows toast using Application class.

Sending messages works.!


回答1:


You need to include an android:path element that identifies the (fully-qualified) capability you're listening for:

<intent-filter>
    <action android:name="com.google.android.gms.wearable.CAPABILITY_CHANGED" />
    <data android:host="*" android:scheme="wear" 
          android:path="/com.mypackagename.my_capability_name"/> 
</intent-filter>

where my_capability_name is defined in the wear.xml of your app running on the other device:

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

as documented at https://developer.android.com/training/wearables/data-layer/messages.html#AdvertiseCapabilities.

A couple of caveats, however:

  • Because your app will run every time the devices dis/connect, you could see significant battery drain as a result.
  • As with all other manifest-declared broadcast receivers, this will stop working in Android O: https://developer.android.com/preview/features/background.html#broadcasts


来源:https://stackoverflow.com/questions/45001358/oncapabilitychanged-is-not-fired-on-both-handheld-and-wearable-while-disabling-e

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!