Retrieve accelerometer data at specific intervals

If a BlackBerry® device application opens a channel to the accelerometer when the application is in the foreground, when the application is in the background, the channel pauses and the accelerometer is not queried. If a BlackBerry device application invokes AccelerometerSensor.Channel.getLastAccelerationData(short[]) at close intervals or when the BlackBerry device is not in motion, the method might return duplicate values.
  1. Import the following classes:
    • net.rim.device.api.system.AccelerometerSensor.Channel;
    • net.rim.device.api.system.AccelerometerSensor;
  2. Open a channel to the accelerometer. While a channel is open, the BlackBerry device application will query the accelerometer for information.
    Channel rawDataChannel = AccelerometerSensor.openRawDataChannel( Application.getApplication() );
  3. Create an array to store the accelerometer data.
    short[] xyz = new short[ 3 ];
  4. Create a thread.
     while( running ) {
  5. Invoke Channel.getLastAccelerationData(short[]) to retrieve data from the accelerometer.
    rawDataChannel.getLastAccelerationData( xyz );
  6. Invoke Thread.sleep() to specify the interval between queries to the accelerometer, in milliseconds.
    Thread.sleep( 500 );
  7. Invoke Channel.close() to close the channel to the accelerometer.

Was this information helpful? Send us your comments.