Hi Developers,
I’m currently working on a project that requires access to both accelerometer and gyroscope data. To get started, I’ve been testing the sample code provided on your developer site (see https://developer.garmin.com/connect-iq/core-topics/sensors/).
However, I’m encountering an issue: I’m not seeing any output for the raw x, y, and z-axis samples from the accelerometer. Despite multiple attempts, the accelHistoryCallback doesn’t seem to be triggered, and no sensor data is printed to the console.
Below is a simplified version of the code I’m using for testing. I would greatly appreciate any insights or guidance on what might be going wrong, as I’ve been troubleshooting this for over a week without success.
Thank you for your time and patience!
import Toybox.Lang;
import Toybox.System;
import Toybox.WatchUi;
import Toybox.Graphics;
import Toybox.Timer;
import Toybox.Sensor;
import Toybox.Application;
import Toybox.Activity;
import Toybox.ActivityRecording;
import Toybox.FitContributor;
class sDelegate extends WatchUi.BehaviorDelegate {
var _accele;
function initialize() {
BehaviorDelegate.initialize();
}
function onSelect() as Boolean {
_accele = new MyAccelHistoryClass();
_accele.enableAccel();
//_accele.disableAccel();
return true;
}
}
class MyAccelHistoryClass
{
private var _samplesX = null;
private var _samplesY = null;
private var _samplesZ = null;
// Initializes the view and registers for accelerometer data
public function enableAccel() as Void {
//var maxSampleRate = Sensor.getMaxSampleRate();
System.println("enableAccel");
// initialize accelerometer to request the maximum amount of data possible
var options = {:period => 4, :sampleRate => 5, :enableAccelerometer => true};
try {
Sensor.registerSensorDataListener(method(:accelHistoryCallback), options);
System.println("Try to register");
}
catch(e) {
System.println("Error: "+e.getErrorMessage());
}
}
// Prints acclerometer data that is recevied from the system
public function accelHistoryCallback(sensorData as Sensor.SensorData) as Void {
_samplesX = sensorData.accelerometerData.x;
_samplesY = sensorData.accelerometerData.y;
_samplesZ = sensorData.accelerometerData.z;
System.println("Raw samples, X axis: " + _samplesX);
System.println("Raw samples, Y axis: " + _samplesY);
System.println("Raw samples, Z axis: " + _samplesZ);
}
public function disableAccel() as Void {
Sensor.unregisterSensorDataListener();
System.println("disableAccel");
}
}