1page.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>, 25the 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> 30provides a builder that makes it easy to create an instance of the client. 31A 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> 36for more information about creating a <a href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html"><code>GoogleApiClient</code></a>, 37implementing its callbacks, and handling error cases.</p> 38 39<pre style="clear:right"> 40GoogleApiClient 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> To avoid client connection errors on devices that do not have the 65<a href="https://play.google.com/store/apps/details?id=com.google.android.wearable.app&hl=en">Android 66Wear app</a> installed, use a separate <a 67href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html">{@code 68GoogleApiClient}</a> instance to access only the <a 69href="{@docRoot}reference/com/google/android/gms/wearable/Wearable.html">{@code 70Wearable}</a> API. For more information, see <a 71href="{@docRoot}google/auth/api-client.html#WearableApi">Access the Wearable API</a>.</p> 72 73<p>Before you use the data layer API, start a connection on your client by calling the 74<a href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html#connect()"> 75<code>connect()</code></a> 76method, as described in 77<a href="{@docRoot}google/auth/api-client.html#Starting">Start a Connection</a>. 78When the system invokes the 79<a href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.ConnectionCallbacks.html#onConnected(android.os.Bundle)"> 80<code>onConnected()</code></a> callback for your client, you're ready to use the Data Layer API.</p> 81