Watch does not vibrate on background task

I am trying to vibrate the watch when a certain condition is met.
To reproduce the issue, here is a simple bit of code and scenario.

Code (WebAPI):

    tizen.humanactivitymonitor.start('GPS', data => {
      for (const gpsData of data.gpsInfo) {
          var speed = gpsData.speed;
          if (speed > 15) {
            // this does not work, watch never vibrates
            navigator.vibrate(1000);
          }
        }
      }
    }, error => {
      console.log('Oops, something went wrong: ', error);
    }, {callbackInterval: 1000, sampleInterval: 1000});

Scenario:
Start the “app” wait for the watch to go back to the “watch face”.
Meet the condition specified in the background task.
The watch does not vibrate.

The watch vibrates ONLY IF the app is visible and active.


What should I do here? Should I use the “Feedback” API instead? How do I use this?
https://docs.tizen.org/application/web/api/3.0/device_api/wearable/tizen/feedback.html

Tizen WebAPI Vibration - Source
https://developer.tizen.org/community/tip-tech/vibration-api-tizen-web-app

Thanks!

A tiny bit of progress on this issue.
It now vibrates even when the app is running on background.

  private vibrateWatchNow(): void {
    console.log('GpsService :: vibrateWatchNow');
    try {
      tizen.feedback.play('GENERAL', 'TYPE_VIBRATION');
      window.setTimeout(() => {
        // stops vibration after 2 secs
        tizen.feedback.stop('GENERAL');
      }, 2000);
    } catch (err2) {
      console.log('GpsService :: vibrate failed :: err:', err2);
    }
  }

However, is not vibrating continuously for 2 secs.
Which “FeedbackPattern” should I use?

Thanks!

Would it not be simple enough to just generate a push message every five minutes using Tasker? I don’t know how (still learning how to use it) but that would be my approach.

The issue here is that you then get the “message” the app would be running on background ( after 5mins ) and you’d run “navigator.vibrate(2000);” and the watch would not vibrate.

Okay finally got this!

So, if anyone else wants to do it too:

  // You will need to add the following to your config.xml
  // <feature name="http://tizen.org/feature/feedback.vibration"/>
  // <tizen:privilege name="http://tizen.org/privilege/power"/>
  public vibrateWatchFor(timeInMs: number): void {
    try {
      // Wakes up the watch so it vibrates
      tizen.power.turnScreenOn();
      tizen.power.request('SCREEN', 'SCREEN_NORMAL');
      tizen.power.request('CPU', 'CPU_AWAKE');
      // The returned "intervalID" is a numeric, non-zero value which identifies the timer created by the call to setInterval()
      // Source: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval
      let vibrateIntervalId = 0;
      // Schedule to stop vibration after the given param ('timeInMs')
      window.setTimeout(() => {
        if (vibrateIntervalId !== 0) {
          window.clearInterval(vibrateIntervalId);
        }
        tizen.power.release('SCREEN');
        tizen.power.release('CPU');
      }, timeInMs);
      // Play the vibration pattern (VIBRATION_ON) every 100ms
      const vibrateIntervalInMs = 100;
      vibrateIntervalId = window.setInterval(() => {
        try {
          // https://docs.tizen.org/application/web/api/3.0/device_api/wearable/tizen/feedback.html
          tizen.feedback.play('VIBRATION_ON', 'TYPE_VIBRATION');
        } catch (err3) {
          console.log('vibrateWatchFor :: play :: err3:', err3);
        }
      }, vibrateIntervalInMs);
    } catch (err2) {
      console.log('vibrateWatchFor :: err2:', err2);
    }
  }

How to use - direct call (just like navigator.vibrate) [app is active]:

this.vibrateWatchFor(2000);

Vibrates after going to sleep / background:

// 90secs = 1m 30s [watch goes to sleep]
window.setTimeout(() => {
  this.vibrateWatchFor(2000);
}, (90*1000));

This will vibrate the watch for 2secs even if the app is running on background.
It won’t vibrate like when the app is active, but at least it vibrates…

Cheers! :+1:

Thanks for sharing your progress. This is something I need in the near future and would have been lost without this example.

1 Like

No worries mate, was hoping that someone from SDP would help / give some advice on the subject.

PS: I have updated the details, have a look; all the best.

1 Like