CurrentSpeed not dispayed

In my first app i want display current speed using Activity object.

In this way:

using Toybox.Activity;

function onUpdate(dc) {

var info = Activity.getActivityInfo();
var speedva = info.currentSpeed;

if (info != null) {
speedva = info.currentSpeed != null ? info.currentSpeed : 0;
}

dc.setColor(Graphics.COLOR_BLACK, Graphics.COLOR_WHITE);

dc.drawText(60,83, Graphics.FONT_SMALL, speedva, Graphics.TEXT_JUSTIFY_CENTER);

}

but display always zero value.

Where is my mistake ?

i have Edge 520.

thanks much

  • Have you created a session to start an activity?  Until you start a recording, most things in Activity,Info will be null or zero.

    See the recordsample in the SDK.

    Also, depending on what you are doing, you'll want to start up GPS (that's in the same sample) and maybe enable some sensors(Heart rate for example)  I'm not sure which sample shows how to do that, but check the Sensor module in the API docs.

  • mmm..i dont started activity.

    Then i must add also...

    using Toybox.ActivityRecording; 

    function onMenu() {
    if( Toybox has :ActivityRecording ) {
    if( ( session == null ) || ( session.isRecording() == false ) ) {
    session = ActivityRecording.createSession({:name=>"Walk", :sport=>ActivityRecording.SPORT_WALKING});
    session.start();
    WatchUi.requestUpdate();
    }
    else if( ( session != null ) && session.isRecording() ) {
    session.stop();
    session.save();
    session = null;
    WatchUi.requestUpdate();
    }
    }
    return true;
    }

    right ?

    i can put only in..

    function initialize {

    session = ActivityRecording.createSession({:name=>"Walk", :sport=>ActivityRecording.SPORT_WALKING});
    session.start();

    }

    next step is to add start activity only when i press start button.

  • Again, take a look at the sample and want to use a delegate to start/stop.

    Typically, things are started and stopped a bit differently, using things like KEY_ENTER /KEY_START, and maybe with a save/discard option when stopping.  See the input sample for things you can do in the delegate.

  • In RecordSampe app explain start activity:

    class BaseInputDelegate extends WatchUi.BehaviorDelegate
    {

    function initialize() {
    BehaviorDelegate.initialize();
    }

    function onMenu() {
    if( Toybox has :ActivityRecording ) {
    if( ( session == null ) || ( session.isRecording() == false ) ) {
    session = ActivityRecording.createSession({:name=>"Generic", :sport=>ActivityRecording.SPORT_CYCLING});
    session.start();
    WatchUi.requestUpdate();
    }
    else if( ( session != null ) && session.isRecording() ) {
    session.stop();
    session.save();
    session = null;
    WatchUi.requestUpdate();
    }
    }
    return true;
    }
    }

    and activity start clicking MENU key on Garmin 520 (function onMenu()).

    But i want start activity clicking START key on Garmin 520.

    I have change onMenu with onStart but not works..

    what can i do for use START key ?

    thanks

  • There is no "onStart".  You use "onKey(evt), get the key from evt, and then compare it to KEY_START

  • Thanks Jim. Now works!

    Now i check if display and record CurrentSpeed...

  • but CurrentSpeed works only with speed sensor ?

    but until now not works without..

    i dont'see value of currentspeed.

    var info = Activity.getActivityInfo(); 
    var speedva = info.currentSpeed;

  • It can take a couple seconds for speed to change once you start moving, so you want to do a Ui.requestUpdate() on a timer, so the display updates to show changes.  In fact you what to get the data with ACTIVITY.getActivityInfo in a timer too and not in onUpdate(), as onUpdate() won't get called if you are running a widget/etc, while the app is running.

  • Now works!

    i have add enabled GPS in Onstart(state) function.

    function onStart(state) {
    Position.enableLocationEvents(Position.LOCATION_CONTINUOUS, method(:onPosition));
    }

    thanks