Hello,
I am trying to develop an app to record a session and save heart rate data in fit files. Here is the code:
import Toybox.Graphics;
import Toybox.WatchUi;
import Toybox.Sensor;
import Toybox.System;
import Toybox.ActivityRecording;
class RawloggerView extends WatchUi.View {
private var heartRate = null;
private var session = null;
function initialize() {
View.initialize();
Sensor.setEnabledSensors([Sensor.SENSOR_HEARTRATE]);
Sensor.enableSensorEvents(method(:onSensor));
session = createSession();
session.start();
}
function createSession() {
var session = ActivityRecording.createSession({
:name => "Rawlogger",
:sport => Activity.SPORT_GENERIC,
:subSport => Activity.SUB_SPORT_GENERIC
});
return session;
}
function onLayout(dc as Dc) as Void {
setLayout(Rez.Layouts.MainLayout(dc));
}
function onShow() as Void {
Sensor.enableSensorEvents(method(:onSensor));
}
function onUpdate(dc as Dc) as Void {
View.onUpdate(dc);
dc.setColor(Graphics.COLOR_WHITE, Graphics.COLOR_BLACK);
// Display the heart rate
if (heartRate != null) {
dc.drawText(10, 10, Graphics.FONT_SMALL, "Heart Rate: " + heartRate, Graphics.TEXT_JUSTIFY_LEFT);
} else {
dc.drawText(10, 10, Graphics.FONT_SMALL, "Heart Rate: N/A", Graphics.TEXT_JUSTIFY_LEFT);
}
}
function onSensor(sensorInfo as Toybox.Sensor.Info) as Void {
System.println("Sensor callback triggered.");
if (sensorInfo != null) {
System.println("Sensor info is not null.");
// Check for heart rate data
if (sensorInfo.heartRate != null) {
heartRate = sensorInfo.heartRate;
System.println("Heart Rate: " + heartRate);
// Here, you might want to log or process the heart rate data as needed
} else {
System.println("Heart rate data is null.");
}
} else {
System.println("Sensor info is null.");
}
// Trigger the screen update
WatchUi.requestUpdate();
}
function onHide() as Void {
Sensor.unregisterSensorDataListener();
session.stop();
var saved = session.save();
if (saved) {
System.println("Session successfully saved.");
} else {
System.println("Failed to save the session.");
}
}
}
And the corresponding fit files:
Decoding the fit file with FitToCSV I don't see the heart rate value. But I can see the value on my console. What might be issue here?
