Connect iQ: общение внутри фоновой службы в поле данных
Я создаю поле данных Connect iQ для отображения данных, полученных из мобильного приложения Garmin Connect. Я создал фоновый сервис, в котором я хочу выполнять все свои коммуникации внутри, потому что иначе он не будет работать в поле данных. Я столкнулся с проблемой при инициализации прослушивателя подключений, и я получил сообщение "Неудачный вызов", хотя я включил игрушечный ящик. Ошибка выскакивает в функции onTemporalEvent, как отмечено в коде ниже.
datafield_delegate.mc:
using Toybox.WatchUi as Ui;
using Toybox.System as Sys;
using Toybox.Communications as Comm;
class CommListener extends Comm.ConnectionListener {
function initialize() {
Sys.println("Communications initialized.");
ConnectionListener.initialize();
}
function onComplete() {
Sys.println( "Transmit Complete" );
}
function onError() {
Sys.println( "Transmit Failed" );
}
}
class BgbgServiceDelegate extends Toybox.System.ServiceDelegate {
function initialize(){
ServiceDelegate.initialize();
Sys.println("BgbgServiceDelegate initialized.");
}
function onTemporalEvent() {
Sys.println("onTemporalEvent");
//--------Open connection and store the data received-----------------
//This is where the error message occurs:
var listener = new CommListener();
//--------------------------------------------------------------------
Sys.println("onTemporalEvent: Exiting event.");
//Return data to the main process. This is caught by onBackGroundData:
Background.exit("test");
}
}
datafieldView.mc:
using Toybox.WatchUi as Ui;
using Toybox.Application as App;
using Toybox.Background;
using Toybox.System as Sys;
using Toybox.Communications as Comm;
class glucose_datafieldView extends Ui.SimpleDataField {
// Set the label of the data field here.
function initialize() {
Sys.println("label initialized.");
SimpleDataField.initialize();
label = "Blood glucose";
}
// The given info object contains all the current workout
// information. Calculate a value and return it in this method.
// Note that compute() and onUpdate() are asynchronous, and there is no
// guarantee that compute() will be called before onUpdate().
function compute(info) {
// See Activity.Info in the documentation for available information.
return 0.0;
}
}
datafieldApp.mc:
using Toybox.Application as App;
using Toybox.Background;
using Toybox.System as Sys;
using Toybox.WatchUi as Ui;
using Toybox.Communications as Comm;
//Counter for testing:
var counter = 0;
//Our background sensor glucose value:
var sgvBGData = "none";
//We must check if the system can do backgrounding:
var canDoBG = false;
//Keys to the object store data:
var OSCOUNTER = "oscounter";
var OSDATA = "osdata";
(:background)
class glucose_datafieldApp extends App.AppBase {
function initialize() {
AppBase.initialize();
Sys.println("initialize");
}
// onStart() is called on application start up
function onStart(state) {
}
// onStop() is called when your application is exiting
function onStop(state) {
}
// Return the initial view of your application here
function getInitialView() {
//Register for temporal events if they are supported:
if(Toybox.System has :ServiceDelegate) {
canDoBG=true;
Background.registerForTemporalEvent(new Time.Duration(5 * 60));
Sys.println("System has bgs support. Registered for events every 5 minutes.");
} else {
Sys.println("****background not available on this device****");
}
return [ new glucose_datafieldView() ];
}
function onBackgroundData(data){
sgvBGData=data;
Sys.println("onBackgroundData: sgvBGData received. Data = " + data);
App.getApp().setProperty(OSDATA,sgvBGData);
Ui.requestUpdate();
}
function getServiceDelegate(){
Sys.println("getServiceDelegate");
return [new BgbgServiceDelegate()];
}
}
Любая помощь с благодарностью.
Спасибо!