• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Accelerometer code!

mucasiola

Lurker
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 :p

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
 
yeah, running on device.

I managed to write new code which works now. I think it's because I was handling the file creation inside the onSensorChanged() event. Not 100% why, but I think it just made new files after it had logged all the data. Thus making it look like it had done nothing but make empty files.

Anyway, if people are interested in source code pop us a reply here and I'll dig it up. Sorry for taking so long to reply, been a busy man of late.
 
Hi Friend ,

Could you please send me that code ....
actually i have to display the value of the accelerometer reading on the emulator screen ....I am using openintent accelerometer ...could you please also let me know if u have worked on this ....I am using Android 2.0 Version...below is the attached code

mport​
org.openintents.hardware.SensorManagerSimulator;



import​
org.openintents.provider.Hardware;

import​
android.app.Activity;

//import android.content.Context;​
import​
android.content.Intent;

import​
android.hardware.Sensor;

//import android.hardware.SensorEvent;
//import android.hardware.SensorEventListener;​
import​
android.hardware.SensorManager;

import​
android.os.Bundle;

import​
android.hardware.SensorListener;

import​
android.widget.TextView;





public​
class test1 extends Activity {

private TextView mTxtView;

private SensorManager sm;


/**private SensorEventListener mySensorEventListener;*/

/** Called when the activity is first created. */



@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

mTxtView = new TextView(this);
setContentView(
mTxtView);
/*setContentView(R.layout.main);*/


Hardware.​
mContentResolver = getContentResolver();

/**sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);*/


sm = (SensorManager) new SensorManagerSimulator((SensorManager)
getSystemService(
SENSOR_SERVICE));



/** Intent intent = new Intent(Intent.ACTION_VIEW,
Hardware.Preferences.CONTENT_URI);
startActivity(intent); */


/** sm.unregisterListener(mGraphView); */


SensorManagerSimulator.connectSimulator();

sm.registerListener( mySensorListener,
Sensor.
TYPE_ACCELEROMETER |
Sensor.
TYPE_MAGNETIC_FIELD |
Sensor.
TYPE_ORIENTATION,
SensorManager.
SENSOR_DELAY_UI);

/** mTxtView = new TextView(this);
setContentView(mTxtView); */

}

public void updateTV(float p_x, float p_y, float p_z)
{
mTxtView.setText("x: "+p_x+", y: "+p_y+", z: "+p_z);

}

private final SensorListener mySensorListener = new SensorListener() {

public void onSensorChanged(int sensor, float[] values) {
if (sensor == Sensor.TYPE_ACCELEROMETER)
{
float x = values[SensorManager.DATA_X];
float y = values[SensorManager.DATA_Y];
float z = values[SensorManager.DATA_Z];
updateTV(x, y , z);
}
}
public void onAccuracyChanged(int sensor, int accuracy) {}
};

/**private final SensorEventListener mSensorListener = new SensorEventListener() {

public void onSensorChanged(SensorEvent se)
{
float x = se.values[0];
float y = se.values[1];
float z = se.values[2];
updateTV(x, y , z);
}

public void onAccuracyChanged(Sensor sensor, int accuracy) {}
}; */




@Override

protected void onResume() {
super.onResume();
sm.registerListener(mySensorListener,
Sensor.
TYPE_ACCELEROMETER |
Sensor.
TYPE_MAGNETIC_FIELD |
Sensor.
TYPE_ORIENTATION,
SensorManager.
SENSOR_DELAY_UI);
}

}




thanks in advance
 
I'm about to some thing similar and cannot get the data stored are willing to share your code, my work will be on detecting fall patterns. I can get the data to the screen but storing it beats me

thanks
alan
 
Could you provide the code you have modified so that to solve this problem?
I have tried your method but didn't get a good result..................

thks ze
 
Hi,
I want to capture the accelerometer reading and want to check how much fast a device is moving around.
can somebody please provide the code for that?

I mean suppose a device is in my hand and I am shaking my hand fast then I want to display a message on screen that you are moving too fast.

Thanks in advance
Parvendra
 
Hi, I'm pretty new to Android programming and I'm trying to get an accelerometer recorder working, can anyone please send me some working accelerometer reading code? Need to find out where I'm going wrong!

Thanks,
Darren
 
I am making an android application to collect accelerometer data for analysis of various activities but im having trouble storing data in a file and transferring it to the pc via wifi. I have recently started doing java programming and working on android so im struggling and would be grateful if someone could give me some sample code to help me out.
 
I tried the code above but im unable to remove the errors would be very grateful if i could look at your code and see where i went wrong.
 
Hi, I am working on a project to develope an andriod application. Can you please help me in providing a code that keep displaying accelerometer values in the screen for 2 minutes (i.e when handset is moved randomly for 2 mins, those values should be displayed). Please help me by providing the above mentioned codes with explaination, as I am new to Andriod SDK. Thank you.
 
Hello Sir, I do need the program that you have developed for the saving of accelerometer data values on a text file for I need it to create an application that clusters the values of the accelerometer for motion detection.
 
Can you please send me the full project to [redacted]

I'm developing a Vibration analysis project, big part of if is to read data from "Linear acceleration snsor" or "acceleration sensor" and plot them.

Thank you
 
Maybe all of you who are asking for the project code should read up on sensors while you wait? That way you have a much better Idea as to what's going on when you implement the code.

Sensors Overview | Android Developers

Personally, If I was Mucasiola. I wouldn't give you the whole project code. But I may feel generous enough to give you a starting point for this.
 
Maybe its only on certain devices? I dunno. Is there a way to check? if say, for example my phone the evo, to see if it has a gyroscope?

Nevermind, I just did a quick search. There is one. I guess I just never thought about it.
 
Back
Top Bottom