I have written a Wear application for the Fossil Gen 6 smartwatch which extracts photoplethysmogram (PPG) data from an onboard sensor and updates the value on the screen continuously. Here is my onSensorChanged
method, which recognizes events from the PPG sensor and updates the TextView
object accordingly. I also attempt to write each datum to a CSV file using a FileWriter
object.
@Override
public void onSensorChanged(SensorEvent event) {
// On event from PPG sensor, update text on screen and
// write to CSV file.
if (event.sensor.getType() == 65572) {
sensorData = event.values[0];
String dataString = Float.toString(sensorData);
textView.setText(dataString);
try {
writer.write(dataString);
} catch (IOException e) {
e.printStackTrace();
}
}
}
The file writer is declared at the top of the MainActivity
:
private FileWriter writer;
And is defined in the onResume
method:
protected void onResume() {
super.onResume();
try {
writer = new FileWriter("data.csv",true);
} catch (IOException e) {
e.printStackTrace();
}
}
What I am not sure how to do is actually have this file available to write to, whether it should be stored on the watch or on the paired phone somehow, and how I can ultimately retrieve the CSV data on my PC for analysis in MATLAB, etc. (Is there a better way to be doing this?) Thanks!
I just want to start by saying that there are many ways to do this, and what's considered the "best" approach is highly dependent on the specifics to your use case (and sometimes personal preferences).
The most important thing to remember is that the watch has very limited resources (battery, memory, CPU) compared to a typical phone.
Here are a few things to keep in mind no matter what your exact solution will look like:
Writing to a file is an expensive operation. Doing this for every data point you receive from a sensor is not ideal. Consider saving your data to a database using Room instead. An easier alternative, as long as you are OK potentially losing some data if the app crashes or the device reboots unexpectedly, is to keep it in memory. This can be done using a basic array or some sort of Map (timestamp and value) depending on exactly what data you need. You can also use a hybrid of the two approaches and save data in batches.
Sending data between the watch and the phone is also expensive. I highly suggest storing things on the watch itself and transfer it to the phone only when needed. This can be a manual trigger, daily at a certain time, when the watch is charging, or some other clever strategy to minimize the impact on the watch's battery life.
You can send your data to the phone using the Wearable Data Layer API. You will have to set up either a MessageClient
or a ChannelClient
depending on how much data you need to transfer. If you stick to data structures that are supported by Android (as opposed to creating your own one), sending them over should be fairly straight forward.
If you need to process or format your data in order for it to be easily analyzed in MATLAB, consider doing this on the phone rather than the watch. Simpler operations on reasonably sized data sets can of course be performed directly on the watch.
Exactly how you decide to get the file to your computer is outside of the scope of what I'm trying to answer here. There are plenty of well-documented ways to do this. The easiest being to manually transfer it via adb. This can be done directly from the watch too, so you don't even have to send data to the phone.