page.title=Accessing the Wearable Data Layer @jd:body <div id="tb-wrapper"> <div id="tb"> <h2>This lesson teaches you to</h2> <ol> <li>Set up a Google Play services client to use the Wearable Data Layer APIs</li> </ol> <h2>Dependencies and Prerequisites</h2> <ol> <li><a href="{@docRoot}training/wearables/apps/creating.html#SetupEmulator">Creating Wearable Apps > Set Up an Android Wear Emulator or Device</a></li> <li><a href="{@docRoot}training/wearables/apps/creating.html#CreateProject">Creating Wearable Apps > Creating a Project</a></li> </ol> </div> </div> <p>To call the Data Layer API, create an instance of <a href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html"><code>GoogleApiClient</code></a>, the main entry point for any of the Google Play services APIs. </p> <p> <a href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html"><code>GoogleApiClient</code></a> provides a builder that makes it easy to create an instance of the client. A minimal <a href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html"><code>GoogleApiClient</code></a> looks like this: </p> <p class="note"><b>Note:</b> For now, this minimal client is enough to get started. However, see <a href="{@docRoot}google/auth/api-client.html">Accessing Google Play services APIs</a> for more information about creating a <a href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html"><code>GoogleApiClient</code></a>, implementing its callbacks, and handling error cases.</p> <pre style="clear:right"> 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(); </pre> <p class="caution"> <strong>Important:</strong> If you are adding multiple APIs to a <a href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html"><code>GoogleApiClient</code></a>, you may run into client connection errors on devices that do not have the <a href="https://play.google.com/store/apps/details?id=com.google.android.wearable.app&hl=en">Android Wear app</a> installed. To avoid connection errors, call the <a href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.Builder.html#addApiIfAvailable(com.google.android.gms.common.api.Api<? extends com.google.android.gms.common.api.Api.ApiOptions.NotRequiredOptions>, com.google.android.gms.common.api.Scope...)">{@code addApiIfAvailable()}</a> method and pass in the <a href="{@docRoot}reference/com/google/android/gms/wearable/Wearable.html">{@code Wearable}</a> API to indicate that your client should gracefully handle the missing API. For more information, see <a href="{@docRoot}google/auth/api-client.html#WearableApi">Access the Wearable API</a>.</p> <p>Before you use the data layer API, start a connection on your client by calling the <a href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html#connect()"> <code>connect()</code></a> method, as described in <a href="{@docRoot}google/auth/api-client.html#Starting">Start a Connection</a>. When the system invokes the <a href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.ConnectionCallbacks.html#onConnected(android.os.Bundle)"> <code>onConnected()</code></a> callback for your client, you're ready to use the Data Layer API.</p>