Android Wear: CapabilityApi times out, doesn't return capabilities

无人久伴 提交于 2019-12-13 05:55:59

问题


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();

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.

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?

EDIT 1:

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;
}

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?


回答1:


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.



来源:https://stackoverflow.com/questions/31328971/android-wear-capabilityapi-times-out-doesnt-return-capabilities

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