Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2964350
  • 博文数量: 674
  • 博客积分: 17881
  • 博客等级: 上将
  • 技术积分: 4849
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-17 10:15
文章分类

全部博文(674)

文章存档

2013年(34)

2012年(146)

2011年(197)

2010年(297)

分类: LINUX

2011-08-29 21:09:58

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.cpp

 本部分提供了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 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. 1)SensorActivity.java代码  
  2.   
  3.   
  4. package com.sensor;  
  5. import android.app.Activity;  
  6. import android.hardware.SensorEventListener;  
  7. import android.hardware.SensorListener;  
  8. import android.hardware.SensorManager;  
  9. import android.os.Bundle;  
  10. import android.util.Log;  
  11. import android.widget.TextView;  
  12.   
  13. public class SensorActivity extends Activity implements SensorListener{  
  14.   
  15. final String tag = "SensorActivity";   
  16. SensorManager sm = null;  
  17.   
  18. TextView xViewA = null;  
  19. TextView yViewA = null;  
  20. TextView zViewA = null;  
  21. TextView xViewO = null;  
  22. TextView yViewO = null;  
  23. TextView zViewO = null;  
  24.   
  25.   
  26. /** Called when the activity is first created. */  
  27. @Override  
  28. public void onCreate(Bundle savedInstanceState) {  
  29.   
  30.     super.onCreate(savedInstanceState);  
  31.     setContentView(R.layout.main);  
  32.           
  33.     sm=(SensorManager)getSystemService(SENSOR_SERVICE);  
  34.     xViewA = (TextView) findViewById(R.id.xbox);  
  35.     yViewA = (TextView) findViewById(R.id.ybox);  
  36.     zViewA = (TextView) findViewById(R.id.zbox);  
  37.     xViewO = (TextView) findViewById(R.id.xboxo);  
  38.     yViewO = (TextView) findViewById(R.id.yboxo);  
  39.     zViewO = (TextView) findViewById(R.id.zboxo);  
  40. }  
  41.   
  42.   
  43.   
  44. @Override  
  45. public void onAccuracyChanged(int sensor, int accuracy) {  
  46.     // TODO Auto-generated method stub   
  47.     Log.d(tag,"onAccuracyChanged: " + sensor + ", accuracy: " + accuracy);  
  48. }  
  49.   
  50.   
  51.   
  52. @Override  
  53. public void onSensorChanged(int sensor, float[] values) {  
  54.     // TODO Auto-generated method stub   
  55.     synchronized (this) {  
  56.         Log.d(tag, "onSensorChanged: " + sensor + ", x: " + values[0] + ", y: " + values[1] + ", z: " + values[2]);  
  57.         if (sensor == SensorManager.SENSOR_ORIENTATION) {  
  58.             xViewO.setText("Orientation X: " + values[0]);  
  59.             yViewO.setText("Orientation Y: " + values[1]);  
  60.             zViewO.setText("Orientation Z: " + values[2]);  
  61.         }  
  62.         if (sensor == SensorManager.SENSOR_ACCELEROMETER) {  
  63.             xViewA.setText("Accel X: " + values[0]);  
  64.             yViewA.setText("Accel Y: " + values[1]);  
  65.             zViewA.setText("Accel Z: " + values[2]);  
  66.         }              
  67.     }  
  68. }  
  69.   
  70. @Override  
  71. protected void onResume() {  
  72.     super.onResume();  
  73.     sm.registerListener(this, SensorManager.SENSOR_ORIENTATION |  
  74.            SensorManager.SENSOR_ACCELEROMETER, SensorManager.SENSOR_DELAY_NORMAL);  
  75. }  
  76.        
  77. @Override  
  78. protected void onStop() {  
  79.     sm.unregisterListener(this);  
  80.     super.onStop();  
  81.   
  82. }  


 

  1. 2)main.xml  布局文件(简单的放些TextView)  
  2.   
  3.   
  4. xml version="1.0" encoding="utf-8"?>  
  5. <LinearLayout xmlns:android=""  
  6.     androidrientation="vertical"  
  7.     android:layout_width="fill_parent"  
  8.     android:layout_height="fill_parent"  
  9.     >  
  10. <TextView    
  11.     android:layout_width="fill_parent"   
  12.     android:layout_height="wrap_content"   
  13.     android:text="@string/hello"  
  14.     />  
  15. <TextView    
  16.     android:layout_width="fill_parent"   
  17.     android:layout_height="wrap_content"   
  18.     android:text="Accelerometer"  
  19.     />  
  20. <TextView    
  21.     android:layout_width="fill_parent"   
  22.     android:layout_height="wrap_content"   
  23.     android:text="X Value"  
  24.     android:id="@+id/xbox"  
  25.     />  
  26. <TextView    
  27.     android:layout_width="fill_parent"   
  28.     android:layout_height="wrap_content"   
  29.     android:text="Y Value"  
  30.     android:id="@+id/ybox"  
  31.     />  
  32. <TextView    
  33.     android:layout_width="fill_parent"   
  34.     android:layout_height="wrap_content"   
  35.     android:text="Z Value"  
  36.     android:id="@+id/zbox"  
  37.     />      
  38.   
  39.   
  40. <TextView    
  41.     android:layout_width="fill_parent"   
  42.     android:layout_height="wrap_content"   
  43.     android:text="Orientation"  
  44.     />  
  45. <TextView    
  46.     android:layout_width="fill_parent"   
  47.     android:layout_height="wrap_content"   
  48.     android:text="X Value"  
  49.     android:id="@+id/xboxo"  
  50.     />  
  51. <TextView    
  52.     android:layout_width="fill_parent"   
  53.     android:layout_height="wrap_content"   
  54.     android:text="Y Value"  
  55.     android:id="@+id/yboxo"  
  56.     />  
  57. <TextView    
  58.     android:layout_width="fill_parent"   
  59.     android:layout_height="wrap_content"   
  60.     android:text="Z Value"  
  61.     android:id="@+id/zboxo"  
  62.     />      
  63. LinearLayout>  


 

五:在模拟器开发测试Sensor要注意,必须要装个传感器插件,才能看到效果,可能有部分手机硬件驱动是不支持Sensor的,不过市面上流行的品牌手机一般都支持!

抽空首次整理做的教程,有不好的地方,不吝指正!

http://blog.csdn.net/gexueyuan/article/details/6716807

阅读(876) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~