Alirezadigi
Alirezadigi

Reputation: 55

How to vibrate every 15 minutes in tizen web app in background?

I'm trying to write a "feel the watch" alternative for tizen (a web tizen app for tizen wearable).

I want the watch to vibrate :

I have tried a lot of methods like "alarm api" and etc but the worker method just works. I use tizen.power.request("CPU","CPU_AWAKE"); to keep app run in background But the watch shows me high battery usage for my web app.

it would be much better if app vibrates without being opened in background.

here is my tree:

│   .tproject
│   config.xml
│   icon.png
│   index.html
│   main.js
│   reicon.jpg
│   reicon_crop.jpg
│   VibrateWatch.wgt
│
├───.settings
│       .jsdtscope
│       org.eclipse.wst.css.core.prefs
│       org.eclipse.wst.jsdt.ui.superType.container
│       org.eclipse.wst.jsdt.ui.superType.name
│
├───.sign
│       .manifest.tmp
│       author-signature.xml
│       signature1.xml
│
├───css
│       style.css
│
└───js
        main.js
        worker.js

here is my worker.js code :

function timedCount() {
    postMessage(0);                     //send data   
    setTimeout("timedCount()",1000);    // set vibration interval (or use specific time)
}

timedCount();

here is my main.js code :

//786
var pause = 150;
var dot = 300;
var ldot = 900;
var vibratePattern;
var tout;
var lastmin = 61;
var currentTime, timeLeft;

function format2Digits(value){
    if(value < 10)
        return "0" + value;
    return value;
}

function capp() {
      var currApp = tizen.application.getCurrentApplication();
      currApp.hide();
}


function multiVibration(list) {
      tizen.application.launch("MWoMC5yuQf.VibrateWatch", onsuccess);
      console.log(onsuccess);
      function onsuccess() {
        console.log("Application launched successfully");
      }
      /* Vibrate SOS */
      
      setTimeout(function (){  navigator.vibrate(list);
        }, 1300);
      
      setTimeout("capp()",5000);
      
    }

window.onload = function () {
    var worker = new Worker("js/worker.js");
    worker.onmessage = secondly;
    
};

function secondly(){
    tizen.power.request("CPU","CPU_AWAKE");

    var date = tizen.time.getCurrentDateTime();
    var houres = date.getHours();
    var minutes = date.getMinutes();
    var seconds = date.getSeconds();
    console.log('Time:',houres,minutes,seconds)
    
    currentTime = document.querySelector("#currentTime");
    currentTime.innerHTML = format2Digits(houres) + ":" + format2Digits(minutes) + ":" + format2Digits(seconds);
    
    if ( ((0 <= seconds) && (seconds <= 10)) && (lastmin !== minutes) ){
      setTimeout(function (){
          
        switch (minutes) {
          case 15:
              tout = 700
              setTimeout(function (){ navigator.vibrate([ldot]); }, 200);
              vibratePattern = [dot];
              multiVibration(vibratePattern);
              break;
          case 30:
            tout = 700
            setTimeout(function (){ navigator.vibrate([ldot]); }, 200);
            vibratePattern = [dot, pause, dot];
            multiVibration(vibratePattern);
              break;
          case 45:
            tout = 700
            setTimeout(function (){ navigator.vibrate([ldot]); }, 200);
            vibratePattern = [dot, pause, dot, pause, dot, pause];
            multiVibration(vibratePattern);
              break;
        }
          
      }, 500);
      
      lastmin = minutes;
      console.log('LM',lastmin);
    
    }

}

if you have any ideas , please inform me. Thanks :)

Upvotes: 0

Views: 355

Answers (1)

15kokos
15kokos

Reputation: 698

There are few concepts that can be useful to you.

  1. Background support of application - you don't need to raise UI every time, when the device need to vibrate. You can configure a background-support and background-category properties to allow to make some actions in background.

Then the application could be very easy (I've used seconds for easier coding), just in main.js:

function checkTime() {
  var sec = new Date().getSeconds();
  switch(sec) {
    case 0:
      /// navigator.vibrate([ldot]);  // EDIT: does not work in background support mode
      // you can use Feedback API instead (hardcoded version of 3 vibrations)
      // you can also experiment with other types of feedback events to find to most suitable
      setTimeout(function() {tizen.feedback.play("LOWBATT", "TYPE_VIBRATION")}, 0);
      setTimeout(function() {tizen.feedback.play("LOWBATT", "TYPE_VIBRATION")}, 300);
      setTimeout(function() {tizen.feedback.play("LOWBATT", "TYPE_VIBRATION")}, 900);
      break;
    case 15:
      /// etc.
}
//check every second
setInterval(checkTime, 1000);

Application in background will also cause the device to vibrate.

  1. If you need more division between UI and worker code, please check the 'service application' concept. But please notice that service application has limited access to some apis (e.g. navigator), so you need to use MessagePort API to tell UI application to vibrate. Some pseudocode:

service.js:

var port = tizen.messageport.requestRemoteMessagePort("znvYsIPC4D.WebUIServiceUI", "portService")

function checkTime() {
    var sec = new Date().getSeconds();
    var message;
    switch(sec) {
    case 15:
        message = [{key: "type", value: "vibrate15"}] 
        break;
    case 30:
        message = [{key: "type", value: "vibrate30"}]
        break;
    case 45:
        message = [{key: "type", value: "vibrate45"}]
        break;
    case 0:
        message = [{key: "type", value: "vibrate0"}]
        break;
    }
    message && port.sendMessage(message);
}

module.exports.onStart = function() {
  console.log("onStart is called");
  setInterval(checkTime, 1000);
};
module.exports.onStop = function() {
  console.log("onStop is called");
};

main.js:

  var port = tizen.messageport.requestLocalMessagePort("portService");

  var listener = function(message) {
      var type = message[0].value;
      
      switch(type) {
      case "vibrate0":
          navigator.vibrate([ldot]);
          break;
      case "vibrate15":
          // etc.
      }
  }
  port.addMessagePortListener(listener);

  // run a service
  tizen.application.launch("znvYsIPC4D.Service", (s) => console.log("success"), (s) => console.log("error"))
  1. Finally if all of above are not enough, you can consider using native service application working with Web UI application as a hybrid application for better performance.

BTW, the direct cause of high battery usage is a line:

tizen.power.request("CPU","CPU_AWAKE");

please just remove it - background support configuration will probably do what you expected.

Upvotes: 1

Related Questions