Accessing Sensor events in a Widget

Hi! I'm trying to access sensor data in a widget by using a background service, but it seems I'm missing something. Maybe someone can put me on the right track?

I want to create a widget that accesses sensor data in a background process. Background processes can be executed every 5 minutes for around 30 seconds, so I am creating a temporal event for that time frame. When my temporal event is fired after 5 minutes, my callback is called. Now i am creating a sensor event where i want to listen to, for example, accelerator events:

    function myBackgroundCallback()    {

        var options = {
            :period=>1,               // 1 second sample time
            :accelerometer=>{
                :enabled=>true,       // Enable the accelerometer
                :sampleRate=>10        
            }
        };        

        Sensor.registerSensorDataListener(method(:sensor_callback), options);

//...

The problem is: my sensor callback sensor_callback will never be called because when i leave my myBackgroundCallback function, it seems my background time gets ended.

Probably I am doing a stupid mistake here. Can somebody point me into the right direction?

  • Are you sure your sensor_callback isn't being called?  Add a println.

    How are to returning data to the widget?  Where are you calling Background.exit()?

  • Hi Jim, yes, I added a println and it is not called. What I am doing is this:

    - I'm registering my temporal event in the "onStart" method of my App
    - my service delegate gets informed when the 5 minutes are over, so I call my callback onTemporalEvent

         Application.getApp().myBackgroundCallback();
         Background.exit(null);

    - in myBackgroundCallback() i register my sensor listener like

         Sensor.registerSensorDataListener(method(:sensor_callback), options);
     

    I guess my error is that when i leave myBackgroundCallback my delegate calls Background.exit(null), so it cancels any processing again. But i'm not sure how to prevent this? I think the only place I can call this is my callback and the onTemporalEvent method?

    For the moment I am not trying to show any data in the widget, i'm focussing on the background process collecting the data.

  • You want to do the BackGround.exit() in your callback in the Background service.  It's kind of the same as if you were doing a makeWebRequest and wanted to return the data received.  Put another println in onTemporalEvent and make sure that's being called when you think it should.

    I've never used Sensor.registerSensorDataListener in a background service, so that might be an issue.  With backgrounding you may want to just use Sensor.getInfo() and return that data just to see how things work.

  • As always, thanks for your fast reply :-)

    I thought about avoiding the sensor listener, but it seems i cannot get the full spectrum of data then (as far as I have seen, HRV data for example is only available via sensor listeners).

    That would be really a pity if I couldn't get the sensor listeners in a background app. According to the docu, that should work I think. I'll try and see if I find something.

  • This can just be a first step to make sure things are working, and then you can try other things  You probably don't want to register the temporal event in onStart, but maybe in getInitalView, as onStart is called in the background when the background service starts.  See https://forums.garmin.com/developer/connect-iq/f/discussion/5287/very-simple-sample-of-a-watch-face-with-a-background-process for a sample with a bunch of println calls to see what's going on.