언어 설정

Menu
Sites
Language
Accessing Sensor Data

Hi, 

I'm new to Tizen and need to find a way to get access to all sensor, not just the few the following function delivers:

tizen.sensorservice.getAvailableSensors();
So since I looked into the API reference I found out that with Tizen 4.0 I can get the AccelerationSensor aswell as a few more which I need, but the problem now is that Tizen 4.0 is still not available for my Samsung Gear S3.

https://developer.tizen.org/development/api-references/web-application?redirect=https://developer.tizen.org/dev-guide/4.0.0/org.tizen.web.apireference/html/device_api/mobile/tizen/sensor.html#AccelerationSensor

So my question now is if there is a way to still get the Sensor data in Tizen 3.0 or if there is a way to include native functions into JS or the webapp or something else.

If there is no other way I have to do it as native app.

Best regards.

답변 바로가기

Responses

10 댓글
K Johnson

By using getAvailableSensors method you may determine which sensors are available on your device. You can only get those sensors data which are available on your device. It's not dependant on API calls or Tizen Version only, it also depends on sensor hardware available on your device as well.

For accelerometer, before using acceleration sensor you may check that the sensor is supported by the device using the getCapability() method of the SystemInfo interface by using below code:

var accelerationCapability = tizen.systeminfo.getCapability('http://tizen.org/feature/sensor.accelerometer');

if (accelerationCapability === true) {
    /* Device supports the acceleration sensor */
   /* Do whatever you need to do with acceleration sensor */
}

I hope, it'll help you to understand.

Lippert

Hi thank you for the reply.

So I know that I can check the availability with the given method and also it delivers true. But the problem that I have, is that even thou I have (so getCapability() returns true) I can't access the sensor via the WebApi, probably I explained my problem a bit misunderstanding.

K Johnson

Hi Lippert,

Would you please share your code here?

Lippert

Hi Johnson,

Sure i can share my code.

function sensors(){
    sensors = tizen.sensorservice.getAvailableSensors();
    console.log("All Sensors: "+sensors);
	var ac,linac;
	ac = tizen.systeminfo.getCapability("http://tizen.org/feature/sensor.accelerometer");
	linac = tizen.systeminfo.getCapability("http://tizen.org/feature/sensor.linear_acceleration");
	console.log(ac); // Both return true
	console.log(linac);
	//tizen.sensorservice.getDefaultSensor("ACCELERATION");
	var sensor;
	for (var s = 0; s < sensors.length; s++) {
		sensor = tizen.sensorservice.getDefaultSensor(sensors[s]);
		console.log(sensors[s]);
		console.log(sensor);
		sensor.setChangeListener(sensorListener(sensors[s]),10,0);
		sensor.start(onSucc);
	}
}

 

this is what i get in the emulator under version 4.0 

file:///js/app.js (88) :All Sensors: ACCELERATION,GRAVITY,LINEAR_ACCELERATION,MAGNETIC,GYROSCOPE,LIGHT,PROXIMITY,PRESSURE,ULTRAVIOLET,GYROSCOPE_ROTATION_VECTOR
file:///js/app.js (92) :true
file:///js/app.js (93) :true
file:///js/app.js (94) :Called Show sensors
file:///js/app.js (99) :ACCELERATION
file:///js/app.js (100) :{"sensorType":"ACCELERATION"}
file:///js/app.js (99) :GRAVITY
file:///js/app.js (100) :{"sensorType":"GRAVITY"}
file:///js/app.js (99) :LINEAR_ACCELERATION
file:///js/app.js (100) :{"sensorType":"LINEAR_ACCELERATION"}
file:///js/app.js (99) :MAGNETIC
file:///js/app.js (100) :{"sensorType":"MAGNETIC"}
file:///js/app.js (99) :GYROSCOPE
file:///js/app.js (100) :{"sensorType":"GYROSCOPE"}
file:///js/app.js (99) :LIGHT
file:///js/app.js (100) :{"sensorType":"LIGHT"}
file:///js/app.js (99) :PROXIMITY
file:///js/app.js (100) :{"sensorType":"PROXIMITY"}
file:///js/app.js (99) :PRESSURE
file:///js/app.js (100) :{"sensorType":"PRESSURE"}
file:///js/app.js (99) :ULTRAVIOLET
file:///js/app.js (100) :{"sensorType":"ULTRAVIOLET"}
file:///js/app.js (99) :GYROSCOPE_ROTATION_VECTOR
file:///js/app.js (100) :{"sensorType":"GYROSCOPE_ROTATION_VECTOR"}

 and this is under version 3.0

js/app.js (88) :All Sensors: GRAVITY,LINEAR_ACCELERATION,MAGNETIC,GYROSCOPE,LIGHT,PROXIMITY,PRESSURE,ULTRAVIOLET,GYROSCOPE_ROTATION_VECTOR,HRM_RAW
js/app.js (92) :true
js/app.js (93) :true
js/app.js (94) :Called Show sensors
js/app.js (99) :GRAVITY
js/app.js (100) :{"sensorType":"GRAVITY"}
js/app.js (99) :LINEAR_ACCELERATION
js/app.js (100) :{"sensorType":"LINEAR_ACCELERATION"}
js/app.js (99) :MAGNETIC
js/app.js (100) :{"sensorType":"MAGNETIC"}
js/app.js (99) :GYROSCOPE
js/app.js (100) :{"sensorType":"GYROSCOPE"}
js/app.js (99) :LIGHT
js/app.js (100) :{"sensorType":"LIGHT"}
js/app.js (99) :PROXIMITY
js/app.js (100) :{"sensorType":"PROXIMITY"}
js/app.js (99) :PRESSURE
js/app.js (100) :{"sensorType":"PRESSURE"}
js/app.js (99) :ULTRAVIOLET
js/app.js (100) :{"sensorType":"ULTRAVIOLET"}
js/app.js (99) :GYROSCOPE_ROTATION_VECTOR
js/app.js (100) :{"sensorType":"GYROSCOPE_ROTATION_VECTOR"}
js/app.js (99) :HRM_RAW
js/app.js (100) :{"sensorType":"HRM_RAW"}

 

