Sensors emulator + Accelerometer tester, please help!

Common bugs/problems with the Android SDK the Emulator and the ADT-Plugin.

Sensors emulator + Accelerometer tester, please help!

Postby monarcharthas89 » Thu Jun 17, 2010 10:58 pm

Hi,
I downloaded and managed to make the sensor simulator works on the Android AVD simulator. I also used a simple accelerometer test code from the internet:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.example.AccelOrientExample;
  2.  
  3. import org.example.AccelOrientExample.R;
  4. import android.app.Activity;
  5. import android.hardware.Sensor;
  6. import android.hardware.SensorEvent;
  7. import android.hardware.SensorEventListener;
  8. import android.hardware.SensorManager;
  9. import android.os.Bundle;
  10. import android.widget.TextView;
  11.  
  12.  
  13. public class AccelOrientExample extends Activity implements SensorEventListener {
  14.  
  15.  // Accelerometer X, Y, and Z values
  16.  private TextView accelXValue;
  17.  private TextView accelYValue;
  18.  private TextView accelZValue;
  19.  
  20.  // Orientation X, Y, and Z values
  21.  private TextView orientXValue;
  22.  private TextView orientYValue;
  23.  private TextView orientZValue;
  24.  
  25.  private SensorManager sensorManager = null;
  26.  
  27.     /** Called when the activity is first created. */
  28.     @Override
  29.     public void onCreate(Bundle savedInstanceState) {
  30.         super.onCreate(savedInstanceState);
  31.         // Get a reference to a SensorManager
  32.         sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
  33.         setContentView(R.layout.main);
  34.        
  35.         // Capture accelerometer related view elements
  36.         accelXValue = (TextView) findViewById(R.id.accel_x_value);
  37.         accelYValue = (TextView) findViewById(R.id.accel_y_value);
  38.         accelZValue = (TextView) findViewById(R.id.accel_z_value);
  39.        
  40.         // Capture orientation related view elements
  41.         orientXValue = (TextView) findViewById(R.id.orient_x_value);
  42.         orientYValue = (TextView) findViewById(R.id.orient_y_value);
  43.         orientZValue = (TextView) findViewById(R.id.orient_z_value);
  44.        
  45.         // Initialize accelerometer related view elements
  46.           accelXValue.setText("0.00");
  47.           accelYValue.setText("0.00");
  48.           accelZValue.setText("0.00");
  49.        
  50.         // Initialize orientation related view elements
  51.           orientXValue.setText("0.00");
  52.           orientYValue.setText("0.00");
  53.           orientZValue.setText("0.00");      
  54.     }
  55.    
  56.     // This method will update the UI on new sensor events
  57.     public void onSensorChanged(SensorEvent sensorEvent) {
  58.      synchronized (this) {
  59.       if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
  60.        accelXValue.setText(Float.toString(sensorEvent.values[0]));
  61.        accelYValue.setText(Float.toString(sensorEvent.values[1]));
  62.        accelZValue.setText(Float.toString(sensorEvent.values[2]));      
  63.       }
  64.      
  65.       if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {
  66.        orientXValue.setText(Float.toString(sensorEvent.values[0]));
  67.        orientYValue.setText(Float.toString(sensorEvent.values[1]));
  68.        orientZValue.setText(Float.toString(sensorEvent.values[2]));      
  69.       }
  70.      }
  71.     }
  72.    
  73.     // I've chosen to not implement this method
  74.     public void onAccuracyChanged(Sensor arg0, int arg1) {
  75.   // TODO Auto-generated method stub
  76.  
  77.  }
  78.    
  79.     @Override
  80.     protected void onResume() {
  81.      super.onResume();
  82.      // Register this class as a listener for the accelerometer sensor
  83.      sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
  84.      // ...and the orientation sensor
  85.      sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL);
  86.     }
  87.    
  88.     @Override
  89.     protected void onStop() {
  90.      // Unregister the listener
  91.      sensorManager.unregisterListener(this);
  92.      super.onStop();
  93.     }
  94.  
  95. }
