In my Watch App i am trying to get an event to fire every time an AntPlus device data is updated. Specifically every time power is updated from a power meter.
To do this I am trying to use BikePowerListener.
if I copy the code from the example in the documentation:
https://developer.garmin.com/connect-iq/api-docs/Toybox/AntPlus/BikePowerListener.html
And remove the ListItem references as I can't see how to use them, everything initializes fine, but the OnCalculatedPowerUpdate event never fires. Elsewhere in code I am acessing the data using the "getBikePowerData" function below, so the device is connected and working. Why isn't the callback firing?
Here is my class:
using Toybox.AntPlus;
using Toybox.System as Sys;
class antplus_bpwr extends AntPlus.BikePowerListener
{
hidden var _antplus_bpwr;
hidden var _antplus_listener;
hidden var _sample_count;
hidden var _samples;
hidden var _head;
hidden var _max_samples_count = 12; // 3 seconds at 4Hz..
function initialize()
{
// Initialise BSC device:
BikePowerListener.initialize();
Sys.println("BWPR Initialized.");
_antplus_bpwr = new AntPlus.BikePower(null);
_sample_count = 0;
_head = 0;
_samples = new [_max_samples_count];
}
// Class to store data. Currently just power but could be other stuff too.
// Keep like this for consistency.
class bpwr_data
{
var power;
function initialize()
{
power = 0;
}
}
// Function to return bike power data object.
function getBikePowerData()
{
var ret = new bpwr_data();
var info = _antplus_bpwr.getCalculatedPower();
if (info!=null)
{
if (info.power > 0)
{
ret.power = info.power;
}
}
return ret;
}
// Returns device number.
function getDeviceNumber()
{
return _antplus_bpwr.getDeviceState().deviceNumber;
}
// Callback on power update.
function onCalculatedPowerUpdate(data)
{
Sys.println("Updating power");
_samples[_head] = data.power;
if ((_sample_count + 1) < _max_samples_count)
{
_sample_count++;
}
_head++;
if (_head == _max_samples_count)
{
_head = 0;
}
}
// get average power.
function getAvgPower()
{
var ret = 0;
if (_sample_count == 0) {return ret;}
for (var i=0;i<_sample_count;i++)
{
ret+=_samples[i];
}
ret/=_sample_count;
return ret;
}
}