Android Sensors
March 15th, 2009
Android devices like the G1 phone support different sensors. Tonight I wrote some code that reports which sensors your Android device supports. Unfortunately, the emulator supports none:

When I run this on my G1, the program reports true for these sensors:
- Accelerometer
- Magnetic Field
- Orientation
- Orientation Raw
- Tricorder (not really)
Sample Code
First, grab the SensorManager:
SensorManager sensorMgr = (SensorManager)
context.getSystemService(Context.SENSOR_SERVICE);
Next, get a list of supported sensors:
int sensorIds = sensorMgr.getSensors();
And finally, test if the device supports the sensor your application needs:
boolean deviceSupportsAccelerometer = (sensorIds & SENSOR_ACCELEROMETER) == SENSOR_ACCELEROMETER;
Note that SENSOR_ACCELEROMETER is a constant in the SensorManager class.
The Complete Example
My SensorListAdapter produces the screen shown above. The complete project is available on GitHub.
In my android application I want to get the acceleration then use it to get the speed of the device, at first I think that getting the acceleration was easy, but I found that I must do reorientation for the device and this is not easy as I try to do this more than once (I mean I try alot of codes)but the result was wrong, so I hope you have any solution for this problem
Thanks in advance
Mohamed Adel____
Mohamed, I was able to calculate speed using the GPS, it is not perfectly accurate, but it is close. Using the accelerometer would be cool to calculate speed, but it is not really possible you cannot calculate speed from acceleration alone, there is too many unknowns. Open a physics book and do some velocity problems. For example, say you are going at a constant 70 mph, so then your acceleration is 0, how are you supposed to calculate speed when acceleration is 0, your speed can be anything. On the other hand if you make an assumption that you are starting from some point 0 and your speed is 0 and then someone turns on your app and then starts moving you should then be able to calculate speed and distance based on acceleration alone.
I think Moussa’s right, though a bit unclear in what he’s saying. You certainly can calculate speed just using the acceleration, but you’ll need to recalibrate it at 0 (or some constant speed) periodically. Also, depending on the accuracy of the accelerometer you could end up way off over time, and this problem would likely involve some complicated math since you’d need a mixture of the accelerometer and orientation detection. Every time the device is rotated in any way you would need to either convert readings back to the original coordinate system or somehow convert the coordinates already taken into the new orientation. The first of those would probably be preferable, but at this point you’re looking at a combination of kinematics (i.e. physics ala Isaac Newton) and vector calculus. A cool thing to do, but not for the mathematically faint of heart. Or the programmatic faint of heart for that matter. Really cool problem though. If you are trying this, I would recommend calibrating your device as still at the start, using a compass in it to determine orientation from North (if available), and then either keeping all coordinates in terms of cardinal directions or the original orientation of the device. Then you just need to account for orientation changes in your calculation of acceleration direction. Hope this helps, and good luck!