Supported sensors

Some devices might support a different set of sensors than others, so it's a good idea to verify what's available on a particular device. You can determine what sensors are available on a device by using the QSensor::sensorTypes() function. This function returns a list containing all the sensor types that are supported on the device.

Accelerometer

The accelerometer reports on changes to the acceleration of the device along three axes. This sensor is particularly useful if your application is designed to handle motion input from a user. For example, in a racing game, the steering could be controlled by the user tilting the device from side to side.

Depending on your application, you might want to configure the sensor to report only on certain types of movements by using the accelerationMode property. Possible values are Gravity (only movements caused by gravity), User (only movements caused by the user), and Combined (the default value).

The following example shows how you can capture readings from the accelerometer:

import bb.cascades 1.0
import QtMobility.sensors 1.2

Page {
    Container {
        Label {
            text: "Accel: " + accel.x.toFixed(3) + "  " +
                    accel.y.toFixed(3) + "  " +
                    accel.z.toFixed(3) + " m/s/s"
            textStyle.base: SystemDefaults.TextStyles.Text
            textStyle.fontSize: FontSize.Medium
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Left
            layoutProperties: StackLayoutProperties {}
        }   
    }
    
    attachedObjects: [
        Accelerometer {
            id: accel
            property double x: 0
            property double y: 0
            property double z: 0
            active: true
            onReadingChanged: {
                accel.x = reading.x;
                accel.y = reading.y;
                accel.z = reading.z;    
            }
        }
    ]
}
#include "SensorsApp.hpp"

#include <QtSensors/QAccelerometer>
#include <QtSensors/QAccelerometerReading>

using namespace bb::cascades;
using namespace QtMobility;

SensorsApp::SensorsApp(bb::cascades::Application *app)
: QObject(app)
{
    root = new Page;
    top_container = new Container;

    accel_label = new Label();
    top_container->add(accel_label);

	m_accel = new QAccelerometer(this);

    // If any Q_ASSERT statement(s) indicate that the slot failed
    // to connect to the signal, make sure you know exactly why
    // this has happened. This is not normal, and will cause your
    // app to stop working!!
    bool connectResult;
    
    // Since the variable is not used in the app, this is added
    // to avoid a compiler warning.
    Q_UNUSED(connectResult);
    
    connectResult = connect(m_accel, SIGNAL(readingChanged()),
                            this, SLOT(accelReadingChanged()));
						    
    // This is only available in Debug builds.
    Q_ASSERT(connectResult);
    
	m_accel->start();
}

void SensorsApp::accelReadingChanged()
{
    QAccelerometerReading *reading = m_accel->reading();
    qreal x = reading->x();
    qreal y = reading->y();
    qreal z = reading->z();
    QString data = QString("Accel: %1  %2  %3 m/s/s")
   	    .arg(x, 0, 'f', 3).arg(y, 0, 'f', 3).arg(z, 0, 'f', 3);
    accel_label->setText(data);
}

For more information, see QAccelerometer, QAccelerometerReading, and QAccelerometerFilter.

Ambient light sensor

The ambient light sensor returns a constant representing the current brightness of the external environment (dark, bright, sunny, and so on). This sensor is commonly used to help save the device battery power level by checking the ambient light level and adjusting the backlighting of the device accordingly.

The following example shows how you can capture readings from the ambient light sensor:

import bb.cascades 1.0
import QtMobility.sensors 1.2

Page {
    Container {
        Label {
            text: "Ambient Light Sensor: " + (als.data)
            textStyle.base: SystemDefaults.TextStyles.Text
            textStyle.fontSize: FontSize.Medium
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Left
        }
    }
    
    attachedObjects: [
        AmbientLightSensor {
            id: als
            property string data: ""
            active: true
            onReadingChanged: {
                if (reading.lightLevel == AmbientLightReading.Dark)
                    als.data = "Dark";
                else if (reading.lightLevel == AmbientLightReading.Twilight)
                    als.data = "Twilight";
                else if (reading.lightLevel == AmbientLightReading.Light)
                    als.data = "Light";
                else if (reading.lightLevel == AmbientLightReading.Bright)
                    als.data = "Bright";
                else if (reading.lightLevel == AmbientLightReading.Sunny)
                    als.data = "Sunny";
                else
                    als.data = "Undefined";
            }
        }
    ]
}
#include "SensorsApp.hpp"

