简体   繁体   English

如何在 Android 内部保存流式传感器数据?

[英]How to save the streaming sensor data internally on Android?

Cheers.干杯。 I'm relatively new to Android programming.我对 Android 编程比较陌生。 I've been trying to implement an application that reads the sensor data (accelerometer + gyroscope) then saves it internally with a button click.我一直在尝试实现一个应用程序,该应用程序读取传感器数据(加速度计 + 陀螺仪),然后通过单击按钮将其保存在内部。 I was able to get the readings from the sensor successfully.我能够成功地从传感器获得读数。 However, only one string of data is saved in a file format, which shows only the current data and previous data was overwritten.但是,文件格式只保存一串数据,只显示当前数据,之前的数据被覆盖。 Please refer here for the output .请参考这里的输出 Thus, I need some help to fix this.因此,我需要一些帮助来解决这个问题。

In the MainActivity.java, I've registered the sensors listener:在 MainActivity.java 中,我注册了传感器侦听器:

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub
}

// Obtain the sensor data from the phone
boolean IsDataRequested = false;
@Override
public void onSensorChanged(SensorEvent event) {
    Sensor sensor = event.sensor;
    if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

        if (IsDataRequested == true){

            Log.d("Accelerometer", "Acc_X:" + event.values[0] + "Acc_Y:" + event.values[1] + "Acc_Z:" + event.values[2]);

            AccXText.setText("AccX:" + event.values[0]);
            AccYText.setText("AccY:" + event.values[1]);
            AccZText.setText("AccZ:" + event.values[2]);

            save(event);
        }
    }

    if (sensor.getType() == Sensor.TYPE_GYROSCOPE) {

        if (IsDataRequested == true) {

            Log.d("Gyroscope", "Gyro_X:" + event.values[0] + "Gyro_Y:" + event.values[1] + "Gyro_Z:" + event.values[2]);
            GyroXText.setText("GyroX:" + event.values[0]);
            GyroYText.setText("GyroY:" + event.values[1]);
            GyroZText.setText("GyroZ:" + event.values[2]);

            save(event);
        }
    }

}

Then in the same file, I've created the save method:然后在同一个文件中,我创建了 save 方法:

public void save(SensorEvent v) {
    float Acc_X = v.values[0];
    float Acc_Y = v.values[1];
    float Acc_Z = v.values[2];

    float Gyro_X = v.values[0];
    float Gyro_Y = v.values[1];
    float Gyro_Z = v.values[2];

    String accString = "Acc=" + "X:" + Acc_X + "Y:" + Acc_Y + "Z:" + Acc_Z;
    String gyroString = "Gyro=" + "X:" + Gyro_X + "Y:" + Gyro_Y + "Z:" + Gyro_Z;

    String FILENAME = "user";

    FileOutputStream fos = null;
    try {
        fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        fos.write(accString.getBytes());
        fos.write(gyroString.getBytes());
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Button click to start and stop sending the data:单击按钮开始和停止发送数据:

private OnClickListener myOnSensorsRequestClickHandler = new OnClickListener() {
    @Override
            public void onClick(View sensors) {

            IsDataRequested = !IsDataRequested;
            Log.d("Sensors", "Sensors Button Pressed");

    }
};

My goals are:我的目标是:

  1. To have the stream of sensor data saved continuously until the same button is clicked to stop reading the data then it will be saved in one file.要连续保存传感器数据流,直到单击同一按钮停止读取数据,然后将其保存在一个文件中。

  2. The next time I start and stop clicking the button, it will add another saved file, so that I can have multiple files in one folder.下次我开始和停止单击按钮时,它会添加另一个保存的文件,以便我可以在一个文件夹中保存多个文件。

Any help or advice is very much appreciated :)非常感谢任何帮助或建议:)

I hope below suggestion is helpful for your below requirement我希望以下建议对您的以下要求有所帮助

    private OnClickListener myOnSensorsRequestClickHandler = new OnClickListener() {
    @Override
            public void onClick(View sensors) {

            IsDataRequested = !IsDataRequested;
            Log.d("Sensors", "Sensors Button Pressed");

            if(IsDataRequested)
            {
            //Create file name when start service button was clicked
        FILENAME = "user"+new Date().getTime() + ".txt"; //Here FILENAME is a path of file in global variable
        }

    }};



    boolean IsDataRequested = false;
    @Override
    public void onSensorChanged(SensorEvent event) {
        Sensor sensor = event.sensor;
        if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

            if (IsDataRequested == true){



                Log.d("Accelerometer", "Acc_X:" + event.values[0] + "Acc_Y:" + event.values[1] + "Acc_Z:" + event.values[2]);

                AccXText.setText("AccX:" + event.values[0]);
                AccYText.setText("AccY:" + event.values[1]);
                AccZText.setText("AccZ:" + event.values[2]);

                save(FILENAME, event);
            }
            else
            {

            }
        }

        if (sensor.getType() == Sensor.TYPE_GYROSCOPE) {

            if (IsDataRequested == true) {

                Log.d("Gyroscope", "Gyro_X:" + event.values[0] + "Gyro_Y:" + event.values[1] + "Gyro_Z:" + event.values[2]);
                GyroXText.setText("GyroX:" + event.values[0]);
                GyroYText.setText("GyroY:" + event.values[1]);
                GyroZText.setText("GyroZ:" + event.values[2]);

                save(FILENAME, event);
            }
        }

    }
    public void save(String FILENAME, SensorEvent v) {
        float Acc_X = v.values[0];
        float Acc_Y = v.values[1];
        float Acc_Z = v.values[2];

        float Gyro_X = v.values[0];
        float Gyro_Y = v.values[1];
        float Gyro_Z = v.values[2];


//Here used StringBuffer instead of string for append string data

        StringBuffer accQyroData = "========";
        accQyroData.append("Acc=" + "X:" + Acc_X + "Y:" + Acc_Y + "Z:" + Acc_Z"+"\n"+"Gyro=" + "X:" + Gyro_X + "Y:" + Gyro_Y + "Z:" + Gyro_Z);
        accQyroData.append("========");

        //Use Stream for java append to file when you are dealing with raw data, binary
        appendUsingFileOutputStream(FILENAME, accQyroData);
    }

        /**
         * Use Stream for java append to file when you are dealing with raw data, binary
         * data
         * 
         * @param data
         */
        private static void appendUsingFileOutputStream(String fileName, String data) {
            OutputStream os = null;
            try {
                // below true flag tells OutputStream to append
                os = new FileOutputStream(new File(fileName), true);
                os.write(data.getBytes(), 0, data.length());
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
  • To have the stream of sensor data saved continuously until the same button is clicked to stop reading the data then it will be saved in one file.要连续保存传感器数据流,直到单击同一按钮停止读取数据,然后将其保存在一个文件中。
  • The next time I start and stop clicking the button, it will add another saved file, so that I can have multiple files in one folder.下次我开始和停止单击按钮时,它会添加另一个保存的文件,以便我可以在一个文件夹中保存多个文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM