Sensor

Put your problem here if it does not fit any of the other categories.

Sensor

Postby k2thedevil » Sun Feb 08, 2009 3:17 am

Hi, I was trying to work out on how to deal with the sensor on the G1. I have a G1 that I am using to test. This is the code


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.GoogleProject.AccReader;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.hardware.SensorListener;
  9.  
  10. import android.hardware.SensorManager;
  11.  
  12. import android.os.Bundle;
  13.  
  14. import android.widget.TextView;
  15.  
  16.  
  17.  
  18. public class AccReader extends Activity implements SensorListener{
  19.  
  20.        
  21.  
  22.         private float x;
  23.  
  24.         private float y;
  25.  
  26.         private float z;
  27.  
  28.         //private TextView tv = new TextView(this);    
  29.  
  30.         private SensorManager mSensMgr;
  31.  
  32.         //private SensorListener mSensLsnr;
  33.  
  34.         public TextView updateTV(TextView tv) {
  35.  
  36.                 tv.setText("X: " + x + " ,Y :" + y+ " ,Z: " + z);
  37.  
  38.                 return tv;             
  39.  
  40.         }
  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.         TextView tv = new TextView(this);
  53.  
  54.         mSensMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
  55.  
  56.         mSensMgr.registerListener(this, SensorManager.SENSOR_ACCELEROMETER, SensorManager.SENSOR_DELAY_FASTEST);
  57.  
  58.        
  59.  
  60.        
  61.  
  62.         setContentView(updateTV(tv));
  63.  
  64.     }
  65.  
  66.  
  67.  
  68.         public void onAccuracyChanged(int sensor, int accuracy) {
  69.  
  70.                 // TODO Auto-generated method stub
  71.  
  72.                
  73.  
  74.         }
  75.  
  76.  
  77.  
  78.         public void onSensorChanged(int sensor, float[] values) {
  79.  
  80.                 // TODO Auto-generated method stub
  81.  
  82.                 if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
  83.  
  84.                         x = values[0];
  85.  
  86.                         y = values[1];
  87.  
  88.                         z = values[2];
  89.  
  90.                 }else {
  91.  
  92.                         x = -1;
  93.  
  94.                         y = -1;
  95.  
  96.                         z = -1;
  97.  
  98.                 }
  99.  
  100.                 //this.setContentView(updateTV(tv));
  101.  
  102.         }
  103.  
  104. }
  105.  
  106.  
  107.  
  108.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Nothing happens when I run the app on G1.

All I get is
[font=Courier New]
X: 0.0 ,Y = 0.0 ,Z = 0.0
[/font]

And it remains that way no matter how hard I shake the phone.
Bug Out!
k2thedevil
Freshman
Freshman
 
Posts: 4
Joined: Fri Jan 30, 2009 9:46 pm

Top

Postby egyptian » Sat Feb 28, 2009 7:26 am

Here's my code for a sample...

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. @Override
  3.  
  4.     protected void onCreate(Bundle savedInstanceState)
  5.  
  6.     {
  7.  
  8.         // Be sure to call the super class.
  9.  
  10.         super.onCreate(savedInstanceState);
  11.  
  12.  
  13.  
  14.         mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
  15.  
  16.         mGraphView = new GraphView(this);
  17.  
  18.        
  19.  
  20.         //Set to full screen
  21.  
  22.         this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  23.  
  24.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  25.  
  26.        
  27.  
  28.         setContentView(mGraphView);
  29.  
  30.     }
  31.  
  32.  
  33.  
  34.    
  35.  
  36.     @Override
  37.  
  38.     protected void onResume()
  39.  
  40.     {
  41.  
  42.         super.onResume();
  43.  
  44.         mSensorManager.registerListener(        mGraphView,
  45.  
  46.                                                                 SensorManager.SENSOR_STATUS_ACCURACY_HIGH |
  47.  
  48.                                                                 SensorManager.SENSOR_ACCELEROMETER,
  49.  
  50.                                                                 SensorManager.SENSOR_DELAY_FASTEST);
  51.  
  52.     }
  53.  
  54.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


I've found that having only one SensorManager.xxxxxx in registerListener does not let it register correctly... strange :-/ that's why I put "SENSOR_STATUS_ACCURACY_HIGH"....

This is actually what i'm working on right now... I'm playing around with this line:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         mSensorManager.registerListener(        mGraphView,
  3.  
  4.                                                                 SensorManager.SENSOR_STATUS_ACCURACY_HIGH |
  5.  
  6.                                                                 SensorManager.SENSOR_ACCELEROMETER,
  7.  
  8.                                                                 SensorManager.SENSOR_DELAY_FASTEST);
  9.  
  10.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


it used to be...

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         mSensorManager.registerListener(        mGraphView,
  3.  
  4.                                                                 SensorManager.SENSOR_DELAY_GAME |
  5.  
  6.                                                                 SensorManager.SENSOR_ACCELEROMETER,
  7.  
  8.                                                                 SensorManager.SENSOR_DELAY_GAME);
  9.  
  10.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


but for some reason, it's not refreshing as much as I'd like... I don't like how DELAY_GAME is OK... but FASTEST is TOOOO fast... I wish we could set how fast it refreshes.

Also, I wish the sensor manager didn't slow down if the phone isn't moving steadily. I wrote "4EverFall"... I've found that when you move the phone left and right, the game's speed is much more steady than if you hold it still... is there any way to fix this that anyone know of???
Without God, I would not have ANY skills.
egyptian
Developer
Developer
 
Posts: 36
Joined: Fri Jan 16, 2009 5:45 am
Location: Lee's Summit, Missouri

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 19 guests