#include <QtSensors/QAmbientLightSensor>
#include <QtSensors/QAmbientLightReading>

using namespace bb::cascades;
using namespace QtMobility;

SensorsApp::SensorsApp(bb::cascades::Application *app)
: QObject(app)
{
    root = new Page;
    top_container = new Container;

    als_label = new Label();
    top_container->add(als_label);

	m_als = new QAmbientLightSensor(this);
	
	// If any Q_ASSERT statement(s) indicate that the slot failed
    // to connect to the signal, make sure you know exactly why 
    // this has happened. This is not normal, and will cause your
    // app to stop working!!
	bool connectResult;
	
	// Since the variable is not used in the app, this is added
    // to avoid a compiler warning.
	Q_UNUSED(connectResult);
	
	connectResult = connect(m_als, 
	                        SIGNAL(readingChanged()), 
	                        this,
							SLOT(alsReadingChanged()));
							 
    // This is only available in Debug builds.
    Q_ASSERT(connectResult);
    
    m_als->start();
}

void SensorsApp::alsReadingChanged()
{
    QAmbientLightReading *reading = m_als->reading();
    QString data = QString ("Ambient Light Sensor: ");
    switch (reading->lightLevel())
    {
        case QAmbientLightReading::Undefined:
           data = data + QString ("Undefined");
           break;
        case QAmbientLightReading::Dark:
        	data = data + QString ("Dark");
        	break;
        case QAmbientLightReading::Twilight:
        	data = data + QString ("Twilight");
        	break;
        case QAmbientLightReading::Light:
        	data = data + QString ("Light");
            break;
        case QAmbientLightReading::Bright:
        	data = data + QString ("Bright");
        	break;
        case QAmbientLightReading::Sunny:
        	data = data + QString ("Sunny");
        	break;
    }
    als_label->setText(data);
}

For more information, see QAmbientLightSensor, QAmbientLightReading, and QAmbientLightFilter.

Compass

The compass sensor takes the 3D device orientation and turns it into a 2D device heading (otherwise known as the azimuth). The azimuth is a value in degrees from magnetic north in a clockwise direction. The reading also includes a value indicating the current calibration level of the device (a low calibration level can mean an inaccurate azimuth reading).

When the device is inverted (upside down), the output will be flipped so that the heading remains correct.

The following example shows how you can capture readings from the compass sensor:

import bb.cascades 1.0
import QtMobility.sensors 1.2

Page {
    Container {
        Label {
            text: "Compass Azimuth: " + compass.azimuth.toFixed(3) + 
                    " deg"
            textStyle.base: SystemDefaults.TextStyles.Text
            textStyle.fontSize: FontSize.Medium
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Left
        }
    }
    
    attachedObjects: [
        Compass {
            id: compass
            property double azimuth: 0
            active: true
            onReadingChanged: {
                compass.azimuth = reading.azimuth;
            }
        }
    ]
}
#include "SensorsApp.hpp"

#include <QtSensors/QCompass>
#include <QtSensors/QCompassReading>

using namespace bb::cascades;
using namespace QtMobility;

SensorsApp::SensorsApp(bb::cascades::Application *app)
: QObject(app)
{
    root = new Page;
    top_container = new Container;

    compass_label = new Label();
    top_container->add(compass_label);

	m_compass = new QCompass(this);
	
	// If any Q_ASSERT statement(s) indicate that the slot failed
    // to connect to the signal, make sure you know exactly why 
    // this has happened. This is not normal, and will cause your
    // app to stop working!!
	bool connectResult;
	
    // Since the variable is not used in the app, this is added
    // to avoid a compiler warning.
	Q_UNUSED(connectResult);
	
	connectResult = connect(m_compass, SIGNAL(readingChanged()), this,
							 SLOT(compassReadingChanged()));							 
							 
    // This is only available in Debug builds.
    Q_ASSERT(connectResult);
							 
	m_compass->start();
}

