[Resolved] Accelerometer woes

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

[Resolved] Accelerometer woes

Postby phix » Wed May 26, 2010 9:23 am

What am I missing? Nothing's being logged, nor the values updated. I'm following some tut's to the "T" just to get used to dealing with the sensors in android, but I can't get much simpler than this.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package geerdesigns.android.AndroidTests.Tests;
  2.  
  3. import geerdesigns.android.AndroidTests.R;
  4.  
  5. import java.util.Timer;
  6. import java.util.TimerTask;
  7.  
  8. import android.app.Activity;
  9. import android.hardware.Sensor;
  10. import android.hardware.SensorEvent;
  11. import android.hardware.SensorEventListener;
  12. import android.hardware.SensorManager;
  13. import android.os.Bundle;
  14. import android.util.Log;
  15. import android.widget.TextView;
  16.  
  17. public class ShakeActivity extends Activity {
  18.         private static final String TAG = "Shake Activity";
  19.         private TextView tx, ty, tz, info;
  20.         private String [] prefix = {"x: ", "y: ", "z: "};
  21.         private float [] sResult = new float[3];
  22.        
  23.         private SensorManager sMgr;
  24.         private SensorEventListener sListener;
  25.        
  26.         @Override
  27.         public void onCreate(Bundle savedInstanceState){
  28.                 super.onCreate(savedInstanceState);
  29.                 setContentView(R.layout.shakelayout);
  30.                
  31.                 sMgr = (SensorManager)getSystemService(SENSOR_SERVICE);
  32.                 sMgr.registerListener(sListener, sMgr.getSensorList(Sensor.TYPE_ORIENTATION).get(0), SensorManager.SENSOR_DELAY_NORMAL);
  33.                 Log.i(TAG, "Manager registered.");
  34.                
  35.                 tx = (TextView)findViewById(R.id.sensorX);
  36.                 ty = (TextView)findViewById(R.id.sensorY);
  37.                 tz = (TextView)findViewById(R.id.sensorZ);
  38.                 info = (TextView)findViewById(R.id.sensorInfo);
  39.                
  40.                 tx.setText(prefix[0] + "0.00");
  41.                 ty.setText(prefix[1] + "0.00");
  42.                 tz.setText(prefix[2] + "0.00");
  43.                
  44.                 sListener = new SensorEventListener(){
  45.                         @Override
  46.                         public void onAccuracyChanged(Sensor sensor, int accuracy) {}
  47.                         @Override
  48.                         public void onSensorChanged(SensorEvent event) {
  49.                                 Log.d(TAG, "Event Changed.");
  50.                                 if(event.sensor.getType() == Sensor.TYPE_ORIENTATION){
  51.                                         sResult[0] = event.values[0];
  52.                                         sResult[1] = event.values[1];
  53.                                         sResult[2] = event.values[2];
  54.                                        
  55.                                         tx.setText(Float.toString(sResult[0]));
  56.                                         ty.setText(Float.toString(sResult[1]));
  57.                                         tz.setText(Float.toString(sResult[2]));
  58.                                 }
  59.                         }
  60.                 };
  61.         }
  62. }
  63.  
  64.  
Parsed in 0.065 seconds, using GeSHi 1.0.8.4
Last edited by phix on Sun May 30, 2010 8:13 pm, edited 1 time in total.
Posting questions without code is like going to the doctor and saying "it hurts, fix it."
phix
Master Developer
Master Developer
 
Posts: 230
Joined: Mon Mar 15, 2010 10:37 am

Top

Re: Accelerometer woes

Postby nicholas.hauschild » Fri May 28, 2010 11:21 pm

The code right there looks pretty solid. Tell me, are you testing this in your emulator or on an actual device. If you are on an actual device, which manufacturer/model is it?

Good Luck!
-Nick
nicholas.hauschild
Master Developer
Master Developer
 
Posts: 310
Joined: Fri Dec 04, 2009 4:50 am

Re: Accelerometer woes

