Accelerometer code!


Last Updated:

  1. mucasiola

    mucasiola New Member This Topic's Starter

    Joined:
    Nov 1, 2009
    Messages:
    2
    Likes Received:
    0
    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 (Text):
    1. package com.test.Acceltest;
    2.  
    3. import java.util.List;
    4. import android.app.Activity;
    5. import android.content.Context;
    6. import android.hardware.Sensor;
    7. import android.hardware.SensorManager;
    8. import android.os.Bundle;
    9. import android.view.View;
    10. import android.view.View.OnClickListener;
    11. import android.widget.Button;
    12. import android.widget.TextView;
    13.  
    14. public class AccelTest extends Activity implements OnClickListener {
    15.     TextView label;
    16.     Button reading;
    17.     int count = 1;
    18.     private SensorManager mSensorManager;
    19.     boolean record = false;
    20.     Sensor myAcc;
    21.     MySensorListener listener;
    22.  
    23.     @Override
    24.     public void onCreate(Bundle savedInstanceState) {
    25.         super.onCreate(savedInstanceState);
    26.         setContentView(R.layout.main);
    27.        
    28.         // Set up the accelerometer reading
    29.         mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
    30.         List<Sensor> sensorList = mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
    31.         myAcc = sensorList.get(0);
    32.        
    33.         label = (TextView) findViewById(R.id.number_label);
    34.         reading = (Button) findViewById(R.id.reading_button);
    35.         reading.setOnClickListener(this);
    36.  
    37.     }
    38.  
    39.     @Override
    40.     public void onClick(View v) {
    41.         // TODO Auto-generated method stub
    42.         if (reading.getText().equals("Start")){
    43.             // Register Listener
    44.             mSensorManager.registerListener(listener = new MySensorListener(count), myAcc , SensorManager.SENSOR_DELAY_UI);
    45.             reading.setText("Stop");
    46.         }
    47.         else{
    48.             reading.setText("Start");
    49.             count ++;
    50.             label.setText(Integer.toString(count));
    51.             mSensorManager.unregisterListener(listener, myAcc);
    52.         }
    53.     }
    54.  
    55. }

    Code (Text):
    1. package com.test.Acceltest;
    2.  
    3. import java.io.File;
    4. import java.io.FileNotFoundException;
    5. import java.io.FileOutputStream;
    6. import java.io.PrintWriter;
    7.  
    8. import android.hardware.Sensor;
    9. import android.hardware.SensorEvent;
    10. import android.hardware.SensorEventListener;
    11.  
    12. public class MySensorListener implements SensorEventListener{
    13.  
    14.     String comma = new String(",");
    15.     private PrintWriter mCurrentFile;
    16.    
    17.     public MySensorListener(int count){
    18.         //Creating a file to print the data into
    19.  
    20.         String nameStr = new String("/sdcard/data" + count + ".csv");
    21.         File outputFile = new File(nameStr);
    22.         mCurrentFile = null;
    23.         try {
    24.             mCurrentFile = new PrintWriter(new FileOutputStream(outputFile));
    25.         } catch (FileNotFoundException e) {
    26.             // TODO Auto-generated catch block
    27.             e.printStackTrace();
    28.         }
    29.  
    30.     }
    31.  
    32.     @Override
    33.     public void onAccuracyChanged(Sensor sensor, int accuracy) {
    34.             // TODO Auto-generated method stub
    35.  
    36.     }
    37.  
    38.  
    39.     @Override
    40.     public void onSensorChanged(SensorEvent event) {
    41.        
    42.         StringBuffer buff = new StringBuffer();
    43.         buff.append(String.valueOf(event.timestamp));
    44.         buff.append(comma);
    45.         buff.append(String.valueOf(event.values[0]));
    46.         buff.append(comma);
    47.         buff.append(String.valueOf(event.values[1]));
    48.         buff.append(comma);
    49.         buff.append(String.valueOf(event.values[2]));
    50.         mCurrentFile.println(buff.toString());
    51.  
    52.     }
    53.  
    54.  
    55. }
    56.  

    Thanks in advance, any questions just ask!

    Mark
     

    Advertisement
  2. UKLooney

    UKLooney Well-Known Member

    Joined:
    Sep 20, 2009
    Messages:
    79
    Likes Received:
    1
    What are you running the app on? Emulator or real device?
     
  3. mucasiola

    mucasiola New Member This Topic's Starter

    Joined:
    Nov 1, 2009
    Messages:
    2
    Likes Received:
    0
    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.
     
  4. anu

    anu New Member

    Joined:
    Jan 4, 2010
    Messages:
    1
    Likes Received:
    0
    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
     
  5. treecarver

    treecarver New Member

    Joined:
    Mar 31, 2010
    Messages:
    1
    Likes Received:
    0
    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
     
  6. zeze528

    zeze528 New Member

    Joined:
    Dec 21, 2010
    Messages:
    1
    Likes Received:
    0
    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
     
  7. parvendra

    parvendra New Member

    Joined:
    Jan 3, 2011
    Messages:
    1
    Likes Received:
    0
    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
     
  8. dwowwza

    dwowwza New Member

    Joined:
    Dec 11, 2011
    Messages:
    1
    Likes Received:
    0
    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
     
  9. alvin123

    alvin123 New Member

    Joined:
    Jan 16, 2012
    Messages:
    2
    Likes Received:
    0
    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.
     
  10. alvin123

    alvin123 New Member

    Joined:
    Jan 16, 2012
    Messages:
    2
    Likes Received:
    0
    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.
     
  11. tejesh k

    tejesh k New Member

    Joined:
    Mar 18, 2012
    Messages:
    1
    Likes Received:
    0
    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.
     
  12. augustusrdm

    augustusrdm New Member

    Joined:
    Mar 4, 2012
    Messages:
    1
    Likes Received:
    0
    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.
     
  13. Anthony87

    Anthony87 New Member

    Joined:
    Feb 19, 2014
    Messages:
    3
    Likes Received:
    0
    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
     
  14. sheri_bano

    sheri_bano New Member

    Joined:
    Feb 27, 2014
    Messages:
    1
    Likes Received:
    0
    Can you please send me the full project to [redacted]
     
  15. ProjectLiquid

    ProjectLiquid Well-Known Member

    Joined:
    Jan 14, 2014
    Messages:
    95
    Likes Received:
    10
    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.
     
  16. BRAINZ2013

    BRAINZ2013 Well-Known Member Contributor

    Joined:
    Feb 21, 2012
    Messages:
    6,420
    Likes Received:
    1,204
    Not to get off the topic but what happened to the gyroscope
     
  17. ProjectLiquid

    ProjectLiquid Well-Known Member

    Joined:
    Jan 14, 2014
    Messages:
    95
    Likes Received:
    10
    gyroscope?
     
  18. BRAINZ2013

    BRAINZ2013 Well-Known Member Contributor

    Joined:
    Feb 21, 2012
    Messages:
    6,420
    Likes Received:
    1,204
    Yes I thought it was better than accelerometer as I see in the URL its the same and add on to the sensor.
     
  19. ProjectLiquid

    ProjectLiquid Well-Known Member

    Joined:
    Jan 14, 2014
    Messages:
    95
    Likes Received:
    10
    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.
     
  20. BRAINZ2013

    BRAINZ2013 Well-Known Member Contributor

    Joined:
    Feb 21, 2012
    Messages:
    6,420
    Likes Received:
    1,204
    If u rooted go into the permission folder
     

Share This Page

Loading...