This module provides easy access to sensors on the device. There are 8 types of sensors supported by this module, namely:

  • Accelerometer Sensor
  • Gyroscope sensor
  • Magnetometer sensor
  • Gravity Sensor
  • User Acceleration Sensor
  • Rotation sensor
  • Step Counter Sensor
  • Pressure sensor

Besides being able to read sensor data, this module can also monitor changes in state of the battery and network connectivity

Use startListening to get continual sensor data updates. And use stopListening to stop getting continual sensor data updates.

You need to add a reference to "Fuse.Sensor" in your project file to use this feature.

This module is an EventEmitter, so the methods from EventEmitter can be used to listen to events.

Please note that this module will not work on Desktop Preview. When running on the device, not all devices have a complete sensor hardware, so not all sensor output data can be obtained, it all depends on the availability of sensors on the device. Make sure to check "error" event for possible error that encounter.

Example

The following example shows how to access accelerometer sensor:

<JavaScript>
    var Observable = require("FuseJS/Observable");
    var Sensor = require("FuseJS/Sensor");
    var accelerometerData = Observable("")
    var errorMessage = Observable("")

    Sensor.on("error", function(failMessage) {
        errorMessage.value = failMessage;
    });

    Sensor.on("changed", function(data) {
        if (data.type == Sensor.ACCELEROMETER) {
            accelerometerData.value = "X axis : " + data.x + " Y axis : " + data.y + " Z axis : " + data.z;
        }
    });

    function startAccelerometerContinuousListener() {
        Sensor.startListening(Sensor.ACCELEROMETER);
    }

    function stopAccelerometerContinuousListener() {
        Sensor.stopListening(Sensor.ACCELEROMETER);
    }

    module.exports = {
        startAccelerometerContinuousListener,
        stopAccelerometerContinuousListener,
        accelerometerData,
        errorMessage
    };
</JavaScript>

<StackPanel ItemSpacing="5" Margin="0,30,0,0">
    <Text>Accelerometer data :</Text>
    <Text Value="{accelerometerData}" />
    <Text Value="{errorMessage}" Color="Red" />

    <Button Text="Start continuous Accelerometer listener" Clicked="{startAccelerometerContinuousListener}" />
    <Button Text="Stop continuous Accelerometer listener" Clicked="{stopAccelerometerContinuousListener}" />
</StackPanel>

In the above example we're using "changed" event. Data returned by this module are JavaScript objects of the following form:

{
    type: sensor type (in this case is Sensor.ACCELEROMETER),
    x: value of x axis,
    y: value of y axis,
    z: value of z axis,
}

Output

Data returned on the "changed" event argument are JavaScript objects with always have type property. Value of type property determine what type sensor data it contains.

Accelerometer, Gyroscope, Magnetometer, Gravity, User Acceleration and Rotation data all have same form of JavaScript object as desribed in the example below:

var Sensor = require("FuseJS/Sensor")
Sensor.on('changed', function(data) {
    switch (data.type) {
        case Sensor.ACCELEROMETER:
            console.log("X axis : " + data.x + " Y axis : " + data.y + " Z axis : " + data.z);
            break;
        case Sensor.GYROSCOPE:
            console.log("X axis : " + data.x + " Y axis : " + data.y + " Z axis : " + data.z);
            break;
        case Sensor.MAGNETOMETER:
            console.log("X axis : " + data.x + " Y axis : " + data.y + " Z axis : " + data.z);
            break;
        case Sensor.GRAVITY:
            console.log("X axis : " + data.x + " Y axis : " + data.y + " Z axis : " + data.z);
            break;
        case Sensor.USER_ACCELERATION:
            console.log("X axis : " + data.x + " Y axis : " + data.y + " Z axis : " + data.z);
            break;
        case Sensor.ROTATION:
            console.log("X axis : " + data.x + " Y axis : " + data.y + " Z axis : " + data.z);
            break;
    }
});

function startListeningSensor() {
    Sensor.startListening(Sensor.ACCELEROMETER);
    Sensor.startListening(Sensor.GYROSCOPE);
    Sensor.startListening(Sensor.MAGNETOMETER);
    Sensor.startListening(Sensor.GRAVITY);
    Sensor.startListening(Sensor.USER_ACCELERATION);
    Sensor.startListening(Sensor.ROTATION);
}

function stopListeningSensor() {
    Sensor.stopListening(Sensor.ACCELEROMETER);
    Sensor.stopListening(Sensor.GYROSCOPE);
    Sensor.stopListening(Sensor.MAGNETOMETER);
    Sensor.stopListening(Sensor.GRAVITY);
    Sensor.stopListening(Sensor.USER_ACCELERATION);
    Sensor.stopListening(Sensor.ROTATION);
}

Step counter and pressure data has slightly different output JavaScript object as described in the example below:

var Sensor = require("FuseJS/Sensor")
Sensor.on('changed', function(data) {
    switch (data.type) {
        case Sensor.STEP_COUNTER:
            console.log("Num Steps taken : " + data.x);
            break;
        case Sensor.PRESSURE:
            console.log("Pressure in hPa / mbar : " + data.x);
            console.log("Relative Altitude (iOS only) in meters : " + data.y);
            break;
    }
});

function startListeningSensor() {
    Sensor.startListening(Sensor.STEP_COUNTER);
    Sensor.startListening(Sensor.PRESSURE);
}

function stopListeningSensor() {
    Sensor.stopListening(Sensor.STEP_COUNTER);
    Sensor.stopListening(Sensor.PRESSURE);
}

Lastly, monitoring state changes of battery or network connectivity has output JavaScript object as follow:

var Sensor = require("FuseJS/Sensor")
Sensor.on('changed', function(data) {
    switch (data.type) {
        case Sensor.BATTERY:
            console.log("Battery level : " + data.level);
            console.log("Battery state : " + data.state); // possible values : charging, unplug, full, not charging, unknown
            break;
        case Sensor.CONNECTION_STATE:
            console.log("connection state : " + data.state); // boolan value : true connected, false disconnected
            console.log("connection state string : " + data.stateString); // possible values : 'connected' or 'disconnected'
            break;
    }
});

function startMonitoringState() {
    Sensor.startListening(Sensor.BATTERY);
    Sensor.startListening(Sensor.CONNECTION_STATE);
}

function stopMonitoringState() {
    Sensor.stopListening(Sensor.BATTERY);
    Sensor.stopListening(Sensor.CONNECTION_STATE);
}

To handle errors from Sensor we can listen to the "error" event, as follows:

var Sensor = require("FuseJS/Sensor")
Sensor.on("error", function(err) { ... })

Location

Namespace
Fuse.Sensor
Package
Fuse.Sensor 2.9.1
Show Uno properties and methods

Interface of SensorModule

Inherited from NativeEventEmitterModule

Emit(object[]) uno

Call emit with the given arguments on the underlying JS EventEmitter.

Inherited from NativeModule

Inherited from Module

GetFile : FileSource uno

Returns the file source that will be watched by the Context for changes in Fuse preview. Override in subclasses and return correct file source to support live updates in Fuse preview.

Inherited from object

Attached UX Attributes

GlobalKey (attached by Resource) : string ux

The ux:Global attribute creates a global resource that is accessible everywhere in UX markup.

Implemented Interfaces