Sensor传感器源码的阅读与应用开发简单实例
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Sensor传感器源码的阅读与应用开发简单实例
Android系统支持多种传感器。
应用到各个层次,有的传感器已经在Android的框架中使用,大多数传感器由应用程序中来使用。
一.Android中支持的传感器类型:
传感器
Java中的名称
本地接口名称
数值
加速度
TYPE_ACCELEROMETER
SENSOR_TYPE_ACCELEROMETER
1
磁场
TYPE_MAGNETIC_FIELD
SENSOR_TYPE_MAGNETIC_FIELD
2
方向
TYPE_ORIENTATION
SENSOR_TYPE_ORIENTATION
3
陀螺仪
TYPE_GYROSCOPE
SENSOR_TYPE_GYROSCOPE
4
光线(亮度)
TYPE_LIGHT
SENSOR_TYPE_LIGHT
5
压力
TYPE_PRESSURE
SENSOR_TYPE_PRESSURE
6
温度
TYPE_TEMPERATURE
SENSOR_TYPE_TEMPERATURE
7
接近
TYPE_PROXIMITY
SENSOR_TYPE_PROXIMITY
8
二.Android 系统的代码分布情况:
1)传感器系统的java代码
代码路径:framework/base/core/java/android/hardware中目录中包含了Camera 和Sensor两部分,Sensor部分的内容为Sensor*.java 文件。
2)传感器系统的JNI部分
代码路径:framework/base/core/jni/android_hardware_SensorManager.cp
p
本部分提供了android.hardware.Sensor.Manager 类的本质支持。
3)传感器系统硬件层实现的接口
头文件路径:hardware/libhardware/include/hardware/sensors.h
传感器系统的硬件抽象层需要各个系统根据sensors.h中定义的接口去实现
Sensor部分的内容还包含了底层部分的驱动和硬件抽象层,以及上层对Sensor的调用部
三.Android的Sensor源码解析:
Android中的Sensor的主要文件为:
Sensor.java 单一传感器描述文件
SensorEvent.java 传感器系统的时间类
SensorEventListener.java 传感器监听事件(是一个接口)
SensorListener.java 传感器监听(接口)
SensorManager.java 传感器的核心管理类
Sensor.java中定义的是传感器常量的一些类型,如public static final TYPE_MAGNETIC_FIELD=2;
等,具体参数参照传感器类型(图一)
SensorManager.java
public Sensor getDefaultSensor(int type){获得默认的传感器}
public List<Sensor> getSensorList(int type) {获得传感器列表}
public boolean registerListener(SensorListener listener, int sensors) {
return registerListener(listener, sensors, SENSOR_DELAY_NORMAL);
} 注册监听事件
public void unregisterListener(SensorListener listener, int sensors) {注销监听事件}
时间关系,源码不逐一说了,大家自己有下个源码看下,如果没有源码的,给我个邮箱我给大家发这部分代码,直接上个简单的DEMO供大家认识下,好像这块的代码,在IBM的一个网站上也能找到!
四。
程序代码
1)SensorActivity.java代码
1.package com.sensor;
2.import android.app.Activity;
3.import android.hardware.SensorEventListener;
4.import android.hardware.SensorListener;
5.import android.hardware.SensorManager;
6.import android.os.Bundle;
7.import android.util.Log;
8.import android.widget.TextView;
9.public class SensorActivity extends Activity implements SensorListener{
10.
11.final String tag = "SensorActivity";
12.SensorManager sm = null;
13.TextView xViewA = null;
14.TextView yViewA = null;
15.TextView zViewA = null;
16.TextView xViewO = null;
17.TextView yViewO = null;
18.TextView zViewO = null;
19.
20.
21.
22./** Called when the activity is first created. */
23.@Override
24.public void onCreate(Bundle savedInstanceState) {
25.super.onCreate(savedInstanceState);
26.setContentView(yout.main);
27.
28.sm=(SensorManager)getSystemService(SENSOR_SERV ICE);
29.xViewA = (TextView) findViewById(R.id.xbox);
30.yViewA = (TextView) findViewById(R.id.ybox);
31.zViewA = (TextView) findViewById(R.id.zbox);
32.xViewO = (TextView) findViewById(R.id.xboxo);
33.yViewO = (TextView) findViewById(R.id.yboxo);
34.zViewO = (TextView) findViewById(R.id.zboxo);
35.
36.
37.
38.
39.}
40.@Override
41.public void onAccuracyChanged(int sensor, int accuracy) {
42.// TODO Auto-generated method stub
43.Log.d(tag,"onAccuracyChanged: " + sensor + ",
accuracy: " + accuracy);
44.}
45.@Override
46.public void onSensorChanged(int sensor, float[] values) {
47.// TODO Auto-generated method stub
48.synchronized (this) {
49.Log.d(tag, "onSensorChanged: " + sensor + ", x: " + values[0] + ", y: " + values[1] + ", z: " + values[2]);
50.if (sensor == SensorManager.SENSOR_ORIENTATION) {
51.xViewO.setText("Orientation X: " + values[0]);
52.yViewO.setText("Orientation Y: " + values[1]);
53.zViewO.setText("Orientation Z: " + values[2]);
54.}
55.if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
56.xViewA.setText("Accel X: " + values[0]);
57.yViewA.setText("Accel Y: " + values[1]);
58.zViewA.setText("Accel Z: " + values[2]);
59.}
60.}
61.
62.}
63.
64.
65.@Override
66.protected void onResume() {
67.super.onResume();
68.sm.registerListener(this,
69.SensorManager.SENSOR_ORIENTATION |
70.SensorManager.SENSOR_ACCELEROMETER,
71.SensorManager.SENSOR_DELAY_NORMAL);
72.}
73.
74.@Override
75.protected void onStop() {
76.sm.unregisterListener(this);
77.super.onStop();
78.}
79.
80.}
81.
复制代码
2)main.xml 布局文件(简单的放些T extView)
1.<?xml version="1.0" encoding="utf-8"?>
2.<LinearLayout xmlns:android="/apk/res/android"
3.android:orientation="vertical"
4.android:layout_width="fill_parent"
5.android:layout_height="fill_parent"
6.>
7.<TextView
8.android:layout_width="fill_parent"
9.android:layout_height="wrap_content"
10.android:text="@string/hello"
11./>
12.<TextView
13.android:layout_width="fill_parent"
14.android:layout_height="wrap_content"
15.android:text="Accelerometer"
16./>
18.android:layout_width="fill_parent"
19.android:layout_height="wrap_content"
20.android:text="X Value"
21.android:id="@+id/xbox"
22./>
23.<TextView
24.android:layout_width="fill_parent"
25.android:layout_height="wrap_content"
26.android:text="Y Value"
27.android:id="@+id/ybox"
28./>
29.<TextView
30.android:layout_width="fill_parent"
31.android:layout_height="wrap_content"
32.android:text="Z Value"
33.android:id="@+id/zbox"
34./>
35.
36.<TextView
37.android:layout_width="fill_parent"
38.android:layout_height="wrap_content"
39.android:text="Orientation"
40./>
41.<TextView
42.android:layout_width="fill_parent"
43.android:layout_height="wrap_content"
44.android:text="X Value"
45.android:id="@+id/xboxo"
46./>
48.android:layout_width="fill_parent"
49.android:layout_height="wrap_content"
50.android:text="Y Value"
51.android:id="@+id/yboxo"
52./>
53.<TextView
54.android:layout_width="fill_parent"
55.android:layout_height="wrap_content"
56.android:text="Z Value"
57.android:id="@+id/zboxo"
58./>
59.</LinearLayout>
复制代码
五:在模拟器开发测试Sensor要注意,必须要装个传感器插件,才能看到效果,可能有部分手机硬件驱动是不支持Sensor的,不过市面上流行的品牌手机一般都支持!
抽空首次整理做的教程,有不好的地方,不吝指正!。