synchronization - Android Wearable: how to get a current variable using DataApi? -
this event-based code
wearable.dataapi.addlistener(googleapiclient, new dataapi.datalistener() { @override public void ondatachanged(dataeventbuffer dataevents) { log.i(tag, "on data changed " + dataevents.tostring()); (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 read datavalue
associated key_data
without using listener pattern? need because nodes not synced if urgent mode has been set. in particular ondatachanged callback called if there change of values.
this scenario: mobile apk has been installed, @ startup writes value on key_data
item. after wearable apk installation set listener receive value data changed event not called, need way current value explicit polling.
i know there messageapi problem same, if device not online message lost.
you can retrieve previous values dataapi
using 1 of getdataitems
methods documented here: https://developers.google.com/android/reference/com/google/android/gms/wearable/dataapi.html
there various forms depending on filtering you'd api before returning data you. simplest form looks this:
wearable.dataapi.getdataitems(googleapiclient).setresultcallback(new resultcallback<dataitembuffer>() { @override public void onresult(@nonnull dataitembuffer buffer) { (dataitem item : buffer) { if (item.geturi().getpath().equals(path_map)) { datamap datamap = datamapitem.fromdataitem(item).getdatamap(); string datavalue = datamap.getstring(key_data); } } } });
and return dataitem
s app has sent api. can access key_data
value outlined above.
Comments
Post a Comment