Garmin Edge sensor data in data frame

I have been trying to create a data field that calculates the DFA Alpha 1 variable based on beat-to-beat intervals. And I have achieved this by using the ANT+ lib by looking at the other posts on the forum. However, this requires that the user disconnects their heart rate monitor, which is not desirable. That is why I want to access the data using the Sensor lib. But again, I have problems using the API in a data field. And I have seen that this is only possible to do in a background process on an edge device? But again I have tried, but I still get either "Encountered an app crash." or that I am not allowed to access the API in a data field.

The app is implemented as a widget for now, but it would be better if the data were shown in a data field. So any ideas if it is possible?

  • Hard to sat why you're seeing the crash. VS Code or Eclipse?

    And you have your background code annotated with (:background), correct?

  • I am running in VS Code, and I have added some (:background), but it is possible that I have added it incorrectly. Basically started adding it everywhere and tried to extend a ServiceDelegate, but again not sure if I registered it correctly

  • Try in eclipse, or put some prinln calls in your background.

  • Added some prints and it crashes on the line with "Sensor.registerSensorDataListener"

    var options = {
    :period => 1,
    :heartBeatIntervals => {
    :enabled => true
    }
    };
    Toybox.System.println("Register Sensor Listener");
    Sensor.registerSensorDataListener(method(:onSessionSensorData), options);
    Toybox.System.println("Register Sensor Listener Done"); // Never reached
  • I don't think you can register a sensorDataListener in a background,  As the background only runs for 30 seconds at most.  Have you tried putting a try/catch to see the exception?

  • That is probably the case, as I get the following error.

    Error: Symbol Not Found Error
    Details: Could not find symbol 'registerSensorDataListener'
    But is it then not possible to get the beat-to-beat data if using a data field?
  • In the 3.2.1 change log:

    • Allow data fields access to read-only Sensor module APIs.

    Not sure, but on 3.2.x devices, you may be able to do in right in the DF and not use a background service.  You'd want to have the logic in compute() so that it only happens once if the DF is used multiple time in the same activity.

  • Interesting! However, it seems this is only the Sensor.Info object, while I want the Sensor.HeartRateData object. And if I understand it correctly can this only be accessed through Sensor.registerSensorDataListener()?

    Btw, was able to register with Sensor.enableSensorEvents() in a background process, but not the other.

    I also noticed that I needed to quit the simulator each time I restarted the data field. Otherwise, would it just crash on launch.

  • The sim crashes or you app crashes?

  • Think it is the sim, as I have only experienced it when trying to implement a background process. And the app launches without any problems when restarting the sim