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 a vector with three elements.
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.
rawDataChannel.close();
Query the accelerometer when the application is in the foreground
1.
Import the following classes:
•
net.rim.device.api.system.AccelerometerChannelConfig
•
net.rim.device.api.system.AccelerometerSensor.Channel
2.
Open a channel to retrieve acceleration data from the accelerometer.
Channel channel = AccelerometerSensor.openRawAccelerationChannel
( Application.getApplication());
3.
Invoke
Thread.sleep()
to specify the interval between queries to the accelerometer, in milliseconds.
short[] xyz = new short[ 3 ];
while( running ) {
channel.getLastAccelerationData( xyz );
Thread.sleep( 500 );
}
Development Guide
Working with the accelerometer of a BlackBerry device
13