Reputation: 31
I have managed to create a basic app which displays the current readings for an Accelerometer sensor. Using similar code, ive done the same for the Gyroscope sensor however, I cannot seem to make them both run at the same time in one activity. I am quite new to programming so the problem is probably quite simple, here is my code,
import java.util.List;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.TextView;
public class Scanner extends FragmentActivity implements SensorEventListener {
Sensor accelerometer;
Sensor gyroscope;
SensorManager sm;
TextView acceleration;
TextView gyro;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanner);
sm=(SensorManager)getSystemService (SENSOR_SERVICE);
accelerometer=sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
acceleration=(TextView)findViewById(R.id.acceleration);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.scanner, menu);
return true;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
acceleration.setText(
"X: "+event.values[0]+
"\nY: "+event.values[1]+
"\nZ: "+event.values[2]);
}
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanner);
sm=(SensorManager)getSystemService (SENSOR_SERVICE);
gyroscope=sm.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
sm.registerListener(this, gyroscope, SensorManager.SENSOR_DELAY_NORMAL);
gyro=(TextView)findViewById(R.id.gyro);
}
public void onSensorChanged1(SensorEvent event) {
// TODO Auto-generated method stub
gyro.setText(
"X: "+event.values[0]+
"\nY: "+event.values[1]+
"\nZ: "+event.values[2]);
}
}
Upvotes: 0
Views: 1677
Reputation: 171
Try this:
public class MainActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mAcc;
private Sensor mGyro;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get an instance of the sensor service, and use that to get an
// instance of
// a particular sensor.
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAcc = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mGyro = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
}
@Override
public void onAccuracyChanged(Sensor sensor, int arg1) {
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
Log.e("ACCELEROMER", "ACC: x=" + event.values[0] + " y="
+ event.values[1] + " z=" + event.values[2]);
} else if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
Log.e("GYROSCOPE", "GY: rotation around x axis=" + event.values[0]
+ " around y axis=" + event.values[1] + " around z axis="
+ event.values[2]);
}
}
@Override
protected void onResume() {
// Register a listener for the sensor.
super.onResume();
mSensorManager.registerListener(this, mAcc,
SensorManager.SENSOR_DELAY_NORMAL);
mSensorManager.registerListener(this, mGyro,
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
// Be sure to unregister the sensor when the activity pauses.
super.onPause();
mSensorManager.unregisterListener(this);
}
}
Upvotes: 0
Reputation: 48871
You have two methods called public void onSensorChanged(SensorEvent event)
- one is the @Override of the android.hardware.SensorEventListener
interface and the other is a local method of your Activity
...
You can't do that and expect it to work.
You need to get rid of the local method and put the code for both sensors into the overridden method and then use the event parameter to identify which sensor has changed.
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
// Execute accelerometer code
}
if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
// Execute gyroscope code
}
}
Upvotes: 1