Home | History | Annotate | Download | only in data-layer
      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/environment.html">Creating
     16   Wearable Apps > Setting up Your Environment</a></li>
     17   <li><a href="{@docRoot}training/wearables/apps/creating.html">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 mGoogleAppiClient = new GoogleApiClient.Builder(this)
     41         .addConnectionCallbacks(new ConnectionCallbacks() {
     42                 &#64;Override
     43                 public void onConnected(Bundle connectionHint) {
     44                     Log.d(TAG, "onConnected: " + connectionHint);
     45                     // Now you can use the data layer API
     46                 }
     47                 &#64;Override
     48                 public void onConnectionSuspended(int cause) {
     49                     Log.d(TAG, "onConnectionSuspended: " + cause);
     50                 }
     51         })
     52         .addOnConnectionFailedListener(new OnConnectionFailedListener() {
     53                 &#64;Override
     54                 public void onConnectionFailed(ConnectionResult result) {
     55                     Log.d(TAG, "onConnectionFailed: " + result);
     56                 }
     57             })
     58         .addApi(Wearable.API)
     59         .build();
     60 </pre>
     61 
     62 <p>Before you use the data layer API, start a connection on your client by calling the
     63 <a href="{@docRoot}reference/com/google/android/gms/common/api/GoogleApiClient.html#connect()">connect()</a>
     64 method, as described in
     65 <a href="{@docRoot}google/auth/api-client.html#Starting">Accessing Google Play services APIs</a>.
     66 When the system invokes the <code>onConnected()</code> callback for your client, you're ready
     67 to use the data layer API.</p>