Download and save a FIT file with makeWebRequest

Context

I am building a Garmin App for running and I would like to know the type of terrain I a m running on (asphalt, ground, paved etc...).
I have a backend querying OpenStreetMap to labelised my tracks as LatLng array with the type of terrain eg : [[45, 5, 'asphalt'], [45.1, 5, 'asphalt'], [45.2, 5, 'ground']].

I would like to store those terrain information in the watch to access it during the activity in offline mode.
First i thought i will store them in the Application.Storage system but the size allowed is very limited.

Then I looked at 
makeWebRequest in order to download and save data into the PersistedContent. I would like to download/import FIT files and not GPX because too few devices support GPX.

First to test, I created a simple FIT file on https://connect.garmin.com/modern/courses.

Then I exported and save the FIT file in my laptop. Then started a webserver serving this file.

Issue

When I try to execute makeWebRequest it succeed with 200 responseCode but the fit file seems to not be saved because there is nothing I can read with PersistedContent.getCourses().
Also the data field in the callback is type Lang.object with a field : _data: Resource and when I try to inspect it in debugger mode it stop the debugger with the issue : 
Kill app for update variable children
Failed to update variable children: Timeout

The code 

function onReceive(responseCode, data) {
    if (responseCode != 200) {
        // THE EXECUTION DOES NOT ENTER HERE
        System.println("data == null\nCode " + responseCode.toString() + "\n");
        return;
    }

    if (data == null) {
        // THE EXECUTION DOES NOT ENTER HERE
        System.println("data is null");
        return;
    }
    var a = PersistedContent.getCourses();
    var b = a.next();
    System.println(a == null); // print 'false'
    System.println(b == null); // print 'false'
    
}

function download() {
   var url = "http://192.168.1.50:8080/track.fit";
   var options = {
       :method => Communications.HTTP_REQUEST_METHOD_GET,
       :headers => {
               "Content-Type" => Communications.REQUEST_CONTENT_TYPE_URL_ENCODED},
       :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_FIT
   };
   Communications.makeWebRequest(url, {}, options, method(:onReceive));
}

Is the fit file automatically saved in PersistedContent with the makeWebRequest call ?
I inspected the HTTP traffic with the simulator and the fit file is correctly fetched.

Thanks for your help

  • I'm working on something similar and found out that the FIT files get automatically saved to the device in the background.

    If you use the simulator and Linux you can find them at "/tmp/com.garmin.connectiq/GARMIN/Courses/".

    Unfortunately we can't access them directly through the callback function. You would have to iterate over PeristedContent.getCourses() and find another way of identifying the downloaded files. Working with FIT files in external apps is just a mess...