Android Wear:CapabilityApi超时,不返回功能 [英] Android Wear: CapabilityApi times out, doesn't return capabilities

查看:423
本文介绍了Android Wear:CapabilityApi超时,不返回功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用播放服务7.3引入了一个新CapabilityApi了解我的Andr​​oid设备磨损(华硕ZenWatch可)的功能。我已经签出这个问题并可以确认Wearable.NodeApi.getConnectedNodes(...)方法并努力获得我连接的节点列表确实看到列表中的手表。这是code,我在一个应用程序在手机上运行的查询连接的设备磨损能力:

I'm trying to use a new CapabilityApi introduced in play services 7.3 to learn about the capabilities of my android wear device(Asus Zenwatch). I've checked out this question and can confirm that Wearable.NodeApi.getConnectedNodes(...) approach does work to get a list of connected nodes and I do see the watch in the list. This is the code I'm running in an app on my phone to query capabilities of connected wear devices:

GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(context)
                .addApi(Wearable.API)
                .build();

PendingResult<CapabilityApi.GetAllCapabilitiesResult> result = 
    Wearable.CapabilityApi.getAllCapabilities(mGoogleApiClient, CapabilityApi.FILTER_ALL);

Map<String, CapabilityInfo> capabilities = result.await().getAllCapabilities();

在这种情况下背景是一种活动。此调用是发生在非UI线程,因此调用的await()是安全的。我插在最后一行一个断点,但是当我打它,步过它,调试器不会返回,仿佛永远方法和运行永远不会返回。如果我更换的等待()等待(10000,TimeUnit.MILLISECONDS),然后它只是超时10秒后和功能分配空。该行为是与Android磨损配对的手机一样,或者不与它配对,还是一个模拟器。

context in this case is an activity. This call is happening on a non-ui thread, so calling await() is safe. I inserted a breakpoint on the last line, but when I hit it and step over it, the debugger never returns, as if the method runs forever and never returns. If I replace await() with await(10000, TimeUnit.MILLISECONDS), then it just times out after 10 seconds and capabilities is assigned null. The behavior is the same on a phone paired with android wear, or not paired with it, or on an emulator.

我缺少的东西吗?什么是使用CapabilityApi和获取可用功能的列表,没有它超时?正确的方法

Am I missing something? What's the correct way to use CapabilityApi and get a list of available capabilities and not have it time out?

编辑1:

随着@ianhanniballake的帮助下,我来到了这个code,不挂,但返回的结果非常迅速:

With help of @ianhanniballake I came up with this code that doesn't hang, but returns the result very quickly:

ConnectionResult connectionResult = mGoogleApiClient.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS);
if (connectionResult.isSuccess()) {
    PendingResult<CapabilityApi.GetAllCapabilitiesResult> result =
                    Wearable.CapabilityApi.getAllCapabilities(mGoogleApiClient, CapabilityApi.FILTER_ALL);
    mGoogleApiClient.disconnect();
    Map<String, CapabilityInfo> capabilities =
                    result.await().getAllCapabilities();
    return capabilities != null && !capabilities.isEmpty();
} else {
    mGoogleApiClient.disconnect();
    return false;
}

然而,当我是与Android智能手表配对的手机,能力上运行此code 最终被一个空列表,或者为null。这是预期的结果?我想我应该得到的我的智能手表的功能列表。或者,这是code应该是对手表本身?

However, when I run this code on a phone that is paired with Android smart watch, capabilities ends up being either an empty list, or null. Is this the expected result? I thought I was supposed to get a list of my smart watch's capabilities. Or is this code supposed to be ran on the watch itself?

推荐答案

您必须连接你的 GoogleApiClient 。请考虑使用<一个href=\"https://developers.google.com/android/reference/com/google/android/gms/common/api/GoogleApiClient#blockingConnect()\"相对=nofollow> blockingConnect()因为你是在后台线程,然后检查结果<一个href=\"https://developers.google.com/android/reference/com/google/android/gms/common/ConnectionResult\"相对=nofollow> ConnectionResult 以确保连接成功。

You have to connect your GoogleApiClient. Consider using blockingConnect() as you are on a background thread, then checking the resulting ConnectionResult to ensure the connection succeeded.

这篇关于Android Wear:CapabilityApi超时,不返回功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