Accessing the Accelerometer

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

Postby plusminus » Thu Feb 28, 2008 7:03 pm

I'm using windows and I seem to need that HDAPS thing. (The path written next to the checkbox shows a linux path).

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby Peli » Thu Feb 28, 2008 8:00 pm

Oh, that is a pity of course... :cry:

Too bad I have not found any useful solution for Windows yet how to access the accelerometer through Java...

(if you find one, please let me know so that I could try that out...)

Peli
Peli
Junior Developer
Junior Developer
 
Posts: 17
Joined: Thu Dec 27, 2007 10:54 am

error running this example

Postby neel » Sun Mar 09, 2008 5:11 am

this is the code tht i am trying to run as given in the sample by plusminus

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.neel;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.os.Bundle;
  8.  
  9.  
  10.  
  11. import android.hardware.Sensors;
  12.  
  13.  
  14.  
  15. public class Accelerometer extends Activity {
  16.  
  17.  
  18.  
  19.     /** True when the Accelerometer-functionality is basically available. */
  20.  
  21.     boolean accelerometerAvailable = false;
  22.  
  23.     boolean isEnabled = false;
  24.  
  25.     private Accelerometer myAcReader = new Accelerometer(true);
  26.  
  27.  
  28.  
  29.     /**
  30.  
  31.      * Sets up an AccelerometerReader. Checks if Accelerometer is available on
  32.  
  33.      * this device and throws UnsupportedOperationException if not .
  34.  
  35.      *
  36.  
  37.      * @param doEnable :
  38.  
  39.      *            enables the devices Accelerometer
  40.  
  41.      *            initially (if sensor available)
  42.  
  43.      * @throws UnsupportedOperationException
  44.  
  45.      *             if Accelerometer is not available on this device.
  46.  
  47.      */
  48.  
  49.     public Accelerometer(boolean doEnable)
  50.  
  51.               throws UnsupportedOperationException {
  52.  
  53.  
  54.  
  55.          /* Check once here in the constructor if an
  56.  
  57.           * Accelerometer is available on this device. */
  58.  
  59.          for (String aSensor : Sensors.getSupportedSensors())
  60.  
  61.               if (aSensor.equals(Sensors.SENSOR_ACCELEROMETER))
  62.  
  63.                    accelerometerAvailable = true;
  64.  
  65.  
  66.  
  67.          if (!accelerometerAvailable)
  68.  
  69.               throw new UnsupportedOperationException(
  70.  
  71.                         "Accelerometer is not available.");
  72.  
  73.  
  74.  
  75.          if (doEnable)
  76.  
  77.               setEnableAccelerometer(true);
  78.  
  79.     }
  80.  
  81.    
  82.  
  83.    
  84.  
  85.  
  86.  
  87.     /**
  88.  
  89.      * En/Dis-able the Accelerometer.
  90.  
  91.      *
  92.  
  93.      * @param doEnable
  94.  
  95.      *            <code>true</code> for enable.<br>
  96.  
  97.      *            <code>false</code> for disable.
  98.  
  99.      * @throws UnsupportedOperationException
  100.  
  101.      */
  102.  
  103.     public void setEnableAccelerometer(boolean doEnable)
  104.  
  105.               throws UnsupportedOperationException {
  106.  
  107.          if (!accelerometerAvailable)
  108.  
  109.               throw new UnsupportedOperationException(
  110.  
  111.                         "Accelerometer is not available.");
  112.  
  113.  
  114.  
  115.          /* If should be enabled and not already is: */
  116.  
  117.          if (doEnable && !this.isEnabled) {
  118.  
  119.               Sensors.enableSensor(Sensors.SENSOR_ACCELEROMETER);
  120.  
  121.               this.isEnabled = true;
  122.  
  123.          } else /* If should be disabled and not already is: */
  124.  
  125.          if (!doEnable && this.isEnabled) {
  126.  
  127.               Sensors.disableSensor(Sensors.SENSOR_ACCELEROMETER);
  128.  
  129.               this.isEnabled = false;
  130.  
  131.          }
  132.  
  133.     }
  134.  
  135.  
  136.  
  137.     /**
  138.  
  139.      * Read out the values currently provided by the Accelerometer.
  140.  
  141.      *
  142.  
  143.      * @return the current Accelerometer-values.
  144.  
  145.      * @throws UnsupportedOperationException
  146.  
  147.      *             if Accelerometer is not available on this device.
  148.  
  149.      * @throws IllegalStateException
  150.  
  151.      *             if Accelerometer was set to disabled.
  152.  
  153.      */
  154.  
  155.     public float[] readAccelerometer() throws UnsupportedOperationException, IllegalStateException {
  156.  
  157.          if (!accelerometerAvailable)
  158.  
  159.               throw new UnsupportedOperationException(
  160.  
  161.                         "Accelerometer is not available.");
  162.  
  163.  
  164.  
  165.          if (!this.isEnabled)
  166.  
  167.               throw new IllegalStateException(
  168.  
  169.                         "Accelerometer was set to disabled!");
  170.  
  171.          /* Get number of sensor-values the sensor will return. Could be
  172.  
  173.           * variable, depending of the amount of axis (1D, 2D or 3D
  174.  
  175.           * accelerometer). */
  176.  
  177.          int sensorValues = Sensors
  178.  
  179.                    .getNumSensorValues(Sensors.SENSOR_ACCELEROMETER);
  180.  
  181.          float[] out = new float[sensorValues];
  182.  
  183.  
  184.  
  185.          /* Make the OS fill the array we passed. */
  186.  
  187.          Sensors.readSensor(Sensors.SENSOR_ACCELEROMETER, out);
  188.  
  189.  
  190.  
  191.          /* And return it. */
  192.  
  193.          return out;
  194.  
  195.     }
  196.  
  197.  
  198.  
  199.         @Override
  200.  
  201.         public void onCreate(Bundle icicle) {
  202.  
  203.              super.onCreate(icicle);
  204.  
  205.              this.setContentView(R.layout.main);
  206.  
  207.              // Perhaps make a button-OnClick show the Accelerometer-Values in a TextView...
  208.  
  209.         }
  210.  
  211.  
Parsed in 0.041 seconds, using GeSHi 1.0.8.4


and i get the following error:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Application error: com.neel
  2.  
  3.  
  4.  
  5. An error has occured in process com.neel. Unable to
  6.  
  7. instantiate activity
  8.  
  9. ComponentInfo{com.neel/com.neel.Accelerometer}:
  10.  
  11. java.lang.InstantiationException:
  12.  
  13. com.neel.Accelerometer.
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


please also show how to make a text box and display the values of accelerometer reading in it on button click

thanks in advance
neel
Developer
Developer
 
Posts: 31
Joined: Fri Feb 15, 2008 5:09 am
Location: San Jose

Postby plusminus » Sun Mar 09, 2008 11:48 am

Hello neel,

why didn't you leave the class as I posted it :?:
You know that you can have ordinary classes that do not have to extend Activity :?:

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby neel » Sun Mar 09, 2008 1:09 pm

hi plusminus

this is yout original code

Code: Select all
package com.neel;

import android.app.Activity;
import android.os.Bundle;

import android.hardware.Sensors;

public class Accelerometer {

     /** 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 Accelerometer(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;
     }
}


i did not intend to change your code. i tried running your code exactly as it was given but got the following error

Code: Select all
Application Error: com.neel
An error has occured in com.neel. Unable to
instantiate activity
ComponentInfo{com.neel/com.neel.Accelerometer};
java.lang.InstantiationException:
com.neel.Accelerometer


and also saw tht some other guy got the same error and you replied to him the following

Code: Select all
public class TestLayout extends Activity {
     
     private AccelerometerReader myAcReader = new AccelerometerReader(true);

     @Override
     public void onCreate(Bundle icicle) {
          super.onCreate(icicle);
          this.setContentView(R.layout.main);
          // Perhaps make a button-OnClick show the Accelerometer-Values in a TextView...
     }
}


i did not understand this and where it has to be put in the code. and was jsut trying to get it to work.

please do not get me wrong brother. :)
neel
Developer
Developer
 
Posts: 31
Joined: Fri Feb 15, 2008 5:09 am
Location: San Jose

Postby plusminus » Sun Mar 09, 2008 1:23 pm

Yes, I told that once.

But you mixed both.

Put my code to a separate file and just use it in a normal Activity, like I said once:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class TestLayout extends Activity {
  2.  
  3.      
  4.  
  5.      private AccelerometerReader myAcReader = new AccelerometerReader(true);
  6.  
  7.  
  8.  
  9.      @Override
  10.  
  11.      public void onCreate(Bundle icicle) {
  12.  
  13.           super.onCreate(icicle);
  14.  
  15.           this.setContentView(R.layout.main);
  16.  
  17.           // Perhaps make a button-OnClick show the Accelerometer-Values in a TextView...
  18.  
  19.      }
  20.  
  21. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby neel » Mon Mar 10, 2008 12:22 am

ok thanks i got it


but i want to display the values [0,0,0] returned by the acceleorometer on screen......pls show how to do this?


thanks for all ur help
neel
Developer
Developer
 
Posts: 31
Joined: Fri Feb 15, 2008 5:09 am
Location: San Jose

Postby plusminus » Mon Mar 10, 2008 12:31 am

So you successfully received the values :?:

:arrow: So search for "findViewById" on this forum and/or have a look at the Novice Tutorials.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby neel » Mon Mar 10, 2008 3:48 am

well when i run the program i dont see anything on the screen. is that what is supposed to happen. I knw taht right now it treats the emulator as in a free fall and so [0,0,0] is the answer.

Code: Select all
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;


as far as i understand after these lines of code "out" will have the accelerometer values. please correct me if i am wrong

thanks
neel
Developer
Developer
 
Posts: 31
Joined: Fri Feb 15, 2008 5:09 am
Location: San Jose

Postby plusminus » Mon Mar 10, 2008 2:57 pm

Hello neel,

I created a little sample-app for you:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.airwriting.android.tests;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.widget.TextView;
  6.  
  7. public class Test extends Activity {
  8.        
  9.         private AccelerometerReader acr;
  10.  
  11.         protected void onCreate(Bundle icicle) {
  12.                 super.onCreate(icicle);
  13.                 setContentView(R.layout.main);
  14.                        
  15.                 this.acr = new AccelerometerReader(true);
  16.                 float[] vals = this.acr.readAccelerometer();
  17.                 ((TextView)findViewById(R.id.mytextview)).setText(
  18.                                 "[x,y,z] = ["
  19.                                 + vals[0] + ","
  20.                                 + vals[1] + ","
  21.                                 + vals[2] + "]");
  22.         }
  23. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4

As promised it shows [font=Lucida Console][x,y,z] = [0,0,0][/font] .

[Edit]Ok, it actually is [font=Lucida Console][x,y,z] = [0.0,0.0,0.0][/font] ;)[/Edit]

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby neel » Wed Mar 12, 2008 6:12 am

thanks plusminus.....it helped :)
neel
Developer
Developer
 
Posts: 31
Joined: Fri Feb 15, 2008 5:09 am
Location: San Jose

not able to install openintent.apk on emulator

Postby dexter » Thu Sep 25, 2008 8:47 am

Hi +/-,

I am trying to install openintent.apk on the emulator. From the commandprompt when i say adb install openintent.apk, it shows that it has install the apk but on emulator screen it does not shows up. Am also not able to locate the application folder where it is supposedly to be installed in the emulator.

so can u help me out
dexter
Freshman
Freshman
 
Posts: 7
Joined: Mon Jun 16, 2008 7:18 am

Postby Bretters » Wed Nov 05, 2008 9:11 pm

have you messed with this by putting your application on the Google Phone? I would like to set the google phone to read the accelerometer data and to change configuration based on the No Hand's application my application development class is working on- to where the phone would switch to blue tooth if say the accelerometer tells notifies the system that the phone is going 15+ mph. willing to work in small steps like just being able to read the accelerometer data and put into txt. we do have a google phone to test with and the emulator on the computer.
Bretters
Freshman
Freshman
 
Posts: 2
Joined: Wed Nov 05, 2008 9:04 pm
Location: Redding

Postby jasont » Thu Nov 06, 2008 1:34 am

Bretters wrote:have you messed with this by putting your application on the Google Phone? I would like to set the google phone to read the accelerometer data and to change configuration based on the No Hand's application my application development class is working on- to where the phone would switch to blue tooth if say the accelerometer tells notifies the system that the phone is going 15+ mph. willing to work in small steps like just being able to read the accelerometer data and put into txt. we do have a google phone to test with and the emulator on the computer.


There is an accelerometer game available in the apps-for-android project.

http://code.google.com/p/apps-for-andro ... unk/Amazed
jasont
Developer
Developer
 
Posts: 28
Joined: Mon Sep 22, 2008 6:39 pm
Location: California

Postby Bretters » Thu Nov 06, 2008 2:00 am

dont really care about a game but maybe will see how the game gets the accelerometer inputs...
Bretters
Freshman
Freshman
 
Posts: 2
Joined: Wed Nov 05, 2008 9:04 pm
Location: Redding

Top
PreviousNext

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests