Sensor.TYPE_LINEAR_ACCELERATION在Android返回null 4+TYPE_LINEAR_ACCELERATION、Sensor、null、Android

2023-09-09 21:02:20 作者:你若是深海我愿深沉永伴

我想在我的应用程序中使用加速度计的数据。

我能够访问 TYPE_ACCELEROMETER 没有任何问题,但许多人认为使用 TYPE_LINEAR_ACCELERATION 这已经是从地球重力过滤。它看起来像我没有访问该传感器虽然在日志下面的code将返回null。据我了解 TYPE_LINEAR_ACCELERATION 可作为在Android API大于8软件传感器...我使用14作为我最小的一个三星Galaxy Tab 2

任何想法,为什么我得到空?是不是因为这个模型没有一个陀螺仪?

code:

  mSensorManager =(的SensorManager)getSystemService(Context.SENSOR_SERVICE);mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);mLinearAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);Log.i(TYPE_LINEAR_ACCELERATION,将String.valueOf(mLinearAccelerometer)); 

感谢

编辑:添加缺少的符号=

解决方案

根据 android的运动传感器文档: Android开源项目(AOSP)提供了三个基于软件的运动传感器:重力传感器,线性加速度传感器和旋转矢量传感器。这些传感器在Android 4.0的更新,现在使用设备的陀螺仪......所有这三个传感器都依赖于陀螺仪:如果设备没有一个陀螺仪,这些传感器没有显示出来,而且不能使用。所以,你的猜测是正确的,没有陀螺仪重力感应器不可用。

I would like to use the accelerometer data in my application.

CMOS光学触控主要元件及优势介绍

I am able to access the TYPE_ACCELEROMETER without any problems, but many suggest to use the TYPE_LINEAR_ACCELERATION which is already filtered from the earth gravity. It looks like I do not have access to that sensor though, as in the code below the log will return null. From what I understand TYPE_LINEAR_ACCELERATION is available as a "software sensor" on Android API greater then 8...I am using 14 as my minimum on a Samsung Galaxy Tab 2.

Any idea why I get null? Is it because this model does not have a gyroscope?

Code:

mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

mLinearAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);

Log.i("TYPE_LINEAR_ACCELERATION", String.valueOf(mLinearAccelerometer));

Thanks

Edit: added missing "=" sign

解决方案

According to the android motion sensor documentation:

The Android Open Source Project (AOSP) provides three software-based motion sensors: a gravity sensor, a linear acceleration sensor, and a rotation vector sensor. These sensors were updated in Android 4.0 and now use a device's gyroscope ... All three of these sensors rely on a gyroscope: if a device does not have a gyroscope, these sensors do not show up and are not available for use.

. So your guess is correct, without the gyroscope the gravity sensor is not available.