Android 手机获取加速度传感器加速度数据并自定义采样频率

原文链接:http://blog.csdn.net/llp1992/article/details/41786865

因为项目需要利用到了Android手机中的加速度传感器来获取三个加速度轴的加速度大小,同时也可以实现自定义采样频率,这个我写了一个类,通过类来控制。


获取加速度数据总体来说比较简单,首先获取服务:

[java]  view plain  copy
  1. SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);  

然后在onResume()方法中注册监听器,开始使用加速度传感器:

[java]  view plain  copy
  1. sm.registerListener(this, SensorManager.SENSOR_ORIENTATION  
  2.                 | SensorManager.SENSOR_ACCELEROMETER,  
  3.                 simpleRate.get_SENSOR_RATE_FAST());  

接着是很重要的一步,在onDestory()方法中解除监听器,不然加速度传感器会一直工作,很浪费电。还有需要注意的是,手机屏幕暗掉的时候,加速度传感器还是会工作的,如果想要屏幕暗掉的时候加速度传感器步工作,则需要在onPause()方法中解除监听器。

[java]  view plain  copy
  1. @Override  
  2. protected void onStop() {  
  3.     // unregister listener  
  4.     sm.unregisterListener(this);  
  5.     super.onStop();  
  6. }  

接下来是自定义采样频率:

注册监听器的时候最后一个参数是延迟时间,其实也就是采样频率,单位是微秒。只需要写入数字即可控制采样频率,这里通过一个类实现:

SimpleRate.java

[java]  view plain  copy
  1. package com.llp.classdifine;  
  2.   
  3. /** 
  4.  * 加速度传感器采样率设定 
  5.  *  
  6.  * @author Liu_Longpo 
  7.  *  
  8.  */  
  9. public class SimpleRate {  
  10.   
  11.     /** 
  12.      * 50Hz 
  13.      */  
  14.     private int SENSOR_RATE_NORMAL = 20000;  
  15.     /** 
  16.      * 80Hz 
  17.      */  
  18.     private int SENSOR_RATE_MIDDLE = 12500;  
  19.     /** 
  20.      * 100Hz 
  21.      */  
  22.     private int SENSOR_RATE_FAST = 10000;  
  23.   
  24.     public SimpleRate() {  
  25.     };  
  26.   
  27.     /** 
  28.      * 50Hz 
  29.      * @return 
  30.      */  
  31.     public int get_SENSOR_RATE_NORMAL() {  
  32.         return this.SENSOR_RATE_NORMAL;  
  33.     }  
  34.   
  35.     /** 
  36.      * 80Hz 
  37.      *  
  38.      * @return 
  39.      */  
  40.     public int get_SENSOR_RATE_MIDDLE() {  
  41.         return this.SENSOR_RATE_MIDDLE;  
  42.     }  
  43.   
  44.     /** 
  45.      * 100Hz 
  46.      *  
  47.      * @return 
  48.      */  
  49.     public int get_SENSOR_RATE_FAST() {  
  50.         return this.SENSOR_RATE_FAST;  
  51.     }  
  52.   
  53. }  

MainActivity.java 代码如下:

