Hey guys,
I realise this is my first post - but please do not judge me on that. I would like to eventually become good enough with android developing to help out others too.
Anyway, here's the issue. I have to create a program using accelerometers. I have written these pieces of code in order to start recording the accelerometer data when you push start, stopping when you press stop and then saving that in a file called data1.csv and so on (data2,3 4 etc). The reason I want to do this is so that I can look at the data for a given gesture and analyse it using graphs and other simple methods.
The issue I'm having is this program just makes random empty files - doesnt seem to log the data. It does make each file though, so that's nice
Anyway, here's the code.
Thanks in advance, any questions just ask!
Mark
I realise this is my first post - but please do not judge me on that. I would like to eventually become good enough with android developing to help out others too.
Anyway, here's the issue. I have to create a program using accelerometers. I have written these pieces of code in order to start recording the accelerometer data when you push start, stopping when you press stop and then saving that in a file called data1.csv and so on (data2,3 4 etc). The reason I want to do this is so that I can look at the data for a given gesture and analyse it using graphs and other simple methods.
The issue I'm having is this program just makes random empty files - doesnt seem to log the data. It does make each file though, so that's nice

Anyway, here's the code.
Code:
package com.test.Acceltest;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class AccelTest extends Activity implements OnClickListener {
TextView label;
Button reading;
int count = 1;
private SensorManager mSensorManager;
boolean record = false;
Sensor myAcc;
MySensorListener listener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set up the accelerometer reading
mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
List<Sensor> sensorList = mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
myAcc = sensorList.get(0);
label = (TextView) findViewById(R.id.number_label);
reading = (Button) findViewById(R.id.reading_button);
reading.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (reading.getText().equals("Start")){
// Register Listener
mSensorManager.registerListener(listener = new MySensorListener(count), myAcc , SensorManager.SENSOR_DELAY_UI);
reading.setText("Stop");
}
else{
reading.setText("Start");
count ++;
label.setText(Integer.toString(count));
mSensorManager.unregisterListener(listener, myAcc);
}
}
}
Code:
package com.test.Acceltest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
public class MySensorListener implements SensorEventListener{
String comma = new String(",");
private PrintWriter mCurrentFile;
public MySensorListener(int count){
//Creating a file to print the data into
String nameStr = new String("/sdcard/data" + count + ".csv");
File outputFile = new File(nameStr);
mCurrentFile = null;
try {
mCurrentFile = new PrintWriter(new FileOutputStream(outputFile));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
StringBuffer buff = new StringBuffer();
buff.append(String.valueOf(event.timestamp));
buff.append(comma);
buff.append(String.valueOf(event.values[0]));
buff.append(comma);
buff.append(String.valueOf(event.values[1]));
buff.append(comma);
buff.append(String.valueOf(event.values[2]));
mCurrentFile.println(buff.toString());
}
}
Thanks in advance, any questions just ask!
Mark