Ahmed Zeki
Ahmed Zeki

Reputation: 31

Tizen web app works on simulator, but doesn't work on gear 3

I'm trying to develop an app that reads data from the acceleration sensor, and save it on a text file. Using web app development, I've managed to make the app work on the emulator, but when I tried it on Samsung Gear 3 frontier, it didn't work. Can some figure out what I did wrong? Below are the html and the java script code.

<!DOCTYPE html>
<html>

<head>
    <meta name="viewport" content="width=device-width,user-scalable=no">
    <title>Basic</title>
    <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">
    <!-- load theme file for your application -->
    <link rel="stylesheet" href="css/style.css">
</head>

<body>

    <div class="ui-page ui-page-active" id="main">
        <header>
            <h2 class="ui-title">TAU Basic</h2>
        </header>
        <div class="ui-content ui-content-padding">
            <p id="readings"> Basic </p>
        </div>
    </div>
    <script src="lib/tau/wearable/js/tau.min.js"></script>
    <script src="js/app.js"></script>
    <script src="js/lowBatteryCheck.js"></script>
    <script src="js/circle-helper.js"></script>
</body>

</html>

Java script code:

function init() {
	console.log("app started");
	document.getElementById("readings").innerHTML="Starting";
	accelerationSensor=tizen.sensorservice.getDefaultSensor("ACCELERATION");
	if (accelerationSensor){
		console.log("Sensor captured");
	}
	
	
    /* Update the clock hands every second */
	accelerationSensor.start(onsuccessCB);
    setInterval(function() {
        updateTime();
    }, 1000);
}

window.onload = init();


function onGetSuccessCB(sensorData)
{
	var datetime = tizen.time.getCurrentDateTime();
	var Date = ("0" + datetime.getHours()).slice(-2)   + ":" + 
    ("0" + datetime.getMinutes()).slice(-2) + ":" + 
    ("0" + datetime.getSeconds()).slice(-2);
	console.log(Date);
    console.log("######## Get acceleration sensor data ########");
    console.log("x: " + sensorData.x);
    console.log("y: " + sensorData.y);
    console.log("z: " + sensorData.z);
   
	
    x = sensorData.x;
    y = sensorData.y;
    z = sensorData.z;
    
    tizen.filesystem.resolve("documents", function(dir) 
    	    {
    	       
    	       var newFile = dir.resolve("newFilePath.txt");;
    	       newFile.openStream(
    	        "a",
    	        function(fs) {
    	        	 fs.write(Date+"\t x:"+x+"\t y:"+y+"\t z:"+z+"\n");
    	        	 fs.close();
    	        }, function(e) {
    	        	 console.log("Error " + e.message);
    	        }, "UTF-8");
    	    },function(){
    	    	document.getElementById("readings").innerHTML="Error";
    	    });
    document.getElementById("readings").innerHTML="Reading";
}
function onerrorCB(error)
{
    console.log("error occurred: " + error.message);
}
function onsuccessCB()
{
    console.log("acceleration sensor start");
    var datetime = tizen.time.getCurrentDateTime();
    var hour = datetime.getHours(),
    var minute = datetime.getMinutes(),
    var second = datetime.getSeconds();

    tizen.filesystem.resolve("documents", function(dir) 
    	    {
    		
    	       
    	       newFile = dir.createFile("newFilePath.txt");
    	       newFile.openStream(
    	        "w",
    	        function(fs) {
    	        	 fs.write(hour+":"+minute+":"+second+"\tstart of recording \n");
    	        	 fs.close();
    	        }, function(e) {
    	        	 console.log("Error " + e.message);
    	        }, "UTF-8");
    	    },function(){
    	    	document.getElementById("readings").innerHTML="Error";
    	    });

}
function updateTime() {
    
	
	accelerationSensor.getAccelerationSensorData(onGetSuccessCB, onerrorCB);
}



(function () {
	window.addEventListener("tizenhwkey", function (ev) {
		var activePopup = null,
			page = null,
			pageid = "";

		if (ev.keyName === "back") {
			activePopup = document.querySelector(".ui-popup-active");
			page = document.getElementsByClassName("ui-page-active")[0];
			pageid = page ? page.id : "";

			if (pageid === "main" && !activePopup) {
				try {
					tizen.application.getCurrentApplication().exit();
				} catch (ignore) {
				}
			} else {
				window.history.back();
			}
		}
	});
}());

Thanks in advance.

Upvotes: 1

Views: 507

Answers (1)

Ahmed Zeki
Ahmed Zeki

Reputation: 31

