Getting Heart Rate Data for your Samsung Gear 2 Apps [Sample Code]

Photo credit : firstpost

In recent versions of the tizen wearable sdk (1.0.0b3), a few important features (mainly about sensors) have been added to the Gear SDK.

– In the Motion API: GPS type has been added for retrieving user location information (latitude, longitude, speed, timestamp)
– The startHandGestureRecognition() and stopHandGestureRecognition() methods have been added to get information about user hand gestures (gesture direction, acceleration, and duration).
– The “HRM” MotionType has been added.
– The Memory interface and DUID attribute have been added in SystemInfo API.
– The MessagePort and SystemSetting and Download API have been added.
– The Sensor API has been added in the Extension APIs, with the Light, Magnetic, Pressure, Proximity, and UV sensor types.
– The badge manager is supported to set the badge number on the application icon.

One important update is the re-introduction of the HRM sensor after it was removed in the 1.0.0b2 release. Nominally, it means you can develop apps that can incorporate data about the wearers heart rate into its functions. Before that, there are a few things that need to be done first.

Upgrade to Partner Priviledge.

To maintain security and integrity of user information, the Tizen OS security architecture implements 3 levels of security privileges – public, platform and partner. Each of the device apis (e.g motion api, application launch) in the tizen wearable help documentation fall under one of these three categories :

– Public:These privileges are open to all Tizen application developers.
– Partner:These privileges can only be used by developers registered as partners on the Tizen store.The developer must be fully identified and permitted by the partner policy of the Tizen Store to use both public and partner level privileges.
– Platform: These privileges are used in system APIs for managing the Tizen platform.These privileges are open only to a specific set of Tizen application developers.

It turns out that the HRM sensor privilege is only available to the public but only developers with a partner status.  To upgrade, you will need to use the new Tizen Wearable IDE to create a certificate request and a deviceprofile in which you specify the partner priviledge.

Add Privileges (Healthinfo and Medicalinfo)

In your config.xml file you should add two privileges – healthinfo and medicalinfo. Thanks to Todd (comments) for sharing this info.

<tizen:privilege name=""/><tizen:privilege name=""/>

Code to Read Heart Rate

Based on the api documentation in the help section, you can then use the following code snippet to read heart rate via the MotionApi.

webapis.motion.start("HRM", onchangedCB);

function onchangedCB(MotionHRMInfo) {
console.log("Heart Rate : " + MotionHRMInfo.heartRate);
console.log("Interval : " + MotionHRMInfo.rRInterval);

The heartrate and interval vvariables are described as follows based on the api documentation.

Motion API (Samsung Extension)
readonly long heartRate
– An attribute to indicate the heart rate in beats per minute. When a user takes off the watch device, the heartRate is set to -3. When a user shakes the watch, the heartRate is set to -2. Since: 2.2

readonly long rRInterval
– Peak-to-peak interval in millisecond(s).


About Vykthur

Mobile and Web App Developer and Researcher. Passionate about learning, teaching, and recently - writing.
This entry was posted in Wearables and tagged , . Bookmark the permalink.
  • Alex

    Hi Vykthur,
    I am new in programming and i wish to know how can i calculate the average of the heart rate within a time frame by using array ? I tried but not works for me.
    i doing right now:
    1) store all the heart rate within a period of time into array.
    2) get the average of heart rate by adding all the heart rate and divide with the array length.
    i was not able to store the heart rate second-by-second.
    All the heart rate is same which is the latest collected heart rate.
    Below is the code i developing.

    window.webapis.motion.start(“HRM”, onchangedCB);

    function onchangedCB(hrmInfo)
    var myArray= [];
    if(hrmInfo.heartRate > 0)
    var avg = sum(myArray);
    $(‘#textbox’).html(“No heart rate detected…”);

    var sum = function(myArray123){
    var total = 0;
    for (var i = 0, len = myArray123.length; i < len; i++){
    total += myArray123[i]; //add all the heart rate
    return total;

    Wish to get some advice from you. Thank you.

    • kyleagronick

      Divide total by i. Thats more of a math question than a programming question.