void SensorsApp::compassReadingChanged()
{
    QCompassReading *reading = m_compass->reading();
    qreal azimuth = reading->azimuth();
    QString data = QString("Azimuth: %1 deg")
   	    .arg(azimuth);
	compass_label->setText(data);
}

For more information, see QCompass, QCompassReading, and QCompassFilter.

Gyroscope

The gyroscope returns readings that measure the velocity of the device along three axes. Unlike the rotation reading, the gyroscope's readings represent the current angular velocity rather than a fixed rotation. The measurements are in degrees per second.

The following example shows how you can capture readings from the gyroscope:

import bb.cascades 1.0
import QtMobility.sensors 1.2

Page {
    Container {
        Label {
            text: "Gyroscope: " + gyro.x.toFixed(3) + "  " +
                    gyro.y.toFixed(3) + "  " + gyro.z.toFixed(3) +
                    " deg/s"
            textStyle.base: SystemDefaults.TextStyles.Text
            textStyle.fontSize: FontSize.Medium
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Left
        }
    }
    
    attachedObjects: [
        Gyroscope {
            id: gyro
            property double x: 0
            property double y: 0
            property double z: 0
            active: true
            onReadingChanged: {
                gyro.x = reading.x;
                gyro.y = reading.y;
                gyro.z = reading.z;
            }
        }
    ]
}
#include "SensorsApp.hpp"

#include <QtSensors/QGyroscope>
#include <QtSensors/QGyroscopeReading>

using namespace bb::cascades;
using namespace QtMobility;

SensorsApp::SensorsApp(bb::cascades::Application *app)
: QObject(app)
{
    root = new Page;
    top_container = new Container;

    gyro_label = new Label();
    top_container->add(gyro_label);

	m_gyro = new QGyroscope(this);
	
	// If any Q_ASSERT statement(s) indicate that the slot failed
    // to connect to the signal, make sure you know exactly why 
    // this has happened. This is not normal, and will cause your
    // app to stop working!!
	bool connectResult;
	
    // Since the variable is not used in the app, this is added
    // to avoid a compiler warning.
	Q_UNUSED(connectResult);
	
	connectResult = connect(m_gyro, SIGNAL(readingChanged()), this,
							 SLOT(gyroReadingChanged()));
							 
    // This is only available in Debug builds.
    Q_ASSERT(connectResult);
    
    m_gyro->start();
}

void SensorsApp::gyroReadingChanged()
{
    QGyroscopeReading *reading = m_gyro->reading();
    qreal x = reading->x();
    qreal y = reading->y();
    qreal z = reading->z();
    QString data = QString("Gyro: %1  %2  %3 deg/s")
   	    .arg(x, 0, 'f', 3).arg(y, 0, 'f', 3).arg(z, 0, 'f', 3);
    gyro_label->setText(data);
}

For more information, see QGyroscope, QGyroscopeReading, and QGyroscopeFilter.

Holster sensor

The holster sensor returns a Boolean value that indicates whether the device is currently in the holster or not.

The following example shows how you can capture readings from the holster:

import bb.cascades 1.0
import QtMobility.sensors 1.2

Page {
    Container {
        Label {
            text: "Holster Sensor: " + holster.holstered
            textStyle.base: SystemDefaults.TextStyles.Text
            textStyle.fontSize: FontSize.Medium
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Left
        }
    }
    
    attachedObjects: [
        HolsterSensor {
            id: holster
            property string holstered: null
            active: true
            onReadingChanged: {
                holster.holstered = reading.holstered;
                if (reading.holstered == true)
                    holster.holstered = "Holstered";
                else
                    holster.holstered = "Not Holstered";
            }
        }
    ]
}
#include "SensorsApp.hpp"

#include <QtSensors/QHolsterSensor>
#include <QtSensors/QHolsterReading>

using namespace bb::cascades;
using namespace QtMobility;

SensorsApp::SensorsApp(bb::cascades::Application *app)
: QObject(app)
{
    root = new Page;
    top_container = new Container;

    holster_label = new Label();
    top_container->add(holster_label);

	m_holster = new QHolsterSensor(this);
	
	// If any Q_ASSERT statement(s) indicate that the slot failed
    // to connect to the signal, make sure you know exactly why 
    // this has happened. This is not normal, and will cause your
    // app to stop working!!
	bool connectResult;
	
    // Since the variable is not used in the app, this is added
    // to avoid a compiler warning.
	Q_UNUSED(connectResult);
	
	connectResult = connect(m_holster, SIGNAL(readingChanged()), this,
							 SLOT(holsterReadingChanged()));
							 
    // This is only available in Debug builds.
    Q_ASSERT(connectResult);
    
    m_holster->start();
}

