Accessing the Accelerometer

Tutorials with advanced 'difficulty' and more Lines of Code.

Postby pizzlefou » Fri May 22, 2009 2:45 pm

hello PI9,

i havent checked your code that much but i noticed that you used SensorListener altho it's now deprecated, havent you got some warnings in eclipse?
In my opinion you should use SensorEventListener, check the references in the google dev page :)
pizzlefou
Freshman
Freshman
 
Posts: 2
Joined: Fri May 15, 2009 11:06 am

Top

Postby noya » Wed Jun 10, 2009 9:32 am

I've got a simple question related to the units of measure used in Android. For acceleration are them expressed in G or in m/^2 ? Because I am using the Openintents Sensor simulator and it gives me the values in m/s^2 but I have to use G measures... Since I can't test my apps on a real device I just want to know if it's ok to convert the values in G (simply dividing the accell given by the Sensor simulator by the GRAVITY_EARTH constant) or it's a mistake ? (I mean, passing the application in a real device will have non-valid values ?)

thanks!
User avatar
noya
Junior Developer
Junior Developer
 
Posts: 14
Joined: Fri May 08, 2009 3:16 pm
Location: Bergamo, Italy

Postby Zonakusu » Sat Jun 13, 2009 7:15 pm

Here is my accelerometer reading code which works with the new SensorEventListener.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.minnamedia.nl.accelerometertest;
  3.  
  4.  
  5.  
  6. import java.util.List;
  7.  
  8.  
  9.  
  10. import android.app.Activity;
  11.  
  12. import android.content.Context;
  13.  
  14. import android.os.Bundle;
  15.  
  16. import android.hardware.Sensor;
  17.  
  18. import android.hardware.SensorEvent;
  19.  
  20. import android.hardware.SensorEventListener;
  21.  
  22. import android.hardware.SensorManager;
  23.  
  24. import android.widget.TextView;
  25.  
  26.  
  27.  
  28. public class accelerometerTest extends Activity {
  29.  
  30.        
  31.  
  32.         private TextView accText;
  33.  
  34.         private SensorManager myManager;
  35.  
  36.         private List<Sensor> sensors;
  37.  
  38.         private Sensor accSensor;
  39.  
  40.         private float oldX, oldY, oldZ = 0f;
  41.  
  42.        
  43.  
  44.     /** Called when the activity is first created. */
  45.  
  46.     @Override
  47.  
  48.     public void onCreate(Bundle savedInstanceState) {
  49.  
  50.         super.onCreate(savedInstanceState);        
  51.  
  52.              
  53.  
  54.         setContentView(R.layout.main);
  55.  
  56.        
  57.  
  58.         accText = (TextView)findViewById(R.id.accText);  
  59.  
  60.        
  61.  
  62.         // Set Sensor + Manager
  63.  
  64.         myManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
  65.  
  66.         sensors = myManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
  67.  
  68.         if(sensors.size() > 0)
  69.  
  70.         {
  71.  
  72.                 accSensor = sensors.get(0);
  73.  
  74.         }
  75.  
  76.     }
  77.  
  78.  
  79.  
  80.     private void updateTV(float x, float y, float z)
  81.  
  82.     {
  83.  
  84.         float thisX = x - oldX * 10;
  85.  
  86.         float thisY = y - oldY * 10;
  87.  
  88.         float thisZ = z - oldZ * 10;
  89.  
  90.        
  91.  
  92.         accText.setText("x: " + Math.round(thisX) + ";\n y:" + Math.round(thisY) + ";\n z: " + Math.round(thisZ));
  93.  
  94.        
  95.  
  96.         oldX = x;
  97.  
  98.         oldY = y;
  99.  
  100.         oldZ = z;
  101.  
  102.     }
  103.  
  104.    
  105.  
  106.     private final SensorEventListener mySensorListener = new SensorEventListener()
  107.  
  108.     {
  109.  
  110.         public void onSensorChanged(SensorEvent event)
  111.  
  112.         {
  113.  
  114.                 updateTV(event.values[0],
  115.  
  116.                                 event.values[1],
  117.  
  118.                                 event.values[2]);
  119.  
  120.         }
  121.  
  122.        
  123.  
  124.         public void onAccuracyChanged(Sensor sensor, int accuracy) {}
  125.  
  126.     };
  127.  
  128.    
  129.  
  130.     @Override
  131.  
  132.     protected void onResume()
  133.  
  134.     {
  135.  
  136.         super.onResume();
  137.  
  138.         myManager.registerListener(mySensorListener, accSensor, SensorManager.SENSOR_DELAY_GAME);      
  139.  
  140.     }
  141.  
  142.    
  143.  
  144.     @Override
  145.  
  146.     protected void onStop()
  147.  
  148.     {          
  149.  
  150.         myManager.unregisterListener(mySensorListener);
  151.  
  152.         super.onStop();
  153.  
  154.     }
  155.  
  156. }
  157.  
  158.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


