This is the event-based code
Wearable.DataApi.addListener(googleApiClient, new DataApi.DataListener() {
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
Log.i(tag, "on data changed " + dataEvents.toString());
for (DataEvent event : dataEvents) {
DataItem item = event.getDataItem();
String path = item.getUri().getPath();
if (path.equals(PATH_MAP)) {
DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
String dataValue = dataMap.getString(KEY_DATA);
}
}
}
}).setResultCallback(new ResultCallbacks<Status>() {
@Override
public void onSuccess(@NonNull Status status) {
Log.i(tag, "success");
}
@Override
public void onFailure(@NonNull Status status) {
Log.i(tag, "failure");
}
});
How to read the dataValue
associated to KEY_DATA
without using the listener pattern? I need this because sometimes the nodes are not synced even if urgent mode has been set. In particular the onDataChanged callback is called only if there was a change of values.
This is a scenario: a mobile apk has been installed, at startup it writes a value on KEY_DATA
item. After a wearable apk installation I set the listener to receive this value but the data changed event is not called, so I need a way to get the current value with an explicit polling.
I know that there is also MessageApi but the problem is the same, if a device is not online the message is lost.