Under Review
over 1 year ago

WERETECH-13396

Position.enableLocations documentation has a bug in code example (causes device to crash)

(See: https://forums.garmin.com/developer/connect-iq/f/discussion/319709/enable-location-bug-after-24-10-update)

The documentation at the following URL has a bug which causes a crash:

[https://developer.garmin.com/connect-iq/api-docs/Toybox/Position.html#enableLocationEvents-instance_function]

Original code:

if (Position has :CONFIGURATION_GPS_GLONASS_GALILEO_BEIDOU_L1_L5) {
    options[:configuration] = :CONFIGURATION_GPS_GLONASS_GALILEO_BEIDOU_L1_L5;

Correct code:

if (Position has :CONFIGURATION_GPS_GLONASS_GALILEO_BEIDOU_L1_L5) {
    options[:configuration] = Position.CONFIGURATION_GPS_GLONASS_GALILEO_BEIDOU_L1_L5;


  • Thanks for the reply and the explanation!

    This post indicates a device crash, but I believe the original code would have given an UnexpectedTypeException (expected Number, got Symbol). If you applied the first fix to just update the reference to get the Number, you'd have got an InvalidValueException on some devices because the requested configuration is not supported. Correct?

    Correct. I did post a more comprehensive fix in a subsequent comment:

    https://forums.garmin.com/developer/connect-iq/i/bug-reports/position-enablelocations-documentation-has-a-bug-in-code-example-causes-device-to-crash?CommentId=8fafd15a-8946-4f75-9b51-039a8e157b8a

    (Sorry for the disjointed nature of the comments)

    Anyway, I was just reporting this on behalf of the person who posted this thread:

    forums.garmin.com/.../enable-location-bug-after-24-10-update

  • Yes, the reference to CONFIGURATION_GPS_GLONASS_GALILEO_BEIDOU_L1_L5 did need to be prefixed with the Position module. We fixed this a while back along with adding a call to check for hasConfiguration. Unfortunately, on older devices that would still generate a compiler warning/error for the hasConfiguration symbol not being has checked.

    I've updated the snippet a bit to try to get L1_L5, or fall back to L1 or just GPS.

    This post indicates a device crash, but I believe the original code would have given an UnexpectedTypeException (expected Number, got Symbol). If you applied the first fix to just update the reference to get the Number, you'd have got an InvalidValueException on some devices because the requested configuration is not supported. Correct?

  • For context, here's the full example:

    using Toybox.Position;
    
    var options = {
        :acquisitionType => Position.LOCATION_CONTINUOUS
    };
    
    if (Position has :POSITIONING_MODE_AVIATION) {
        options[:mode] = Position.POSITIONING_MODE_AVIATION;
    }
    
    if (Position has :CONFIGURATION_GPS_GLONASS_GALILEO_BEIDOU_L1_L5) {
        options[:configuration] = :CONFIGURATION_GPS_GLONASS_GALILEO_BEIDOU_L1_L5;
    } else if (Position has :CONSTELLATION_GPS_GLONASS) {
        options[:constellations] = [ Position.CONSTELLATION_GPS, Position.CONSTELLATION_GLONASS ];
    } else {
        options = Position.LOCATION_CONTINUOUS;
    }
    
    // Continuous location updates using selected options
    Position.enableLocationEvents(options, method(:onPosition));
    
    function onPosition(info) {
        var myLocation = info.position.toDegrees();
    }

    [https://developer.garmin.com/connect-iq/api-docs/Toybox/Position.html#enableLocationEvents-instance_function]

  • Furthermore, the documented example still fails after the suggested correction because there's no check for hasConfigurationSupport().

    A more comprehensive fix would look like this:

    //...
    if (
      Position has :CONFIGURATION_GPS_GLONASS_GALILEO_BEIDOU_L1_L5 &&
      Position has :hasConfigurationSupport &&
      Position.hasConfigurationSupport(Position.CONFIGURATION_GPS_GLONASS_GALILEO_BEIDOU_L1_L5)
    ) {
        options[:configuration] = Position.CONFIGURATION_GPS_GLONASS_GALILEO_BEIDOU_L1_L5;
    }
    //...

  • To be clear:

    - :CONFIGURATION_GPS_GLONASS_GALILEO_BEIDOU_L1_L5 is a symbol (invalid value for options[:configuration])

    - Position.CONFIGURATION_GPS_GLONASS_GALILEO_BEIDOU_L1_L5 is an integer value (valid value for options[:configuration])