Best practices

Checking for the existence of sensors

The sensors that are available to your application may vary depending on which device your application is running on. You should verify that a sensor exists before attempting to use it in your application. You do this in BPS by passing the appropriate sensor_type_t to sensor_is_supported().

The following code snippet demonstrates how to verify whether the accelerometer is supported:
if (!sensor_is_supported(SENSOR_TYPE_ACCELEROMETER)) {    
    printf("Accelerometer not supported by device! Exiting program.");
    bps_shutdown();
    return EXIT_FAILURE;
}
            
In the above example, if sensor_is_supported() returns true, then you can use the accelerometer in your application.

Filtering sensor readings

Device sensors can generate a lot of data. There are two primary ways to control the data that is used by your program to tune your app's performance:
  1. Setting the sensor's refresh rate. This setting is directly tied to how often sensor data is reported. The higher the rate, the more processing that is required.
  2. Skipping duplicate sensor data. Removing duplicate data means less data needs to be processed, which improves performance.

A sensor's refresh rate is the rate at which the sensor provides updates. You can set a sensor's rate by calling sensor_set_rate() specifying the appropriate sensor_type_t and how often (in microseconds) the sensor should try to provide updates. The device attempts to achieve the sensor rate you specify, but this is not guaranteed. Updates may occur more or less frequently because of different internal hardware sensor rates, or other applications using a different rate. For example, if you set a sensor's rate to 50Hz, and the closest supported rate is 48Hz, the rate will automatically be adjusted to 48Hz. Call the function sensor_info() to retrieve information about the sensor, including the minimum, maximum, and default sensor rates (also referred to as delays).

The following code snippet demonstrates setting the refresh rate of the accelerometer to 250000 microseconds (or four times a second):
if (sensor_set_rate(SENSOR_TYPE_ACCELEROMETER, 250000) == BPS_SUCCESS)
{
  // rate successfully set
} else {
  // rate was not set
}
            

You can call sensor_set_skip_duplicates() to reduce the number of sensor updates. When this function is called with enable_skipdup set to true, successive readings with the same or very similar values are omitted and skipped. Ignoring similar values is necessary because some sensors (for example, the accelerometer) can report very small changes in their readings even if the device is stationary.

The following code snippet requests that the device should attempt to skip duplicate events from the accelerometer:
if (sensor_set_skip_duplicates(SENSOR_TYPE_ACCELEROMETER, true) == BPS_SUCCESS)
{
  // rate successfully set
} else {
  // rate was not set
}                
            

Avoiding overuse of expensive sensor functions

As discussed in the Influences on sensors section, the Earth's magnetic field has an impact on the readings taken by various sensors. The function wmm_get_geomagnetic_field() returns the geomagnetic field for the specified location and time, but it is an expensive call that should only be called when the location of the device has changed, and not on every sensor sample. Your application might handle a location change in a BPS event handling loop by calling a function that updates a global variable containing the geomagnetic field value returned by wmm_get_geomagnetic_field(). Using a globally accessible instance of the geomagnetic field value in your app avoids the need to call wmm_get_geomagnetic_field() in multiple places.

Using sensors with the simulator

If you are running your app on the BlackBerry 10 Device Simulator, you can simulate the following sensor events/data by using the controller application:
  • device orientation
  • device tilt
  • device rotation
  • proximity sensor values
  • ambient light sensor values
For more information on using the controller application with the simulator, see Using the simulator.

Last modified: 2013-12-21

comments powered by Disqus