언어 설정

Menu
Sites
Language
Vibrate in background

I have a Samsung Gear S and I'm bussy writing a web app. I would like to vibrate my app when I get a message from my server. I simply use navigator.vibrate(1000) and this works just fine. But when my app goes to the background the vibration doesn't work anymore. I've found this thread (https://developer.tizen.org/ko/forums/web-application-development/vibration-background?langswitch=ko) in which they face the same problem but there is no solution. Someone also suggested to use alarms to make your app active but I don't know how to do that.  How can I make this work?

 

 

 

Edited by: Tim Deweert on 12 5월, 2016

Responses

16 댓글
Nafisul Islam Kiron

Have you tried with background-support on?

Open config.xml, select Tizen tab and enable background-support.

Tim Deweert

Yes it is enabled.

Nafisul Islam Kiron

One other way is to develop a Service app (acting as the server-listener) that will trigger your web app on. After coming to foreground it will vibrate as you planned.

Stephan König

Hi,

I think nackground vibration is "not official" supported in web-apps, but you can activate it.
Just add following key to your config.xml:

<tizen:setting background-support="enable" background-vibration="enable" />

This will allow you to vibrate even if the screen is off. (See also page 12 in tizen-2.3-wrt-core-spec.pdf )

But I think you'll need at least a partner certificate or some other sepcial permission to upload an app to store with this setting...

Tim Deweert

I tried it but unfortunately it gave an error: "attribute background-vibration is not allowed in tizen setting"

Marco Buettner

Its only available on 2.3 and higher ;)

Stephan König

Yes, the IDE may give this error, but you can ignore it ;)

Marco Buettner

But as far I know, the Gear2 and Gear S have as lastest Firmware only Tizen 2.2.x
 

Stephan König

Trust me, it will work. And so far I think Gear S and S2 use API 2.3.1. 
But as I said you can't upload to store if you use this key ;)

Tim Deweert

I tried it but unfortunately I get the following error:

Error code: PRIVILEGE_LEVEL_VIOLATION
Error message: 
Command: /usr/bin/pkgcmd -i -q -t wgt -p "/opt/usr/apps/tmp/Activity Monitor.wgt"
Management: Getting Started with Tizen > Native(Web) Application > Understanding Tizen Programming > Security and API Privilege 

Tim Deweert

I tried it but unfortunately I get the following error:

Error code: PRIVILEGE_LEVEL_VIOLATION
Error message: 
Command: /usr/bin/pkgcmd -i -q -t wgt -p "/opt/usr/apps/tmp/Activity Monitor.wgt"
Management: Getting Started with Tizen > Native(Web) Application > Understanding Tizen Programming > Security and API Privilege 

Marco Buettner

Yeah, but trust me... Gear S latest Tizen version is 2.2.2.1 ;)

Marco Buettner

Correct version is 2.2.1.2

Mathieu

Hello !

In my app I have the following code :

"var alarm = new tizen.AlarmRelative(12); //Wake the app up when in background
            tizen.alarm.add(alarm, "org.tizen.browser");
            navigator.vibrate([100, 100, 100]);
            $("#myDialog").dialog("open");"

Hope this will help you !

Tim Deweert

Unfortunately this also didn't work, thanks anyway.

Stephan König

OK, 

