This is a typical case of "it used to work but now it doesn't" and I cannot figure out why. I have modified the TemeAppView example found in the Wearable Programming for the Active Lifes PDF so that it reads the heart rate from my HRM-tri strap. So I created this extremely simple application and I used to be able to get the heart rate (the application was not finished just the first stages trying to figure out ANT programming). However, for no reason that I can explain the onMessageReceived listener is no longer called so the application is not working any more. Can anybody help me or point me to the correct direction? For some reason this code never gets into onMessageReceived().
using Toybox.Ant as Ant;
using Toybox.System as System;
using Toybox.Time as Time;
class TempeSensor extends Ant.GenericChannel
{
const DEVICE_TYPE = 120; // HRM is 120, temp sensor is 25; // The ANT+ device type
const PERIOD = 8070; // 65535; // How often we expect new data
hidden var chanAssign; // The channel assigned by the radio
var data; // Stores the data received from the
// sensor
var searching; // Whether we're currently searching for the sensor
var deviceCfg; // Device configuration details
// This flag indicates we've obtained enough
// data to read the temperature
var tempDataAvailable = false;
class TempeData
{
var currentTemp;
function initialize()
{
currentTemp = 35;
}
}
class TempeDataPage
{
static const PAGE_NUMBER = 1;
function parse(payload, data)
{
// The payload (what we received from the sensor) has
// a few data points in it. We're just interested in
// the current temperature.
System.println("payload");
data.currentTemp = parseCurrentTemp(payload);
} // parse
hidden function parseCurrentTemp(payload)
{
System.println("parseCurrentTemp");
var currentTemp = payload[7];
System.println(currentTemp);
return currentTemp;
}
} // end class TempeDataPage
function initialize()
{
// Get the channel
chanAssign = new Ant.ChannelAssignment(CHANNEL_TYPE_RX_ONLY,Ant.NETWORK_PLUS);
var x = GenericChannel.initialize(method(:onMessageReceived), chanAssign);
// Set the configuration
deviceCfg = new Ant.DeviceConfig( {
:deviceNumber => 0, // Wildcard (any matching device)
:deviceType => DEVICE_TYPE,
:transmissionType => 0,
:messagePeriod => PERIOD,
:radioFrequency => 57, // ANT+ Frequency
:searchTimeoutLowPriority => 10, // Timeout in 25s
:searchTimeoutHighPriority => 2, // Timeout in 5s
:searchThreshold => 0//,
//:networkKey64Bit => ["0xB9","0xA5","0x21","0xFB","0xBD","0x72","0xC3","0x45"],
} ); // Pair w/all sensors
GenericChannel.setDeviceConfig(deviceCfg);
data = new TempeData();
searching = true;
System.println("init");
}
function open()
{
// Open the channel
GenericChannel.open();
data = new TempeData();
searching = true;
System.println("open");
}
function closeSensor()
{
System.println("close");
GenericChannel.close();
}
function onMessageReceived(msg)
{
// see: forums.garmin.com/.../150229-background-scanning-for-ant-devices
System.println("device number: " + msg.deviceNumber + " device type: " + msg.deviceType);
// Parse the payload
var payload = msg.getPayload();
if( Ant.MSG_ID_BROADCAST_DATA == msg.messageId )
{
System.println(payload);
System.println(msg.messageID);
var dp = new TempeDataPage();
dp.parse(msg.getPayload(),data);
tempDataAvailable = true;
if( TempeDataPage.PAGE_NUMBER == (payload[0].toNumber() & 0xFF) )
{
// Were we searching?
if(searching)
{
searching = false;
// Update our device configuration primarily to see the
// device number of the sensor we paired to
deviceCfg = GenericChannel.getDeviceConfig();
}
//var dp = new TempeDataPage();
//dp.parse(msg.getPayload(), data);
//tempDataAvailable = true;
}
} // end broadcast data
else if( Ant.MSG_ID_CHANNEL_RESPONSE_EVENT == msg.messageId )
{
if( Ant.MSG_ID_RF_EVENT == (payload[0] & 0xFF) )
{
if( Ant.MSG_CODE_EVENT_CHANNEL_CLOSED == (payload[1] & 0xFF) )
{
System.println("closed, so try to open");
open();
}
else if( Ant.MSG_CODE_EVENT_RX_FAIL_GO_TO_SEARCH == (payload[1] & 0xFF) )
{
System.println("Searching");
searching = true;
}
}
else
{
//It is a channel response.
}
} // end channel response event
} // end on message
}