Background problems in 2.3

Hi, I'm new here and also new to Connect IQ so I might be doing something wrong.

I'm looking at the new Background functionality in 2.3 but the 2 things I want to do keep failing and I don't get why.

The first thing I want to do is fetch some JSON and pass the data back to the app.

function onTemporalEvent() {
System.println("temporal event");
var url = "http://ip.jsontest.com/";
Communications.makeWebRequest(
url,
{},
{},
method(:responseCallback)
);
}

function responseCallback(responseCode, data) {
System.println("got data");
System.println(responseCode);
System.println(data);
if(responseCode == 200) {
Background.exit(data);
} else {
System.println("FETCH FAILED");
Background.exit(null);
}
}


This gets me

temporal event
got data
-1001
null
FETCH FAILED


Second thing I wanted to do was getting position from background.

function onTemporalEvent() {
System.println("temporal event");
Position.enableLocationEvents(Position.LOCATION_ONE_SHOT, method(:onPosition));
}

function onPosition(info) {
System.println("position");
System.println(info.position.toDegrees());
}


and this is what I get

temporal event
Permission requiredFailed invoking <symbol>


And yes, I do have all the permissions

<iq:permissions>
<iq:uses-permission id="Positioning"/>
<iq:uses-permission id="Background"/>
<iq:uses-permission id="Communications"/>
</iq:permissions>


Am I doing something retarded or did I find a bug?

Thanks,
Miha
  • On the first one, as of 2.3.0, you can only use https. The -1001 is what you get with http

    For the positioning thing, did you change the manifest yourself or with eclipse? What kind of an app? (watchface, widget, etc)

    With eclipse, positioning is grayed out for watch faces, for example, and for other app types may not be available in the background process.

    The background can only run for 30 seconds at a time, and getting a GPS fix can take far longer than that, so to save memory, it may simply not be available in the background.
  • zuid cruells

    Thanks for response, Jim.

    On the first one, as of 2.3.0, you can only use https. The -1001 is what you get with http


    Eh, this is what I get when I replace original URLs. It is https but it has api key so I can't share it publicly. The actual error I get is -402. But that same URL works just fine if I call it from the main app. Exact same code.

    var coordinates = "46.060368,14.509909";
    var url = "api.darksky.net/.../";
    Communications.makeWebRequest(
    url + coordinates,
    {"exclude" => "currently,minutely,daily,alerts", "units" => "si"},
    {},
    method(:responseCallback)
    );


    temporal event
    got data
    -402
    null
    FETCH FAILED


    For the positioning thing, did you change the manifest yourself or with eclipse? What kind of an app? (watchface, widget, etc)

    With eclipse, positioning is grayed out for watch faces, for example, and for other app types may not be available in the background process.

    The background can only run for 30 seconds at a time, and getting a GPS fix can take far longer than that, so to save memory, it may simply not be available in the background.


    Yes, with Eclipse. I'm making a widget. I understand that, I'm simulating it so it is instant. At least it is if I call it from the main app.
  • I don't see it in the api doc (it's still beta), but I suspect that you can't use position in the background. If you can get it it the foreground, you could put what you need in the object store and then pull the info out of it in the background process...

    -402 (the api doc is your friend! :) ) is NETWORK_RESPONSE_TOO_LARGE. How much data are you trying to get? Even if the response is smaller, to avoid this error, it could be too large for the background process, as it's not got a lot of memory, and with data like this it needs memory for the response and it's conversion into a dictionary.
  • I don't see it in the api doc (it's still beta), but I suspect that you can't use position in the background. If you can get it it the foreground, you could put what you need in the object store and then pull the info out of it in the background process...


    Yeah I tried figuring this out, but I couldn't make it work :D

    -402 (the api doc is your friend! :) ) is NETWORK_RESPONSE_TOO_LARGE. How much data are you trying to get? Even if the response is smaller, to avoid this error, it could be too large for the background process, as it's not got a lot of memory, and with data like this it needs memory for the response and it's conversion into a dictionary.


    Still need to learn how to navigate this api doc :( I thought it has something to do with HTTP 402 code and it made no sense.

    I requested a smaller payload now

    function onTemporalEvent() {
    System.println("temporal event");
    var coordinates = "46.060368,14.509909";
    var url = "api.darksky.net/.../";
    Communications.makeWebRequest(
    url + coordinates,
    {"exclude" => "currently,minutely,daily,alerts,flags", "units" => "si"},
    {},
    method(:responseCallback)
    );
    }

    function responseCallback(responseCode, data) {
    System.println("got data");
    System.println(responseCode);
    System.println(data);
    if(responseCode == 200) {
    Background.exit(data);
    } else {
    System.println("FETCH FAILED");
    Background.exit(null);
    }
    }


    and I get

    temporal event
    failed inside handle_json_callback
  • That;s what you see if the response made it to the watch, but there wasn't enough memory/objects to convert it.

    try excluding all but one thing at a time, and see if you can get that one thing