Android 磨损 - 意外错误代码 16

Android Wear - Unexpected error code 16

我有一个使用 Android Wear API 的应用程序。为了创建它,我遵循了这里的指南:

https://developer.android.com/training/building-wearables.html

我使用以下方式访问可穿戴设备 APIs:

new GoogleApiClient.Builder(context)
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .addApi(Wearable.API)
    .build();

在我的 onConnectionFailedListener 中,我收到错误代码 16,导致向用户弹出一个提示 "GooglePlayServicesUtil﹕ Unexpected error code 16"。

@Override
public void onConnectionFailed(ConnectionResult result) {
    if (mResolvingError) {
        // Already attempting to resolve an error.
        return;
    } else if (result.hasResolution()) {
        try {
            mResolvingError = true;
            result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR);
        } catch (SendIntentException e) {
            // There was an error with the resolution intent. Try again.
            mGoogleApiClient.connect();
        }
    } else {
        // Show dialog using GooglePlayServicesUtil.getErrorDialog()
        showErrorDialog(result.getErrorCode());
        mResolvingError = true;
    }
}

我无法在 SO 问题中找到为什么会发生这种情况的答案,因此我将添加自己的答案。

在设置我的 Android Wear 客户端时,我错过了一个特定于 wear 的部分。参见:

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

"Access the Wearable API"

// 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
    }
    ...
}

将此添加到我的 onConnectionFailedListener 中解决了我的问题。 Android Wear 应用程序未安装在这些设备上。