So what I want in best case would be all the sensor data that i could also get via the native app but also the once I can get via version 4.0 would be sufficient.

 

so right now I'm trying to solve my problem via a hybride App that reads the sensor data via a native service and sends it back to my webapp but im not sure how well this will work nor if it is possible to do it like that.

thanks for your support.

 

Best regards.

 

K Johnson

As you're using Gear S3 with Tizen 3.0, you may try with LINEAR_ACCELERATION. At first check whether it is present in the available sensor list using getAvailableSensors  method or not. If it's available then you may try to use getLinearAccelerationSensorData to get acceleration data. Please try using below line of code:

 var linearAccelerationSensor = tizen.sensorservice.getDefaultSensor("LINEAR_ACCELERATION");

 function onGetSuccessCB(sensorData)
 {
     console.log("######## Get the linear acceleration sensor data ########");
     console.log("x: " + sensorData.x);
     console.log("y: " + sensorData.y);
     console.log("z: " + sensorData.z);
 }

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

 function onsuccessCB()
 {
     console.log("linear acceleration sensor start");
     linearAccelerationSensor.getLinearAccelerationSensorData(onGetSuccessCB, onerrorCB);
 }

 linearAccelerationSensor.start(onsuccessCB);

You may also refer to this link. Hope it'll help.

Lippert

Hi,

thanks for the suggestion, i already tried this and yes it works.

as i mentioned above I'm trying to get now all sensrs via a native service app which i will call from the webapp.

 

Best regards.

K Johnson

I would like to request you to accept an answer if it really works for you so that it can help other developers coming into this forum.

Mark as answer
Lippert

So thanks again for helping me Johnson,

So the solution i found now is to to have two projects one native service app and one normal Webapp.

So in the WebApp we register a message port where we will later sned the data to from the native app

Webapp: 

function showSensors(){
    var appControlReplyCallback = {
			// callee sent a reply
			onsuccess: function(data) {
				console.log(JSON.stringify(data));

			},
			// callee returned failure
			onfailure: function() {
				console.log('The launch application control failed');
			}
	}
    
	var localPort = tizen.messageport.requestLocalMessagePort('MY_PORT');
    //register the listener callback that gets triggered by the native app
	var localPortWatchId = localPort.addMessagePortListener(messageCB)
	
	//starting native app
	var returnData = new tizen.ApplicationControlData("AppID of this WebApp", ["Success"]);

	var appControl = new tizen.ApplicationControl("http://tizen.org/appcontrol/operation/service",
			null,
			null,
			null,
			[returnData] 
	);
	
	tizen.application.launchAppControl(appControl,
			"AppID of native service App",
			function() {console.log("Launch Service succeeded"); },
			function(e) {console.log("Launch Service failed : " + e.message);},
			appControlReplyCallback);
}

function messageCB(data, replyPort){	
	for(var i = 0; i< data.length; i++){
    //do whatever you want with your data here :D
	} 
	
}

In the native we only have to register the needed sensor listener and send the data we recive over the messagin port to the WebApp.

void
sensor_cb(sensor_h sensor, sensor_event_s *event, void *user_data)
{
    appdata_s *ad = (appdata_s *) user_data;
	bundle *b = bundle_create();
    //TODO: put all Values and everything you need in the Bundle
	bundle_add_str(b, "command", "cb");
	bundle_add_str(b, "data", "sensor");
	message_port_send_message("AppID of the WebApp that started the native app", "MY_PORT", b);
	//dlog_print(DLOG_INFO, "MYTAG", "Accel data nr. %d: %.2f",i ,event->values[0]);
    
    //if needed Stop the sensor again
    sensor_listener_stop(ad->listener);
}

bool service_app_create(void *data)
{
    // Todo: add your code here.
    //dlog_print(DLOG_DEBUG, "MYTAG", "Create Called");
	
    //register the sensors you need
    if (is_supported(SENSOR_ACCELEROMETER)) {
		add_listener(SENSOR_ACCELEROMETER, sensor_cb,data);
	}
    return true;
}

Also don't forget to set in the WebApp properties the multi Package to the native service app.

 

I hope I could atleast help someone who has the same problem.

I'm still not sure how well and how fast this messaging works compared to recieving sensor data.

 

Best regards.

K Johnson

Hi Lippert,

Basically your problem was about accessing sensor data but in your last reply you've focused more on passing data from native app to web app rather than retrieving acceleration data. I think, it would be helpful for others if you modify the answer according to the problem you mentioned in main post.

Thanks.

Lippert

Hi Johnson,

I guess the explanation of my problem was really bad, but what I actually wanted was to access Sensors via the webapp that I can't access via the normal method (the one also you provided).

So thanks again for your support and help. I also marked your answer as correct because you are right.

Best regards.