I have a list of 10 objects that I need to send from the handheld to the wearable. The sending works perfect for all of them, but onDataChanged() only gets triggered 2 times and it seems like the first one is random. So i recreated the exact same problem with the same results:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = findViewById(R.id.button);
mDataClient = Wearable.getDataClient(this);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i = 0; i < 10; i++) {
//own mock up class with string id of i to differ them
Model model = new Model(String.valueOf(i)
, "title"
, new Date().getTime());
sendDataToWear(model);
}
}
});
}
private void sendDataToWear(Model model) {
PutDataMapRequest putDataMapRequest = PutDataMapRequest.create("/my_path");
putDataMapRequest.getDataMap().putString("id", model.getId());
putDataMapRequest.getDataMap().putString("title", model.getTitle());
putDataMapRequest.getDataMap().putLong("timestamp", model.getTimeStamp());
PutDataRequest putDataRequest = putDataMapRequest.asPutDataRequest();
Task<DataItem> putDataTask = mDataClient.putDataItem(putDataRequest);
putDataTask.addOnSuccessListener(new OnSuccessListener<DataItem>() {
@Override
public void onSuccess(DataItem dataItem) {
Log.d(TAG, "onSuccess: dataitem");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "onFailure: dataitem " + e.getMessage());
}
});
}
The onSuccess logs are called 10 times with the correct payload. The wearable activity with implemented OnDataChangeListener:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Wearable.getDataClient(this).addListener(this);
}
@Override
public void onDataChanged(@NonNull DataEventBuffer dataEventBuffer) {
for(DataEvent event : dataEventBuffer) {
if(event.getType() == DataEvent.TYPE_CHANGED) {
DataItem item = event.getDataItem();
if(item.getUri().getPath().compareTo("/my_path") == 0) {
DataMap map = DataMapItem.fromDataItem(item).getDataMap();
Log.d(TAG, "onDataChanged: name: " + map.getString("id"));
}
}
}
}
onDataChanged logs are called 2 times with the first one always different and the second one always 9. I have no idea whats going on. I also tested it with 1 second wait time between the putDataItem() calls and it works as intended. The onDataChanged method is called 10 times and the 10 payloads are transfferred. But i cant wait one second between each call. Does somebody know whats going on here?
All your requests are being created with the same path, so later requests overwrite the earlier - and if this happens before the earlier requests have been sent to the phone (as is likely), it'll only receive the last one.
To send different items, use different paths in your PutDataMapRequest.create()
calls. Preferably, create a path that sensibly differentiates each item (such as one that ends in a unique ID, like "/my_path/[item_id]").