创建新的 GoogleApiClient 时状态代码 16

Status Code 16 when creating new GoogleApiClient

我正在尝试创建一个基本的可穿戴应用程序,它接受来自 android 主应用程序的新位图。不幸的是,当我尝试 .build() GoogleApiClient 时,我调用了连接失败的侦听器,错误代码为 16.

这是从 ConnectionFailedListener 收到的确切错误

03-21 13:36:35.903 3089-3089/com.example.android.wearable.watchface D/Watch Face Config﹕ onConnectionFailed: ConnectionResult{statusCode=unknown status code 16, resolution=null}


应用代码:

        // create the api client to allow sending information to the wearable
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle connectionHint) {
                    Log.d(TAG, "onConnected: " + connectionHint);
                    // Now you can use the Data Layer API
                }
                @Override
                public void onConnectionSuspended(int cause) {
                    Log.d(TAG, "onConnectionSuspended: " + cause);
                }
            })
            .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult result) {
                    Log.d(TAG, "onConnectionFailed: " + result);
                }
            })
                    // Request access only to the Wearable API
            .addApi(Wearable.API)
            .build();

在可穿戴应用程序中,我创建了一个 DataListenerService,但目前这并不重要,因为 API 未正确构建。

感谢您的帮助或意见!

更新 - 我将模拟器设备目标从 Google APIs 更改为 Android 5.0.1 错误变为:

03-21 14:18:24.620 3025-3025/com.example.android.wearable.watchface D/Watch Face Config﹕ onConnectionFailed: ConnectionResult{statusCode=SERVICE_MISSING, resolution=null}

更新 2 - 找到 this 文章并按照说明进行操作。然后我的应用程序无法编译。之后我根据标记的答案建议去了 SDK 管理器并检查了 Google 播放服务(它们没有安装)。现在应用程序构建但仍然给出上面的 SERVICE_MISSING 错误。

您需要在设备上安装 Google Play 服务和 Android Wear app 才能使用可穿戴设备 API。默认情况下,模拟器没有它,所以要么手动安装它,要么使用已经搭载它的设备。

如果您使用的是 Genymotion,只需 download Google Play Services 并拖放到模拟器上即可。然后使用您的 Google 帐户登录并使用 Play 商店下载 Android Wear 应用程序。

更多信息here and here

如果您没有使用 Genymotion,您应该现在就开始使用它,因为它比标准 AVD 快得多。

请记住,虽然这会让您的 GoogleApiClient 成功连接,但如果您想制作一个真正的使用可穿戴设备 API。所以请参考其他关于如何创建或模拟蓝牙连接的问题,for example this one.

编辑:官方 Android 模拟器现在速度更快并且开箱即用地安装了 Google Play 服务。

查看这个 SO 问题:

我遇到了类似的问题。错误代码告诉您未安装 Android wear 应用程序。在您的 onConnectionFailedListener 中,您需要添加如下内容:

// Connection failed listener method for a client that only
// requests access to the Wearable API
@Override
public void onConnectionFailed(ConnectionResult result) {
    if (result.getErrorCode() == ConnectionResult.API_UNAVAILABLE) {
    // The Android Wear app is not installed
    }
    ...
}

从这里开始:

https://developer.android.com/google/auth/api-client.html#Starting

"Access the Wearable API"