Accessing the Accelerometer
What is this: Your will learn how to access the Accelerometer
Currently the emulator does not support the emulation of an accelerometer (it just returns [0, 0, 0] what is a Free-Fall
). But when the functionality is available I will build a Class, where you can register, for special events, like, when the device is being shaken or similar.Difficulty: 2 of 5
Description: The accelerometer values indicate the acceleration in units of 1 = 1g = 9.8 m/s[sup]2[/sup] along the X, Y, and Z axes. With the device lying flat on a horizontal surface in front of the user, oriented so the screen is readable by the user in the normal fashion, the X axis goes from left to right, the Y axis goes from the user toward the device, and the Z axis goes upwards perpendicular to the surface.
Acceleration is defined such that a device in free fall has an accleration value of (0, 0, 0). Thus a stationary device will have an acceleration value of (0, 0, -1) since it is being acted on with 1g of force by the Earth's gravity. When the device is stationary, the acceleration vector will thus indicate the downward direction.
0.) So I created a small wrapper around the basic-functionality of Sensor-Reading, that will save you some lines:
Using java Syntax Highlighting
- import android.hardware.Sensors;
- public class AccelerometerReader {
- /** True when the Accelerometer-functionality is basically available. */
- boolean accelerometerAvailable = false;
- boolean isEnabled = false;
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
1.) Constructing a AccelerometerReader which does some initial work:
Using java Syntax Highlighting
- /**
- * Sets up an AccelerometerReader. Checks if Accelerometer is available on
- * this device and throws UnsupportedOperationException if not .
- *
- * @param doEnable :
- * enables the devices Accelerometer
- * initially (if sensor available)
- * @throws UnsupportedOperationException
- * if Accelerometer is not available on this device.
- */
- public AccelerometerReader(boolean doEnable)
- throws UnsupportedOperationException {
- /* Check once here in the constructor if an
- * Accelerometer is available on this device. */
- for (String aSensor : Sensors.getSupportedSensors())
- if (aSensor.equals(Sensors.SENSOR_ACCELEROMETER))
- accelerometerAvailable = true;
- if (!accelerometerAvailable)
- throw new UnsupportedOperationException(
- "Accelerometer is not available.");
- if (doEnable)
- setEnableAccelerometer(true);
- }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
2.) Dis/Enabling of the underlying Accelerometer:
Using java Syntax Highlighting
- /**
- * En/Dis-able the Accelerometer.
- *
- * @param doEnable
- * <code>true</code> for enable.<br>
- * <code>false</code> for disable.
- * @throws UnsupportedOperationException
- */
- public void setEnableAccelerometer(boolean doEnable)
- throws UnsupportedOperationException {
- if (!accelerometerAvailable)
- throw new UnsupportedOperationException(
- "Accelerometer is not available.");
- /* If should be enabled and not already is: */
- if (doEnable && !this.isEnabled) {
- Sensors.enableSensor(Sensors.SENSOR_ACCELEROMETER);
- this.isEnabled = true;
- } else /* If should be disabled and not already is: */
- if (!doEnable && this.isEnabled) {
- Sensors.disableSensor(Sensors.SENSOR_ACCELEROMETER);
- this.isEnabled = false;
- }
- }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
3.) Readign out the actual values:
Using java Syntax Highlighting
- /**
- * Read out the values currently provided by the Accelerometer.
- *
- * @return the current Accelerometer-values.
- * @throws UnsupportedOperationException
- * if Accelerometer is not available on this device.
- * @throws IllegalStateException
- * if Accelerometer was set to disabled.
- */
- public float[] readAccelerometer() throws UnsupportedOperationException, IllegalStateException {
- if (!accelerometerAvailable)
- throw new UnsupportedOperationException(
- "Accelerometer is not available.");
- if (!this.isEnabled)
- throw new IllegalStateException(
- "Accelerometer was set to disabled!");
- /* Get number of sensor-values the sensor will return. Could be
- * variable, depending of the amount of axis (1D, 2D or 3D
- * accelerometer). */
- int sensorValues = Sensors
- .getNumSensorValues(Sensors.SENSOR_ACCELEROMETER);
- float[] out = new float[sensorValues];
- /* Make the OS fill the array we passed. */
- Sensors.readSensor(Sensors.SENSOR_ACCELEROMETER, out);
- /* And return it. */
- return out;
- }
- }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
Thats it 

The full source:
"/src/your_package_structure/AccelerometerReader.java"
Using java Syntax Highlighting
- package org.anddev.dummyproject;
- import android.hardware.Sensors;
- public class AccelerometerReader {
- /** True when the Accelerometer-functionality is basically available. */
- boolean accelerometerAvailable = false;
- boolean isEnabled = false;
- /**
- * Sets up an AccelerometerReader. Checks if Accelerometer is available on
- * this device and throws UnsupportedOperationException if not .
- *
- * @param doEnable :
- * enables the devices Accelerometer
- * initially (if sensor available)
- * @throws UnsupportedOperationException
- * if Accelerometer is not available on this device.
- */
- public AccelerometerReader(boolean doEnable)
- throws UnsupportedOperationException {
- /* Check once here in the constructor if an
- * Accelerometer is available on this device. */
- for (String aSensor : Sensors.getSupportedSensors())
- if (aSensor.equals(Sensors.SENSOR_ACCELEROMETER))
- accelerometerAvailable = true;
- if (!accelerometerAvailable)
- throw new UnsupportedOperationException(
- "Accelerometer is not available.");
- if (doEnable)
- setEnableAccelerometer(true);
- }
- /**
- * En/Dis-able the Accelerometer.
- *
- * @param doEnable
- * <code>true</code> for enable.<br>
- * <code>false</code> for disable.
- * @throws UnsupportedOperationException
- */
- public void setEnableAccelerometer(boolean doEnable)
- throws UnsupportedOperationException {
- if (!accelerometerAvailable)
- throw new UnsupportedOperationException(
- "Accelerometer is not available.");
- /* If should be enabled and not already is: */
- if (doEnable && !this.isEnabled) {
- Sensors.enableSensor(Sensors.SENSOR_ACCELEROMETER);
- this.isEnabled = true;
- } else /* If should be disabled and not already is: */
- if (!doEnable && this.isEnabled) {
- Sensors.disableSensor(Sensors.SENSOR_ACCELEROMETER);
- this.isEnabled = false;
- }
- }
- /**
- * Read out the values currently provided by the Accelerometer.
- *
- * @return the current Accelerometer-values.
- * @throws UnsupportedOperationException
- * if Accelerometer is not available on this device.
- * @throws IllegalStateException
- * if Accelerometer was set to disabled.
- */
- public float[] readAccelerometer() throws UnsupportedOperationException, IllegalStateException {
- if (!accelerometerAvailable)
- throw new UnsupportedOperationException(
- "Accelerometer is not available.");
- if (!this.isEnabled)
- throw new IllegalStateException(
- "Accelerometer was set to disabled!");
- /* Get number of sensor-values the sensor will return. Could be
- * variable, depending of the amount of axis (1D, 2D or 3D
- * accelerometer). */
- int sensorValues = Sensors
- .getNumSensorValues(Sensors.SENSOR_ACCELEROMETER);
- float[] out = new float[sensorValues];
- /* Make the OS fill the array we passed. */
- Sensors.readSensor(Sensors.SENSOR_ACCELEROMETER, out);
- /* And return it. */
- return out;
- }
- }
Parsed in 0.043 seconds, using GeSHi 1.0.8.4
Regards,
plusminus






