I am trying to synchronize data from a SQLite database on a PDA with a wearable. Therefore, I use the data API with the following PDA code:
cursor.moveToFirst(); while (!cursor.isAfterLast()) { final PutDataMapRequest putRequest = PutDataMapRequest.create("/SAMPLE"); putRequest.setUrgent(); final DataMap map = putRequest.getDataMap(); int i = 0; for (String columnName: cursor.getColumnNames()) { map.putString(columnName,cursor.getString(i)); i++; } Wearable.DataApi.putDataItem(googleApiClient, putRequest .asPutDataRequest()).setResultCallback(new ResultCallback() { @Override public void onResult(Result result) { if (!result.getStatus().isSuccess()) { Log.v("MainActivity", "data could not be sent"); } else { Log.v("MainActivity", "data sent"); } } }); cursor.moveToNext(); } cursor.close();
Then on wearable I have a DataApi.DataListener to look for changes:
@Override public void onDataChanged(DataEventBuffer dataEventBuffer) { for (DataEvent event : dataEventBuffer) { String data = null; try { data = new String(event.getDataItem().getData(), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } Log.v(TAG, "onDataChanged data: "+ data); if (event.getType() == DataEvent.TYPE_CHANGED && event.getDataItem().getUri().getPath().equals("/SAMPLE")) { DataMapItem item = DataMapItem.fromDataItem(event.getDataItem()); String column1_value = item.getDataMap().getString(column1); Log.v(TAG, "onDataChanged column1: "+ column1_value); } } }
This works well, I can send all the rows of my database to wearables and get them there.
Now I read that there is no need to copy data to a new SQLite database on wearable, but instead use DataItembuffer directly, which already has data stored in its own database. e.g. How to use DataItem for Android Wear or Android Wear How long DataMap entries remain valid
But when I want to get previously synchronized data in the method later, I get only one element from the 20 rows sent before the last one. I seem to be rewriting my stuff ... But to me, of course, all of them ...
public void getDataItems(){ final PendingResult<DataItemBuffer> results = Wearable.DataApi.getDataItems(googleApiClient); Runnable r = new Runnable() { @Override public void run() { DataItemBuffer dataItems = results.await(); for (int i = 0; i < dataItems.getCount(); i++) { DataMapItem item = DataMapItem.fromDataItem(dataItems.get(i)); String column1_value = item.getDataMap().getString(column1); Log.v(TAG, "getDataItems column1: "+ column1_value); } } }; Thread thread = new Thread(r); thread.start(); }
Obviously, there is no difference between using await () and ResultCallback, I always get only the last element ...
What am I doing wrong here?
UPDATE: I noticed that it matters if I use different URIs. If i use
final PutDataMapRequest putRequest = PutDataMapRequest.create("/SAMPLE1");
and
final PutDataMapRequest putRequest = PutDataMapRequest.create("/SAMPLE2");
then I get at least two elements in the getDataItems method, but still only the last for each URI. According to the Docs, each DataItem is identified by one URI, usually consisting of a node identifier and a path. https://developers.google.com/android/reference/com/google/android/gms/wearable/DataItem Since it seems inefficient using hundreds or thousands of paths, the question arises whether DataItems can even replace SQLite db or not .. .
Or is there a way that has multiple data elements in one URI?