Implementation of Motion Sensor
Here's how you use the Motion Sensor API in your app.
Verify that this API is supported
First, examine if the Motion Sensor API exists on your current webOS TV platform.
To that end, call the getSensorState
method and check the response to it.
var handle = webOS.service.request("luna://com.webos.service.mrcu", {
method: "sensor2/getSensorState",
parameters: {},
onSuccess: function (inResponse) {
console.log("Result: " + JSON.stringify(inResponse));
if (inResponse.isAlive === true) {
return;
}
},
//If isAlive is false, the motion sensor is in a sleep state. Wake up the
// Magic Remote to make the motion sensor operational. },
onFailure: function (inError) {
console.log("[" + inError.errorCode + "]: " + inError.errorText);
// If the errorCode is -1, the current webOS TV version may not support the
// Motion Sensor API. (For the cause of the error, check errorText).
// In that case, because the upgraded API is not supported, you can use the
// previous API
//(https://webostv.developer.lge.com/develop/references/magic-remote#).
},
});
-
If
returnValue
is true, you can use the Motion Sensor API. Check the value ofisAlive
. It indicates whether the motion sensor is currently operable. If the value ofisAlive
is true, a motion sensor is operable. If false, the motion sensor is in a sleep state, so you need to wake up the Magic Remote. Since the Magic Remote uses a battery, the sensor turns into the sleep state when the remote is not used for a certain period of time for efficiency. -
If
returnValue
is false anderrorCode
is -1, the platform version that you are using probably does not support the Motion Sensor API. To find out what causes the error, check theerrorText
.
Check the motion sensor event response interval
If it is confirmed that the Magic Remote's motion sensor is in an operable state,
check the motion sensor event response interval using the getSensorInterval
method as in the following example.
var handle = webOS.service.request("luna://com.webos.service.mrcu", {
method: "sensor2/getSensorInterval",
parameters: {},
onSuccess: function (inResponse) {
console.log("Result: " + JSON.stringify(inResponse));
},
onFailure: function (inError) {
console.log("Failed to get sensor interval");
},
});
minInterval
and maxInterval
represent the minimum and maximum configurable event response intervals,
and the unit is msec. If the interval is out of this range, errorText
will say it is a wrong interval.
interval
is the currently-set event response interval and can be set to a desired interval within the allowed range.
Set the motion sensor event response interval
Set the event response interval within the allowed range (10 ≤ interval
≤ 1000) by calling the setSensorInterval
method.
The unit is msec.
var handle = webOS.service.request("luna://com.webos.service.mrcu", {
method: "sensor2/setSensorInterval",
parameters: { interval: 10 },
onSuccess: function (inResponse) {
console.log("Result: " + JSON.stringify(inResponse));
},
onFailure: function (inError) {
console.log("[" + inError.errorCode + "]: " + inError.errorText);
// If errorCode is 1006 and errorText is "Wrong Callback Interval", it means a
// wrong interval error. interval is the currently-set event response interval
// and can be set to a desired interval within the allowed range.
},
});
Get the motion sensor event data
To get motion sensor event data, call the getSensorEventData
method.
When calling the getSensorEventData
method, you can choose to receive certain kinds of data, among various kinds, if necessary,
by adding names of the data you need with delimiters to sensorType
.
Set subscribe
to true to receive event data periodically at set intervals or false to receive only once.
If subscribe
is true already, meaning the method has been subscribed, you cannot make a duplicate subscription request,
so in such cases, errorText
will notify you that the method is already subscribed. If event data is no longer necessary,
you need to cancel the subscription.
The getSensorEventData
method can process requests from multiple clients. It can process interval
and different sensorType
requested by multiple clients at the same time. However, requests from maximum twenty clients can be processed, and if more,
an error response will be returned with errorText
saying that the number of requests exceeds the maximum count proceessable.
In such a case, unnecessary subscriptions need to be canceled.
var subscriptionHandle = webOS.service.request(
"luna://com.webos.service.mrcu",
{
method: "sensor2/getSensorEventData",
parameters: { sensorType: "all", subscribe: true },
onSuccess: function (inResponse) {
if (typeof inResponse.subscribed !== "undefined") {
if (!inResponse.subscribed) {
console.log("Failed to subscribe");
}
}
console.log("Result: " + JSON.stringify(inResponse));
// To-Do something
return;
},
onFailure: function (inError) {
console.log("[" + inError.errorCode + "]: " + inError.errorText);
// If errorCode is 1004 and errorText is "Already add subscription", it means
// that the method has been already subscribed. If motion sensor event data is
// no longer necessary, cancel the subscription.
// If errorCode is 1005 and errorText is "Over subscription app/service
// number", it means the number of processable request is used up. Cancel
// unnecessary subscriptions to make room for processing of requests.
},
}
);
// If you need to unsubscribe the data, use cancel() method as below.
subscriptionHandle.cancel();
Re-initialize the center of rotation
As the user rotates the Magic Remote, the center of rotation of the Magic Remote is sometimes missing,
so it becomes difficult to identify the rotation of the Magic Remote.
At the time, by calling resetQuaternion
, you can re-initialize the center of rotation.
Refer to the Motion Sensor sample app
and check the game rotation vector.
var handle = webOS.service.request("luna://com.webos.service.mrcu", {
method: "sensor2/resetQuaternion",
parameters: {},
onSuccess: function (inResponse) {
console.log("Result: " + JSON.stringify(inResponse));
},
onFailure: function (inError) {
console.log("Failed to get sensor interval");
},
});
Cancel the subscription
To cancel the subscription, call the cancelSensorDataSubscribe
method.
After cancellation, even if the Magic Remote is rotated, no more event data will be transmitted.
var handle = webOS.service.request("luna://com.webos.service.mrcu", {
method: "sensor2/cancelSensorDataSubscribe",
parameters: {},
onSuccess: function (inResponse) {
console.log("Result: " + JSON.stringify(inResponse));
return;
},
onFailure: function (inError) {
console.log("[" + inError.errorCode + "]: " + inError.errorText);
// If errorCode is 1003 and errorText is "Magic remote is not ready", it means
// the Magic Remote’s motion sensor is not operable. Make it ready and try
// again.
},
});