This lesson teaches you to
A
DataItem
defines the data interface that the system uses to synchronize data between handhelds
and wearables. A
DataItem
generally
consists of the following items:
- Payload - A byte array, which you can set with whatever data you wish, allowing you to do your own object serialization and deserialization. The size of the payload is limited to 100KB.
-
Path
- A unique string that must start with a forward slash (for instance,
"/path/to/data")
You normally don't implement
DataItem
directly. Instead, you:
-
Create a
PutDataRequestobject, specifying a string path to uniquely identify the item. - Call setData() to set the payload.
-
Call
DataApi.putDataItem()to request the system to create the data item. -
When requesting data items, the system returns objects
that properly implement the
DataIteminterface.
However, instead of working with raw bytes using
setData()
,
we recommend you
use a data map
, which exposes
a data item in an easy-to-use
Bundle
-like interface.
Sync Data with a Data Map
When possible, use the
DataMap
class,
which lets you work with data items in the form of an Android
Bundle
,
so object serialization and de-serialization is done for you, and you can manipulate data with key-value pairs.
To use a data map:
-
Create a
PutDataMapRequestobject, setting the path of the data item.Note: The path string is a unique identifier for the data item that allows you to access it from either side of the connection. The path must begin with a forward slash. If you're using hierarchical data in your app, you should create a path scheme that matches the structure of the data.
-
Call
PutDataMapRequest.getDataMap()to obtain a data map that you can set values on. -
Set any desired values for the data map using the
put...()methods, such asputString(). -
Call
PutDataMapRequest.asPutDataRequest()to obtain aPutDataRequestobject. -
Call
DataApi.putDataItem()to request the system to create the data item.Note: If the handset and wearable devices are disconnected, the data is buffered and and synced when the connection is re-established.
The following example shows how to create a data map, set data on it, and create it:
PutDataMapRequest dataMap = PutDataMapRequest.create("/count");
dataMap.getDataMap().putInt(COUNT_KEY, count++);
PutDataRequest request = dataMap.asPutDataRequest();
PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi
.putDataItem(mGoogleApiClient, request);
Listen for Data Item Events
If one side of the data layer connection changes a data item, you probably want to be notified of any changes on the other side of the connection. You can do this by implementing a listener for data item events.For example, here's what a typical callback looks like to carry out certain actions when data changes.
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
for (DataEvent event : dataEvents) {
if (event.getType() == DataEvent.TYPE_DELETED) {
Log.d(TAG, "DataItem deleted: " + event.getDataItem().getUri());
} else if (event.getType() == DataEvent.TYPE_CHANGED) {
Log.d(TAG, "DataItem changed: " + event.getDataItem().getUri());
}
}
}
This is just a snippet that requires more implementation details. Learn about how to implement a full listener service or activity in Listening for Data Layer Events .