Reading accelerometer forces

How to

Read the accelerometer G-forces when the BlackBerry PlayBook tablet 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;


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

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 BlackBerry Tablet 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.