安卓开发 G-Sensor

这篇博客主要探讨了如何在安卓项目中集成加速度传感器(G-Sensor)。作者分享了研究过程中收集的资源,并介绍了Sensor类和SensorManager的作用,强调了在不使用传感器时必须关闭以节省电池的重要性。此外,还预告了将详细讲解常用传感器的运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近项目要加入加速度传感器模块,所以这几天一直在研究,暂时先把资料放上面,随后会把成果慢慢讲解下。

http://www.apkbus.com/forum.php?mod=viewthread&tid=16153

http://blog.csdn.net/ekeuy/article/details/39214379

http://www.eoeandroid.com/forum.php?mod=viewthread&tid=75084

http://www.2cto.com/kf/201107/98082.html

一、介绍Sensor类

SDK只有一句介绍“Class representing a sensor. Use getSensorList(int) to get the list of available Sensors.”,表示一个感应器的类,可以使用getSensorList方法(此方法属于接下来要讲的SensorManager)获得所有可用的感应器,该方法返回的是一个List<Sensor>

下面的列表显示了,Sensor所提供的所有服务
----------------------------------------------------------------------------------------------------------------------------------------
Constants

int     TYPE_ACCELEROMETER     A constant describing an accelerometer sensor type.
//三轴加速度感应器 返回三个坐标轴的加速度  单位m/s2
int     TYPE_ALL     A constant describing all sensor types.                    
//用于列出所有感应器
int     TYPE_GRAVITY     A constant describing a gravity sensor type.               
//重力感应器

int     TYPE_GYROSCOPE     A constant describing a gyroscope sensor type           
//陀螺仪 可判断方向 返回三个坐标轴上的角度

int     TYPE_LIGHT     A constant describing an light sensor type.               
//光线感应器 单位 lux 勒克斯

int     TYPE_LINEAR_ACCELERATION     A constant describing a linear acceleration sensor type.
//线性加速度

int     TYPE_MAGNETIC_FIELD     A constant describing a magnetic field sensor type.            
  //磁场感应 返回三个坐标轴的数值  微特斯拉

int     TYPE_ORIENTATION     This constant is deprecated. use SensorManager.getOrientation() instead.
//方向感应器 已过时 可以使用方法获得

int     TYPE_PRESSURE     A constant describing a pressure sensor type                           
//压力感应器  单位 千帕斯卡

int     TYPE_PROXIMITY     A constant describing an proximity sensor type.                        
//距离传感器

int     TYPE_ROTATION_VECTOR     A constant describing a rotation vector sensor type.            
//翻转传感器

int     TYPE_TEMPERATURE     A constant describing a temperature sensor type                 
//温度传感器 单位 摄氏度

----------------------------------------------------------------------------------------------------------------------------------------
此类中包含的方法都是get型的 用来获取所选sensor的一些属性,sensor类一般不需要new而是通过SensorManager的方法获得
二、介绍SensorManager

SDK解释:“SensorManager lets you access the device's sensors. Get an instance of this class by calling Context.getSystemService() with the argument SENSOR_SERVICE.
Always make sure to disable sensors you don't need, especially when your activity is paused. Failing to do so can drain the battery in just a few hours. Note that the system will not disable sensors automatically when the screen turns off. ”
SensorManager 允许你访问设备的感应器。通过传入参数SENSOR_SERVICE参数调用Context.getSystemService方法可以获得一个sensor的实例。永远记得确保当你不需要的时候,特别是Activity暂定的时候,要关闭感应器。忽略这一点肯能导致几个小时就耗尽电池,注意当屏幕关闭时,系统不会自动关闭感应器。

三、常用的传感器

(1) 加速度感应器
可以通过这个感应器获得三个浮点型
x-axis 
y-axis
z-axis
可参阅《android 高级编程2》中的一个插图分析次数据


