1 page.title=Accessing the Wearable Data Layer 2 3 @jd:body 4 5 <div id="tb-wrapper"> 6 <div id="tb"> 7 8 <h2>This lesson teaches you to</h2> 9 <ol> 10 <li>Set up a Google Play services client to use the Wearable Data Layer APIs</li> 11 </ol> 12 13 <h2>Dependencies and Prerequisites</h2> 14 <ol> 15 <li><a href="{@docRoot}training/wearables/apps/creating.html#SetupEmulator">Creating 16 Wearable Apps > Set Up an Android Wear Emulator or Device</a></li> 17 <li><a href="{@docRoot}training/wearables/apps/creating.html#CreateProject">Creating 18 Wearable Apps > Creating a Project</a></li> 19 </ol> 20 </div> 21 </div> 22 23 <p>To call the Data Layer API, create an instance of 24 <a href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html"><code>GoogleApiClient</code></a>, 25 the main entry point for any of the Google Play services APIs. 26 </p> 27 28 <p> 29 <a href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html"><code>GoogleApiClient</code></a> 30 provides a builder that makes it easy to create an instance of the client. 31 A minimal <a href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html"><code>GoogleApiClient</code></a> looks like this: 32 </p> 33 34 <p class="note"><b>Note:</b> For now, this minimal client is enough to get started. However, see 35 <a href="{@docRoot}google/auth/api-client.html">Accessing Google Play services APIs</a> 36 for more information about creating a <a href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html"><code>GoogleApiClient</code></a>, 37 implementing its callbacks, and handling error cases.</p> 38 39 <pre style="clear:right"> 40 GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this) 41 .addConnectionCallbacks(new ConnectionCallbacks() { 42 @Override 43 public void onConnected(Bundle connectionHint) { 44 Log.d(TAG, "onConnected: " + connectionHint); 45 // Now you can use the Data Layer API 46 } 47 @Override 48 public void onConnectionSuspended(int cause) { 49 Log.d(TAG, "onConnectionSuspended: " + cause); 50 } 51 }) 52 .addOnConnectionFailedListener(new OnConnectionFailedListener() { 53 @Override 54 public void onConnectionFailed(ConnectionResult result) { 55 Log.d(TAG, "onConnectionFailed: " + result); 56 } 57 }) 58 // Request access only to the Wearable API 59 .addApi(Wearable.API) 60 .build(); 61 </pre> 62 63 <p class="caution"> 64 <strong>Important:</strong> If you are adding multiple APIs to a 65 <a href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html"><code>GoogleApiClient</code></a>, 66 you may run into client connection errors on devices that do not have the 67 <a href="https://play.google.com/store/apps/details?id=com.google.android.wearable.app&hl=en">Android 68 Wear app</a> installed. To avoid connection errors, call the 69 <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> 70 method and pass in the <a 71 href="{@docRoot}reference/com/google/android/gms/wearable/Wearable.html">{@code 72 Wearable}</a> API to indicate that your client should gracefully handle the missing API. 73 For more information, see <a 74 href="{@docRoot}google/auth/api-client.html#WearableApi">Access the Wearable API</a>.</p> 75 76 <p>Before you use the data layer API, start a connection on your client by calling the 77 <a href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html#connect()"> 78 <code>connect()</code></a> 79 method, as described in 80 <a href="{@docRoot}google/auth/api-client.html#Starting">Start a Connection</a>. 81 When the system invokes the 82 <a href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.ConnectionCallbacks.html#onConnected(android.os.Bundle)"> 83 <code>onConnected()</code></a> callback for your client, you're ready to use the Data Layer API.</p> 84