Use Tizen Web To Measure Heart Rate With Galaxy Watches
health monitoring is a very popular feature in smart watches, and tizen provides various apis for this purpose. one api is humanactivitymonitor, which allows users to monitor health by providing metrics such as heart rate, step count, and stress level. this blog describes how to measure heart rate using this api. a sample web app combined with a widget is attached. this sample app measures heart rate and stores data by timestamp, and the widget shows the last measured heart rate. this blog focuses on how to measure heart rate using the device sensor, stored data, and communication between an app and a widget. create and run a tizen project 1. create a project in tizen studio. because our target is an app that is combined with a widget, select the widget template to create this project. 2. add the tizen advanced ui (tau) framework library to your project. you can create and manage various ui functionalities using the tau library as an alternative to designing a ui with html and css components. 3. link the tau.circle.min.css and tau.min.js in the app’s index.html file. <link rel="stylesheet" href="lib/tau/wearable/theme/default/tau.min.css"> <link rel="stylesheet" media="all and (-tizen-geometric-shape: circle)" href="lib/tau/wearable/theme/default/tau.circle.min.css"> <script src="lib/tau/wearable/js/tau.min.js"> </script> measure heart rate and manage data to measure heart rate using the humanactivitymonitor api, add the following feature and privilege to the config.xml file: <feature name="http://tizen.org/feature/humanactivitymonitor"/> <tizen:privilege name="http://tizen.org/privilege/healthinfo"/> applications must request a user’s permission for privacy-related features. the tizen.ppm.requestpermission() method invokes the permission pop-up. when users give permission through the pop-up, they can then use the app features related to that privilege. to measure heart rate, users must give permission to access the sensor using the following code snippet: function onsuccess() { function onchangedcb(hrminfo) { console.log(‘heart rate:’ + hrminfo.heartrate); tizen.humanactivitymonitor.stop('hrm'); } tizen.humanactivitymonitor.start('hrm', onchangedcb); } function onerror(e) { console.log("error " + json.stringify(e)); } tizen.ppm.requestpermission("http://tizen.org/privilege/healthinfo",onsuccess, onerror); note that the humanactivitymonitor api provides heart rate data directly in bpm (beats per minute). if you need the raw data of the sensor, you can use sensorhrmrawdata. the following screenshot shows how to measure heart rate using the sample app: the web storage api stores data in key-value format. this api offers two types of storage - local and session. in the sample, we use local storage, along with date as key and heartrate as value. the date key retrieves data according to date, week, and month. if you do not want to retrieve history according to date, you can use a different key, such as an integer value; just make sure there is no repetition in key value. create a date object to get the current time stamp. localstorage.setitem() puts the key (date)-value (heartrate) pair in local storage. var date_key = new date(); localstorage.setitem(date_key, hrminfo.heartrate); to retrieve data, filter according to the key. date and month are retrieved from the date object and filter data using localstorage.getitem(localstorage.key(i)). var date = new date(); var lastdate = new date(localstorage.key(i)); if (lastdate.getdate() == date.getdate() && lastdate.getmonth() == date.getmonth()) { console.log(localstorage.key(i)+ " :" + localstorage.getitem(localstorage.key(i))); the following screenshot shows how the history is categorized by day, week, and month: communication between the app and a widget the sample widget shows the last measured heart rate value, and heart rate history is stored in the local storage of the web app. to establish communication between the app and widget, we’ll use the preference api. the preference api stores key-pair values, allowing web widgets to communicate with their parent apps. in the app, data is stored with ‘key’: tizen.preference.setvalue('key', hrminfo.heartrate); in the widget, data is retrieved using the same ‘key’: if (tizen.preference.exists('key')) { console.log(‘last measured: ‘ + tizen.preference.getvalue('key')); } the following screenshot of the sample widget shows the last measured heart rate: launch the app from a widget to launch an app from widget, add the following privilege to config.xml: <tizen:privilege name="http://tizen.org/privilege/application.launch"/> to launch an app, provide your app id in tizen.application.launch(). tizen.application.launch(yourappid); in the sample widget, the user taps measure to launch the heart-rate measuring page or history to launch the heart-rate history page, and preference can be used to determine which one is clicked. to implement, a different key-value pair is set for each page in the widget, and in the web app, key values are checked to detect which page is clicked. for example, in the widget, the open_measure key is set to 1 to link to the measure page. the app then launches. tizen.preference.setvalue(‘open_measure’, 1); tizen.application.launch(yourappid); the app checks for the open_measure key. if the key exists, the user is redirected to the measure page with tau.changepage(). window.onload = function() { if (tizen.preference.exists(' open_measure ')) { tau.changepage(yourpageid); } } you can also use the humanactivitymonitor api to implement a step counter, stress level reading, gps, and other features in your wearable device. because enabling other features is similar to implementing the heart rate monitor, you can use this blog as a guide and the attached sampleheartratemonitor to enable a full range of health monitoring metrics to your galaxy watch.