[英]What is the best way to save and transfer smartwatch sensor data on Android Wear OS (i.e. as CSV)?
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.我为 Fossil Gen 6 智能手表编写了一个 Wear 应用程序,它从板载传感器中提取光电体积描记 (PPG) 数据并不断更新屏幕上的值。 Here is my
onSensorChanged
method, which recognizes events from the PPG sensor and updates the TextView
object accordingly.这是我的
onSensorChanged
方法,它识别来自 PPG 传感器的事件并相应地更新TextView
object。 I also attempt to write each datum to a CSV file using a FileWriter
object.我还尝试使用
FileWriter
object 将每个数据写入 CSV 文件。
@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
:文件编写器在
MainActivity
的顶部声明:
private FileWriter writer;
And is defined in the onResume
method:并在
onResume
方法中定义:
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!我不知道该怎么做实际上是可以写入这个文件,它是否应该以某种方式存储在手表上或配对的手机上,以及我如何最终在我的 PC 上检索 CSV 数据以在 MATLAB 中进行分析,等(有没有更好的方法来做到这一点?)谢谢!
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.最重要的是要记住,与典型的手机相比,手表的资源(电池、memory、CPU)非常有限。
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.
考虑使用 Room将数据保存到数据库。 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.
如果应用程序崩溃或设备意外重启,只要您确定可能会丢失一些数据,一个更简单的替代方法是将其保存在 memory 中。 This can be done using a basic array or some sort of Map (timestamp and value) depending on exactly what data you need.
这可以使用基本数组或某种 Map(时间戳和值)来完成,具体取决于您需要的数据。 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 .您可以使用可穿戴数据层 API将数据发送到手机。 You will have to set up either a
MessageClient
or a ChannelClient
depending on how much data you need to transfer.您必须根据需要传输的数据量来设置
MessageClient
或ChannelClient
。 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.如果您坚持使用 Android 支持的数据结构(而不是创建自己的数据结构),那么发送它们应该是相当简单的。
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.如果您需要处理或格式化您的数据以便在 MATLAB 中轻松分析,请考虑在手机而不是手表上执行此操作。 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.您决定如何将文件传输到您的计算机的确切方式不在我试图在这里回答的 scope 之外。 There are plenty of well-documented ways to do this.
有很多有据可查的方法来做到这一点。 The easiest being to manuallytransfer it via adb .
最简单的方法是通过 adb 手动传输它。 This can be done directly from the watch too, so you don't even have to send data to the phone.
这也可以直接在手表上完成,因此您甚至不必向手机发送数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.