Postby phix » Fri May 28, 2010 11:48 pm

Thanks for getting back to me Nicholas. I'm testing on the Motorola Backflip. I thought maybe it's not getting registered or something. I tried a few different Sensor types (the orientation one here was the 2nd or 3rd I attempted) and nada. Tried logging in case I wasn't updating my UI correctly, but no. Nothing seems to be firing.
Posting questions without code is like going to the doctor and saying "it hurts, fix it."
phix
Master Developer
Master Developer
 
Posts: 230
Joined: Mon Mar 15, 2010 10:37 am

Re: Accelerometer woes

Postby nicholas.hauschild » Sat May 29, 2010 1:57 am

Are you seeing any exceptions being logged to LogCat when you use the emulator?
nicholas.hauschild
Master Developer
Master Developer
 
Posts: 310
Joined: Fri Dec 04, 2009 4:50 am

Re: Accelerometer woes

Postby phix » Sat May 29, 2010 2:08 am

Negative. That's why I think I don't have something hoked up right in terms of the sensor listener but no way to tell as of now.
Posting questions without code is like going to the doctor and saying "it hurts, fix it."
phix
Master Developer
Master Developer
 
Posts: 230
Joined: Mon Mar 15, 2010 10:37 am

Re: Accelerometer woes

Postby phix » Sat May 29, 2010 9:25 am

I've made mistakes before at this time of the morning, but the registerListener() returns a boolean. An if() around that line means my assumption was correct:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. sMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
  2. if(sMgr.registerListener(sListener, sMgr.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0), SensorManager.SENSOR_DELAY_NORMAL)){
  3.         Log.d(TAG, "Manager registered.");
  4. }else{
  5.         Log.e(TAG, "Manager not registerd.");
  6. }
  7.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

Guess what? "Manager not registered."

I tried a few variations on getSystemService including the app context.

What the backflip is going on here?
Posting questions without code is like going to the doctor and saying "it hurts, fix it."
phix
Master Developer
Master Developer
 
Posts: 230
Joined: Mon Mar 15, 2010 10:37 am

Top

Re: Accelerometer woes

Postby nicholas.hauschild » Sat May 29, 2010 4:46 pm

I will take a look when I get back to my computer, but otherwise I would recommend checking the android developers documentation. Their javadoc stuff is there.

Good luck!
-Nick
nicholas.hauschild
Master Developer
Master Developer
 
Posts: 310
Joined: Fri Dec 04, 2009 4:50 am

Re: Accelerometer woes

Postby nicholas.hauschild » Sat May 29, 2010 5:57 pm

http://developer.android.com/reference/android/hardware/SensorManager.html#registerListener%28android.hardware.SensorEventListener,%20android.hardware.Sensor,%20int%29


Returns
* true if the sensor is supported and successfully enabled.

I do not quite understand what this means, but there it is...
nicholas.hauschild
Master Developer
Master Developer
 
Posts: 310
Joined: Fri Dec 04, 2009 4:50 am

Re: Accelerometer woes

Postby phix » Sun May 30, 2010 7:16 am

Hmm.. funny how with this stuff an answer leads to another question, rinse & repeat. I missed that part of "supported" so thanks for pointing that out. Another thing I thought about is the Backflip runs 1.5, but far as I can tell it shouldnt matter.

Cheers.. if I find an answer I'll post up. :)
Posting questions without code is like going to the doctor and saying "it hurts, fix it."
phix
Master Developer
Master Developer
 
Posts: 230
Joined: Mon Mar 15, 2010 10:37 am

Re: Accelerometer woes

Postby nicholas.hauschild » Sun May 30, 2010 4:08 pm

Are you sure the backflip isn't supported? That was the first thing I looked up for this question, and I thought it said it had an accelerometer...
nicholas.hauschild
Master Developer
Master Developer
 
Posts: 310
Joined: Fri Dec 04, 2009 4:50 am

Re: Accelerometer woes

Postby phix » Sun May 30, 2010 4:20 pm

