收听断开连接的节点

时间:2016-03-01 15:39:36

标签: android wear-os

在表盘配置活动中,我想警告用户手表在更改表盘配置时是否断开连接。

不推荐使用

Wearable.NodeApi.addListener,如何在活动中监听断开连接的节点?

使用WearableListenerService并不合适,因为我不想在活动未开启时倾听。

1 个答案:

答案 0 :(得分:2)

CapabilityApi应该有效。有两种方法可以根据功能获取连接节点列表,包括更改回调:

  1. 针对节点更改的回调:CapabilityApi.CapabilityListener
  2. 请求当前节点:Wearable.CapabilityApi.getCapability()
  3. 更多详情:

    以下是Google RuntimePermissionsWear示例的MainWearActivity.java文件的摘录。在 onConnected()方法中,它为节点中的更改设置侦听器并请求当前节点列表(涵盖两种方案)。

    请注意,此示例使用自定义功能(包括手机和佩戴),因此您可能会更改该部分。如果您使用自定义功能,则必须在 wear.xml 文件中声明。 (Here是电话中的样本,以防您好奇。)

        // Set up listeners for capability and message changes.
        Wearable.CapabilityApi.addCapabilityListener(
                mGoogleApiClient,
                this,
                Constants.CAPABILITY_PHONE_APP); // custom capability
        ...
    
        // Initial check of capabilities to find the phone.
        PendingResult<CapabilityApi.GetCapabilityResult> pendingResult =
                Wearable.CapabilityApi.getCapability(
                        mGoogleApiClient,
                        Constants.CAPABILITY_PHONE_APP, // custom capability
                        CapabilityApi.FILTER_REACHABLE);
    
        pendingResult.setResultCallback(new ResultCallback<CapabilityApi.GetCapabilityResult>() {
            @Override
            public void onResult(CapabilityApi.GetCapabilityResult getCapabilityResult) {
    
                if (getCapabilityResult.getStatus().isSuccess()) {
                    CapabilityInfo capabilityInfo = getCapabilityResult.getCapability();
                    // Realistically, there is only on phone node with this capability, but you should check for multiple nodes to be safe (if wearable, many more possibilities)
                    mPhoneNodeId = pickBestNodeId(capabilityInfo.getNodes());
    
                } else {
                    Log.d(TAG, "Failed CapabilityApi result: "
                            + getCapabilityResult.getStatus());
                }
            }
        });