AntPlusListener events not firing

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;
	}
	
}

Top Replies

  • Thank you Kurev.  The bike power object is working though - I can read data from the sensor, it is just the callback that isn't firing.

    That's exactly it. To use BikePower APIs like…

All Replies

  • Actually this is working in sim but not on real device.  Any ideas why that may be?

    So onCalculatedPowerUpdate() is not called on actual device, everything else works? And you are able to read data using getCalculatedPower()?

    In general the code looks good to me now, so I don't see why callback wouldn't be called. Unless there is nothing to be updated, are you using the power meter while you are testing? I haven't really used these BikePower/Listener APIs, so I don't know what triggers them.

    I have only used callbacks from base DeviceListener class, and those I'm at least able to get. You could implement for example onMessage(msg) callback from base class, to see if you are receiving anything at all. Just println something there to see if it is called.

    About the simulator, I'm curious how you have managed to test it? How do you pair your ANT+ Power device with simulator, so that it is visible? I have ANT+ dongle, but that I can only use with direct ANT+ communication, but not via AntPlus API.

  • Yes, everything else works fine.  getCalculatedPower works no problem, so seems to be a problem with callback mechanism.  Adding an onMessage event handler fires fine, and i can see payload etc as expected, but still the onCalculatedPowerUpdate handler doesn't get called.  

    I am using SimulANT to simulate a bike power device.  It behaves as a real power meter through a separate ANT dongle.

    Actually I notice in simulator it is a bit intermittent whether onCalculatedPowerUpdate gets called or not.  Restarting my app sometimes it works, sometimes not.  I think for now I will use the onMessage event as trigger mechanism.  All I am trying to do is create a rolling average.

    For NAT+ sensors in simulator, in simulator, go to settings->Mange ANT+ settings, then wait for a device (must be on) to show up.  I dont find it very intuative, and it doesn't pick up heart rate monitor.  It picks up power and speed/cadence sensors though.

    Thanks again for the help!

  • For NAT+ sensors in simulator, in simulator, go to settings->Mange ANT+ settings, then wait for a device (must be on) to show up.  I dont find it very intuative, and it doesn't pick up heart rate monitor.  It picks up power and speed/cadence sensors though.

    Right, I've been doing this every now and then, opening the view and trying to get different sensors to show up. I never saw anything.

    However I just tried it, and one of my sensors actually showed up! This may have been first time I tried it with 3.2., looks like something has changed for me. Anyway now I'm happy that I can develop more on simulator Slight smile