I am working on an developing an application for Mobile and Wearable to get sensor data at the same time from both mobile and handheld. When I press 'start collecting data' it sends a message to the Wearable to start the Sensor Service and starts collecting data, it also start collecting sensor data simultaneously from mobile sensors. Similarly, when I press the 'Stop' it stops collecting data. I am sending every value of Wearable sensor data back to mobile using DataItem to be saved later on in the mobile storage.
public void onSensorChanged(SensorEvent event) {
int sensorType = event.sensor.getType();
if (sensorType == Sensor.TYPE_ACCELEROMETER) {
float[] values = event.values;
Log.d(TAG, "onSensorChanged: Changed" );
sendSensorData (values);
}
}
private void sendSensorData (float[] values) {
PutDataMapRequest putDataMapRequest= PutDataMapRequest.create(NEW_VALUE);
putDataMapRequest.getDataMap().putFloatArray(KEY, values);
putDataMapRequest.getDataMap().putLong("Time", System.currentTimeMillis());
PutDataRequest putDataRequest= putDataMapRequest.asPutDataRequest().setUrgent();
Task<DataItem> dataItemTask = Wearable.getDataClient(this).putDataItem(putDataRequest);
dataItemTask.addOnSuccessListener(new OnSuccessListener<DataItem>() {
@Override
public void onSuccess(DataItem dataItem) {
Log.d(TAG, "onSuccess: "+ dataItem);
}
});
}
I am using the onDataChanged in Mobile package to listen to data changes from Wearable. I am using System.currentTimeMillis() in Wearable package to ensure a continuous stream of sensor data back to mobile.
This is the code on the receiving side i.e. the handheld.
public void onDataChanged(DataEventBuffer dataEventBuffer) {
for(DataEvent event: dataEventBuffer) {
if (event.getType() == DataEvent.TYPE_CHANGED) {
DataItem dataItem= event.getDataItem();
Uri uri = dataItem.getUri();
String path = uri.getPath();
if(path.equals(NEW_VALUE)) {
DataMap dataMap = DataMapItem.fromDataItem(dataItem).getDataMap();
getSensorData (dataMap);
//Log.d(TAG, "onDataChanged: "+ dataMap);
}
}
}
super.onDataChanged(dataEventBuffer);
}
The problem is that when I stop collecting data and compare both data records, wearable data records received by the mobile are very less in number which I understand is due to communication time between mobile and sensor and which causes the delay. I do understand the data records will not be exactly similar in number in any ideal scenario. Is there any way that I can minimize the delay of the data between the two devices sensor data?
The problem you are facing is because the sample rate of the sensors inside your phone is different than the sample rate of the wearable device. For instance the accelerometer on your phone may run at 400 Hz, and the one on your wearable at only 100 Hz or less.
A while ago I compiled a small list showing the maximum sensor sample rate of smartphones. As you can see almost every model has a different sample rate:
https://docs.google.com/spreadsheets/d/1vZEryeslHOq-pl_C-scoS4us21goxg8oyqviUgDGq2k/edit?usp=sharing