I dont have any other experience with the accelerometer and I am not really happy with the result of this code, I think I need to understand the accelerometer better.
Zonakusu
Freshman
Freshman
 
Posts: 5
Joined: Sun Jun 07, 2009 7:56 pm

Postby sunil.android » Fri Aug 07, 2009 6:51 pm

Hi...
I took your code and try to run this but i got errors.... plz help me..

this is my code....


package sun.pack;

import android.hardware.Sensors;
public class AccelerometerReader
{

/** True when the Accelerometer-functionality is basically available. */
boolean accelerometerAvailable = false;
boolean isEnabled = false;
/**
* Sets up an AccelerometerReader. Checks if Accelerometer is available on
* this device and throws UnsupportedOperationException if not .
*
* @param doEnable :
* enables the devices Accelerometer
* initially (if sensor available)
* @throws UnsupportedOperationException
* if Accelerometer is not available on this device.
*/
public AccelerometerReader(boolean doEnable)
throws UnsupportedOperationException {

/* Check once here in the constructor if an
* Accelerometer is available on this device. */
for (String aSensor : Sensors.getSupportedSensors())
if (aSensor.equals(Sensors.SENSOR_ACCELEROMETER))
accelerometerAvailable = true;

if (!accelerometerAvailable)
throw new UnsupportedOperationException(
"Accelerometer is not available.");

if (doEnable)
setEnableAccelerometer(true);
}

/**
* En/Dis-able the Accelerometer.
*
* @param doEnable
* <code>true</code> for enable.<br>
* <code>false</code> for disable.
* @throws UnsupportedOperationException
*/
public void setEnableAccelerometer(boolean doEnable)
throws UnsupportedOperationException
{
if (!accelerometerAvailable)
throw new UnsupportedOperationException(
"Accelerometer is not available.");

/* If should be enabled and not already is: */
if (doEnable && !this.isEnabled) {
Sensors.enableSensor(Sensors.SENSOR_ACCELEROMETER);
this.isEnabled = true;
} else /* If should be disabled and not already is: */
if (!doEnable && this.isEnabled) {
Sensors.disableSensor(Sensors.SENSOR_ACCELEROMETER);
this.isEnabled = false;
}
}


/**
* Read out the values currently provided by the Accelerometer.
*
* @return the current Accelerometer-values.
* @throws UnsupportedOperationException
* if Accelerometer is not available on this device.
* @throws IllegalStateException
* if Accelerometer was set to disabled.
*/
public float[] readAccelerometer() throws UnsupportedOperationException, IllegalStateException {
if (!accelerometerAvailable)
throw new UnsupportedOperationException(
"Accelerometer is not available.");

if (!this.isEnabled)
throw new IllegalStateException(
"Accelerometer was set to disabled!");
/* Get number of sensor-values the sensor will return. Could be
* variable, depending of the amount of axis (1D, 2D or 3D
* accelerometer). */
int sensorValues = Sensors
.getNumSensorValues(Sensors.SENSOR_ACCELEROMETER);
float[] out = new float[sensorValues];

/* Make the OS fill the array we passed. */
Sensors.readSensor(Sensors.SENSOR_ACCELEROMETER, out);

/* And return it. */
return out;
}
}

//--------------------------------------------------------------------------------------------------


package sun.sensors;

import android.app.Activity;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;

@SuppressWarnings("deprecation")
public class SensorsDemoActivity extends Activity implements SensorListener
{
final String TAG = "SensorDemoActivity";
SensorManager sm =null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

sm = (SensorManager) getSystemService(SENSOR_SERVICE);

setContentView(R.layout.main);
}

@Override
public void onAccuracyChanged(int sensor, int accuracy)
{
// TODO Auto-generated method stub
Log.d(TAG,"onAccuracyChanged:()--" + sensor + ", accuracy: " + accuracy);
}

@Override
public void onSensorChanged(int sensor, float[] values)
{
// TODO Auto-generated method stub
if(sensor == SensorManager.SENSOR_ACCELEROMETER)
{
Log.v(TAG, "x-->"+values[0]);
Log.v(TAG, "y-->"+values[1]);
Log.v(TAG, "z-->"+values[2]);
}
}
}
Thanks & Regards

Sun
sunil.android
Freshman
Freshman
 
Posts: 6
Joined: Mon Aug 03, 2009 6:17 pm

Postby MartinClave » Thu Oct 15, 2009 12:27 pm