X Y Z分别对应values[0]到[2]
X表示左右移动的加速度
Y表示前后移动的加速度
Z表示垂直方向的加速度 (测试时发现,将手机置于水平桌面稳定后 XY均为0 Z的值为9.4 约等于重力加速度,依次可以做一个简单的算法实现重力测力计,有时间会给大家一个例子)
下面先看一个基本的获取加速的demo,希望大家好好注意代码中的注释
/*
2  * @author octobershiner
3  * 2011 07 27
4  * SE.HIT
5  * 一个演示android加速度感应器的例子
6  * */
7 
8 package uni.sensor;
9 
10 import java.util.Iterator;
11 import java.util.List;
12 
13 import android.app.Activity;
14 import android.content.Context;
15 import android.hardware.Sensor;
16 import android.hardware.SensorEvent;
17 import android.hardware.SensorEventListener;
18 import android.hardware.SensorManager;
19 import android.os.Bundle;
20 import android.util.Log;
21 
22 public class SensorDemoActivity extends Activity {
23     /** Called when the activity is first created. */
24     //设置LOG标签
25     private static final String TAG = "sensor";
26     private  SensorManager sm;
27     @Override
28     public void onCreate(Bundle savedInstanceState) {
29         super.onCreate(savedInstanceState);
30         setContentView(R.layout.main);
31         //创建一个SensorManager来获取系统的传感器服务
32         sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
33         //选取加速度感应器
34         int sensorType = Sensor.TYPE_ACCELEROMETER;
35         
36         /*
37          * 最常用的一个方法 注册事件
38          * 参数1 :SensorEventListener监听器
39          * 参数2 :Sensor 一个服务可能有多个Sensor实现,此处调用getDefaultSensor获取默认的Sensor
40          * 参数3 :模式 可选数据变化的刷新频率
41          * */
42         sm.registerListener(myAccelerometerListener,sm.getDefaultSensor(sensorType),SensorManager.SENSOR_DELAY_NORMAL);
43         
44     }
45     
46     /*
47      * SensorEventListener接口的实现,需要实现两个方法
48      * 方法1 onSensorChanged 当数据变化的时候被触发调用
49      * 方法2 onAccuracyChanged 当获得数据的精度发生变化的时候被调用,比如突然无法获得数据时
50      * */
51     final SensorEventListener myAccelerometerListener = new SensorEventListener(){
52         
53         //复写onSensorChanged方法
54         public void onSensorChanged(SensorEvent sensorEvent){
55             if(sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
56                 Log.i(TAG,"onSensorChanged");
57                 
58                 //图解中已经解释三个值的含义
59                 float X_lateral = sensorEvent.values[0];
60                 float Y_longitudinal = sensorEvent.values[1];
61                 float Z_vertical = sensorEvent.values[2];
62                 Log.i(TAG,"\n heading "+X_lateral);
63                 Log.i(TAG,"\n pitch "+Y_longitudinal);
64                 Log.i(TAG,"\n roll "+Z_vertical);
65             }
66         }
67         //复写onAccuracyChanged方法
68         public void onAccuracyChanged(Sensor sensor , int accuracy){
69             Log.i(TAG, "onAccuracyChanged");
70         }
71     };
72     
73     public void onPause(){
74         /*
75          * 很关键的部分:注意,说明文档中提到,即使activity不可见的时候,感应器依然会继续的工作,测试的时候可以发现,没有正常的刷新频率
76          * 也会非常高,所以一定要在onPause方法中关闭触发器,否则讲耗费用户大量电量,很不负责。
77          * */
78         sm.unregisterListener(myAccelerometerListener);
79         super.onPause();
80     }
81     
82 }
四、一种使用感应器的应用如何刷新UI的好办法,值得学习
package uni.sensor;

 

import java.util.Timer;

import java.util.TimerTask;

 

import android.app.Activity;

import android.content.Context;

import android.hardware.Sensor;

import android.hardware.SensorEvent;

import android.hardware.SensorEventListener;

import android.hardware.SensorManager;

import android.os.Bundle;

import android.widget.TextView;

 

public class ForceometerActivity extends Activity{

 

       SensorManager sensorManager;

       TextView accelerationTextView;

       TextView maxAccelerationTextView;

      

       float currentAcceleration = 0;

       float maxAcceleration = 0;

 

       @Override

       protected void onCreate(Bundle savedInstanceState) {

              // TODO Auto-generated method stub

              super.onCreate(savedInstanceState);

              setContentView(R.layout.main);

              //获取两个文本显示域

              accelerationTextView = (TextView)findViewById(R.id.acceleration);

              maxAccelerationTextView = (TextView)findViewById(R.id.maxAcceleration);

              //获取sensor服务,选择加速度感应器

              sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);

              Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

              //注册事件

              sensorManager.registerListener(sensorEventListener,

              accelerometer,

              SensorManager.SENSOR_DELAY_FASTEST);

 

              Timer updateTimer = new Timer("gForceUpdate");