I've managed to find the solution, and I post it for helping others who would face the same issue.

It turns out that there is no acceleration sensor in S3, and everything works fine when I change the sensor from Acceleration to linear_acceleration. The codes for both html and javascript are as follow:

<!DOCTYPE html>
<html>

<head>
    <meta name="viewport" content="width=device-width,user-scalable=no">
    <title>Basic</title>
    <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">
    <!-- load theme file for your application -->
    <link rel="stylesheet" href="css/style.css">
</head>

<body>

    <div class="ui-page ui-page-active" id="main">
        <header>
            <h2 class="ui-title">TAU Basic</h2>
        </header>
        <div class="ui-content ui-content-padding">
            <p id="readings"> Basic </p>
        </div>
    </div>
    <script src="lib/tau/wearable/js/tau.min.js"></script>
    <script src="js/app.js"></script>
    <script src="js/lowBatteryCheck.js"></script>
    <script src="js/circle-helper.js"></script>
</body>

</html>

The javascript:

var accelerationSensor;



function onsuccessCB() {
    console.log("acceleration sensor start");
    var datetime = tizen.time.getCurrentDateTime();
    var hour = datetime.getHours();
    var minute = datetime.getMinutes();
    var second = datetime.getSeconds();

    tizen.filesystem.resolve("documents", function(dir) {


        var newFile = dir.createFile("newFilePath.txt");
        newFile.openStream(
            "w",
            function(fs) {
                fs.write(hour + ":" + minute + ":" + second + "\tstart of recording \n");
                fs.close();
                document.getElementById("readings").innerHTML = "Reading";

            },
            function(e) {
                document.getElementById("readings").innerHTML = "File Error";
            }, "UTF-8");
    });

}


function init() {
    console.log("app started");
    document.getElementById("readings").innerHTML = "Starting";
    accelerationSensor = tizen.sensorservice.getDefaultSensor("LINEAR_ACCELERATION");
    document.getElementById("readings").innerHTML = "Starting1";

    if (accelerationSensor) {
        console.log("Sensor captured");
        document.getElementById("readings").innerHTML = "Acceleration";
    } else {
        document.getElementById("readings").innerHTML = "Error";
    }


    /* Update the clock hands every second */
    accelerationSensor.start(onsuccessCB);
    document.getElementById("readings").innerHTML = "onsuccessCB done";
    console.log("onsuccessCB done");
    setInterval(function() {
        updateTime();
    }, 1000);
}

window.onload = init();


function onGetSuccessCB(sensorData) {
    var datetime = tizen.time.getCurrentDateTime();
    var Date = ("0" + datetime.getHours()).slice(-2) + ":" +
        ("0" + datetime.getMinutes()).slice(-2) + ":" +
        ("0" + datetime.getSeconds()).slice(-2);
    console.log(Date);
    console.log("######## Get acceleration sensor data ########");
    console.log("x: " + sensorData.x);
    console.log("y: " + sensorData.y);
    console.log("z: " + sensorData.z);


    var x = sensorData.x;
    var y = sensorData.y;
    var z = sensorData.z;

    tizen.filesystem.resolve("documents", function(dir) {

        var newFile = dir.resolve("newFilePath.txt");
        newFile.openStream(
            "a",
            function(fs) {
                fs.write(Date + "\t x:" + x + "\t y:" + y + "\t z:" + z + "\n");
                fs.close();
            },
            function(e) {
                console.log("Error " + e.message);
            }, "UTF-8");
    }, function() {
        document.getElementById("readings").innerHTML = "Error";
    });
    document.getElementById("readings").innerHTML = "Reading";
}

function onerrorCB(error) {
    console.log("error occurred: " + error.message);
}



function updateTime() {

	accelerationSensor.getLinearAccelerationSensorData(onGetSuccessCB);
}



(function() {
    window.addEventListener("tizenhwkey", function(ev) {
        var activePopup = null,
            page = null,
            pageid = "";

        if (ev.keyName === "back") {
            activePopup = document.querySelector(".ui-popup-active");
            page = document.getElementsByClassName("ui-page-active")[0];
            pageid = page ? page.id : "";

            if (pageid === "main" && !activePopup) {
                try {
                    tizen.application.getCurrentApplication().exit();
                } catch (ignore) {}
            } else {
                window.history.back();
            }
        }
    });
}());

The above code will get linear_acceleration sensor readings, and save them to a text file in "Documents" folder. You need filesystem.read and filesystem.write privileges to have access to "Document" folder.

Upvotes: 2

Related Questions