1 page.title=Syncing Data Items 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><a href="#SyncData">Sync Data with a Data Map</a></li> 11 <li><a href="#ListenEvents">Listen for Data Item Events</a></li> 12 </ol> 13 14 </div> 15 </div> 16 17 <p> 18 A <a href="@{docRoot}reference/com/google/android/gms/wearable/DataItem.html"><code>DataItem</code></a> 19 defines the data interface that the system uses to synchronize data between handhelds 20 and wearables. A <a href="@{docRoot}reference/com/google/android/gms/wearable/DataItem.html"><code>DataItem</code></a> generally 21 consists of the following items:</p> 22 <ul> 23 <li><b>Payload</b> - A byte array, which you can set with whatever data you wish, allowing you 24 to do your own object serialization and deserialization. The size of the payload is limited 25 to 100KB.</li> 26 <li><b>Path</b> - A unique string that must start with a forward slash (for instance, 27 <code>"/path/to/data"</code>)</li> 28 </ul> 29 30 <p> 31 You normally don't implement <a href="@{docRoot}reference/com/google/android/gms/wearable/DataItem.html"><code>DataItem</code></a> 32 directly. Instead, you: 33 34 <ol> 35 <li>Create a <a href="{@docRoot}reference/com/google/android/gms/wearable/PutDataRequest.html"><code>PutDataRequest</code></a> object, 36 specifying a string path to uniquely identify the item. 37 </li> 38 <li>Call <a href="{@docRoot}reference/com/google/android/gms/wearable/PutDataRequest.html#setData(byte[])">setData()</a> to set 39 the payload. 40 </li> 41 <li>Call <a href="{@docRoot}reference/com/google/android/gms/wearable/DataApi.html#putDataItem(com.google.android.gms.common.api.GoogleApiClient, com.google.android.gms.wearable.PutDataRequest)"><code>DataApi.putDataItem()</code></a> to request the system to create the data item. 42 </li> 43 <li>When requesting data items, the system returns objects 44 that properly implement the <a href="{@docRoot}reference/com/google/android/gms/wearable/DataItem.html"><code>DataItem</code></a> interface. 45 </li> 46 </ol> 47 48 <p> 49 However, instead of working with raw bytes using <a href="{@docRoot}reference/com/google/android/gms/wearable/PutDataRequest.html#setData(byte[])">setData()</a>, 50 we recommend you <a href="#data-map">use a data map</a>, which exposes 51 a data item in an easy-to-use {@link android.os.Bundle}-like interface. 52 </p> 53 54 <h2 id="SyncData">Sync Data with a Data Map</h2> 55 <p> 56 When possible, use the <a href="{@docRoot}reference/com/google/android/gms/wearable/DataMap.html"><code>DataMap</code></a> class, 57 which lets you work with data items in the form of an Android {@link android.os.Bundle}, 58 so object serialization and de-serialization is done for you, and you can manipulate data with key-value pairs. 59 </p> 60 61 <p>To use a data map:</p> 62 63 <ol> 64 <li>Create a 65 <a href="{@docRoot}reference/com/google/android/gms/wearable/PutDataMapRequest.html"><code>PutDataMapRequest</code></a> 66 object, setting the path of the data item. 67 <p class="note"><b>Note:</b> The path string is a unique identifier for the 68 data item that allows you to access it from either side of the connection. The path must begin 69 with a forward slash. If you're using hierarchical data in your 70 app, you should create a path scheme that matches the structure of the data. 71 </p> 72 </li> 73 <li>Call 74 <a href="{@docRoot}reference/com/google/android/gms/wearable/PutDataMapRequest.html#getDataMap()"><code>PutDataMapRequest.getDataMap()</code></a> 75 </a> to obtain a data map that you can set values on.</li> 76 <li>Set any desired values for the data map using the <code>put...()</code> methods, such as 77 <a href="{@docRoot}reference/com/google/android/gms/wearable/DataMap.html#putString(java.lang.String, java.lang.String)"><code>putString()</code></a>. 78 </li> 79 <li>Call <a href="{@docRoot}reference/com/google/android/gms/wearable/PutDataMapRequest.html#asPutDataRequest()"><code>PutDataMapRequest.asPutDataRequest()</code></a> 80 to obtain a <a href="{@docRoot}reference/com/google/android/gms/wearable/PutDataRequest.html"><code>PutDataRequest</code></a> object. 81 </li> 82 <li>Call <a href="{@docRoot}reference/com/google/android/gms/wearable/DataApi.html#putDataItem(com.google.android.gms.common.api.GoogleApiClient, com.google.android.gms.wearable.PutDataRequest)"><code>DataApi.putDataItem()</code></a> to request the system to create the data item. 83 <p class="note"><b>Note:</b> 84 If the handset and wearable devices are disconnected, 85 the data is buffered and and synced when the connection is re-established. 86 </p> 87 </li> 88 </ol> 89 90 <p>The following example shows how to create a data map, set data on it, and create it:</p> 91 92 <pre> 93 PutDataMapRequest dataMap = PutDataMapRequest.create("/count"); 94 dataMap.getDataMap().putInt(COUNT_KEY, count++); 95 PutDataRequest request = dataMap.asPutDataRequest(); 96 PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi 97 .putDataItem(mGoogleApiClient, request); 98 </pre> 99 100 <h2 id="ListenEvents">Listen for Data Item Events</h2> 101 If one side of the data layer connection changes a data item, you probably want 102 to be notified of any changes on the other side of the connection. 103 You can do this by implementing a listener for data item events. 104 105 <p>For example, here's what a typical callback looks like to carry out certain actions 106 when data changes.</p> 107 108 <pre> 109 @Override 110 public void onDataChanged(DataEventBuffer dataEvents) { 111 for (DataEvent event : dataEvents) { 112 if (event.getType() == DataEvent.TYPE_DELETED) { 113 Log.d(TAG, "DataItem deleted: " + event.getDataItem().getUri()); 114 } else if (event.getType() == DataEvent.TYPE_CHANGED) { 115 Log.d(TAG, "DataItem changed: " + event.getDataItem().getUri()); 116 } 117 } 118 } 119 </pre> 120 <p> 121 This is just a snippet that requires more implementation details. Learn about 122 how to implement a full listener service or activity in 123 <a href="{@docRoot}training/wearables/data-layer/events.html">Listening for Data Layer Events</a>. 124 </p>