GoogleApiClient onConnected never called on Wearab

2020-02-09 23:35发布

问题:

I have a wearable device that I'm trying to connect to the GoogleApiClient but the callbacks are never called (onConnected, onConnectionSuspended or onConnectionFailed). Everything else is working fine, the DataLayerListenerService is able to receive messages from the handheld and the onPeerConnected is being called when it connects. I've tried in on both the emulator and a Samsung Gear Live device. This is the code that I have in the Activity where I'm trying to connect to the GoogleApiClient.

public class WearReaderActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
    public static final String CONTENT_EXTRA = "contentExtra";
    private String LOG_TAG = WearReaderActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reader);
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Wearable.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }

    private GoogleApiClient mGoogleApiClient;

    @Override
    protected void onStart() {
        super.onStart();
        Log.e("Connected?", String.valueOf(mGoogleApiClient.isConnected()));
        //new Thread(new GetContent()).start();
    }

    @Override
    public void onConnected(Bundle bundle) {
        Log.d("Connected", "Connected");
        new Thread(new GetContent()).start();
    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.d("Connection suspened", "Connection suspended");
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.d("Connection suspened", "Connection suspended");
    }
...
}

Not sure if it'll help but this is my Manifest for the Wearable app

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="my.packagename">
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.DeviceDefault" >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

    <activity
        android:name=".WearReaderActivity"
        android:label="Reading" >
        <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" />

    <service
        android:name=".DataLayerListenerService" >
        <intent-filter>
            <action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
        </intent-filter>
    </service>

</application>

Any ideas?

EDIT: Added the following to the wearable manifest, still not working though

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

回答1:

Oh wow, the answer is embarrassingly simple. I missed the part where in onStart() you need to call mGoogleApiClient.connect().



回答2:

You can call the connect and disconnect manually in the onStart and onStop lifecycle methods, or you can use the enableAutoManage feature.

mCredentialsApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .enableAutoManage(this, this)
            .addApi(Auth.CREDENTIALS_API)
            .build();


回答3:

Additionally, I noticed that if you try to do GoogleApiClient when user hasn't setup Google Play there can be a conflict connecting. (I just reset my device and that's what happened). So it's a good practice to test connection from GoogleApiClient using

mCredentialsApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)

Or just check mCredentialsApiClient.isConnected() before peforming any task.