0

On the mobile device, from a Service, I'm using the same path to update a bitmap asset using the following code:

PutDataMapRequest dataMap = PutDataMapRequest.create("/result/1");
dataMap.getDataMap().putString(FIELD_NAME, name);
if(bitmap != null) {
    Asset asset = createAssetFromBitmap(bitmap);
    dataMap.getDataMap().putAsset(FIELD_IMAGE, asset);
}
PutDataRequest request = dataMap.asPutDataRequest();
PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi
        .putDataItem(mGoogleApiClient, request);
DataApi.DataItemResult result = pendingResult.await(5L, TimeUnit.SECONDS);

On the wearable, I'm watching for changes as follows:

@Override
public void onDataChanged(DataEventBuffer dataEvents) {
    for (DataEvent event : dataEvents) {
        if (event.getType() == DataEvent.TYPE_CHANGED) {
            Log.d(TAG, "DataItem changed: " + event.getDataItem().getUri().toString());
...
    dataEvents.release();

Now the first time it runs, I get one dataEvent. But, the next time I run it, I get 2 dataEvents with the same Uri, the old one and the new one. And so on.

I thought that using the same path in PutDatMapRequest.create() would simply update the data in the DataApi sync store, and sync it.

I've tried Wearable.DataApi.deleteDataItems() but this just gives me a big list of CHANGED and DELETED events, all with the same Uri.

How do I avoid getting loads of dataEvents on my wearable as time goes on?

barkside
  • 3,951
  • 4
  • 23
  • 32

2 Answers2

2

I figured it out. I have the following code:

@Override
protected void onStop() {
    if ((mGoogleApiClient != null) && mGoogleApiClient.isConnected()) {
        Wearable.DataApi.removeListener(mGoogleApiClient, WearIpCamActivity.this);
        mGoogleApiClient.disconnect();
    }
    mIsConnected = false;

However, because I copy/pasted the loadBitmapFromAsset from the official website, I forgot to check it carefully, and I found it had the line mGoogleApiClient.disconnect() in it.

This was preventing my if block to be executed in onStop, so my DataApi listener was not being removed. Next time I ran my app, I now had the same callback registered twice, with OnDataChanged called twice for the same dataItem.

Removing mGoogleApiClient.disconnect() from loadBitmapFromAsset solved my problem.

barkside
  • 3,951
  • 4
  • 23
  • 32
0

A date item is unique by full uri, which includes the host that first created an item. You may have multiple items having the same path, but each would have a unique host.

So, if you create an item on one host, then fetch/onDataChanged on the other host, you should modify and put DataItem the item rather than creating a new DataItem.fromPath

jlapenna
  • 51
  • 2