I have tried multiple solutions but none seems to work completely. My current strategy is to keep a running count of the Comm transmit queue and to not let the queue exceed 3 Comm messages. I don't know if I'm not doing this correctly or if it's just not a good solution.
TLDR: I can't get Comm.transmit to work reliably
Here's some relevant code:
function onTimerUpdate() {
var info = Activity.getActivityInfo();
...
hrCur = (info.currentHeartRate != null)?info.currentHeartRate:0;
if(hrCur > 0){
...
var comString = hrCur;
if(myTxQueueCounter < 3){
comListener = new CardioTrackCommListener(method(:onTransmitComplete));
Comm.transmit(comString, null, comListener);
myTxQueueCounter++;
}
else{
myTxStatus = "Queue";
}
}
}
...
function onTransmitComplete(status) {
if (status == CardioTrackCommListener.SUCCESS) {
myTxQueueCounter--;
myTxStatus = "OK";
} else {
myTxQueueCounter++;
myTxStatus = "Fail";
}
comListener = null;
Ui.requestUpdate();
}
...
class CardioTrackCommListener extends Comm.ConnectionListener {
static var SUCCESS = 0;
static var FAILURE = 1;
hidden var mCallback;
//! Constructor
//! @param callback The method to call on a result
function initialize(callback) {
Comm.ConnectionListener.initialize();
mCallback = callback;
}
//! Call the callback with a result of CardioTrackCommListener.SUCCESS
function onComplete() {
mCallback.invoke(SUCCESS);
}
//! Call the callback with a result of CardioTrackCommListener.FAILURE
function onError() {
mCallback.invoke(FAILURE);
}
}