传感器


[ 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 谨慎选择传感器延时





更多相关文章

  1. Android(安卓)Exception No such table android_metadata
  2. Android学习之Image操作及时间日期选择器
  3. Android动态获取json解析后显示到RecyclerView
  4. Android列表组件ListView使用详解之数据为空提示信息
  5. Android(安卓)Laucher3 去掉二级菜单
  6. android 向数据库写入图片信息 读取图片信息
  7. Android(安卓)MMS 数据存取数据表
  8. android之通过Button的监听器往adapter中添加数据时出错
  9. 【Android(安卓)开发教程】如何选择最佳的存储方式

随机推荐

  1. 智慧公安情指勤一体化指挥调度系统开发解
  2. 最新2018年全球DevOps薪资报告:行业和团队
  3. php如何使用date去掉时分秒
  4. kubernetes-ingress 安装
  5. PowerHA 常见故障排查
  6. css如何设置文字加粗
  7. Kubernetes 中定时任务的实现
  8. 争议 | 银行一定要上分布式数据库吗?有没
  9. Kubernetes 指标采集组件的部署
  10. 情报研判可视化分析平台开发解决方案