Moto.com says it has the accelerometer, I don't see why it wouldn't be supported. I can see the getSensorList().get(0".getName(), some serial number, but... I'm just at a loss. I've tried a few different variations since but still zero. All are pretty similar to this code too.

Ill see if I can try it on a friends droid and if that doesn't work its def something I'm missing. I would hope at least.
Posting questions without code is like going to the doctor and saying "it hurts, fix it."
phix
Master Developer
Master Developer
 
Posts: 230
Joined: Mon Mar 15, 2010 10:37 am

Re: Accelerometer woes

Postby phix » Sun May 30, 2010 8:12 pm

So bizzare.. it looks like it really doesn't like it when the Activity implements the SensorEventListener.

I don't know what I did, but it works now.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package geerdesigns.android.AndroidTests.Tests;
  2.  
  3. import geerdesigns.android.AndroidTests.R;
  4.  
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8. import android.app.Activity;
  9. import android.content.Context;
  10. import android.hardware.Sensor;
  11. import android.hardware.SensorEvent;
  12. import android.hardware.SensorEventListener;
  13. import android.hardware.SensorManager;
  14. import android.os.Bundle;
  15. import android.util.Log;
  16. import android.widget.ArrayAdapter;
  17. import android.widget.ListView;
  18. import android.widget.TextView;
  19. import android.widget.Toast;
  20.  
  21. public class ShakeActivity extends Activity {
  22.         private static final String TAG = "Shake Activity";
  23.  
  24.         private SensorManager sMgr;
  25.         private SensorEventListener sListener;
  26.  
  27.         ArrayAdapter<String> adapter;
  28.         ArrayList<String> sensorList;
  29.         @Override
  30.         public void onCreate(Bundle savedInstanceState) {
  31.                 super.onCreate(savedInstanceState);
  32.                 setContentView(R.layout.shakelayout);
  33.                
  34.                 ListView sList = (ListView)findViewById(R.id.sensor_list);
  35.                 TextView info = (TextView)findViewById(R.id.sensor_info);
  36.                 final TextView x = (TextView)findViewById(R.id.sensor_resultX);
  37.                 final TextView y = (TextView)findViewById(R.id.sensor_resultY);
  38.                 final TextView z = (TextView)findViewById(R.id.sensor_resultZ);
  39.                
  40.                 sMgr = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
  41.                 sensorList = new ArrayList<String>();
  42.                
  43.                 List<Sensor> allSensors = sMgr.getSensorList(Sensor.TYPE_ACCELEROMETER);
  44.                 sListener = new SensorEventListener(){
  45.                         @Override
  46.                         public void onAccuracyChanged(Sensor sensor, int accuracy) {
  47.                                
  48.                         }
  49.                         @Override
  50.                         public void onSensorChanged(SensorEvent event) {
  51.                                 //Lazy string coercion
  52.                                 x.setText("" + event.values[0]);
  53.                                 y.setText("" + event.values[1]);
  54.                                 z.setText("" + event.values[2]);
  55.                         }
  56.                 };
  57.                 // Loop through all sensors, try registering, put into the adapter array list
  58.                 for(Sensor s : allSensors){
  59.                         String reg =  "False";
  60.                         if(sMgr.registerListener(sListener, s, SensorManager.SENSOR_DELAY_NORMAL)){
  61.                                 reg = "True";
  62.                         }else{
  63.                                 reg = "False";
  64.                         }
  65.                         sensorList.add(s.getName() +  " (" + reg + ")");
  66.                 }
  67.                
  68.                 info.setText("Number of Sensors: " + allSensors.size());
  69.                 adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, sensorList);
  70.                 sList.setAdapter(adapter);
  71.         }
  72. }
  73.  
Parsed in 0.045 seconds, using GeSHi 1.0.8.4
Posting questions without code is like going to the doctor and saying "it hurts, fix it."
phix
Master Developer
Master Developer
 
Posts: 230
Joined: Mon Mar 15, 2010 10:37 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot], Yahoo [Bot] and 14 guests