Reading accelerometer forces

How to

Read the accelerometer G-forces when the BlackBerry 10 device is tilted or moved. These forces allow for the screen to be aligned on the direction that the device is being held, either portrait or landscape.


static const int ACCELEROMETER_RATE = 25000;
float force_x, force_y, force_z;
int angle = 0; // Default orientation angle of the device.


if (!sensor_is_supported(SENSOR_TYPE_ACCELEROMETER)) {
    printf("Accelerometer not supported by device!");
    return EXIT_FAILURE;

sensor_set_skip_duplicates(SENSOR_TYPE_ACCELEROMETER, true);

while (!shutdown) {
    bps_event_t *event = NULL;
    bps_get_event(&event, 0);

    if (event) {
        if (bps_event_get_domain(event) == sensor_get_domain()) {
            if (SENSOR_ACCELEROMETER_READING == bps_event_get_code(event)) {

                // Handle accelerometer readings here
        } else if (bps_event_get_domain(event) == navigator_get_domain()) {
            switch (bps_event_get_code(event)) {
                // Get device orientation angle with respect to gravity.
                angle = navigator_event_get_orientation_angle(event);
                // Instruct the sensor service to remap sensor data to
                // match the device orientation.
                // This remaps data that is returned by the functions:
                //   - sensor_event_get_apr()
                //   - sensor_event_get_rotation_matrix()
                //   - sensor_event_get_rotation_xyz()

Build requirements

You must include the following header files from the BlackBerry Platform Services (BPS) library:

#include <bps/sensor.h>
#include <bps/bps.h>


The accelerometer detects motion in various directions. An accelerometer measures weight per unit of mass, a quantity known as g-force. It can be used in many apps, such as ones that control on-screen movement by tilting or moving the device.

The function bps_initialize() sets up the infrastructure of the BlackBerry Platform Services library to read accelerometer forces.

Before you start using the accelerometer service, you must ensure that the device supports it. This can be done through a simple call to sensor_is_supported(). This function is needed to distinguish between the actual device and the simulator.

The hardware update rate is the frequency at which the accelerometer data is updated from the hardware. It is the maximum waiting time (in microseconds) before taking an accelerometer reading. The sensor_set_skip_duplicates() function enables or disables the skipping of duplicate events that are received from the accelerometer.

The sensor_event_get_xyz() function returns the G-force values for the x, y, and z axes from the accelerometer hardware.

If your app supports rotation, you will want to map sensor values from the sensor coordinate system to the screen coordinate system, so that the sensor values are aligned to the orientation of the screen. This is done by getting the device orientation angle when a NAVIGATOR_ORIENTATION event is received, and then passing that angle to the function sensor_remap_coordinates() . Once the remap function has been called, sensor values returned by sensor_event_get_apr() , sensor_event_get_rotation_matrix() , and sensor_event_get_xyz() are mapped to the screen coordinate system. For more information, see Orientation.

Last modified: 2014-05-14

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

comments powered by Disqus