I played a bit with the vibration.
If you dont use the 'background-vibration="enable" ' key it seems you have two options, but both are more like a workaround:

  1. Just use notifications (https://developer.tizen.org/development/tutorials/web-application/tizen-features/application/notification)
    This will push a Notification to the notification area of the watch including a option to vibrate
  2. The other way with the alarm is the other way, but it will need a bit more than just an alarm.
    Your app need some conditions to be able to vibrate from background:
    1. the screen needs to be on
    2. your app needs to be in foreground

So for the second way you need to do a bit more stuff. I created a new project with the wizard (TAU basic) and midified the app.js as followed:

var myAlarm = null;
var page = document.querySelector('#main');
var remoteMsgPort = null;

( function () {
    window.addEventListener( 'tizenhwkey', function( ev ) {
		if( ev.keyName === "back" ) {
			var page = document.getElementsByClassName( 'ui-page-active' )[0],
				pageid = page ? page.id : "";
			if( pageid === "main" ) {
				try {
					// cleanup 
					showAfterWakeup(false);
					tizen.power.unsetScreenStateChangeListener();
					tizen.application.getCurrentApplication().exit();
				} catch (ignore) {
				}
			} else {
				window.history.back();
			}
		}
	} );

	/**
	 * Method to set the app as topmost app. This causes the app to be shown
	 * after display turns on, instead of the clock face.
	 * 
	 * @param {boolena}enable
	 *            True to enable, false to disable
	 */
	function showAfterWakeup(enable) {
		var param = [{
			key: "state",
			value: enable ? "show" : "hide"
		}];
		
		if(remoteMsgPort !== null) {
			remoteMsgPort.sendMessage(param);
			console.debug("remoteMsgPort.sendMessage(" + JSON.stringify(param) + ")");
		}
	}
	
	function checkLaunchRequest() {
		var appControl,
			appOperation,
			ret = false;
		
		try {
			appControl = tizen.application.getCurrentApplication().getRequestedAppControl().appControl;
			appOperation = appControl.operation;
			console.debug("checkLaunchRequest operation: " + appOperation);
			
			// check if operation view was used, as we set this for the alarm
			if (appOperation.indexOf('http://tizen.org/appcontrol/operation/view') !== -1) {
				console.debug("URI: " + JSON.stringify(appControl.uri));
				// set as topmost app to be in foreground when screen turns on
				showAfterWakeup(true);
				// turn the screen on
				tizen.power.turnScreenOn();
				ret = true;
			}
		} catch (err) {
			console.error("checkLaunchRequest Invalid launch request: " + err);
		}
	}
	
	function onScreenStateChanged(previousState, changedState) {
		console.log("Screen state changed from " + previousState + " to " + changedState);
		
		if(previousState === "SCREEN_OFF" && changedState !== "SCREEN_OFF" ){
			console.log("screen changed to ON");
			navigator.vibrate([250,100,350]);
			showAfterWakeup(false);
		}else if(previousState !== "SCREEN_OFF" && changedState === "SCREEN_OFF" ){
			// Triggers an alarm on a given date/time --> 10sec from now
			var date = new Date();
			date.setSeconds(date.getSeconds() + 10);
			
			// check if already a alarm was set and remove it to avoid multiple alarms
			if(myAlarm !== null){
				tizen.alarm.remove(myAlarm);
				myAlarm = null;
			}
			
			myAlarm = new tizen.AlarmAbsolute(date);
			var appControl = new tizen.ApplicationControl("http://tizen.org/appcontrol/operation/view");
			tizen.alarm.add(myAlarm, tizen.application.getCurrentApplication().appInfo.id, appControl);
			console.log("Alarm added set for: " + date.toLocaleTimeString() + " with id: " + myAlarm.id);
			
		}
	}
	
	/**
	 * Callback for pageshow event
	 */
	function onPageShow(){
		console.log("pageshow");

		/*
		 * check if alarm called app
		 * if false we interpret as the first app start, not clean but will do it for demo ;)
		 */
		if(checkLaunchRequest() !== true){
			// just for testing vibration
			var text = document.querySelector('.ui-listview');
			text.addEventListener('click', function(ev) {
				console.log("clicked: " + ev.target.nodeName);
				navigator.vibrate(500);
			});
		}
	}

	/**
	 * init view
	 */
	function init(){
		try {
			remoteMsgPort = tizen.messageport.requestRemoteMessagePort('starter', 'Home.Reserved.Display');
		} catch (err) {
			console.error("Exception for requestRemoteMessagePort: " + err);
		}
		showAfterWakeup(false);
		page.addEventListener('pageshow', onPageShow);

		// Sets the screen state change listener.
		tizen.power.setScreenStateChangeListener(onScreenStateChanged);
	}
	
	window.onload = init();
} () );

So what is done here:

  1. in the onload event we register a remote message port (https://developer.tizen.org/development/tutorials/web-application/tizen-features/application/message-port)
    and add some event listener for "page show" and change of screen states
  2. After app is started we wait till app goes to background and screen turns off
  3. in onScreenStateChanged we get that the screen turned off and we add a new alarm which will be triggered in 10sec
  4. The alarm triggers the app and the pageShow event so that checkLaunchRequest will be called
  5. The app control operation is .../view so the app is set as top most app and the screen is turned on
  6. Now we are again in the onScreenStateChanged, but this time see that the screen was turned on and do vibrate

I know this seems to be overloaded to just vibrate, but I think this is the only way to get the vibration 100% to work after the screens was turned off. 
In https://www.w3.org/TR/2014/WD-vibration-20140211/ they descripe that if the app is not visible it should not vibrate, so I think this is why the screens needs to be on and the app needs to be in foreground.
Somehow it seems logical to me that an app can't vibrate if it is in background...

I hope this helps  ;)