Android P九轴传感器数据读取

相关资料:
https://www.st.com/zh/mems-and-sensors/lsm9ds1.html#tools-software
https://github.com/STMicroelectronics/STMems_Android_Sensor_HAL_IIO
https://www.st.com/content/st_com/zh/products/embedded-software/mems-and-sensors-software/drivers-for-mems/androidhal-iio.html

陀螺仪,Gyroscope,GYRO-Sensor,通过测量自身的旋转状态,判断出设备当前运动状态,是向前、向后、向左还是向右,是加速还是减速。

加速度计,Accelerometer,G-Sensor,重力感应器,可以感知任意方向上的加速度,可以感知受力情况。

磁力计,Magnetic,M-Sensor,测试磁场强度和方向,定位设备的方位,可以测量出东南西北四个方位的夹角。

总之,陀螺仪知道我们转了几个圈,加速度计知道我们向前走了几米,磁力计知道我们的方位。

安卓中通过监听SensorEventListener来获取传感器的数据。

事件类型:

import android.hardware.Sensor;
public static final String STRING_TYPE_TEMPERATURE = "android.sensor.temperature";
public static final int TYPE_ACCELEROMETER = 1;							 //加速度计
public static final int TYPE_ACCELEROMETER_UNCALIBRATED = 35;
public static final int TYPE_ALL = -1;
public static final int TYPE_AMBIENT_TEMPERATURE = 13;
public static final int TYPE_DEVICE_PRIVATE_BASE = 65536;
public static final int TYPE_GAME_ROTATION_VECTOR = 15;
public static final int TYPE_GEOMAGNETIC_ROTATION_VECTOR = 20;
public static final int TYPE_GRAVITY = 9;
public static final int TYPE_GYROSCOPE = 4;										//陀螺仪
public static final int TYPE_GYROSCOPE_UNCALIBRATED = 16;
public static final int TYPE_HEART_BEAT = 31;
public static final int TYPE_HEART_RATE = 21;
public static final int TYPE_LIGHT = 5;
public static final int TYPE_LINEAR_ACCELERATION = 10;
public static final int TYPE_LOW_LATENCY_OFFBODY_DETECT = 34;
public static final int TYPE_MAGNETIC_FIELD = 2;								//磁力计
public static final int TYPE_MAGNETIC_FIELD_UNCALIBRATED = 14;
public static final int TYPE_MOTION_DETECT = 30;

注册监听:

sensorManager.registerListener(this,
        sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
        SensorManager.SENSOR_DELAY_NORMAL);

sensorManager.registerListener(this,
        sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED),
        SensorManager.SENSOR_DELAY_NORMAL);

sensorManager.registerListener(this,
        sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE),
        SensorManager.SENSOR_DELAY_NORMAL);

完整代码:

public class MainActivity extends AppCompatActivity implements SensorEventListener{
   
   
    private static final String TAG = "Magnetometer"
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sunxiaolin2016

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值