I have spent a few days trying to share a dataItem between a wear device and a phone, but I have had no success. No matter what I do, the wearable only listens for its own dataItem. I mean, if I create a dataResquest inside the wear activity asking for a StringArray, the phone will receive it, I can change it on the phone service, but the wearable will still get the original StringArray when its onDataChanged is called.
I have even tried sending a message from the wearable to the phone, initiate a request from the phone, and have the wearable just listen for it, but it never receives anything if I do that.
This is what the WearListenerService currently looks like on the phone:
public class WearableService extends WearableListenerService {
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
super.onDataChanged(dataEvents);
GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.build();
mGoogleApiClient.connect();
for (DataEvent event : dataEvents) {
if (event.getType() == DataEvent.TYPE_CHANGED) {
DataItem item = event.getDataItem();
item.freeze();
DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
dataMap.putStringArray(Constants.SET_LIST, new String[]{"table 14", "table 27", "table 12323"});
PutDataRequest putDataReq = PutDataRequest.createFromDataItem(item).setUrgent();
Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq);
return;
}
}
}
}
And this is what part of my wearable's activity looks like:
public class MainActivity extends Activity implements
DataApi.DataListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient mGoogleApiClient;
private String[] set_list = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.empty_database);
// Listen for data item events
// http://developer.android.com/training/wearables/data-layer/data-items.html
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
Wearable.DataApi.addListener(mGoogleApiClient, this);
// Send request to phone asking for set list
PutDataMapRequest putDataMapReq = PutDataMapRequest.create(Constants.SET_LIST);
putDataMapReq.getDataMap().putLong("time", new Date().getTime());
putDataMapReq.getDataMap().putStringArray(Constants.SET_LIST, set_list);
PutDataRequest putDataReq = putDataMapReq.asPutDataRequest().setUrgent();
Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq);
}
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
for (DataEvent event : dataEvents) {
if (event.getType() == DataEvent.TYPE_CHANGED) {
// DataItem changed
DataItem item = event.getDataItem();
DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
set_list = dataMap.getStringArray("/new_string");
if (set_list != null) {
createList();
return;
}
}
}
}
Does anyone know what could be causing this? Thank you.