I'm trying execute this code to check the Accelerometer access on my device. I included this code in one java file and added that from Actibity as same as plusminus suggested, but it is throwing error for 'Sendsors' class has not found. Error: android.hardware.Sensors cannot be resolved.
How to find it or Where are the implementation for that?

I'M ACTUALLY TALKING ABOUT THE INITIAL CODE WHICH HAS PASTED BY MR.PLUSMINUS.

thanks.
MartinClave
Freshman
Freshman
 
Posts: 5
Joined: Thu Oct 15, 2009 10:49 am

Postby MartinClave » Thu Oct 15, 2009 1:10 pm

Hi Zonakusu,

I am able to run your SensorEventListner code on my 1.5 Android device. It is running x, y, z values continuously. But i want to know what is this about mentioning? I need to develop a module which should detect notification whenever Android device is being moved(whether device is taken or shaken or any kind movement). For that i know that i should have AcceleroMeter access programmatically. As i'm very new to Android application development, have gone through some of tutorial. Could you please send me some sample source? (or) I would like to know how is this x, y, z values help me on this to detect whether accelerometer is triggered or idle?

thanks.
MartinClave
Freshman
Freshman
 
Posts: 5
Joined: Thu Oct 15, 2009 10:49 am

Top

Postby dickwan » Sun Dec 27, 2009 11:46 pm

Drainman wrote:I have this code that i made to test the accelerometer out. I'll just paste it here. Hope it helps someone.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ...    
  2.         @Override
  3.         protected void onResume()
  4.         {
  5.                 super.onResume();
  6.                 mSensorManager.registerListener(mSensorListener,
  7.                                 SensorManager.<span style="font-weight: bold"><span style="color: darkred">SENSOR_ORIENTATION</span></span>,
  8.                                 SensorManager.SENSOR_DELAY_GAME);
  9.         }
  10. }
  11.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Hi Drainman,
Thanks a lot for you code, but since you said it was for the accelerometer, set the sensor manager to accelerometer and not orientation

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.         public void onResume(){
  3.                 super.onResume();
  4.                 mSensorManager.registerListener(mSensorListener,
  5.                                         SensorManager.<span style="font-weight: bold"><span style="color: darkblue">SENSOR_ACCELEROMETER</span></span>,
  6.                                         SensorManager.SENSOR_DELAY_GAME);
  7.         }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
dickwan
Freshman
Freshman
 
Posts: 8
Joined: Sun Dec 27, 2009 11:39 pm

help me to Change this part of code please

Postby letroll » Tue Mar 16, 2010 5:12 pm

Hello i'm a french begginer on java and android :)
I want to change this part of your code that I edit for use like in the first tutorial, (my code in a another class with a call of type : private accel mAccel = new accel; in my main programme ).
A wrapper you call it I think

but I can not make the changes correctly, I think we should use getters and setters but I do not know how?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package fr.letroll.accel;
  3.  
  4.  
  5.  
  6. import java.util.List;
  7.  
  8.  
  9.  
  10. import android.app.Activity;
  11.  
  12. import android.content.Context;
  13.  
  14. import android.os.Bundle;
  15.  
  16. import android.hardware.Sensor;
  17.  
  18. import android.hardware.SensorEvent;
  19.  
  20. import android.hardware.SensorEventListener;
  21.  
  22. import android.hardware.SensorManager;
  23.  
  24. import android.widget.TextView;
  25.  
  26.  
  27.  
  28. public class accel extends Activity {
  29.  
  30.      
  31.  
  32.      private TextView accText;
  33.  
  34.      private SensorManager myManager;
  35.  
  36.      private List<Sensor> sensors;
  37.  
  38.      private Sensor accSensor;
  39.  
  40.      private float oldX, oldY, oldZ = 0f;
  41.  
  42.      
  43.  
  44.     /** Called when the activity is first created. */
  45.  
  46.     @Override
  47.  
  48.     public void onCreate(Bundle savedInstanceState) {
  49.  
  50.         super.onCreate(savedInstanceState);        
  51.  
  52.              
  53.  
  54.         setContentView(R.layout.main);
  55.  
  56.        
  57.  
  58.         accText = (TextView)findViewById(R.id.TextView01);  
  59.  
  60.        
  61.  
  62.         // Set Sensor + Manager
  63.  
  64.         myManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
  65.  
  66.         sensors = myManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
  67.  
  68.         if(sensors.size() > 0)
  69.  
  70.         {
  71.  
  72.           accSensor = sensors.get(0);
  73.  
  74.         }
  75.  
  76.     }
  77.  
  78.  
  79.  
  80.     private void updateTV(float x, float y, float z)
  81.  
  82.     {
  83.  
  84.      float thisX = x - oldX * 10;
  85.  
  86.      float thisY = y - oldY * 10;
  87.  
  88.      float thisZ = z - oldZ * 10;
  89.  
  90.      
  91.  
  92.      accText.setText("x: " + Math.round(thisX) + ";\n y:" + Math.round(thisY) + ";\n z: " + Math.round(thisZ));
  93.  
  94.      
  95.  
  96.      oldX = x;
  97.  
  98.      oldY = y;
  99.  
  100.      oldZ = z;
  101.  
  102.     }
  103.  
  104.    
  105.  
  106.     private final SensorEventListener mySensorListener = new SensorEventListener()
  107.  
  108.     {
  109.  
  110.      public void onSensorChanged(SensorEvent event)
  111.  
  112.      {
  113.  
  114.           updateTV(event.values[0],
  115.  
  116.                     event.values[1],
  117.  
  118.                     event.values[2]);
  119.  
  120.      }
  121.  
  122.      
  123.  
  124.      public void onAccuracyChanged(Sensor sensor, int accuracy) {}
  125.  
  126.     };
  127.  
  128.    
  129.  
  130.     @Override
  131.  
  132.     protected void onResume()
  133.  
  134.     {
  135.  
  136.      super.onResume();
  137.  
  138.      myManager.registerListener(mySensorListener, accSensor, SensorManager.SENSOR_DELAY_GAME);      
  139.  
  140.     }
  141.  
  142.    
  143.  
  144.     @Override
  145.  
  146.     protected void onStop()
  147.  
  148.     {    
  149.  
  150.      myManager.unregisterListener(mySensorListener);
  151.  
  152.      super.onStop();
  153.  
  154.     }
  155.  
  156. }
