Android重力传感器代码
- 格式:doc
- 大小:21.00 KB
- 文档页数:3
Android传感器HAL头文件sensors.h详细注释/* Android Sensor HAL头文件分析笔记 */#ifndef ANDROID_SENSORS_INTERFACE_H /* 此宏的作用就是避免重复包含此头文件 */#define ANDROID_SENSORS_INTERFACE_H#include <stdint.h>#include <sys/cdefs.h>#include <sys/types.h>#include <hardware/hardware.h>#include <cutils/native_handle.h>/* 定义传感器头文件的版本 */#define SENSORS_HEADER_VERSION 1/* 下面这几个宏定义了传感器API的版本 */#defineSENSORS_MODULE_API_VERSION_0_1 HARDWARE_MODULE_A PI_VERSION(0, 1)#defineSENSORS_DEVICE_API_VERSION_0_1 HARDWARE_DEVICE_API_ VERSION_2(0, 1, SENSORS_HEADER_VERSION)#defineSENSORS_DEVICE_API_VERSION_1_0 HARDWARE_DEVICE_API_ VERSION_2(1, 0, SENSORS_HEADER_VERSION)#defineSENSORS_DEVICE_API_VERSION_1_1 HARDWARE_DEVICE_API_ VERSION_2(1, 1, SENSORS_HEADER_VERSION)#defineSENSORS_DEVICE_API_VERSION_1_2 HARDWARE_DEVICE_API_ VERSION_2(1, 2, SENSORS_HEADER_VERSION)#defineSENSORS_DEVICE_API_VERSION_1_3 HARDWARE_DEVICE_API_ VERSION_2(1, 3, SENSORS_HEADER_VERSION)/*** 有关Android传感器类型的介绍和详细描述,请参考下面的网址,里面有详细的说明.* /devices/sensors/index.html*//* 传感器模块的ID,在写HAL模块时使用 */#define SENSORS_HARDWARE_MODULE_ID "sensors"/* 要打开的传感器设备的名称 */#define SENSORS_HARDWARE_POLL "poll"/*** Handles必须高于SENSORS_HANDLE_BASE,必须是唯一的;* 一个Handle标识给定的传感器. Handle用于启动和/或停用传感器* 在这个版本的API中,只能有256个Handle*/#define SENSORS_HANDLE_BASE 0#define SENSORS_HANDLE_BITS 8#defineSENSORS_HANDLE_COUNT (1<<SENSORS_HANDLE_BITS) /** **** 过时的东东 ****** flags for (*batch)()* Availability: SENSORS_DEVICE_API_VERSION_1_0* see (*batch)() documentation for details.* Deprecated as of SENSORS_DEVICE_API_VERSION_1_3.* WAKE_UP_* sensors replace WAKE_UPON_FIFO_FULL concept.*/enum {SENSORS_BATCH_DRY_RUN = 0x00000001,SENSORS_BATCH_WAKE_UPON_FIFO_FULL = 0x00000002 };/** meta_data_event_t的what字段使用的值*/enum {/* 上一次刷新操作已完成 */META_DATA_FLUSH_COMPLETE = 1,META_DATA_VERSION /* 总是最后一个,保留自动分配 */};/** 关于身体传感器使用的权限(比如,心率监测器).* 有关哪些传感器需要使用此权限,请参阅传感器类型获得更详细信息.*/#define SENSOR_PERMISSION_BODY_SENSORS "android.permission.BODY_SENSORS"/* 下面的枚举值只对"SENSORS_DEVICE_API_VERSION_1_3"版本有效;传感器标志,用于设置sensor_t结构体的flags成员,表示传感器的报告模式 */enum {/** 当数据可用时,传感器是否唤醒处于挂起模式的系统.每当从唤醒传感器传送传感器事件时,* 驱动程序需要持有唤醒锁,直到传感器服务读取事件,即直到下次调用sensors_poll_device_t.poll()函数;* 一旦poll函数再次被调用,这意味着传感器服务已经读取了事件,驱动程序可以安全地释放唤醒锁.* 传感器服务将继续持有唤醒锁,直到应用程序实际读取事件。
如何实现Android游戏开发之小球重力感应重力感应主要是依靠手机的加速度传感器(accelerometer)来实现、在Android的开发中一共有八种传感器但是不一定每一款真机都支持这些传感器。
因为很多功能用户根本不care的所以可能开发商会把某些功能屏蔽掉。
还是得根据真机的实际情况来做开发,今天我们主要来讨论加速度传感器的具体实现方式。
传感器名称如下:加速度传感器(accelerometer)陀螺仪传感器(gyroscope)环境光照传感器(light)磁力传感器(magnetic field)方向传感器(orientation)压力传感器(pressure)距离传感器(proximity)温度传感器(temperature)1.SensorMannager传感器管理对象手机中的所有传感器都须要通过SensorMannager来访问,调用getSystemService(SENSOR_SERVICE)方法就可以拿到当前手机的传感器管理对象。
SensorManager mSensorMgr = (SensorManager)getSystemService(SENSOR_SERVICE);2.实现SensorEventListener接口说道SensorEventListener接口就不得不说SensorListener接口。
在Android1.5一下是通过实现SensorListener接口来捕获手机传感器状态,但是在1.5以上如果实现这个接口系统会提示你这行代码已经过期。
今天我们不讨论SensorListener因为它已经是过时的东西了。
主要讨论一下SensorEventListener接口。
我们须要实现SensorEventListener这个接口onSensorChanged(SensorEvent event)方法来捕获手机传感器的状态,拿到手机X轴Y轴Z轴三个方向的重力分量,有了这三个方向的数据重力感应的原理我们就已经学会了,简单吧哇咔咔~~public void onSensorChanged(SensorEvent e) {float x = e.values[SensorManager.DATA_X];float y = e.values[SensorManager.DATA_Y];float z = e.values[SensorManager.DATA_Z]; }如图所示:上例代码中float x y z 3个方向的取值范围是在-10 到10 之间,我向同学们说明一下X轴Y轴Z轴重力分量的含义。
手机的感应器在Android里边所代表的类是Sensor,你只要看到在android.hardware这个包下边的都是封装的关于一些特殊的硬件方面的类,比如说Camera、Sensor之类的。
一直都很怀疑为什么HTC的Google手机没有前置摄像头。
怨念啊。
PS:虽然是2.1的机子,但是我用的是1.5的SDK。
代码很简单:我们首先要得到一个手机上的传感器。
Java代码1.SensorManager sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);这行代码只要用过类似开发的都应该知道是什么。
getSystemService(String name)可以用来返回一个硬件设备的控制器。
比如说LocationManage(和GPS相关用来确定位置的)、TelephonyManage(查询电话相关内容,比如说IMEI码)、AudioManager(顾名思义,是视频播放用的)等等。
具体可以观看SDK文档里边Activity的讲解。
/int ... d/app/Activity.html得到重力感应的硬件控制了,然后我们就应该得到一个Sensor了。
Java代码1.Sensor sensor = sensorMgr.getDefaultSensor(Sensor.TYPE_ALL);关于这个参数。
其实挺复杂的。
我开始用的是TYPE_ALL,出的是XYZ三条轴线的偏移量,至于其他的大家可以看下边:下边是官方SDK中对于各种类型的解析(粗体红字是我加上去的)<!-- =========== ENUM CONSTANT SUMMARY =========== -->Constantsint TYPE_ACCELEROMETERA constant describing an accelerometer sensortype.加速度int TYPE_ALL A constant describing all sensor types.所有类型,NexusOne 默认为 加速度int TYPE_GYROSCOPE A constant describing a gyroscope sensor type 回转仪(这个不太懂)int TYPE_LIGHT A constant describing an light sensor type.光线感应吗int TYPE_MAGNETIC_FIELD A constant describing a magnetic field sensor type.磁场int TYPE_ORIENTATION A constant describing an orientation sensor type.定向(指北针)和角度int TYPE_PRESSURE A constant describing a pressure sensor type 压力计int TYPE_PROXIMITY A constant describing an proximity sensor type.距离?不太懂int TYPE_TEMPERATURE A constant describing a temperature sensor type 温度啦然后就是我们需要即时了解手机的偏转度。
Android传感器代码详细说明各传感器调用方式,并附加详细的代码Android下调用传感器代码在这里介绍一下所写的在android下调用传感器的程序。
Android中支持的几种传感器:Sensor.TYPE_*****OMETER:加速度传感器。
Sensor.TYPE_*****PE:陀螺仪传感器。
Sensor.TYPE_LIGHT:亮度传感器。
Sensor.TYPE_*****C_FIELD:地磁传感器。
Sensor.TYPE_*****TION:方向传感器。
Sensor.TYPE_*****E:压力传感器。
Sensor.TYPE_*****TY:近程传感器。
Sensor.TYPE_*****TURE:温度传感器。
使用传感器最关键的一些知识是:SensorManager是所有传感器的一个综合管理类,包括了传感器的种类、采样率、精准度等。
我们可以通过getSystemService方法来取得一个SensorManager对象。
使用传感器时,需要通过registerListener函数注册传感器,使用完后需要通过unregisterListener取消注册。
百闻不如一见,还是直接讲代码:新建一个Sensors的工程,创建一个Sensors.java,内容如下:***-********-**********01 package me.sigma.sensors;020304import android.app.Activity;05import android.hardware.SensorListener;06import android.hardware.SensorManager;07import android.os.Bundle;08import android.widget.TextView;0910public class Sensors extends Activity {详细说明各传感器调用方式,并附加详细的代码11 TextView myTextView1;//t12 //gen13 TextView myTextView2;//x14 TextView myTextView3;//y15 TextView myTextView4;//z16 //acc17 TextView myTextView5;//x18 TextView myTextView6;//y19 TextView myTextView7;//z20 //ori21 TextView myTextView8;//x22 TextView myTextView9;//y23 TextView myTextView10;//z24 //Light25 TextView myTextView11;//z2627 SensorManager mySensorManager;//28 @Override29 public void onCreate(Bundle savedInstanceState) {30 super.onCreate(savedInstanceState);31 setContentView(yout.main);32 myTextView1 = (TextView) findViewById(R.id.myTextView1);33 myTextView2 = (TextView) findViewById(R.id.myTextView2);34 myTextView3 = (TextView) findViewById(R.id.myTextView3);35 myTextView4 = (TextView) findViewById(R.id.myTextView4);36 myTextView5 = (TextView) findViewById(R.id.myTextView5);37 myTextView6 = (TextView) findViewById(R.id.myTextView6);38 myTextView7 = (TextView) findViewById(R.id.myTextView7);39 myTextView8 = (TextView) findViewById(R.id.myTextView8);40 myTextView9 = (TextView) findViewById(R.id.myTextView9);41 myTextView10 = (TextView) findViewById(R.id.myTextView10);42 myTextView11 = (TextView) findViewById(R.id.myTextView11);43 mySensorManager = (SensorManager)getSystemService(SENSOR_*****); 44 }45 private SensorListener mySensorListener = new SensorListener(){46 @Override47 public void onAccuracyChanged(int sensor, int accuracy) {}详细说明各传感器调用方式,并附加详细的代码48 @Override49 public void onSensorChanged(int sensor, float[] values) { 50if(sensor == SensorManager.SENSOR_*****TURE){51 myTextView1.setText(“Current Temprature:"+values); 52 }elseif(sensor == SensorManager.SENSOR_*****C_FIELD){53 myTextView2.setText("Current Magnetic x:"+values); 54 myTextView3.setText("Current Magnetic y:"+values); 55myTextView4.setText("Current Magnetic z:"+values); 56 }else if(sensor == SensorManager.SENSOR_*****OMETER){57 myTextView5.setText("Current Accelero x:"+values); 58 myTextView6.setText("Current Accelero y:"+values); 59myTextView7.setText("Current Accelero z:"+values); 60 }else if(sensor == SensorManager.SENSOR_*****TION){61 myTextView8.setText("Current Oraenttation x:"+values); 62 myTextView9.setText("Current Oraenttation y:"+values); 63 myTextView10.setText("Current Oraenttation z:"+values); 64 }elseif(sensor == SensorManager.SENSOR_LIGHT){65 myTextView11.setText("Current Oraenttation x:"+values); 66 }67 }68 };69 @Override70 protected void onResume() {71 mySensorManager.registerListener(72 mySensorListener,73 SensorManager.SENSOR_*****TURE |74 SensorManager.SENSOR_*****C_FIELD |75 SensorManager.SENSOR_*****OMETER |76 SensorManager.SENSOR_LIGHT |77 SensorManager.SENSOR_*****TION,78 SensorManager.SENSOR_DELAY_UI79 );80 super.onResume();81 }82 @Override83 protected void onPause() {84 mySensorManager.unregisterListener(mySensorListener);详细说明各传感器调用方式,并附加详细的代码85 super.onPause();86 }87}更改res/layout/下面的main.xml,为如下内容:***-********-**********01 ?xml version="1.0" encoding="utf-8"?02LinearLayout xmlns:android="/apk/res/android" 03 android:orientation="vertical"04 android:layout_width="fill_parent"05 android:layout_height="fill_parent"06 TextView07 android:id="@+id/title"08 android:gravity="center_horizontal"09 android:textSize="20px"10 android:layout_width="fill_parent"11 android:layout_height="wrap_content"12 android:text="@string/title"/13 TextView14 android:id="@+id/myTextView1"15 android:textSize="18px"16 android:layout_width="fill_parent"17 android:layout_height="wrap_content"18 android:text="@string/myTextView1"/19 TextView20 android:id="@+id/myTextView2"21 android:textSize="18px"22 android:layout_width="fill_parent"23 android:layout_height="wrap_content"24 android:text="@string/myTextView2"/25 TextView26 android:id="@+id/myTextView3"27 android:textSize="18px"28 android:layout_width="fill_parent"29 android:layout_height="wrap_content"30 android:text="@string/myTextView3"/详细说明各传感器调用方式,并附加详细的代码31 TextView32 android:id="@+id/myTextView4"33 android:textSize="18px"34 android:layout_width="fill_parent"35 android:layout_height="wrap_content"36 android:text="@string/myTextView4"/37 TextView38 android:id="@+id/myTextView5"39 android:textSize="18px"40 android:layout_width="fill_parent"41 android:layout_height="wrap_content"42 android:text="@string/myTextView5"/43 TextView44 android:id="@+id/myTextView6"45 android:textSize="18px"46 android:layout_width="fill_parent"47 android:layout_height="wrap_content"48 android:text="@string/myTextView6"/49 TextView50 android:id="@+id/myTextView7"51 android:textSize="18px"52 android:layout_width="fill_parent"53 android:layout_height="wrap_content"54 android:text="@string/myTextView7"/55 TextView56 android:id="@+id/myTextView8"57 android:textSize="18px"58 android:layout_width="fill_parent"59 android:layout_height="wrap_content"60 android:text="@string/myTextView8"/61 TextView62 android:id="@+id/myTextView9"63 android:textSize="18px"64 android:layout_width="fill_parent"65 android:layout_height="wrap_content"66 android:text="@string/myTextView9"/67 TextView详细说明各传感器调用方式,并附加详细的代码68 android:id="@+id/myTextView10"69 android:textSize="18px"70 android:layout_width="fill_parent"71 android:layout_height="wrap_content"72 android:text="@string/myTextView10"/73 TextView74 android:id="@+id/myTextView11"75 android:textSize="18px"76 android:layout_width="fill_parent"77 android:layout_height="wrap_content"78 android:text="@string/myTextView11"/79/LinearLayout80更改res/values/strings.xml为如下内容:***-********-**********01 ?xml version="1.0" encoding="utf-8"?02resources03 string name="hello"templator!/string04 string name="app_name"templator/string05 string name="title"Sigma Sensors/string06 string name="myTextView1"Current Temprature:/string07 string name="myTextView2"Current Magnetic x:/string08 string name="myTextView3"Current Magnetic y:/string09 string name="myTextView4"Current Magnetic z:/string10 string name="myTextView5"Current Accelero x:/string11 string name="myTextView6"Current Accelero y:/string12 string name="myTextView7"Current Accelero z:/string13 string name="myTextView8"Current Oraenttation x:/string14 string name="myTextView9"Current Oraenttation y:/string15 string name="myTextView10"Current Oraenttation z:/string16 string name="myTextView11"Current Light:/string。
android系统传感器获取1显示系统获取的传感器的布局文件activity_main.xml<RelativeLayout xmlns:android="/apk/res/andr oid"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.shunchang.yingyong.test.cgq.MainActivity"><TextViewandroid:id="@+id/tips_tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/hello_world"/><ScrollViewandroid:id="@+id/scrollView1"android:layout_below="@+id/tips_tv"android:layout_width="match_parent"android:layout_height="wrap_content"><LinearLayoutandroid:id="@+id/all_ll"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"></LinearLayout></ScrollView></RelativeLayout>2获取系统的传感器:setContentView(yout.activity_main);all_ll=(LinearLayout)findViewById(R.id.all_ll);tips_tv=(TextView)findViewById(R.id.tips_tv);SensorManager sensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);List<Sensor>sensors=sensorManager.getSensorList(Sensor.TYPE_ALL);tips_tv.setText("传感器的个数:"+ sensors.size());for (Sensor s :sensors) {Button bt=new Button(this);bt.setText(getSensorName(s.getType()));bt.setId(s.getType());bt.setOnClickListener(this);all_ll.addView(bt);}3根据传感器类型获取传感器的名称private String getSensorName(int type){String name="";switch (type) {case Sensor.TYPE_ACCELEROMETER:name="重力加速度感测器";break;case Sensor.TYPE_MAGNETIC_FIELD:name="磁场传感器";break;case Sensor.TYPE_ORIENTATION:name="方位传感器";break;case Sensor.TYPE_GYROSCOPE:name="陀螺仪传感器";break;case Sensor.TYPE_LIGHT:name="环境光线传感器";break;case Sensor.TYPE_PRESSURE:name="压力传感器";break;case Sensor.TYPE_TEMPERATURE:name="温度传感器";break;case Sensor.TYPE_PROXIMITY:name="距离传感器";break;case Sensor.TYPE_GRAVITY:name="重力传感器";break;case Sensor.TYPE_LINEAR_ACCELERATION:name="加速度传感器";break;case Sensor.TYPE_ROTATION_VECTOR:name="矢量传感器";break;case Sensor.TYPE_RELATIVE_HUMIDITY:name="湿度传感器";break;case Sensor.TYPE_AMBIENT_TEMPERATURE:name="温度传感器";break;case Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED: name="磁场传感器";break;case Sensor.TYPE_GAME_ROTATION_VECTOR:name="旋转矢量传感器";break;case Sensor.TYPE_GYROSCOPE_UNCALIBRATED:name="陀螺传感器";break;case Sensor.TYPE_SIGNIFICANT_MOTION:name="运动触发传感器";break;case Sensor.TYPE_STEP_DETECTOR:name="步探测器传感器";break;case Sensor.TYPE_STEP_COUNTER:name="一步计数器传感器";break;case Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR: name="地球磁旋转矢量";break;case Sensor.TYPE_HEART_RATE:name="心率监测";break;// case Sensor.TYPE_TILT_DETECTOR:// name="唤醒传感器";// break;// case Sensor.TYPE_WAKE_GESTURE:// name="唤醒传感器";// break;// case Sensor.TYPE_GLANCE_GESTURE:// name="唤醒传感器";// break;// case Sensor.TYPE_PICK_UP_GESTURE:// name="唤醒传感器";// break;case 26:name="腕倾斜姿态传感器";break;default:break;}return name;}‘4获取光线传感器的例子:publicclass LightActivity extends Activity implements OnClickListener {public Button button1_add;Sensor lightSensor = null;//光线传感器引用SensorManager sensorManager;private TextView tips_tv,name_tv;@Overrideprotectedvoid onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(yout.activity_light);button1_add=(Button)findViewById(R.id.button1_add);tips_tv=(TextView)findViewById(R.id.tips_tv);name_tv=(TextView)findViewById(_tv);name_tv.setText("光线传感器");sensorManager=(SensorManager)getSystemService(Context.SENSOR_SERV ICE);lightSensor=sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);sensorManager.registerListener(new SensorEventListener() {@Overridepublicvoid onSensorChanged(SensorEvent event) {// TODO Auto-generated method stubtips_tv.setText(event.toString());if(event.sensor.getType() == Sensor.TYPE_LIGHT){//将values的值显示到屏幕上float[] values = event.values;Log.v("","value[0]:"+values[0]);tips_tv.setText(tips_tv.getText()+""+"value[0]:"+values[0]);tips_tv.setText(tips_tv.getText()+""+"value[1]:"+values[1]);tips_tv.setText(tips_tv.getText()+""+"value[2]:"+values[2]);}}@Overridepublicvoid onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stubif(sensor.getType() == Sensor.TYPE_LIGHT){//设置将accuracy的值显示到屏幕上// accuracy_view.setText("accuracy:"+accuracy);tips_tv.setText("accuracy:"+accuracy+""+sensor.getMaxDelay()+" "+sensor.getMinDelay()+""+sensor.getResolution());}}},lightSensor,SensorManager.SENSOR_DELAY_NORMAL);}@Overridepublicvoid onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.button1_add:break;default:break;}}}对应的布局文件activity_light<RelativeLayout xmlns:android="/apk/res/andr oid"xmlns:tools="/tools" android:layout_width="match_parent"android:layout_height="match_parent"><ScrollViewandroid:id="@+id/scrollView1"android:layout_width="match_parent"android:layout_height="wrap_content"><LinearLayoutandroid:id="@+id/all_ll"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:id="@+id/name_tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/hello_world"/><TextViewandroid:id="@+id/tips_tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/hello_world"/><Buttonandroid:id="@+id/button1_add"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="22dp"android:layout_marginTop="61dp"android:text="Button"/></LinearLayout></ScrollView></RelativeLayout>。
在Android2.3 gingerbread系统中,google提供了11种传感器供应用层使用。
#define SENSOR_TYPE_ACCELEROMETER 1 //加速度#define SENSOR_TYPE_MAGNETIC_FIELD 2 //磁力#define SENSOR_TYPE_ORIENTATION 3 //方向#define SENSOR_TYPE_GYROSCOPE 4 //陀螺仪#define SENSOR_TYPE_LIGHT 5 //光线感应#define SENSOR_TYPE_PRESSURE 6 //压力#define SENSOR_TYPE_TEMPERATURE 7 //温度#define SENSOR_TYPE_PROXIMITY 8 //接近#define SENSOR_TYPE_GRAVITY 9 //重力#define SENSOR_TYPE_LINEAR_ACCELERATION 10//线性加速度#define SENSOR_TYPE_ROTATION_VECTOR 11//旋转矢量我们依次看看这十一种传感器1 陀螺仪传感器陀螺仪传感器叫做Gyro-sensor,返回x、y、z三轴的角加速度数据。
角加速度的单位是radians/second。
根据Nexus S手机实测:水平逆时针旋转,Z轴为正。
水平逆时针旋转,z轴为负。
向左旋转,y轴为负。
向右旋转,y轴为正。
向上旋转,x轴为负。
向下旋转,x轴为正。
ST的L3G系列的陀螺仪传感器比较流行,iphone4和google的nexus s中使用该种传感器。
2 方向传感器方向传感器简称为O-sensor,返回三轴的角度数据,方向数据的单位是角度。
为了得到精确的角度数据,E-compass需要获取G-sensor的数据,经过计算生产O-sensor数据,否则只能获取水平方向的角度。
方向传感器提供三个数据,分别为azimuth、pitch和roll。
星期天在家没有事情,一边翻译sdk中onSensorChanged 的解释,一遍摸索G1下onSensorChanged 第二个参数values的含义:总结如下,发完赶紧吃饭!1 public abstract void onAccuracyChanged (int sensor, int accuracy复制代码Called when the accuracy of a sensor has changed. See SensorManager for details. Parameters:sensor The ID of the sensor being monitoredaccuracy The new accuracy of this sensor.当sensor的"精确度"发生改变的时候,该方法会被回调,详细信息请参看SensorManager 类参数:sensor 被监视的sensor的IDaccuracy 关于此sensor的新精确度描述2 public abstract void onSensorChanged (int sensor, float[] values复制代码Called when sensor values have changed. The length and contents of the values array vary depending onwhich sensor is being monitored. See SensorManager for details on possible sensor types.当sensor的值发生改变的时候被回调,values数组的元素个数和其中的内容取决于哪个sensor正在被监视,sensor的可用类型请参看SensorManager类Definition of the coordinate system used below.The X axis refers to the screen's horizontal axis (the small edge in portrait mode, the long edge inlandscape mode and points to the right.The Y axis refers to the screen's vertical axis and points towards the top of the screen (the origin isin the lower-left corner.The Z axis points toward the sky when the device is lying on its back on a table.X轴:和屏幕平行并且指向右边的轴(在portrait中使用"小边角",在landscape模式中使用"长边角")Y轴:和屏幕垂直并且指向屏幕上方的轴(坐标原点在左下角)Z轴:当设备(手机背面着地放在桌子上的时候,指向天空的方向就是Z轴IMPORTANT NOTE: The axis are swapped when the device's screen orientation changes. To access theunswapped values, use indices 3, 4 and 5 in values[].一定要大家注意的:坐标系是跟随手机屏幕的方向改变而改变的,values数组中下标为3、4、5的3个元素,使用的是恒定的坐标系,(和尚注:你可以通过这三个元素得到一个不变的坐标,游戏开发中可能会用到(和尚注:此方法的第一个参数sensor可能是SensorManager.SENSOR_ORIENTATIONSensorManager.SENSOR_ORIENTATION中的一个解释如下:SENSOR_ORIENTATION, SENSOR_ORIENTATION_RAW:All values are angles in degrees.values[0]: Azimuth, rotation around the Z axis (0<=azimuth<360. 0 = North, 90 = East, 180 = South, 270 =Westvalues[1]: Pitch, rotation around X axis (-180<=pitch<=180, with positive values when the z-axis movestoward the y-axis.values[2]: Roll, rotation around Y axis (-90<=roll<=90, with positive values when the z-axis movestoward the x-axis.所有的值都是以角度为单位的values[0]:经度,以Z轴为中心旋转(0-360之间,北方=0,东方=90,南方=180,西方=270(和尚注:这个方向指的是google标志所对的方向,为了测试这个值,手都酸了(*^__^*values[1]: 着地点(终于选了一个合适的词,以X为轴转动(-180到180,Z轴向Y 轴方向运动的相对偏移量(和尚注:当手机垂直放在面前的时候为-90,当你面对着手机屏幕看天花板的时候为180/-180,当手机屏幕朝上放在水平的桌子上的时候为0,当手机的USB口朝向天花板垂直放置的时候为90values[2]: 转动,以Y为中心转动(-90到90,Z轴向X轴方向运动的相对偏移量(和尚注:这个运动其实就是我们所说的"垂直屏幕和水平屏幕的切换"Note that this definition of yaw, pitch and roll is different from the traditional definition used inaviation where the X axis is along the long side of the plane (tail to nose.需要注意的是在传统意义上的航空定位中,X轴指的是飞机的机身,而这里的yaw、pitch、roll和它是不一样的,这里的X轴指的是飞机的水平翼(和尚注:SDK中的这个解释对于西方人可能有帮助,对于我们可能会更加迷茫,我相信我上边已经说的够清楚了。
Android操作系统11种传感器介绍在Android2.3 gingerbread系统中,google提供了11种传感器供应用层使用。
#define SENSOR_TYPE_ACCELEROMETER 1 //加速度#define SENSOR_TYPE_MAGNETIC_FIELD 2 //磁力#define SENSOR_TYPE_ORIENTATION 3 //方向#define SENSOR_TYPE_GYROSCOPE 4 //陀螺仪#define SENSOR_TYPE_LIGHT 5 //光线感应#define SENSOR_TYPE_PRESSURE 6 //压力#define SENSOR_TYPE_TEMPERATURE 7 //温度#define SENSOR_TYPE_PROXIMITY 8 //接近#define SENSOR_TYPE_GRAVITY 9 //重力#define SENSOR_TYPE_LINEAR_ACCELERATION 10//线性加速度#define SENSOR_TYPE_ROTATION_VECTOR 11//旋转矢量我们依次看看这十一种传感器1 加速度传感器加速度传感器又叫G-sensor,返回x、y、z三轴的加速度数值。
该数值包含地心引力的影响,单位是m/s^2。
将手机平放在桌面上,x轴默认为0,y轴默认0,z轴默认9.81。
将手机朝下放在桌面上,z轴为-9.81。
将手机向左倾斜,x轴为正值。
将手机向右倾斜,x轴为负值。
将手机向上倾斜,y轴为负值。
将手机向下倾斜,y轴为正值。
加速度传感器可能是最为成熟的一种mems产品,市场上的加速度传感器种类很多。
手机中常用的加速度传感器有BOSCH(博世)的BMA系列,AMK的897X系列,ST的LIS3X系列等。
这些传感器一般提供±2G至±16G的加速度测量范围,采用I2C或SPI接口和MCU 相连,数据精度小于16bit。
所建立Android项目包的名称可自定义修改
//=============syx==============//
package com.example.yidong;
//==============================//
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
public class MainActivity extends Activity implements SensorEventListener {
SensorManager sensorManager;
EditText editText;
EditText editText1;
EditText editText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(yout.activity_main);
editText=(EditText)findViewById(R.id.editText1);
editText1=(EditText)findViewById(R.id.editText2);
editText2=(EditText)findViewById(R.id.editText3);
//获取重力传感器服务
sensorManager=(SensorManager)getSystemService(MainActivity.this.SENSOR_SERVICE);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//注册加速度传感器监听器
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
sensorManager.unregisterListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stub
}
//当参数值改变时调用此函数
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
float[] values=event.values;
StringBuilder sb=new StringBuilder();
StringBuilder sb1=new StringBuilder();
StringBuilder sb2=new StringBuilder();
sb.append("X的加速度=");
sb.append(values[0]+"\n");
sb1.append("Y的加速度=");
sb1.append(values[1]+"\n");
sb2.append("z的加速度=");
sb2.append(values[2]+"\n");
editText.setText(sb.toString());
editText1.setText(sb1.toString());
editText2.setText(sb2.toString());
}
}。