Accessing the Wearable Data Layer

This lesson teaches you to

  1. Set up a Google Play services client to use the Wearable Data Layer APIs

Dependencies and Prerequisites

  1. Creating Wearable Apps > Set Up an Android Wear Emulator or Device
  2. Creating Wearable Apps > Creating a Project

To call the data layer API, create an instance of GoogleApiClient, the main entry point for any of the Google Play services APIs.

GoogleApiClient provides a builder that makes it easy to create an instance of the client. A minimal GoogleApiClient looks like this:

Note: For now, this minimal client is enough to get started. However, see Accessing Google Play services APIs for more information about creating a GoogleApiClient, implementing its callbacks, and handling error cases.

GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks(new 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 OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult result) {
                    Log.d(TAG, "onConnectionFailed: " + result);
                }
            })
        // Request access only to the Wearable API
        .addApi(Wearable.API)
        .build();

Important: To avoid client connection errors on devices that do not have the Android Wear app installed, use a separate GoogleApiClient instance to access only the Wearable API. For more information, see Access the Wearable API.

Before you use the data layer API, start a connection on your client by calling the connect() method, as described in Accessing Google Play services APIs. When the system invokes the onConnected() callback for your client, you're ready to use the data layer API.