Android wear app can't connect to google api s

2020-03-31 08:44发布

问题:

I'm trying to set up a connection between Android Wear and an Android phone. The googleApiClient fails to connect, and returns a SERVICE_VERSION_UPDATE_REQUIRED status code. What am I doing wrong?

Here's my Activity's code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    int conResult = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
    if(conResult!=ConnectionResult.SUCCESS){
        Log.e(LOG_TAG,"Google play services are not available on this device");
    }

    googleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    googleApiClient.connect();
}

This is my build.gradle file on the wear folder:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 20
    buildToolsVersion "20.0.0"

    defaultConfig {
        applicationId "com.example"
        minSdkVersion 20
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    //compile 'com.google.android.gms:play-services-wearable:+'
    compile 'com.google.android.support:wearable:+'
    compile 'com.android.support:appcompat-v7:20.+'
    compile 'com.google.android.gms:play-services:5.+'

}

And my AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example" >

<uses-feature android:name="android.hardware.type.watch" />
<uses-permission android:name="android.permission.BODY_SENSORS" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.DeviceDefault" >
    <activity
        android:name=".MyActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
</application>

</manifest>

回答1:

Android Wear requires you have Google Play services version >= 5.0.89 installed on your phone

You can check your version and update the Play Services APK here: https://play.google.com/store/apps/details?id=com.google.android.gms&hl=en

Also Useful:

Important: Because it is hard to anticipate the state of each device, you must always check for a compatible Google Play services APK before you access Google Play services features. For many apps, the best time to check is during the onResume() method of the main activity.

Code to check if Play Services is available (on your phone):

// ########################################################################
// Check if Google Play Services is installed. If not show error dialog.
int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if( ConnectionResult.SUCCESS == result ){
    mGoogleApiClient.connect();
    loadingTextView.setText("Connecting to Wear...");
}
else {
    // Show appropriate dialog
    Dialog d = GooglePlayServicesUtil.getErrorDialog(result, this, 0);
    d.show();
}

More details here:

https://developer.android.com/google/play-services/setup.html#ensure



回答2:

Looks like putting the googleClient.connect() on onStart() solves the issue:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

    // Build a new GoogleApiClient
    checkIfGoogleApiClientExists();
    googleClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
}

@Override
protected void onStart() {
    Log.i(LOG_TAG,"entered onStart");
    super.onStart();
    googleClient.connect();
}