[英]Phone can't find sensor for Sensor.TYPE_LINEAR_ACCELERATION
I'm trying to use the linear acceleration fusion sensor on my android app. 我正在尝试在我的Android应用上使用线性加速度融合传感器。 However, it cannot find the sensor for Sensor.TYPE_LINEAR_ACCELERATION.
但是,它找不到用于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. 在上面之后,定义了加速度计,但mLinearAcceleration为空。 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:
我知道TYPE_LINEAR_ACCELERATION直到API级别9才添加,但是我认为我至少在API级别9上运行。这是清单文件的片段:
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
So I believe I am working under the correct API level. 因此,我相信我在正确的API级别下工作。
However, when I try listing all sensors that are available, I do not find all sensors that should be available for this API level. 但是,当我尝试列出所有可用的传感器时,找不到该API级别应具有的所有传感器。
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. 这些都不是线性加速度,一个是Orientation(已弃用的传感器)。 It appears my sensors are operating as though they were from before API level 9, but I don't know why that would be.
看来我的传感器好像在使用API级别9之前的版本一样运行,但是我不知道为什么会这样。 Is there some field in my project that could be forcing the app to act as an older API?
我的项目中是否有某些字段可能会迫使应用程序充当旧API? (other than the minimum supported API field in the AndroidManifest.xml file, which is already set to 9)
(除了AndroidManifest.xml文件中支持的最低API字段,该字段已设置为9)
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.