40

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" />
N J
  • 27,217
  • 13
  • 76
  • 96
odiggity
  • 4,117
  • 7
  • 34
  • 41

3 Answers3

142

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

N J
  • 27,217
  • 13
  • 76
  • 96
odiggity
  • 4,117
  • 7
  • 34
  • 41
  • Sadly, this happened to me, too, in the beginning. Lol. – gatlingxyz Jul 15 '14 at 19:33
  • 97
    It's not that embarrassing when the documentation 'lessons' neglect to mention this. – iforce2d Dec 27 '14 at 15:01
  • 1
    Exactly what I was thinking iforce2d. – bmeulmeester Jan 21 '15 at 11:22
  • 13
    Shocking omission from the tutorial at https://developer.android.com/training/location/retrieve-current.html! – Craig McMahon Jan 29 '15 at 14:33
  • 2
    I have put in a request to get the documentation fixed here. – Wayne Piekarski Jul 09 '15 at 21:34
  • 3
    The documentation lessons neglect to mention it, as @iforce2d was pointing to us. Be careful to call mGoogleApiClient.disconnect() in onStop() too. – Sébastien BATEZAT Jul 13 '15 at 21:10
  • .... this is STILL an issue with the documentation.... kind of amazing. To their credit, you can check the sample code (which itself a hard link to find on the page), where they show indeed onStart() and connecting: https://github.com/googlesamples/android-play-location/blob/master/BasicLocationSample/app/src/main/java/com/google/android/gms/location/sample/basiclocationsample/MainActivity.java – Don Cheadle Aug 22 '15 at 22:56
3

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();
poolie
  • 9,289
  • 1
  • 47
  • 74
Imre
  • 31
  • 2
0

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.

Juan Mendez
  • 2,658
  • 1
  • 27
  • 23