android imu数据存储

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ImuData {

    private SensorManager sensorManager;
    private Handler sensorHandler;
    private Context context;
    private float accx;
    private float accy;
    private float accz;
    private float gyrox;
    private float gyroy;
    private float gyroz;
    private float magx;
    private float magy;
    private float magz;
    private static File sensorDir = null;
    private FileOutputStream sensorFOS;
    private File sensorData;
    private int count = 1;
    private float[] accOutput = new float[3];     // 加速度计变量
    private float[] gyroOutput = new float[3];     // 陀螺仪变量
    private float[] magOutput = new float[3];     // 磁力计变量


    public ImuData(Context context, File datadir) {
        this.context = context;
        initSensor();
        sensorHandler = new Handler() {

            @Override
            public void handleMessage(Message msg) {
                if(msg.what == 0x125){
                    saveSensorData();
                }
            }
        };
    }

    private void saveSensorData() {
        try{

            sensorFOS.write( (count + " ").getBytes() );
            sensorFOS.write( (accOutput[0] + " " + accOutput[1] + " " + accOutput[2] + " " ).getBytes() );
            sensorFOS.write( (gyroOutput[0] + " " + gyroOutput[1] + " " + gyroOutput[2] + " ").getBytes() );
            sensorFOS.write( (magOutput[0] + " " + magOutput[1] + " " + magOutput[2] + " "+"\n").getBytes() );

            sensorFOS.flush();

        } catch (IOException e){
            e.printStackTrace();
        }
    }

    private void initSensor() {
        sensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
        Sensor sensora = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(listenera, sensora, SensorManager.SENSOR_DELAY_GAME);
        Sensor sensorg = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
        sensorManager.registerListener(listenerg, sensorg, SensorManager.SENSOR_DELAY_GAME);
        Sensor sensorm = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
        sensorManager.registerListener(listenerm, sensorm, SensorManager.SENSOR_DELAY_GAME);
        sensorData = getSensorFile();
        try{
            sensorFOS = new FileOutputStream(sensorData);
        } catch (FileNotFoundException e){
            e.printStackTrace();
        }
    }


    private SensorEventListener listenera = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {
            float accx = event.values[0];
            float accy = event.values[1];
            float accz = event.values[2];
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };
    private SensorEventListener listenerg = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {
            float gyrox = event.values[0];
            float gyroy = event.values[1];
            float gyroz = event.values[2];
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };
    private SensorEventListener listenerm = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {
            float magx = event.values[0];
            float magy = event.values[1];
            float magz = event.values[2];
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };
    private static File getSensorFile(){

        //  类的创建方法直接获得MainActivity里建立的dataDir路径

        if(sensorDir == null)
        {
            String folderName = new SimpleDateFormat("yyyyMMdd_HHmm").format(new Date());
            sensorDir = new File("/sdcard/sensor_data/" + folderName);

            // 创建sensor存储目录

            if (!sensorDir.exists()){
                if (!sensorDir.mkdirs()){
                    Log.d("MyCameraApp", "failed to create sensor directory");
                    return null;}
            }

        }
//

        File sensorFile;

        sensorFile = new File( sensorDir.getPath() + File.separator +
                "Sensor.txt");

        return sensorFile;
    }
    public void releaseSensor(){
        try{
            sensorFOS.close();
        } catch (IOException e){
            e.printStackTrace();
        }
        if (sensorManager != null) {
            sensorManager.unregisterListener(listenera);
            sensorManager.unregisterListener(listenerg);
            sensorManager.unregisterListener(listenerm);
        }
    }
}

你可能感兴趣的:(android,imu,数据存储)