Accelerometer sample app
The accelerometer sample application demonstrates how you can gather values from the accelerometer in a BlackBerry device that has an accelerometer. You can then apply the values to move an image of a ball on the screen of the BlackBerry device.
By holding the BlackBerry device parallel to the ground and tilting it in any direction, the image of the ball moves by reacting to the force of gravity and the programmatically defined coefficient of friction. This process simulates a ball rolling around a tilting table. When the ball hits the edge of the screen, the ball bounces off the edge as if it were a barrier.
In addition to demonstrating how to gather data from the BlackBerry device's accelerometer, the sample application also provides some basic gravity-simulating algorithms that you might find useful when you develop gravity-based games.
If the BlackBerry device that is running the sample application does not have an accelerometer, the sample application displays a dialog box with the message This device does not support accelerometer. and the sample application closes.
However, if the BlackBerry Smartphone Simulator is running the sample application, the sample application generates random accelerometer readings to simulate someone tilting the BlackBerry device.
This sample features the following classes:
This class provides methods that you can use to check if the BlackBerry device has an accelerometer and to get readings from the accelerometer. To get accelerometer readings, you must first open a Channel to the accelerometer. There are two types of preconfigured channels that the following two methods can open:
- openOrientationChannel(): This method opens a Channel that returns constants that specify the orientation of the BlackBerry device.
- openRawDataChannel(): This method opens a Channel that receives data that specifies the orientation of the BlackBerry device along the x, y, and z axes.
This class also provides a method, openChannel(), to open a channel with a specific configuration that the AccelerometerChannelConfig object specifies. You can use this method to open an orientation or raw data Channel to specify whether the Channel should receive accelerometer readings while the application is in foreground mode or background mode. You can also use this method to limit the number of readings that you want to get from the accelerometer. Limiting the context in which the BlackBerry device should receive accelerometer readings is useful in conserving battery life.
The sample application uses the raw data channel exclusively since the trajectory of the image of the rolling ball is affected by the minute changes in the BlackBerry device's relationship with the x, y, and z axes. The sample application also uses the isAccelerometerSupported() method to check if the BlackBerry device that is running the sample application has an accelerometer.
This class provides methods to close a connection to the accelerometer, retrieve data from the accelerometer, check the state of the accelerometer channel (for example, whether it is open or active), and to set or remove an AccelerometerListener object.
The sample application uses this class to retrieve raw data readings from the accelerometer. Because the sample application continuously queries the accelerometer and does not just passively react to changes in the accelerometer, the sample application queries the readings using a loop in a Thread and not through an AccelerometerListener object.
Install the sample app
Visit http://www.blackberry.com/go/accelerometersample to download the sample application.
Extract the accelerometerdemo.zip file.
On the taskbar, click Start > Programs > Research In Motion > BlackBerry JDE 4.7.0 > JDE.
Open the workspace that you want to add the sample application to.
In the workspace tree, right-click the project that you want to add the sample application to.
Click Add project to <workspace directory>.
In the Add project to <workspace> dialog box, browse to the folder where you extracted the sample application.
Click the AcceleratometerDemo.jdp file.
Run the sample app
In the workspace where you added the accelerometerdemo project, right-click accelerometerdemo.
Click Build project.
On the taskbar, click Start > Applications > Research in Motion > BlackBerry JDE 4.7.0 > Device Simulator.
On the Home screen of the BlackBerry Smartphone Simulator, click the Downloads folder.
Click the accelerometerdemo icon.
In the accelerometer sample application, press the Menu key.