              updateTimer.scheduleAtFixedRate(new TimerTask() {

              public void run() {

              updateGUI();

              }

              }, 0, 100);

 

             

       }

      

       //添加的新方法,退出activity的时候,关闭监听器

       public void onPause(){

              sensorManager.unregisterListener(sensorEventListener);

              super.onPause();

       }

 

      

       private final SensorEventListener sensorEventListener = new SensorEventListener() {

              //系统设置的重力加速度标准值,设备在水平静止的情况下就承受这个压力,所以默认Y轴方向的加速度值为STANDARD_GRAVITY

              double calibration = SensorManager.STANDARD_GRAVITY;

             

              public void onAccuracyChanged(Sensor sensor, int accuracy) { }

             

              public void onSensorChanged(SensorEvent event) {

              double x = event.values[0];

              double y = event.values[1];

              double z = event.values[2];

             

              //计算三个方向的加速度

              double a = Math.round(Math.sqrt(Math.pow(x, 2) +

              Math.pow(y, 2) +

              Math.pow(z, 2)));

             

              //消去原有的重力引起的压力

              currentAcceleration = Math.abs((float)(a-calibration));

              if (currentAcceleration > maxAcceleration)

              maxAcceleration = currentAcceleration;

              }

              };

 

              private void updateGUI() {

                     /*

                      * 推荐的一个刷新UI的方法

                      * Activity.runOnUiThread(Runnable)

                      * 在新的线程中更新UI

                      * Runnable是一个接口,需要你实现run方法,上面的TimerTask就是实现了这个接口同样需要实现run方法

                      * */

                     runOnUiThread(new Runnable() {

                     public void run() {

                     String currentG = currentAcceleration/SensorManager.STANDARD_GRAVITY

                     + "Gs";

                     accelerationTextView.setText(currentG);

                     accelerationTextView.invalidate();

                     String maxG = maxAcceleration/SensorManager.STANDARD_GRAVITY + "Gs";

                     maxAccelerationTextView.setText(maxG);

                     maxAccelerationTextView.invalidate();

                     }

                     });

                    

                     }

 

     

}
布局文件
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<TextView android:id="@+id/acceleration"

android:gravity="center"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textStyle="bold"

android:textSize="32sp"

android:text="CENTER"

android:editable="false"

android:singleLine="true"

android:layout_margin="10px"/>

 

<TextView android:id="@+id/maxAcceleration"

android:gravity="center"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textStyle="bold"

android:textSize="40sp"

android:text="CENTER"

android:editable="false"

android:singleLine="true"

android:layout_margin="10px"/>

 

</LinearLayout>
五、Git上面的一个Demo
搞懂了这些后主要是开始看算法,在Git上面找到一个Demo,里面的算法挺不错可以很精确测算出走动的步数,里面的核心代码贴出来
 private void detectStep(SensorEvent event) {
        if (event == null) throw new NullPointerException();

        detecting = true;

        float vSum = 0;
        for (int i = 0; i < 3; i++) {
            final float v = mYOffset + event.values[i] * mScale[0];
            vSum += v;
        }
        int k = 0;
        float v = vSum / 3;

        float direction = (v > mLastValues[k] ? 1 : (v < mLastValues[k] ? -1 : 0));
        if (direction == -mLastDirections[k]) {
            // Direction changed
            int extType = (direction > 0 ? 0 : 1); // minumum or maximum?
            mLastExtremes[extType][k] = mLastValues[k];
            float diff = Math.abs(mLastExtremes[extType][k] - mLastExtremes[1 - extType][k]);

            if (diff > mLimit) {
                boolean isAlmostAsLargeAsPrevious = diff > (mLastDiff[k] * 2 / 3);
                boolean isPreviousLargeEnough = mLastDiff[k] > (diff / 3);
                boolean isNotContra = (mLastMatch != 1 - extType);

                if (isAlmostAsLargeAsPrevious && isPreviousLargeEnough && isNotContra) {
                    for (StepListener stepListener : mStepListeners) {
                        stepListener.onStep();
                    }
                    mLastMatch = extType;
                } else {
                    mLastMatch = -1;
                }
            }
            mLastDiff[k] = diff;
        }
        mLastDirections[k] = direction;
        mLastValues[k] = v;

        detecting = false;
    }
当然里面还用的很多其他技术,就不一一说明了,我把代码上传到CSDN上面,有兴趣的可以研读下。
                                                                           


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值