[笔记]Sensor概述

来源:互联网 发布:淘宝可以删差评吗 编辑:程序博客网 时间:2024/05/29 16:14
传感器


[ Android支持的传感器 ]
1 分为三大类:
- 动作传感器:加速计,重力传感器,陀螺仪,旋转矢量传感器
- 环境传感器:气压计,光度计,温度计
- 位置传感器:方向传感器,磁力计


2 各种传感器


2.1 TYPE_ACCELEROMETER
- 硬件
- Measures the acceleration force in m/s2 that is applied to a device on all three physical axes (x, y, and z), including the force of gravity.
- 动作的探测:摇晃,倾斜等


2.2 TYPE_AMBIENT_TEMPERATURE
- 硬件
- Measures the ambient room temperature in degrees Celsius (°C). 
- 监测气温


2.3 TYPE_GRAVITY
- 软件或硬件
- Measures the force of gravity in m/s2 that is applied to a device on all three physical axes (x, y, z).
- 动作的探测


2.4 TYPE_GYROSCOPE
- 硬件
- Measures a device's rate of rotation in rad/s around each of the three physical axes (x, y, and z).
- 旋转的探测:旋转,翻转


2.5 TYPE_LIGHT
- 硬件
- Measures the ambient light level (illumination) in lx.
- 控制屏幕亮度


2.6 TYPE_LINEAR_ACCELERATION
- 软件或硬件
- Measures the acceleration force in m/s2 that is applied to a device on all three physical axes (x, y, and z), excluding the force of gravity.
- 监测沿着某一个坐标轴的加速度


2.7 TYPE_MAGNETIC_FIELD
- Measures the ambient geomagnetic field for all three physical axes (x, y, z) in μT.
- 指南针


2.8 TYPE_ORIENTATION
- 软件
- Measures degrees of rotation that a device makes around all three physical axes (x, y, z). As of API level 3 you can obtain the inclination matrix and rotation matrix for a device by using the gravity sensor and the geomagnetic field sensor in conjunction with the getRotationMatrix() method.
- 获取设备的位置


2.9 TYPE_PRESSURE
- 硬件
- Measures the ambient air pressure in hPa or mbar.
- 监测气压变化


2.10 TYPE_PROXIMITY
- 硬件
- 监测物体距离屏幕的距离,如打电话时是否靠近耳朵


2.11 TYPE_RELATIVE_HUMIDITY
- 硬件
- 相对适度


2.12 TYPE_ROTATION_VECTOR
- 软件或硬件
- Measures the orientation of a device by providing the three elements of the device's rotation vector.


2.13 TYPE_TEMPERATURE
- 硬件
- Measures the temperature of the device in degrees Celsius (°C). This sensor implementation varies across devices and this sensor was replaced with the TYPE_AMBIENT_TEMPERATURE sensor in API Level 14






[ Android传感器框架 ]
1 功能
- 获取设备的可用的传感器
- 获取特定传感器的功能:最大值,制造商,耗电,解析度
- 获取传感器原始数据,定义获取数据的最小频率
- 注册及注销传感器事件侦听器


2 组件
2.1 SensorManager
- 访问、列举传感器
- 注册、注销传感器事件侦听器
- 获取方向信息
- 常量:传感器精度,数据获取频率,传感器校正


2.2 Sensor
- 传感器实例


2.3 SensorEvent
- 传感器事件对象
- 传感器原始数据,传感器类型,数据精度,事件时间戳


2.4 SensorEventListener
- 传感器变化
- 传感器精度变化


3 传感器相关API的任务
- 标识传感器及其功能
- 监视传感器事件


4 getSensorList(type): 取传感器列表,可以是全部,也可以是指定类型


5 getDefaultSensor(type): 取特定类型的传感器中的默认传感器(如果有多个)


6 同意通过Sensor类提供的公共方法来获知各个传感器的功能和属性。
- 设备上的传感器不同,或者传感器功能有差异,导致应用程序需要采取不同的行为
- getResolution(), getMaximumRange(), getPower(), ...


7 getVendor(), getVersion
- 如果需要针对不同生产商或者不同版本的传感器分别进行优化,这两个方法非常有用
- 例如,假设腰监视“倾斜”或者“摇晃”动作,对于比较新的带有重力传感器的设备,以及哪些老的、仅有加速计的设备,可以分别采取不同的数据过滤规则和优化策略。


8 getMinDelay()
- 传感器感知数据的最小周期(微秒)
- 可用来获知传感器能够达到应用程序获取数据频率的要求


[ 监视传感器事件 ]
1 接口: SensorEventListener
- onAccuracyChanged(): 传感器精度变化
  * SENSOR_STATUS_ACCURACY_LOW
  * SENSOR_STATUS_ACCURACY_MEDIUM
  * SENSOR_STATUS_ACCURACY_HIGH
  * SENSOR_STATUS_ACCURACY_UNRELIABLE
- onSensorChanged(): 传感器报告新数据
  * 数据精度
  * 产生数据的传感器
  * 时间戳
  * 数据


2 注册侦听器
- mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL);
- delay要尽可能往大了设置
- 应当有对应的注销操作


[ 坐标系统 ]
1 传感器坐标系统与设备的默认方向相关联(手机:竖屏,平板:有的是横屏)
2 如要在屏幕上显示传感器数据,需要调用getRotation()方法来侦测屏幕旋转,然后用remapCoordinateSystem()来重新映射传感器坐标系到屏幕坐标系
3 一些传感器使用世界坐标系,返回的数据以地球为参照而非设备


[ 最佳实践 ]
1 注销传感器监听以节省资源
2 不要在模拟器测试代码
3 不要阻塞onSensorChanged()方法: 传感器可能快速变换
4 避免实用作废的方法和传感器类型
5 使用之前验证传感器
6 谨慎选择传感器延时





原创粉丝点击