Parsed in 0.045 seconds, using GeSHi 1.0.8.4
letroll
Freshman
Freshman
 
Posts: 4
Joined: Tue Mar 16, 2010 4:59 pm

Simulator

Postby jerrygarciame » Wed Mar 31, 2010 8:27 pm

OpenIntents looks great, but does not work with the current API. Any suggestions for simulators that work with the latest API? Thanks!
jerrygarciame
Freshman
Freshman
 
Posts: 9
Joined: Tue Mar 30, 2010 4:25 am

Postby petterwr » Sun Apr 18, 2010 6:15 pm

Does the functions for calling the accelerometer vary from phone-model to model or are they standardized?
petterwr
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Apr 18, 2010 6:05 pm

Re: Accessing the Accelerometer

Postby Mafiozo » Mon Jun 21, 2010 11:45 am

I am also interested how to make current sensorsimulator work with new api 2.2. Did anyone try to do that or does anyone has some guides how to do that?
Mafiozo
Freshman
Freshman
 
Posts: 8
Joined: Mon May 24, 2010 8:29 pm

Re: Accessing the Accelerometer

Postby BlackBird » Thu Jun 24, 2010 10:43 am

I am making an accelerometer speedometer for my project. I get x,y,z values but dunno what to do next? Should i multiple each from time? How to get some time period? for instance, 1 sec passes and each axis value should be multiplied by it? i mean v=at...
Could someone help me implement this?
BlackBird
Freshman
Freshman
 
Posts: 3
Joined: Tue Jun 22, 2010 6:30 pm

Re: Accessing the Accelerometer

Postby Mafiozo » Tue Jul 06, 2010 8:56 pm

Mafiozo wrote:I am also interested how to make current sensorsimulator work with new api 2.2. Did anyone try to do that or does anyone has some guides how to do that?


Since no one replied, I am trying to change SensorSimulator to work on 2.1 emulator. If someone is interested or needs the same thing I could use everyhelp i can get. :)
Mafiozo
Freshman
Freshman
 
Posts: 8
Joined: Mon May 24, 2010 8:29 pm

Re: Accessing the Accelerometer

Postby Treet11 » Tue Jul 13, 2010 3:20 pm

Seems like
Code: Select all
android.hardware.Sensor

is not supported by android?
Wifi widget Beta

Download with Appoke
Download .apk
Also available in the Android market
Treet11
Freshman
Freshman
 
Posts: 8
Joined: Tue Apr 06, 2010 8:38 pm

Re: Accessing the Accelerometer

Postby edreamz » Wed Sep 15, 2010 11:26 pm

Treet11 wrote:Seems like
Code: Select all
android.hardware.Sensor

is not supported by android?


It is supported, but the functions which were previously available in the Sensor class is now moved/renamed/re-organized to the SensorManager class.
User avatar
edreamz
Junior Developer
Junior Developer
 
Posts: 18
Joined: Fri Aug 06, 2010 2:29 am
Location: U.S

Top
PreviousNext

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 6 guests