public interface

DataApi

com.google.android.gms.wearable.DataApi

Class Overview

Exposes an API for components to read or write data items and assets.

A DataItem is synchronized across all devices in an Android Wear network. It is possible to set data items while not connected to any nodes. Those data items will be synchronized when the nodes eventually come online.

Data items are private to the application that created them, and are only accessible by that application on other nodes. They should generally be small in size, relying on assets for the transfer of larger, more persistent data objects such as images.

DataItem URI format

Each data item is identified by a URI, accessible with getUri(), that indicates the item's creator and path. Fully specified URIs follow the following format:
wear://<node_id>/<path>
where <node_id> is the node ID of the wearable node that created the data item, and <path> is an application-defined path. This means that given a data item's URI, calling getHost() will return the creator's node ID.

In some of the methods below (such as getDataItems(GoogleApiClient, Uri)), it is possible to omit the node ID from the URI, and only leave a path. In that case, the URI may refer to multiple data items, since multiple nodes may create data items with the same path. Partially specified data item URIs follow the following format:

wear:/<path>
Note the single / after wear:.

Concurrency

Concurrent modification of data items from different nodes may result in inconsistent results. It is recommended that apps are built with some concept of ownership of data items. Two common patterns are:
  • Creator owns data items: Data items may be updated only by the original creator node.
  • Producer-consumer: One node is responsible for creating a data item, and a second node is responsible for deleting it, once it has been processed. Data items should have unique IDs when using this pattern, and data items should not be modified once created.

Summary

Nested Classes
interface DataApi.DataItemResult Contains a single data item. 
interface DataApi.DataListener Used with addListener(GoogleApiClient, DataApi.DataListener) to receive data events. 
interface DataApi.DeleteDataItemsResult Contains the number of deleted items. 
interface DataApi.GetFdForAssetResult Contains a file descriptor for the requested asset. 
Public Methods
abstract PendingResult<Status> addListener(GoogleApiClient client, DataApi.DataListener listener)
Registers a listener to receive data item changed and deleted events.
abstract PendingResult<DataApi.DeleteDataItemsResult> deleteDataItems(GoogleApiClient client, Uri uri)
Removes all specified data items from the Android Wear network.
abstract PendingResult<DataApi.DataItemResult> getDataItem(GoogleApiClient client, Uri uri)
Retrieves a single DataItem from the Android Wear network.
abstract PendingResult<DataItemBuffer> getDataItems(GoogleApiClient client)
Retrieves all data items from the Android Wear network.
abstract PendingResult<DataItemBuffer> getDataItems(GoogleApiClient client, Uri uri)
Retrieves all data items matching the provided URI, from the Android Wear network.
abstract PendingResult<DataApi.GetFdForAssetResult> getFdForAsset(GoogleApiClient client, DataItemAsset asset)
Retrieves a ParcelFileDescriptor pointing at the bytes of an asset.
abstract PendingResult<DataApi.GetFdForAssetResult> getFdForAsset(GoogleApiClient client, Asset asset)
Retrieves a ParcelFileDescriptor pointing at the bytes of an asset.
abstract PendingResult<DataApi.DataItemResult> putDataItem(GoogleApiClient client, PutDataRequest request)
Adds a DataItem to the Android Wear network.
abstract PendingResult<Status> removeListener(GoogleApiClient client, DataApi.DataListener listener)
Removes a data listener which was previously added through addListener(GoogleApiClient, DataListener).

Public Methods

public abstract PendingResult<Status> addListener (GoogleApiClient client, DataApi.DataListener listener)

Registers a listener to receive data item changed and deleted events. This call should be balanced with a call to removeListener(GoogleApiClient, DataListener), to avoid leaking resources.

The listener will be notified of changes initiated by this node.

public abstract PendingResult<DataApi.DeleteDataItemsResult> deleteDataItems (GoogleApiClient client, Uri uri)

Removes all specified data items from the Android Wear network.

If uri is fully specified, this method will delete at most one data item. If uri contains no host, multiple data items may be deleted, since different nodes may create data items with the same path. See DataApi for details of the URI format.

public abstract PendingResult<DataApi.DataItemResult> getDataItem (GoogleApiClient client, Uri uri)

Retrieves a single DataItem from the Android Wear network. A fully qualified URI must be specified. The URI's host must be the ID of the node that created the item.

See DataApi for details of the URI format.

public abstract PendingResult<DataItemBuffer> getDataItems (GoogleApiClient client)

Retrieves all data items from the Android Wear network.

Callers must call release() on the returned buffer when finished processing results.

public abstract PendingResult<DataItemBuffer> getDataItems (GoogleApiClient client, Uri uri)

Retrieves all data items matching the provided URI, from the Android Wear network.

The URI must contain a path. If uri is fully specified, at most one data item will be returned. If uri contains no host, multiple data items may be returned, since different nodes may create data items with the same path. See DataApi for details of the URI format.

Callers must call release() on the returned buffer when finished processing results.

public abstract PendingResult<DataApi.GetFdForAssetResult> getFdForAsset (GoogleApiClient client, DataItemAsset asset)

Retrieves a ParcelFileDescriptor pointing at the bytes of an asset. Only assets previously stored in a DataItem may be retrieved.

public abstract PendingResult<DataApi.GetFdForAssetResult> getFdForAsset (GoogleApiClient client, Asset asset)

Retrieves a ParcelFileDescriptor pointing at the bytes of an asset. Only assets previously stored in a DataItem may be retrieved.

public abstract PendingResult<DataApi.DataItemResult> putDataItem (GoogleApiClient client, PutDataRequest request)

Adds a DataItem to the Android Wear network. The updated item is synchronized across all devices.

public abstract PendingResult<Status> removeListener (GoogleApiClient client, DataApi.DataListener listener)

Removes a data listener which was previously added through addListener(GoogleApiClient, DataListener).