문제

I am trying to build an application that would allow me to retrieve data from multiple sensors simultaneously. I still have not figured out how I will get there, so I started by implementing a SensorObject class for a structured layout.

This sensorobject implements the SensorEventListener together with the usual onaccuracychanged and onsensorchanged methods.I have left both methods empty for now since this class is inherited. I also implemented OnResume method like so:

    public void OnResume() {
    sensorManager.registerListener(this, sensor,
            SensorManager.SENSOR_DELAY_NORMAL);
}

Successively I implemented an accelerometer object that extends the sensor object. Substantially, it does stuff in the onSensor Changed.

My main questions lie here 1) When in the activity, I am never getting a result from the accelerometer object, since it only enters the 'onSensorChanged' method until AFTER the below code has been executed:

    AccelerometerObject ao =  new AccelerometerObject(Sensor.TYPE_ACCELEROMETER, this);
    ao.OnResume();

    txtAccelerometerData = (TextView) findViewById(R.id.txtAccelerometerData);
    txtAccelerometerData.setText(ao.toString());

Why isn't it accessing the sensor data immediately but proceeding sequentially? The object still has no values since the onresume method hasnt been accessed. Will i need to use seperate threads? Efficiency is not that important, all I need is valid data for experimentation.

2) is it possible to implement multiple simultaneous sensors using the above approach?

Any help would be greatly appreciated.

Below is the AccelerometerObject as requested.

public class AccelerometerObject extends SensorObject {

public float[] values;

public AccelerometerObject(int sensorType, Context _Context) {
    super(sensorType, _Context);
}   

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }

@Override
public void onSensorChanged(SensorEvent event) {
    AccelerometerValues = event.values;
    onPause();
}


@Override
public String toString() {
    if (values != null) {
        return "X is " + values[0]
                + ", Y is " + values[1]
                + " and Z is "
                + values[2]);
    } else
        return "N/A";
}

}

도움이 되었습니까?

해결책

I have just uploaded an eclipse project here: http://cyrixmorten.net/CA-Project.zip

The project has been used to gather data for a course in Context Awareness.

It demonstrates getting sensor values from

  • Accelerometer
  • Gyroscope
  • Orientation
  • GPS

In the implementation I have implemented a Service for each sensor. This is perhaps unnecessarily complex but we needed the data gathering to continue in the background even when not in focus.

Edit:

By the way all the *ResultReceiver objects merely acts as glue between the Activity and the Service they represent. The *ResultReceiver object receive the sensor events and translate them into a custom interface which is then used by the MainActivity.

I could imaging that part could lead to confusion but I just do not like messing with a lot of different Bundles and Strings in my Activities when I can get exact values through an interface - merely a design choice for personal protection against typos I guess.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top