Reputation: 75
I'm trying to use the linear acceleration fusion sensor on my android app. However, it cannot find the sensor for Sensor.TYPE_LINEAR_ACCELERATION.
I perform these calls:
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mLinearAcceleration = mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
After the above, mAccelerometer is defined, but mLinearAcceleration is null. I know that TYPE_LINEAR_ACCELERATION was not added until API level 9, but I think I am running in at least API level 9. Here is a snippet of my manifest file:
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
So I believe I am working under the correct API level.
However, when I try listing all sensors that are available, I do not find all sensors that should be available for this API level.
When I call...
List<Sensor> l = mSensorManager.getSensorList(Sensor.TYPE_ALL);
for(Sensor s : l)
System.out.println(s.getName());
I get
Goldfish 3-axis Accelerometer
Goldfish 3-axis Magnetic field sensor
Goldfish Orientation sensor
Goldfish Temperature sensor
Goldfish Proximity sensor
None of these are linear acceleration, and one is Orientation, a deprecated sensor. It appears my sensors are operating as though they were from before API level 9, but I don't know why that would be. Is there some field in my project that could be forcing the app to act as an older API? (other than the minimum supported API field in the AndroidManifest.xml file, which is already set to 9)
Upvotes: 1
Views: 1555
Reputation: 1639
put this in your manifest
<uses-permission android:name=”android.permission.SET_ORIENTATION” />
Is for access to your accelerometer
try this:
/* put this into your activity class */
private SensorManager mSensorManager;
private float mAccel; // acceleration apart from gravity
private float mAccelCurrent; // current acceleration including gravity
private float mAccelLast; // last acceleration including gravity
private final SensorEventListener mSensorListener = new SensorEventListener() {
public void onSensorChanged(SensorEvent se) {
float x = se.values[0];
float y = se.values[1];
float z = se.values[2];
mAccelLast = mAccelCurrent;
mAccelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z));
float delta = mAccelCurrent - mAccelLast;
mAccel = mAccel * 0.9f + delta; // perform low-cut filter
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
mSensorManager.unregisterListener(mSensorListener);
super.onPause();
}
And this:
/* do this in onCreate */
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
mAccel = 0.00f;
mAccelCurrent = SensorManager.GRAVITY_EARTH;
mAccelLast = SensorManager.GRAVITY_EARTH;
Upvotes: 1