Collecting Heart Rate Data From Galaxy Watch 3

Hello, I’ve been trying to collect Heart Rate data from a Samsung Galaxy Watch 3. My goal is to have the watch collect the data save it on the watch, and after a couple minutes save it to a text file and send the text file to dropbox. However, when I try to push the data to the array, it doesn’t seem to save. I have attached my code from app.js file below. I would appreciate any help.

var array = ;

(function() {
function keyEventHandler(event) {
if (event.keyName === “back”) {
try {
// If the back key is pressed, exit the application.
tizen.application.getCurrentApplication().exit();
} catch (ignore) {}
}
}

/**
 * Initializes the application.
 * @private
 */
function init() {
    var textbox = document.querySelector("#contents");
    
    var counter = 0;

    function onchangedCB(hrmInfo) {
        for(var i = 0;i < 100;i++){
        	textbox.innerHTML = "a " + counter + ":" + hrmInfo.heartRate + ", " + hrmInfo.rRInterval;
            setTimeout(function(){
            	array.push(hrmInfo.heartRate);
                }, 15000);
            counter++;
        }
     
    }
    
    
    tizen.humanactivitymonitor.start('HRM', onchangedCB);

}

// The function "init" will be executed after the application successfully loaded.
window.onload = init;

}());

Hi,
Would you please try to add this line instead of yours?

array.push(String(hrmInfo.heartRate));

Thank you

1 Like

Hi Iqbal, I was able to go off of your suggestion and get it to work. I believe it was some form of type mismatch error. I resolved that, but now I am having issues with saving the data to a txt file. I can get the data to save once but not after that. I believe it is because the second time it gets to that portion of the code, the file already exists, so it times out. I tried deleting the directory right after I write and read from it, but still no luck. Here is my updated code:

/*
 * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
var counter = 0;
var HRMArray = [];
var RRArray = [];

(function() {
    /**
     * Handles the hardware key event.
     * @private
     * @param {Object} event - The hardware key event object
     */
    function keyEventHandler(event) {
        if (event.keyName === "back") {
            try {
                // If the back key is pressed, exit the application.
                tizen.application.getCurrentApplication().exit();
            } catch (ignore) {}
        }
    }

    /**
     * Initializes the application.
     * @private
     */
    function init() {
        var textbox = document.querySelector("#contents");

        function onchangedCB(hrmInfo) {
            	if(hrmInfo.heartRate > 0){
            		var a = hrmInfo.heartRate;
                	HRMArray.push(a);
                	var b = hrmInfo.rRInterval;
                	RRArray.push(b);
                	//textbox.innerHTML = HRMArray.length+":"+HRMArray[HRMArray.length -1] + ":" + RRArray[RRArray.length -1];
                	//setTimeout(init(), 2000);
	                counter++;
	                
	                //Write to file:
	                if(counter % 50 == 0){
	                	var newDir, newFile;
	                	tizen.filesystem.resolve("documents", function(dir) 
	                	    {
	                	       newDir = dir.createDirectory("DirD");
	                	       newFile = newDir.createFile("FilePathD.txt");
	                	       newFile.openStream(
	                	        "w",
	                	        function(fs) {
	                	        	for(var i = 0; i < 5;i++){
	                	        		textbox.innerHTML = i;
	                	        		fs.write(HRMArray[HRMArray.length -1] + ":" + RRArray[RRArray.length -1] + ", ");
	                	        	}
	                	        	 fs.close();
	                	        }, function(e) {
	                	        	textbox.innerHTML = "Error " + e.message;
	                	        	 for(var i = 0; i < 200;i++){}
	                	        }, "UTF-8");
	                	    });
	                	
	                	//Read from file:
	
	                		tizen.filesystem.resolve("documents", function(dir) 
	                		    {
	                		       file = dir.resolve("DirD/FilePathD.txt");
	                		       file.openStream(
	                		    	    "r", 
	                				    function(fs) {
	                		                var text = fs.read(file.fileSize);
	                		                fs.close();
	                		                for(var i = 0; i < 200;i++){
	                		                	textbox.innerHTML = text;
	                		                }
	                		            }, function(e) {
	                		            	textbox.innerHTML = "Error " + e.message;
	                		                for(var i = 0; i < 200;i++){}
	                		            }, "UTF-8");
	                		    });
	                		
	                		function errorCallback(error)
	                		{
	                			textbox.innerHTML = "An error occurred, during directory deletion: " + error.message;
	                		}

	                		function successCallback(path)
	                		{
	                			textbox.innerHTML = "The directory has been deleted, path to the parent of deleted directory: " + path;
	                		}

	                		try
	                		{
	                		  tizen.filesystem.deleteDirectory("documents/DirD", true, successCallback, errorCallback);
	                		}
	                		catch (error)
	                		{
	                			textbox.innerHTML = "Directory cannot be deleted: " + error.message;
	                		}

	                }
            	}
            	else{
            		textbox.innerHTML = "Invalid HRM";
            	}
            //}
         
        }
        
        
        tizen.humanactivitymonitor.start('HRM', onchangedCB);

    }

    // The function "init" will be executed after the application successfully loaded.
    window.onload = init;
}());