void SensorsApp::holsterReadingChanged()
{
    QHolsterReading *reading = m_holster->reading();
    QString data = QString("Holster Sensor: ");
    if (reading->holstered())
    	data = data + QString("Holstered");
    else
    	data = data + QString("Not Holstered");
    holster_label->setText(data);
}

For more information, see QHolsterSensor, QHolsterReading, and QHolsterFilter.

Infrared proximity sensor

The infrared proximity sensor is able to detect the proximity of a nearby object by emitting infrared light and detecting how much of the light the object reflects back to the device. The limitation to this type of sensor is that there is no reliable way to turn reflectance values into distances unless both the item that light is reflecting off and the ambient lighting conditions are known. For example, a material like black felt will absorb a lot of the inferred light, making the object appear farther away than it actually is.

This sensor is often used for face detection while a user is on a call, so that the user's cheek does not activate any controls.

The following example shows how you can capture readings from the infrared proximity sensor:

import bb.cascades 1.0
import QtMobility.sensors 1.2

Page {
    Container {
        Label {
            text: "IR Proximity Sensor: " + 
            		(irproximity.reflectance * 100).toFixed(3) + " %"
            textStyle.base: SystemDefaults.TextStyles.Text
            textStyle.fontSize: FontSize.Medium
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Left
        }
    }
    
    attachedObjects: [
        IRProximitySensor {
            id: irproximity
            property double reflectance: 0
            active: true
            onReadingChanged: {
                irproximity.reflectance = reading.reflectance
            }
        }
    ]
}
#include "SensorsApp.hpp"

#include <QtSensors/QIRProximitySensor>
#include <QtSensors/QIRProximityReading>

using namespace bb::cascades;
using namespace QtMobility;

SensorsApp::SensorsApp(bb::cascades::Application *app)
: QObject(app)
{
    root = new Page;
    top_container = new Container;

    irProx_label = new Label();
    top_container->add(irProx_label);

	m_irProx = new QIRProximitySensor(this);
	
	// If any Q_ASSERT statement(s) indicate that the slot failed
    // to connect to the signal, make sure you know exactly why 
    // this has happened. This is not normal, and will cause your
    // app to stop working!!
	bool connectResult;
	
    // Since the variable is not used in the app, this is added
    // to avoid a compiler warning.
	Q_UNUSED(connectResult);
	
	connectResult = connect(m_irProx, SIGNAL(readingChanged()), this,
							 SLOT(irProxReadingChanged()));
							 
    // This is only available in Debug builds.
    Q_ASSERT(connectResult);
    
    m_irProx->start();
}

void SensorsApp::irProxReadingChanged()
{
    QIRProximityReading *reading = m_irProx->reading();
    qreal reflectance = reading->reflectance();
    QString data = QString("IR Proximity Sensor: %1  %")
   	    .arg(reflectance, 0, 'f', 3);
    irProx_label->setText(data);
}

For more information, see QIRProximitySensor, QIRProximityReading, and QIRProximityFilter.

Light sensor

The light sensor returns a value representing the actual illuminance of the external environment. Values are measured in lux.

The following example shows how you can capture readings from the light sensor:

import bb.cascades 1.0
import QtMobility.sensors 1.2

Page {
    Container {
        Label {
            text: "Light: " + light.lux.toFixed(3) + " lux"
            textStyle.base: SystemDefaults.TextStyles.Text
            textStyle.fontSize: FontSize.Medium
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Left
        }
    }
    
    attachedObjects: [
        LightSensor {
            id: light
            property double lux: 0
            active: true
            onReadingChanged: {
                light.lux = reading.lux;
            }
        } 
    ]
}
#include "SensorsApp.hpp"

#include <QtSensors/QLightSensor>
#include <QtSensors/QLightReading>

