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.
- Import the following classes:
- net.rim.device.api.system.AccelerometerSensor.Channel;
- net.rim.device.api.system.AccelerometerSensor;
- 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() );
- Create an array to store the accelerometer data.
short[] xyz = new short[ 3 ];
- Create a thread.
while( running ) {
- Invoke Channel.getLastAccelerationData(short[]) to retrieve data from the accelerometer.
rawDataChannel.getLastAccelerationData( xyz );
- Invoke Thread.sleep() to specify the interval between queries to the accelerometer, in milliseconds.
Thread.sleep( 500 );
- Invoke Channel.close() to close the channel to the accelerometer.
rawDataChannel.close();
Was this information helpful? Send us your comments.