original source: https://developer.android.com/training/wearables/data-layer/data-items.html

Syncing Data Items

A DataItem defines the data interface that the system uses to synchronize data between handhelds and wearables.

Payload – A byte array, which you can set with whatever data you wish. 크기는  100kb

Path – A unique string that must start with a forward slash

normally don’t implement DataItem directly. Instead, you do the following:

  1. Create a PutDataRequest object, specifying a string path to uniquely identify the item.
  2. Call setData() to set the payload.
  3. If a delay in syncing would negatively impact user experience, call setUrgent().
  4. Call DataApi.putDataItem() to request the system to create the data item.

Sync Data with a Data Map

use the DataMap class. This approach lets you work with data items in the form of an Android Bundle, so the system does object serialization and deserialization for you, and you can manipulate data with key-value pairs.

  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.

Note: If the handset and wearable devices are disconnected, the data is buffered and synced when the connection is re-established.

ex)

public class MainActivity extends Activity implements
       DataApi.DataListener,
       GoogleApiClient.ConnectionCallbacks,
       GoogleApiClient.OnConnectionFailedListener {

   private static final String COUNT_KEY = "com.example.key.count";

   private GoogleApiClient mGoogleApiClient;
   private int count = 0;

   ...

   // Create a data map and put data in it
   private void increaseCounter() {
       PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/count");
       putDataMapReq.getDataMap().putInt(COUNT_KEY, count++);
       PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
       PendingResult<DataApi.DataItemResult> pendingResult =
               Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq);
   }

   ...
}

Set DataItem priority

In Google Play services 8.3 and later, the DataApi interface allows urgent requests for syncing of DataItems.
setUrgent() 을 설정한다.

Listen for Data Item Events

public class MainActivity extends Activity implements
       DataApi.DataListener,
       GoogleApiClient.ConnectionCallbacks,
       GoogleApiClient.OnConnectionFailedListener {

   private static final String COUNT_KEY = "com.example.key.count";

   private GoogleApiClient mGoogleApiClient;
   private int count = 0;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       mGoogleApiClient = new GoogleApiClient.Builder(this)
               .addApi(Wearable.API)
               .addConnectionCallbacks(this)
               .addOnConnectionFailedListener(this)
               .build();
   }

   @Override
   protected void onResume() {
       super.onResume();
       mGoogleApiClient.connect();
   }

   @Override
   public void onConnected(Bundle bundle) {
       Wearable.DataApi.addListener(mGoogleApiClient, this);
   }

   @Override
   protected void onPause() {
       super.onPause();
       Wearable.DataApi.removeListener(mGoogleApiClient, this);
       mGoogleApiClient.disconnect();
   }

   @Override
   public void onDataChanged(DataEventBuffer dataEvents) {
       for (DataEvent event : dataEvents) {
           if (event.getType() == DataEvent.TYPE_CHANGED) {
               // DataItem changed
               DataItem item = event.getDataItem();
               if (item.getUri().getPath().compareTo("/count") == 0) {
                   DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
                   updateCount(dataMap.getInt(COUNT_KEY));
               }
           } else if (event.getType() == DataEvent.TYPE_DELETED) {
               // DataItem deleted
           }
       }
   }

   // Our method to update the count
   private void updateCount(int c) { ... }

   ...
}

Comments are closed.

Post Navigation