My Goal
I am creating an app/package where the wear and handheld are able to both make changes on the Data Layer and be notified of such changes. I am using the GoogleApiClient and the DataApi.DataListener interface.
Current Progress
As of right now, an action on my Wearable successfully updates the counts in the Data Layer, and this change is then detected and reflected in the Handheld via new counts being displayed.
Problem
The detection only works one way. While a change initiated by the Wearable successfully updates counts on the Handheld, a change initiated by the Handheld is not detected on the Wearable.
What I Have Tried
I've looked on StackExchange and Google for hours and I haven't found cases where both the Wearable and the Handheld have been updating data layer objects. In most cases, it was only the Handheld updating something and the Wearable detecting, or vice-versa. One-way communication works just fine for me. I'm needing two-way.
Snippet of Handheld Code
public class ListenActivity extends Activity implements
DataApi.DataListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
// ConnectionCallback methods, UI methods not included in snippet
private static final String COUNT_KEY = "com.example.count";
private int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listen);
handler.post(displayCounts);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
@Override
public void onDataChanged(DataEventBuffer dataEvents)
{
for (DataEvent event : dataEvents)
{
if (event.getType() == DataEvent.TYPE_CHANGED)
{
DataItem item = event.getDataItem();
if (item.getUri().getPath().compareTo("/count") == 0)
{
DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
count = dataMap.getInt(COUNT_KEY);
}
}
}
}
// A UI button calls this method to reset the count, but the Wearable isn't calling its onDataChanged() method.
public void resetCount(View view)
{
PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/count");
putDataMapReq.getDataMap().putInt(COUNT_KEY, 0);
PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
PendingResult<DataApi.DataItemResult> pendingResult =
Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq);
} }
Snippet of Wearable Code
public class WearListenActivity extends Activity implements
DataApi.DataListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
// ConnectionCallback methods, UI methods not included in snippet
private static final String COUNT_KEY = "com.example.count";
private int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDismissOverlay = (DismissOverlayView) findViewById(R.id.dismiss_overlay);
mDismissOverlay.showIntroIfNecessary();
mGestureDetector = new GestureDetectorCompat(this, new GestureListener());
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApiIfAvailable(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
// This method is never called on the Wearable.
// It isn't detecting the changes made by Handheld's resetCount() method?
@Override
public void onDataChanged(DataEventBuffer dataEvents)
{
for (DataEvent event : dataEvents)
{
if (event.getType() == DataEvent.TYPE_CHANGED)
{
DataItem item = event.getDataItem();
if (item.getUri().getPath().compareTo("/count") == 0)
{
DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
count = dataMap.getInt(COUNT_KEY);
}
}
}
}
// User taps the screen, this method is called. Works perfectly fine since handheld updates its counts.
private void updateDataLayer()
{
PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/count");
putDataMapReq.getDataMap().putInt(COUNT_KEY, count);
PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
PendingResult<DataApi.DataItemResult> pendingResult =
Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq);
} }
That may be because you are listening to events in Activity. I'm using WearableListenerService and it works for in in both ways.
You can find an example here: Handling Data Layer Events