Cheers. I'm relatively new to Android programming. 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:
@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:
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:
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.
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();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.