Tizen Background Vibration

680 Views Asked by At

I'm developing the application for Samsung Gear S2 (web) with a timer that vibrates when time ends. What I need to do so my application can vibrate even if it's in background mode?

1

There are 1 best solutions below

1
On

In general, background vibration (from screen off state) is not directly available for web apps unless you are using Alarm API or Notification.

But a timed background vibration can be easily tricked out using Power API and web workers.I am sharing a sample code:

main.js

   window.onload = function() {
       document.addEventListener('tizenhwkey', function(e) {
           if (e.keyName === "back") {
               try {
                   tizen.application.getCurrentApplication().hide();
               }catch (ignore) {}
           }
        });

        var mainPage = document.querySelector('#main');
        mainPage.addEventListener("click", function() {
        var contentText = document.querySelector('#content-text');


        var worker;                             //web worker
        worker = new Worker("js/worker.js");    //load from directory

        worker.onmessage = function(event) {    //receive data from worker
            tizen.power.turnScreenOn();        // forcefully turn the screen on
            setTimeout(function (){
                contentText.innerHTML = event.data;  // time counter
                navigator.vibrate(1000);
            }, 500);                          // just being safe (vibrate after screen is on)
        };

    });
};

worker.js

var i=0;

function timedCount() {
    i=i+1;
    postMessage(i);                     //send data   
    setTimeout("timedCount()",5000);    // set vibration interval (or use specific time)
}

timedCount();

add these lines on your config.xml

<tizen:privilege name="http://tizen.org/privilege/power"/>
<tizen:setting background-support="enable" encryption="disable" hwkey-event="enable"/>

Once background-support is enabled the app would response while minimized, when you are applying web workers. Using getCurrentApplication().hide() instead of getCurrentApplication().exit() on back key event would do the task for you.

Check Vibration Guide for different types of vibration.