enableLocationEvents doesn't invoke callback method

So I am working on a class featuring the Position module. However, the callback method for enableLocationEvents doesn't get invoked. Any idea why?

import Toybox.Position;
using Toybox.System;
using Toybox.Time.Gregorian;
using Toybox.Lang;


class GPS{
    var posInfo;

    function initialize(){
        System.println("Initialize is called");
    }

    function startPositioning() {
        System.println("StartPositioning is called");
        Position.enableLocationEvents(Position.LOCATION_CONTINUOUS, method(:onPosition));
        System.println("enableLocationEvents is called");
    }
    
    function onPosition(info) {
        System.println("onPosition is called" + info);
        posInfo = info;
    }
}

I instantiate this class in the main App file in the Onstart() function to test the code:

    function onStart(state as Dictionary?) as Void {
        gps.startPositioning();
    }

The result I get in the consol is:


Initialize is called
StartPositioning is called
enableLocationEvents is called

Meaning that onPosition() callback method is never invoked. I would greatly appreciate it if someone could help me.


NOTE: I haven't tested it on an actual watch I am running the simulator on a vivoactive 4 and API version 3.3.6

  • How are you feeding GPS data to your app in the sim?  Playing back your own fit file or using Data Simulation? If you are playing back your own fit file, does it have GPS data?

    You do need to start Simulation>Activity Data for your app to get GPS data.

  • I didn’t realise that I have to feed it data. I assumed the function enableLocationEvents generates the GPS data and passes it to the callback method or am I wrong?

  • Yes, you need to use Simulation>Activity Data and then start either data simulation or start paying back a fit file with GPS data.

  • It worked, Thank you very much. I am kinda confused though. It says in the API docs that enableLocationEvents "requests a location event". On the same page, there is also a function called getInfo(). To my understanding, I thought enableLocationEvents gives me the data I need as an object of the 'info' subclass in the callback method. Wouldn't that be the same thing as what getInfo() does?

  • Look at it this way 

    enableLocationEvents is how the GPS receiver gets turned on - that's exactly the case on a real device.  If you don't call it, GPS is off on a real device

    The callback is then called when there is new GPS info "Received".  The "info" parameter. is exactly what you get if you call

    Position.getInfo()

    Position.getInfo() is there and you can use it to get the last GPS data from a device, even if you don't turn on the GPS receiver with enableLocationEvents(), or you've turned off the receiver.

  • Okay thank you very much!