How to troubleshoot this error after trying to make a BackGroud Service ? Error: Illegal Access (Out of Bounds) Details: Failed invoking <symbol> Stack: - initialize() at .......chapp1App.mc:27 0x10000026

On the file from App I Have:

(:background)
class watchapp1App extends Application.AppBase {
     hidden var scanner;
     hidden var model;
     private var _view;

function initialize() {
     AppBase.initialize();

     model = new ResultsModel(); <---- this is the line that the error present, no issue before try to join a background on this project
     scanner = new Scanner(model);

function getInitialView() {
     //register for temporal events if they are supported
     if(Toybox.System has :ServiceDelegate) {
          canDoBG=true;
          Background.registerForTemporalEvent(new Time.Duration(5 * 60));
          System.println("Registo Efectuado BackGround - TRUE");
     } else {
          System.println("****background not available on this device****");
     }
     _view = new watchapp1View(model);
     return [ _view, new KeyHandler(scanner) ];
}

function getServiceDelegate() as ServiceDelegate{
     return new BackGServiceDelegate();
}

On my Background file:

(:background)
class BackGServiceDelegate extends Toybox.System.ServiceDelegate {

     function initialize() {
          Sys.ServiceDelegate.initialize();
          //inBackground=true; //trick for onExit()
          Sys.println("inBackGround: TRUE");
     }
function onTemporalEvent() {
     System.println("TTTTEEEEMMMPPPOOORRRAAALLLLLLL");
     Background.exit(null);
}
}

Im using the CIQ, i receive the error message every 5 minutes ( but no movement on onTemporalEvent())

With the same code I manage to start a new App and runs just fine (the temporal event).

I'm noob on this, can anyone help me ? thanks in advance

  • Understand that initialize() in your appBase runs for the main app, but also runs each time the background runs, so,

    model = new ResultsModel();

    is run in the background every 5 minutes. (thus the error every 5 minutes.  Without seeing the code for ResultsModel, I'm thinking you are trying to use resources that aren't scoped for the background.  See https://developer.garmin.com/connect-iq/core-topics/resources/#resources and Resource Scopes

    Move that to getInitalView(), as that only runs in the main app.  Or move it to initialize() in your view class if it's not needed in the Background..

  • im lost....

    My Results model file:

    using Toybox.System;

    class ResultsModel {
    hidden var data;
    hidden var scanning;
    hidden var listener;

    function initialize() {
    self.data = new [0];
    }

    function reset() {
    self.data = new [0];
    }

    function size() {
    return self.data.size();
    }

    function get(i) {
    return data[i];
    }

    function add(result) {
    var contains = false;
    for (var i = 0; i < data.size(); i++) {
    if (data[i].isSameDevice(result)) {
    contains = true;
    break;
    }
    }
    if (!contains) {
    self.data.add(result);
    }
    }

    function setListener(listener) {
    self.listener = listener;
    }

    function setScanning(scanning) {
    self.scanning = scanning;
    self.listener.onModelChange();
    }
    function isScanning() {
    return self.scanning;
    }
    }

    My Scanner file that receivs (model):

    using Toybox.Timer;

    class Scanner extends Toybox.BluetoothLowEnergy.BleDelegate {
    hidden var running;
    hidden var model;
    /**
    * All Bluetooth SIG defined UUIDs use a common base UUID, and more specifically the following one:
    0x0000xxxx-0000-1000-8000-00805F9B34FB

    www.oreilly.com/.../ch04.html
    The UUID for any CCCD is always the standard 16-bit UUIDCCCD (0x2902).

    www.davidgyoungtech.com/.../hacking-with-contact-tracing-beacons

    btprodspecificationrefs.blob.core.windows.net/.../16-bit UUID Numbers Document.pdf
    */

    var profile = {
    :uuid => BluetoothLowEnergy.stringToUuid("000018fe-1212-efde-1523-785feabcd123"),
    :characteristics => [ {
    :uuid => BluetoothLowEnergy.cccdUuid()} ]
    };
    function initialize(model) {
    BleDelegate.initialize();
    BluetoothLowEnergy.setDelegate(self);
    BluetoothLowEnergy.registerProfile(profile);
    running = false;
    self.model = model;
    }

    function onScanResults(scanResults) {
    //System.println("Results received");
    var i = 0;
    for (var item = scanResults.next(); item != null; item = scanResults.next()) {
    model.add(item);
    i++;
    }
    }

    function onScanStateChange(scanState, status) {
    }

    function start() {
    //for(var i=0;i<10;i++){
    //System.println("start");
    model.reset();
    BluetoothLowEnergy.setScanState(BluetoothLowEnergy.SCAN_STATE_SCANNING);
    running = true;
    model.setScanning(true);
    var myTimer = new Timer.Timer();
    myTimer.start(method(:stop), 5000, false);

    //}
    }

    function stop() {
    //System.println("stop");
    model.setScanning(false);
    BluetoothLowEnergy.setScanState(BluetoothLowEnergy.SCAN_STATE_OFF);
    running = false;
    }

    function isRunning() {
    return running;
    }
    }

  • Another one of my examples that shows using BLE in the background service for a watch face.  It might help:

    https://forums.garmin.com/developer/connect-iq/f/discussion/8182/a-very-simple-wf-with-a-background-that-talks-ble-to-the-thingy52

  • Thanks for the try, but no help.

    Im running BLE scanning every 30 seconds, not possible to put all in background....

    And im kine of noob on this...

    Previous i have try the app that you mension....

    in my view class i need the "model"....


    function initialize(model) {
    View.initialize();
    self.model = model;

    sorry for the noob questions.... thanks

    Hi Wizard Jim

    After taking out all the notation (:background), seams to work.... strange and good :)

    What are the cost of it in the consumption of the my vivoactive 4...

  • Hi Wizard Jim

    After taking out all the notation (:background), seams to work.... strange and good :)

    What are the cost of it in the consumption of the my vivoactive 4...

    I receive a message when building the program Slight smile

    BUILD: WARNING: The background permission was enabled but no source code was annotated. The entire application will be loaded as a background process. (background application)
    BUILD: Complete

    :)

  • Background services have less memory than the main app.  Turn off the background permission and see if it still works (without the warning)

  • The Warning not boder me.

    But after delete line: "using Toybox.Background;", supose that's you saying "turn off the backg..."

    after that receive the error:

    Error: Permission Required
    Details: Permission for module 'Toybox.Background' required

    as expected i think

  • It's hard to say as you've only posted parts of your code,  And the way you have it now, your app can be at most 60k on a va4

  • Hi Jim, what you mean on a "va4" ?

    The App that i am making, receive BLE signs, after some date collected (one minute), I "saved" on disk, after on background send it to one aPI trought HTTPrequest... it seams OK.... let's see when i put more BLE sensores (at this moment i only have 2, but i will need 8....

    In 5 minutes it is supose that i receive 200 blocks of data (the name of the BLE, the RSSI and the date)

    Thanks for the help

  • va4 = vivoactive 4.

    Just a shorthand for your device

    I'm not sure I understand, but with CIQ BLE, you will likely have problems with more than 3 sensors.