Thought I'd give this a shot myself, since the idea is so AWESOME. I got something that doesn't crash, but nor does it give me the info I want ;) I only know a smattering of Java, just enough to hurt myself. Based on the Compass.java example, I also updated deprecated SensorListener stuff to SensorEventListener, with other requirements. But honestly I really don't know what much about what I'm doing in the Java.
When I run the below code which
should detect the accelerometer (Samsung Captivate, 2.1) it prints this, runs, but doesn't print anything else (which, I was hoping it would). And based on my print statements inside various bits of code, it looks like its never detecting a sensor change via the SensorEventListener.
Any thoughts?
- RESUMED!
- Number of sensors found: 1
- Sensor Name:SMB380
- Sensor Max Range:0.0
- Sensor Type:1
- Starting to print values:
And here is the code. Any ideas how to make the magic happen?
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Looper;
- import android.content.Context;
- import android.hardware.SensorManager;
- import android.hardware.SensorEvent;
- import android.hardware.Sensor;
- import android.hardware.SensorEventListener;
- import java.util.List;
- SensorManager mSensorManager;
- MySensorEventListener mSensorEventListener;
- Handler mHandle;
- Sensor mSensor;
- float[] mValues;
- List<Sensor> sensors;
- void setup() {
- orientation(PORTRAIT);
- Looper.prepare(); // need to do this to make our Handler happy.
- mHandle = new Handler(); // need this to make SensorManager.registerListener() happy
- mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
- mSensorEventListener = new MySensorEventListener();
-
- sensors = new ArrayList<Sensor>();
- sensors = mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
- mSensor = (Sensor) sensors.get(0);
-
- println("Number of sensors found: " + sensors.size());
- println("Sensor Name:" + mSensor.getName());
- println("Sensor Max Range:" + mSensor.getMaximumRange());
- println("Sensor Type:" + mSensor.getType());
- println("Starting to print values:");
- }
- void draw() {
- if (mValues != null) {
- println(mValues);
- }
- }
- void onCreate(Bundle b) {
- super.onCreate(b);
- mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
- println( "CREATED!");
- }
- void onResume() {
- super.onResume();
- mSensorManager.registerListener(mSensorEventListener, mSensor, SensorManager.SENSOR_DELAY_GAME, mHandle);
- println("RESUMED!");
- }
- void onStop() {
- mSensorManager.unregisterListener(mSensorEventListener);
- super.onStop();
- println("STOPPED!");
- }
- class MySensorEventListener implements SensorEventListener {
- void onSensorChanged(SensorEvent event) {
- mValues = event.values;
- println("SENSOR CHANGED!");
- }
- void onAccuracyChanged(Sensor sensor, int accuracy) {
- println("ACCURACY CHANGED!");
- }
- }