[java]  view plain  copy
  1. package com.llp.acceleration;  
  2.   
  3. import java.io.File;  
  4. import java.util.ArrayList;  
  5.   
  6. import android.app.Activity;  
  7. import android.hardware.SensorListener;  
  8. import android.hardware.SensorManager;  
  9. import android.os.Bundle;  
  10. import android.os.Environment;  
  11. import android.util.Log;  
  12. import android.view.View;  
  13. import android.widget.Button;  
  14. import android.widget.EditText;  
  15. import android.widget.TextView;  
  16. import android.widget.Toast;  
  17.   
  18. import com.llp.classdifine.FileService;  
  19. import com.llp.classdifine.SimpleRate;  
  20.   
  21. public class MainActivity extends Activity implements SensorListener {  
  22.     SensorManager sm = null;  
  23.     TextView acx = null;  
  24.     TextView acy = null;  
  25.     TextView o = null;  
  26.     EditText fileName;  
  27.     EditText contentWrite;  
  28.     Button btnSave;  
  29.     Button btnRead;  
  30.     EditText contentRead;  
  31.     String mfileName;  
  32.     private FileService fileService;  
  33.   
  34.     double ax = 0;  
  35.     double ay = 0;  
  36.     double oy = 0;  
  37.     double oz = 0;  
  38.     double axp = 0;  
  39.     double ayp = 0;  
  40.     double g = 10;  
  41.     double gp = 0;  
  42.   
  43.     /** 
  44.      * 采样频率设定 
  45.      */  
  46.     public SimpleRate simpleRate;  
  47.     // ArrayList<String> mDataListX = new ArrayList<String>();  
  48.     // ArrayList<String> mDataListY = new ArrayList<String>();  
  49.     // ArrayList<String> mDataListZ = new ArrayList<String>();  
  50.     double tmp1 = 0, tmp2 = 0, tmp3 = 0, tmp4 = 0;  
  51.   
  52.     /** 
  53.      * 获取X轴加速度 
  54.      *  
  55.      * @return 
  56.      */  
  57.     public double getAccelerationX() {  
  58.         return axp;  
  59.     }  
  60.   
  61.     /** 
  62.      * 获取Y轴加速度 
  63.      *  
  64.      * @return 
  65.      */  
  66.     public double getAccelerationY() {  
  67.         return ayp;  
  68.     }  
  69.   
  70.     /** 
  71.      * 获取Z轴加速度 
  72.      *  
  73.      * @return 
  74.      */  
  75.     public double getOrientation() {  
  76.   
  77.         return Math.asin(tmp4) / Math.PI * 180.0;  
  78.     }  
  79.   
  80.     @Override  
  81.     public void onCreate(Bundle savedInstanceState) {  
  82.         super.onCreate(savedInstanceState);  
  83.         // get reference to SensorManager  
  84.   
  85.         simpleRate = new SimpleRate();  
  86.         sm = (SensorManager) getSystemService(SENSOR_SERVICE);  
  87.         setContentView(R.layout.activity_main);  
  88.   
  89.         /** 
  90.          * 创建文件夹 
  91.          */  
  92.         File sd = Environment.getExternalStorageDirectory();  
  93.         String mPath = sd.getPath() + "/datas";  
  94.         File file = new File(mPath);  
  95.         if (!file.exists()) {  
  96.             file.mkdir();  
  97.         }  
  98.         /** 
  99.          * 控件初始化 
  100.          */  
  101.         initView();  
  102.   
  103.         btnSave.setOnClickListener(new View.OnClickListener() {  
  104.   
  105.             @Override  
  106.             public void onClick(View v) {  
  107.                 // TODO Auto-generated method stub  
  108.                 mfileName = fileName.getText().toString();  
  109. //              saveAcceleration(mfileName);  
  110.             }  
  111.   
  112.         });  
  113.     }  
  114.   
  115.     /** 
  116.      * 保存加速度数据 
  117.      *  
  118.      * @param FileName 
  119.      * @param content 
  120.      */  
  121.     public void saveAcceleration(String FileName, String content) {  
  122.         try {  
  123.             // 判断sd卡是否存在手机上并且可以进行读写  
  124.             if (Environment.getExternalStorageState().equals(  
  125.                     Environment.MEDIA_MOUNTED)) {  
  126.                 fileService.write2Path(FileName, content);  
  127.             } else {  
  128.             }  
  129.         } catch (Exception e) {  
  130.             Log.v("test", e.toString());  
  131.         }  
  132.     }  
  133.   
  134.     public void initView() {  
  135.         acx = (TextView) findViewById(R.id.xbox);  
  136.         acy = (TextView) findViewById(R.id.ybox);  
  137.         o = (TextView) findViewById(R.id.obox);  
  138.         fileName = (EditText) findViewById(R.id.fileName);  
  139.         contentWrite = (EditText) findViewById(R.id.content);  
  140.         btnSave = (Button) findViewById(R.id.btnsave);  
  141.         btnRead = (Button) findViewById(R.id.btnread);  
  142.         contentRead = (EditText) findViewById(R.id.contentread);  
  143.     }  
  144.   
  145.     public void onSensorChanged(int sensor, float[] values) {  
  146.         synchronized (this) {  
  147.             if (sensor == SensorManager.SENSOR_ORIENTATION) {  
  148.                 oy = values[1];  
  149.                 oz = values[2];  
  150.             }  
  151.             if (sensor == SensorManager.SENSOR_ACCELEROMETER) {  
  152.                 ax = values[0];  
  153.                 ay = values[1];  
  154.             }  
  155.   
  156.             tmp1 = Math.sin(oz * Math.PI / 180.0);  
  157.             tmp2 = Math.sin(Math.abs(oy) * Math.PI / 180.0);  
  158.             tmp3 = Math.sqrt(tmp1 * tmp1 + tmp2 * tmp2);  
  159.             tmp4 = tmp1 / tmp3;  
  160.   
  161.             gp = 10 * tmp3;  
  162.             axp = ax * Math.cos(tmp4) + ay * Math.sin(tmp4);  
  163.             ayp = -ax * Math.sin(tmp4) + ay * Math.cos(tmp4) + gp;  
  164.             acx.setText("a X: " + getAccelerationX());  
  165.             acy.setText("a Y: " + getAccelerationY());  
  166.             o.setText("Orientation : " + getOrientation());  
  167.             // mDataListX.add(getAccelerationX() + "\n");  
  168.             // mDataListY.add(getAccelerationY() + "\n");  
  169.             // mDataListZ.add(getOrientation() + "\n");  
  170.         }  
  171.     }  
  172.   
  173.     public void onAccuracyChanged(int sensor, int accuracy) {  
  174.     }  
  175.   
  176.     @Override  
  177.     protected void onResume() {  
  178.         super.onResume();  
  179.         // register this class as a listener for the orientation and  
  180.         // accelerometer sensors  
  181.         sm.registerListener(this, SensorManager.SENSOR_ORIENTATION  
  182.                 | SensorManager.SENSOR_ACCELEROMETER,  
  183.                 simpleRate.get_SENSOR_RATE_FAST());  
  184.     }  
  185.   
  186.     @Override  
  187.     protected void onStop() {  
  188.         // unregister listener  
  189.         sm.unregisterListener(this);  
  190.         super.onStop();  
  191.     }  
  192. }  

猜你喜欢

转载自blog.csdn.net/bin470398393/article/details/78919284