Languages

Menu
Sites
Language
Store data from Gear 2

I am using a heart rate monitor app in order to get the hear rate from Samsung Gear 2. I want to store the heart rate measurement and send it either to  a pc or a Galaxy device. How is it possible to store and send data with Gear device?

Responses

14 Replies
AVSukhov

Hello,

To store data you can use DB, localStorage or store to file.

For transfer between provider and consumer apps you need use SAP:

http://developer.samsung.com/gear

Jose Ramon

Thanks for the reply. Firstly how is it possible to store the data to file? Secondly what do you mean with SAP?

Marco Buettner

Yeah, you can also store on file.

The follow code will save the file to the private location "wgt-private".

var location = "wgt-private",
    localDB = localStorage.getItem('hrtdata');

function onsuccess(dir) {
    var file = null;
    try { // create new file
        file = dir.createFile('data.txt');
        if(file !== null)
        {
            file.openStream("w", function(stream) {
                stream.writeFile(localDB);
                stream.close();
            }, function(error) {
                alert("write to file failed");
                
            }, 'UTF-8');
        }
    }
    catch (e) { // file already exist -> append content
        file = dir.resolve('data.txt');
        if(file !== null)
        {
            file.openStream("a", function(stream) {
                stream.writeFile("-------");
                stream.writeFile(localDB);
                stream.close();
            }, function(error) {
                alert("write to file failed");
                
            }, 'UTF-8');
        }
    }
}

function onerror(error) {
    alert("failed to open location");
}

tizen.filesystem.resolve(location, onsuccess, onerror);

SAP stands for Samsung Accessory Protocol and is a communication way between Provider (HostApp) and Consumer (GearApp)

Jose Ramon

The only way is to store the data in a Galaxy device? Is there a possibility to send the data either from local storage or from text  with SAP to a pc? 

Marco Buettner

Yes, SAP only allows communication between HostApp on Android and Gear-device.. Replace in the code the location to "documents" or "others" and it will export to the "MyFiles"... Than you can copy the file to pc via USB.

Jose Ramon

Ok I got it. One last question, the function that calculates and shows heart rate is the following:

 

var updateChart = function (heartrate) {
    	time = new Date().getTime() - initial;
		console.log("[" + time + ", " + heartrate + "]");
		tizen.filesystem.resolve(location, onsuccess, onerror);
		dps.push({
			x: time / 1000.0,
			y: heartrate
		});
		if (dps.length > dataLength)
		{
			dps.shift();				
		}
		var second = Math.round(time / 1000.0);
		console.log(history.length);
		if(lastSecond != second) {
			// TODO use avg heart rate instead of smapshot.
			history.push({
				x: second,
				y: heartrate
			});
			if(history.length > historyDataLength) {
				history.shift();
			}
			lastSecond = second;
		}

		if(dps.length >= dataLength) {
			chart.render();
		}
		var hrchart = "<center>" + heartrate + "bps</center><table width='100%' cellpadding=4px>";
		for(var i = history.length - historyDataLength; i >= 0 && i < history.length; i++) {
			hrchart += "<tr><td align='right' width='50%'>" + history[i].x + "s</td><td width='50%'>" + history[i].y + "bps</td></tr>";
		}
		hrchart += "</table>";
		$('#tex

How can I store this heartrate variable to localstore and afterthat to the text file?

Marco Buettner

You can also write always directly to the file if you want... You dont need to store the data in localStorage...

The localStorage can save only strings... 

I use in my most app the following usage to store and load data

// save
function save(key, value) {
    var data = load(key),
        saveObject = {};
    
    if(!data) {
        saveObjects.results = [value];
    }
    else {
        saveObjects.results = data.results;
        saveObjects.results.push(value);
    }

    localStorage.setItem(key, JSON.stringify(saveObjects));
}

// load
function load(key) {
    var data = localStorage.getItem(key);

    if(!data) {
        localStorage.setItem(key, JSON.stringify({"results": []}));
        data = localStorage.getItem(key);
    }

    return JSON.parse(data);
}

 

Jose Ramon

OK but where in my code should I add tizen.filesystem.resolve(location, onsuccess, onerror); function in order to store the localStorage Data?

Jose Ramon

I am using your code in order to store heartrate inside the function(heartrate) 

var updateChart = function (heartrate) {
    	time = new Date().getTime() - initial;
		console.log("[" + time + ", " + heartrate + "]");
		if(heartrate != null){
			save("hrm", heartrate);
		}
		//localStorage.setItem("hrm", heartrate);
...
}

//in order to store the localstorage to data
var location = "documents",
localDB = localStorage.getItem('hrm');
window.webapis.motion.start("HRM", onchangedCB);
tizen.filesystem.resolve(location, onsuccess, onerror);

However I am getting error for the save function: TypeError: 'undefined' is not an object (evaluating 'saveObjects.results.push')

Marco Buettner

I got a mistake in my code above! 

The variable calls "saveObject" not "saveObjects" --- just remove the s from the variable at the end :)

Jose Ramon

I change it to saveObject  but nada I still got the same error: 'undefined' is not an object (evaluating 'saveObject.results.push') in this line: saveObject.results.push(value);. As far as writing to file concerning I tried to write directly something to file. The file is created but I got the following error:  (33) :TypeError: 'undefined' is not a function (evaluating 'stream.writeFile('hello')') where 33 line is the following: stream.writeFile('hello');

AVSukhov

Hello,

There some error: use saveObject.results... instead saveObjects.results...

John Ixion

I'm not sure but SAMI could be compatible: https://developer.samsungsami.io/

AVSukhov

Hello, 
According documentation FileStream interface has folowing methods: 
write
writeBytes
writeBase64
there is no writeFile method.