Sunday, May 29, 2016

Sending and Syncing Data with Android Wear

  1. Accessing the Wearable Data Layer

The Wearable Data Layer API, which is part of Google Play services, provides a communication channel for your handheld and wearable app.

1.1       Google Api Client and Wearable API

 



To call the Data Layer API, create an instance of GoogleApiClient.

1.2        Data Objects in API

The API consists of a set of data objects that the system can send and synchronize over the wire and listeners that notify your apps of important events with the data layer

object
description
DataItem
Sets up shared data storage between the devices with automatic syncing. These objects will be automatically synced between the mobile and wearable devices. You usually need to define a byte array called payload for serialization and deserialization. There is a 100KB limit. Also, set a unique string identifier called path starting with a forward slash "/".
MessageApi
Sends fireandforgettype commands, such as controlling and starting an intent on the wearable from the handheld or controlling a   phone app from the wearable. Delivers an error when the devices are disconnected or the message if they are connected. Payload is optional in this case, but path is required.
Asset
Sends binary blobs of data, such as images. Attached to data items, the system handles the transfer automatically. Minimizes Bluetooth bandwidth by caching large assets to avoid retransmission.
WearableListenerService (for services)
For  services. Listens to data layer events.
DataListener
(for foreground activities)
Used with foreground services. Listens to data layer events when the activity is in the foreground.
Channel
You can use the ChannelApi class to transfer large data items, such as music and movie files, from a handheld to a wearable device. The Channel API for data transfer has the following benefits:Transfer large data files between two or more connected devices, without the automatic synchronization provided when using Asset objects attached to DataItem objects.Reliably send a file that is too large in size to send using the MessageApi class.Transfer streamed data, such as music pulled from a network server or voice data from the microphone.

2. Syncing Data Items and Listen for Data Events

·         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")
·         Don’t implement DataItem directly.

2.1  Sync Data with a Data Map

  1. Create a putDataMapRequest object, setting the path of the data item. 
  2. Call putDataMapRequest.getDataMap() to obtain a data map that you can set values on. 
  3. Set any desired values for the data map using the put...() methods, such as putString(). 
  4. If a delay in syncing would negatively impact user experience, call setUrgent(). 
  5. Call putDataMapRequest.asPutDataRequest() to obtain a PutDataRequest object. 
  6. Call DataApi.putDataItem() to request the system to create the data item. 

2.2 Wait for the Status of Data Layer Calls

The Data Layer API sometimes return a PendingResult, such as putDataItem(). As soon as the PendingResult is created, the operation is queued in the background. If you do nothing else after this, the operation eventually completes silently.
The PendingResult lets you wait for the result status, either synchronously or asynchronously.
Asynchronous calls:
If code is running on the main UI thread, do not make blocking calls to the Data Layer API.



Synchronous calls:
If code is running on a separate handler thread in a background service (which is the case in a WearableListenerService), it's fine for the calls to block.

2.3       Listen for Data Item Events

Using DataListener
Using service

3 . Transferring Assets

3.1       Transfer an Asset

To send large blobs of binary data over the Bluetooth transport, such as images, attach an Asset to a data item and the put the data item into the replicated data store.

3.2        Receive assets

To implement the callback to detect an asset change and extract the asset:


4.1        Sending a Message

Using the MessageApi and attach the following items to the message:
  • An arbitrary payload (optional)
  • A path that uniquely identifies the message's action.
A wearable app can provide functionality for users such as voice transcription. Users can speak into their wearable device's microphone, and have a transcription saved to a note.
Since a wearable device typically does not have the processing power and battery capacity required to handle the voice transcription activity, the app should offload this work to a more capable, connected device.



4.2        Receive a Message