Parsed in 0.044 seconds, using GeSHi 1.0.8.4

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:layout_width="fill_parent"
  4.    android:layout_height="fill_parent"
  5.    android:stretchColumns="1">
  6.  
  7.     <TableRow>
  8.         <TextView
  9.            android:id="@+id/accelerometer_label"
  10.            android:layout_column="1"
  11.            android:text="Accelerometer"
  12.            android:textSize="9pt"
  13.            android:padding="3dip" />
  14.     </TableRow>
  15.    
  16.     <TableRow>
  17.         <TextView
  18.            android:id="@+id/accel_x_label"
  19.            android:layout_column="1"
  20.            android:text="X:"
  21.            android:textSize="8pt"
  22.            android:padding="3dip" />
  23.         <TextView
  24.            android:id="@+id/accel_x_value"
  25.            android:gravity="right"
  26.            android:textSize="8pt"
  27.            android:padding="3dip" />
  28.     </TableRow>
  29.  
  30.     <TableRow>
  31.         <TextView
  32.            android:id="@+id/accel_y_label"
  33.            android:layout_column="1"
  34.            android:text="Y:"
  35.            android:textSize="8pt"
  36.            android:padding="3dip" />
  37.         <TextView
  38.            android:id="@+id/accel_y_value"
  39.            android:gravity="right"
  40.            android:textSize="8pt"
  41.            android:padding="3dip" />
  42.     </TableRow>
  43.  
  44.     <TableRow>
  45.         <TextView
  46.            android:id="@+id/accel_z_label"
  47.            android:layout_column="1"
  48.            android:text="Z:"
  49.            android:textSize="8pt"
  50.            android:padding="3dip" />
  51.         <TextView
  52.            android:id="@+id/accel_z_value"
  53.            android:gravity="right"
  54.            android:textSize="8pt"
  55.            android:padding="3dip" />
  56.     </TableRow>
  57.  
  58.     <View
  59.        android:layout_height="2dip"
  60.        android:background="#FF909090" />
  61.        
  62.  <TableRow>
  63.         <TextView
  64.            android:id="@+id/orientation_label"
  65.            android:layout_column="1"
  66.            android:text="Orientation"
  67.            android:textSize="9pt"
  68.            android:padding="3dip" />
  69.     </TableRow>
  70.    
  71.     <TableRow>
  72.         <TextView
  73.            android:id="@+id/orient_x_label"
  74.            android:layout_column="1"
  75.            android:text="X:"
  76.            android:textSize="8pt"
  77.            android:padding="3dip" />
  78.         <TextView
  79.            android:id="@+id/orient_x_value"
  80.            android:gravity="right"
  81.            android:textSize="8pt"
  82.            android:padding="3dip" />
  83.     </TableRow>
  84.  
  85.     <TableRow>
  86.         <TextView
  87.            android:id="@+id/orient_y_label"
  88.            android:layout_column="1"
  89.            android:text="Y:"
  90.            android:textSize="8pt"
  91.            android:padding="3dip" />
  92.         <TextView
  93.            android:id="@+id/orient_y_value"
  94.            android:gravity="right"
  95.            android:textSize="8pt"
  96.            android:padding="3dip" />
  97.     </TableRow>
  98.  
  99.     <TableRow>
  100.         <TextView
  101.            android:id="@+id/orient_z_label"
  102.            android:layout_column="1"
  103.            android:text="Z:"
  104.            android:textSize="8pt"
  105.            android:padding="3dip" />
  106.         <TextView
  107.            android:id="@+id/orient_z_value"
  108.            android:gravity="right"
  109.            android:textSize="8pt"
  110.            android:padding="3dip" />
  111.     </TableRow>
  112.  
  113. </TableLayout>
  114.  
Parsed in 0.012 seconds, using GeSHi 1.0.8.4


I was thinking if I run the sensor simulator first, then run the AccelOrientExample, then move the Visual Phone, the data on the AccelOrientExample would also change, but it doesn't. Only the values on the sensor simulator changes obviously. Did I do something wrong or the AccelOrientExample just doesn't work. Sorry because I don't have any Android phone to test atm so is there anybody can help me to test the code above on a real device. I really appreciate it.
monarcharthas89
Freshman
Freshman
 
Posts: 5
Joined: Thu Jun 17, 2010 10:44 pm

Top

Re: Sensors emulator + Accelerometer tester, please help!

Postby vengeance_mj » Fri Jun 18, 2010 12:15 am

There is no accelerometer on the emulator so it is always going to return 0,0
vengeance_mj
Senior Developer
Senior Developer
 
Posts: 174
Joined: Thu Oct 29, 2009 2:32 am

Re: Sensors emulator + Accelerometer tester, please help!

Postby monarcharthas89 » Fri Jun 18, 2010 3:18 am

thats why I said I used a sensors emulator and expect that the sensors emulator can change the value of the accelerometer values
here is the Sensors Emulator I'm using: http://www.botskool.com/geeks/how-use-s ... k-emulator
monarcharthas89
Freshman
Freshman
 
Posts: 5
Joined: Thu Jun 17, 2010 10:44 pm

Top

Return to SDK/ADT/Emulator Problems

Who is online

Users browsing this forum: No registered users and 5 guests