using namespace bb::cascades;
using namespace QtMobility;

SensorsApp::SensorsApp(bb::cascades::Application *app)
: QObject(app)
{
    root = new Page;
    top_container = new Container;

    light_label = new Label();
    top_container->add(light_label);

	m_light = new QLightSensor(this);
	
	// If any Q_ASSERT statement(s) indicate that the slot failed
    // to connect to the signal, make sure you know exactly why 
    // this has happened. This is not normal, and will cause your
    // app to stop working!!
	bool connectResult;
	
    // Since the variable is not used in the app, this is added
    // to avoid a compiler warning.
	Q_UNUSED(connectResult);
	
	connectResult = connect(m_light, SIGNAL(readingChanged()), this,
							 SLOT(lightReadingChanged()));
							 
    // This is only available in Debug builds.
    Q_ASSERT(connectResult);
							 
	m_light->start();
}

void SensorsApp::lightReadingChanged()
{
    QLightReading *reading = m_light->reading();
    qreal lux = reading->lux();
    QString data = QString("Light: %1 lux")
      	    .arg(lux);
    light_label->setText(data);
}

For more information, see QLightSensor, QLightReading, and QLightFilter.

Magnetometer

The magnetometer is used for measuring the strength and direction of magnetic fields. Unlike the compass, readings are returned as magnetic flux density values along three axes, in teslas. This sensor supports both raw magnetic flux values or geomagnetic flux values, with raw magnetic flux values being the default. Unlike raw magnetic flux values, geomagnetic flux values are processed to compensate for local interference, and provide data based on the Earth's magnetic field only.

The following example shows how you can capture readings from the magnetometer:

import bb.cascades 1.0
import QtMobility.sensors 1.2

Page {
    Container {
        Label {
            text: "Mag: " + mag.x.toFixed(3) + "  " + mag.x.toFixed(3) 
                   + "  " + mag.x.toFixed(3) + " μTesla"
            textStyle.base: SystemDefaults.TextStyles.Text
            textStyle.fontSize: FontSize.Medium
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Left
        }
    }
    
    attachedObjects: [
        Magnetometer {
            id: mag
            property double x: 0
            property double y: 0
            property double z: 0
            active: true
            onReadingChanged: {
                mag.x = reading.x * 1000000;
                mag.y = reading.y * 1000000;
                mag.z = reading.z * 1000000;
            }
        }
    ]
}
#include "SensorsApp.hpp"

#include <QtSensors/QMagnetometer>
#include <QtSensors/QMagnetometerReading>

using namespace bb::cascades;
using namespace QtMobility;

SensorsApp::SensorsApp(bb::cascades::Application *app)
: QObject(app)
{
    root = new Page;
    top_container = new Container;

    mag_label = new Label();
    top_container->add(mag_label);

	m_mag = new QMagnetometer(this);
	
	// If any Q_ASSERT statement(s) indicate that the slot failed
    // to connect to the signal, make sure you know exactly why 
    // this has happened. This is not normal, and will cause your
    // app to stop working!!
	bool connectResult;
	
    // Since the variable is not used in the app, this is added
    // to avoid a compiler warning.
	Q_UNUSED(connectResult);
	
	connectResult = connect(m_mag, SIGNAL(readingChanged()), this,
							 SLOT(magReadingChanged()));
							 
    // This is only available in Debug builds.
    Q_ASSERT(connectResult);
	
	m_mag->start();
}

void SensorsApp::magReadingChanged()
{
    QMagnetometerReading *reading = m_mag->reading();
    qreal x = reading->x() * 1000000;
    qreal y = reading->y() * 1000000;
    qreal z = reading->z() * 1000000;
    QString data = QString("Mag: %1  %2  %3 uTesla")
   	    .arg(x, 0, 'f', 3).arg(y, 0, 'f', 3).arg(z, 0, 'f', 3);
    mag_label->setText(data);
}

For more information, see QMagnetometer, QMagnetometerReading, and QMagnetometerFilter.

Orientation sensor

The orientation sensor reports on the orientation of the device. You can detect which side of the device is pointing up, and whether the device is face up or face down. Since this sensor operates below the UI level, it's not able to detect the UI orientation. For information on handling app orientation, see Orientation.

The following example shows how you can capture readings from the orientation sensor:

import bb.cascades 1.0
import QtMobility.sensors 1.2

Page {
    Container {
        Label {
            text: "Orientation: " + orientation.data
            textStyle.base: SystemDefaults.TextStyles.Text
            textStyle.fontSize: FontSize.Medium
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Left
        }
    }
    
    attachedObjects: [
        OrientationSensor {
            id: orientation
            property string data: ""
            active: true
            onReadingChanged: {
                if (reading.orientation == OrientationReading.TopUp)
                    orientation.data = "Top Up";
                else if (reading.orientation == OrientationReading.TopDown)
                    orientation.data = "Top Down";
                else if (reading.orientation == OrientationReading.LeftUp)
                    orientation.data = "Left Up";
                else if (reading.orientation == OrientationReading.RightUp)
                    orientation.data = "Right Up";
                else if (reading.orientation == OrientationReading.FaceUp)
                    orientation.data = "Face Up";
                else if (reading.orientation == OrientationReading.FaceDown)
                    orientation.data = "Face Down";
                else
                    orientation.data = "Undefined";
            }
        }
    ]
}
#include "SensorsApp.hpp"

#include <QtSensors/QOrientationSensor>
#include <QtSensors/QOrientationReading>

using namespace bb::cascades;
using namespace QtMobility;

SensorsApp::SensorsApp(bb::cascades::Application *app)
: QObject(app)
{
    root = new Page;
    top_container = new Container;

    orientation_label = new Label();
    top_container->add(orientation_label);

	m_orientation = new QOrientationSensor(this);
	
	// If any Q_ASSERT statement(s) indicate that the slot failed
    // to connect to the signal, make sure you know exactly why 
    // this has happened. This is not normal, and will cause your
    // app to stop working!!
	bool connectResult;
	
    // Since the variable is not used in the app, this is added
    // to avoid a compiler warning.
	Q_UNUSED(connectResult);
	
	connectResult = connect(m_orientation, SIGNAL(readingChanged()),
						this, SLOT(orientationReadingChanged()));

    // This is only available in Debug builds.
    Q_ASSERT(connectResult);

	m_orientation->start();
}

void SensorsApp::orientationReadingChanged()
{
    QOrientationReading *reading = m_orientation->reading();
    QString data = QString ("Orientation: ");
    switch (reading->orientation())
    {
        case QOrientationReading::Undefined:
           data = data + QString ("Undefined");
           break;
        case QOrientationReading::TopUp:
        	data = data + QString ("Top Up");
        	break;
        case QOrientationReading::TopDown:
        	data = data + QString ("Top Down");
        	break;
        case QOrientationReading::LeftUp:
        	data = data + QString ("Left Up");
            break;
        case QOrientationReading::RightUp:
        	data = data + QString ("Right Up");
        	break;
        case QOrientationReading::FaceUp:
        	data = data + QString ("Face Up");
        	break;
        case QOrientationReading::FaceDown:
            data = data + QString ("Face Down");
            break;
    }
    orientation_label->setText(data);
}

For more information, see QOrientationSensor, QOrientationReading, and QOrientationFilter.

Proximity sensor

The proximity sensor determines whether an object is close to the device or not. Unlike the IR proximity sensor, which returns a raw reflectance value, the proximity sensor returns a Boolean that specifies whether an object is close or far away. The proximity is still based on reflectance, but takes other factors into consideration when determining the value.

The following example shows how you can capture readings from the proximity sensor:

import bb.cascades 1.0
import QtMobility.sensors 1.2

Page {
    Container {
        Label {
            text: "Proximity: " + prox.data
            textStyle.base: SystemDefaults.TextStyles.Text
            textStyle.fontSize: FontSize.Medium
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Left
        }
    }
    
    attachedObjects: [
        ProximitySensor {
            id: prox
            property string data: ""
            active: true
            onReadingChanged: {
                if (reading.close == true)
                    prox.data = "Close";
                else
                    prox.data = "Far";
            }
        }
    ]
}
#include "SensorsApp.hpp"

#include <QtSensors/QProximitySensor>
#include <QtSensors/QProximityReading>

using namespace bb::cascades;
using namespace QtMobility;

SensorsApp::SensorsApp(bb::cascades::Application *app)
: QObject(app)
{
    root = new Page;
    top_container = new Container;

    prox_label = new Label();
    top_container->add(prox_label);

	m_prox = new QProximitySensor(this);
	
	// If any Q_ASSERT statement(s) indicate that the slot failed
    // to connect to the signal, make sure you know exactly why 
    // this has happened. This is not normal, and will cause your
    // app to stop working!!
	bool connectResult;
	
    // Since the variable is not used in the app, this is added
    // to avoid a compiler warning.
	Q_UNUSED(connectResult);
	
	connectResult = connect(m_prox, SIGNAL(readingChanged()), this,
							 SLOT(proxReadingChanged()));
							 
    // This is only available in Debug builds.
    Q_ASSERT(connectResult);
	
	m_prox->start();
}

void SensorsApp::proxReadingChanged()
{
    QProximityReading *reading = m_prox->reading();
    QString data = QString ("Proximity: ");
    if(reading->close() == true)
    	data = data + QString ("Close");
    else
    	data = data + QString ("Far");
    prox_label->setText(data);
}

For more information, see QProximitySensor, QProximityReading, and QProximityFilter.

Rotation sensor

The rotation sensor returns a reading containing three angles, measured in degrees, that define the orientation of the device in three-dimensional space. These angles are similar to yaw, pitch, and roll but are defined using only right-hand rotation with axes as defined by the right-hand Cartesian coordinate system.

The following example shows how you can capture readings from the rotation sensor:

import bb.cascades 1.0
import QtMobility.sensors 1.2

Page {
    Container {
        Label {
            text: "Rotation: " + rotation.x.toFixed(3) + "  " + 
                    rotation.y.toFixed(3) + "  " + 
                    rotation.z.toFixed(3) + " deg"
            textStyle.base: SystemDefaults.TextStyles.Text
            textStyle.fontSize: FontSize.Medium
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Left
        }
    }
    
    attachedObjects: [
        RotationSensor {
            id: rotation
            property double x: 0
            property double y: 0
            property double z: 0
            active: true
            onReadingChanged: {
                rotation.x = reading.x;
                rotation.y = reading.y;
                rotation.z = reading.z;
            }
        }
    ]
}
#include "SensorsApp.hpp"

#include <QtSensors/QRotationSensor>
#include <QtSensors/QRotationReading>

using namespace bb::cascades;
using namespace QtMobility;

SensorsApp::SensorsApp(bb::cascades::Application *app)
: QObject(app)
{
    root = new Page;
    top_container = new Container;

    rotation_label = new Label();
    top_container->add(rotation_label);

    //RotationSensor
	m_rotation = new QRotationSensor(this);
	
	// If any Q_ASSERT statement(s) indicate that the slot failed
    // to connect to the signal, make sure you know exactly why 
    // this has happened. This is not normal, and will cause your
    // app to stop working!!
	bool rotation_res = connect(m_rotation, SIGNAL(readingChanged()),
                        this, SLOT(rotationReadingChanged()));

    // This is only available in Debug builds.
    Q_ASSERT(rotation_res);
	
    // Since the variable is not used in the app, this is added
    // to avoid a compiler warning.
	Q_UNUSED(rotation_res);
	m_rotation->start();
}

void SensorsApp::rotationReadingChanged()
{
    QRotationReading *reading = m_rotation->reading();
    qreal x = reading->x();
    qreal y = reading->y();
    qreal z = reading->z();
    QString data = QString("Rotation: %1  %2  %3 deg")
   	    .arg(x, 0, 'f', 3).arg(y, 0, 'f', 3).arg(z, 0, 'f', 3);
    rotation_label->setText(data);
}

For more information, see QRotationSensor, QRotationReading, and QRotationFilter.

Related sample apps

  • SensorDemo - A collection of pages that demonstrate how to process data from various sensors.
  • accelgame - A game that demonstrates how to use the accelerometer to navigate through a maze.
  • compass - A basic compass that uses readings from the compass sensor.

More samples are available in GitHub at Cascades-Samples and Qt2Cascades-Samples.

Last modified: 2013-12-21



Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus