I'm trying to get both accelerometer and orientation data. Currently, I created an AccelerometerManager and OrientationManager. They both do the same thing; implement SensorEventListener and retrieve data from values[] in onSensorChanged() listeners.
Is there an easier way to do this? It seems like having 2 handlers with duplicate code is uneccessary. Is there a way to access a values[] array with the combined accelerometer and orientation data together?
Because of the 2 handlers, I'm also having to do this:
orientationManager = new OrientationManager(this);
orientationSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
orientationSensorManager.registerListener(orientationManager,
orientationSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_GAME);
accelerometerManager = new AccelerometerManager(this);
accelerometerSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
accelerometerSensorManager.registerListener(accelerometerManager,
orientationSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_GAME);
Can you try something like this:
private SensorManager mSensorManager;
private SensorEventListener mSensorListener;
////
mSensorManager = (SensorManager) this
.getSystemService(Context.SENSOR_SERVICE);
mSensorListener = new SensorEventListener() {
#Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
#Override
public void onSensorChanged(SensorEvent event) {
Sensor sensor = event.sensor;
if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
//TODO: get values
}else if (sensor.getType() == Sensor.TYPE_ORIENTATION) {
//TODO: get values
}
}
}
mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME);
Related
I am trying to display the data from two different sensors and if i comment out the accelerometer data the app will run and show the light sensor data but trying to display the accelerometer data being displayed causes it to crash on launch. if i comment out everything to do with the light sensor i do get the accelerometer data to be shown but they will not be able to be shown at the same time.
the issue being that both sensors use event.values yet at least from my own research this should work although else if gives error in on sensorChanged
which was how Get multiple sensor data at the same time in Android recommended to solve this issue
any help would be great
public class MainActivity extends AppCompatActivity implements SensorEventListener {
//System sensor manager
SensorManager sensorManager;
//active sensors
Sensor lightSensor;
Sensor acelSensor;
TextView xText, yText, zText, lightS;
double ax,ay,az;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initulise veriables
lightS = (TextView) findViewById(R.id.lightS);
xText = (TextView) findViewById(R.id.xText);
yText = (TextView) findViewById(R.id.yText);
zText = (TextView) findViewById(R.id.zText);
//instance of sensor manager
sensorManager = (SensorManager) getSystemService(Service.SENSOR_SERVICE);
//get sensors from sensor manager
lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
acelSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//if error
if (lightSensor == null) { lightS.setText("sensor_error"); }
if (acelSensor == null) {
xText.setText("sensor_error");
}}
#Override
public void onStart() {
super.onStart();
if (lightSensor !=null)
{
sensorManager.registerListener(this, lightSensor, SensorManager.SENSOR_DELAY_FASTEST);
}
if (acelSensor !=null)
{
sensorManager.registerListener(this, acelSensor, SensorManager.SENSOR_DELAY_FASTEST);
}
}
#Override
public void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
#Override
public void onResume() {
super.onResume();
sensorManager.registerListener(this, lightSensor, SensorManager.SENSOR_DELAY_FASTEST);
sensorManager.registerListener(this, acelSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
#Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_LIGHT) ;
{
float lux = event.values[0];
lightS.setText(String.valueOf(lux));
}
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) ;{
/*ax=event.values[0];
ay=event.values[1];
az=event.values[2];*/
xText.setText("X: " + event.values[0]);
yText.setText("Y: " + event.values[1]);
zText.setText("Z: " + event.values[2]);
}
}
You are registering your sensors twice, once at onstart(), and again at onresume().
I removed the extra onregistersensor().
public class MainActivity extends AppCompatActivity implements SensorEventListener {
//System sensor manager
SensorManager sensorManager;
//active sensors
Sensor lightSensor;
Sensor acelSensor;
TextView xText, yText, zText, lightS;
double x;
double y;
double z;
double ax, ay, az;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initulise veriables
// lightS = (TextView) findViewById(R.id.lightS);
// xText = (TextView) findViewById(R.id.xText);
// yText = (TextView) findViewById(R.id.yText);
// zText = (TextView) findViewById(R.id.zText);
//instance of sensor manager
sensorManager = (SensorManager) getSystemService(Service.SENSOR_SERVICE);
}
#Override
public void onStart() {
super.onStart();
}
#Override
public void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
#Override
public void onResume() {
super.onResume();
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_UI);
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT),
SensorManager.SENSOR_DELAY_UI);
}
#Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_LIGHT) ;
{
float lux = event.values[0];
System.out.println("lux " + lux);
}
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
x = event.values[0] /9.8;
y = event.values[1] /9.8;
z = event.values[2] /9.8;
System.out.println("x: " + x);
System.out.println("y: " + y);
System.out.println("z: " + z);
}
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
I am using AccessibilityService to monitor whenever the foreground activity is changed. Additionally whenever a such a change occurs, I need to read the accelerometer readings for the next 5 seconds. I implemented the following code, but I am not getting continuous readings. Sometimes No reading is shown in between 2 app switches
I am able to detect the foreground activity change accurately, and I can run an activity independently that can show the sensor state changes. But problem exists in combining both.
public class WindowChangeDetectingService extends AccessibilityService implements SensorEventListener {
#Override
protected void onServiceConnected() {
...
}
SensorManager mSensorManager;
Sensor mAccelerometerSensor;
SensorEventListener mListener;
#Override
public void onAccessibilityEvent(AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
try {
mSensorManager.unregisterListener(mListener);
}
catch (Exception e){
Log.d("not unregs",e.toString());
}
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mAccelerometerSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
Handler h = new Handler();
h.postDelayed(new Runnable() {
#Override
public void run() {
// do stuff with sensor values
mSensorManager.unregisterListener(mListener);
}
}, 5000);
ComponentName componentName = new ComponentName(
event.getPackageName().toString(),
event.getClassName().toString()
);
ActivityInfo activityInfo = tryGetActivity(componentName);
boolean isActivity = activityInfo != null;
if (isActivity)
Log.i("CurrentActivity", componentName.flattenToShortString());
}
}
#Override
public void onInterrupt() {}
#Override
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
Log.i("\nAccelerometer: \n","X:"+x+"\tY:"+y+"\tZ:"+z);
}
}
Use SENSOR_DELAY_GAME or SENSOR_DELAY_FASTEST instead of SENSOR_DELAY_NORMAL
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mAccelerometerSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this,
mAccelerometerSensor, SensorManager.SENSOR_DELAY_GAME);//change here
I want to get the brightness level from the Android camera.
I need to display the light level detected by the Android camera, is there any posibility to do that ?
private final SensorManager mSensorManager;
private final Sensor mLightSensor;
private float mLightQuantity;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Obtain references to the SensorManager and the Light Sensor
sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
// Implement a listener to receive updates
SensorEventListener listener = new SensorEventListener() {
#Override
public void onSensorChanged(SensorEvent event) {
lightQuantity = event.values[0];
}
}
// Register the listener with the light sensor -- choosing
// one of the SensorManager.SENSOR_DELAY_* constants.
sensorManager.registerListener(
listener, lightSensor, SensorManager.SENSOR_DELAY_UI);
}
I know that there are only two values of proximity : 0.0 & 1.0
I need a simple code to detect proximity through the device's sensors, and perform any task if proximity changes to 1.0
this is what i found.
public class SensorActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mSensor;
ImageView iv;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sensor_screen);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
iv = (ImageView) findViewById(R.id.imageView1);
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mSensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
if (event.values[0] == 0) {
iv.setImageResource(R.drawable.near);
} else {
iv.setImageResource(R.drawable.far);
}
}
}
You can use the getMaximumRange() method to obtain your proximity sensor's max range and use it to determine what to do.
SensorManager sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
Sensor proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
float maxRange = proximitySensor.getMaximumRange();
Now use maxRange to do what you want to do.
public void onSensorChanged(SensorEvent event) {
if(maxRange == event.values[0]) {
// Do something when something is far away.
}
else {
// Do something when something is near.
}
}
One very important thing. The proximity sensor varies from device to device. It not always reports 1.0 and 0.0. For example my Moto G (1st Gen.) reports 3 cm as its min range and 100 cm as its max range. So its best to obtain the max range of the device's sensor and use it in your code rather then hard-coding the value.
Hi I am building a 3D game for Android.
I am currently trying to add a sensor to my game that allows the player to tilt the android as its controls.
Ideally I would like to use the ORIENTATION sensor, but I noticed it has been deprecated. Does anyone know how to detect tilt in the android and doesn't use this sensor?
There is no actual "orientation" sensor - this is (was) actually a composite sensor, generated from a combination of the accelerometer and the magnometer.
From http://developer.android.com/reference/android/hardware/SensorEvent.html
"This sensor type exists for legacy reasons, please use getRotationMatrix() in conjunction with remapCoordinateSystem() and getOrientation() to compute these values instead."
public class OrientationTestActivity extends Activity implements SensorEventListener
{
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private Sensor mMagnetometer;
private float[] mLastAccelerometer = new float[3];
private float[] mLastMagnetometer = new float[3];
private boolean mLastAccelerometerSet = false;
private boolean mLastMagnetometerSet = false;
private float[] mR = new float[9];
private float[] mOrientation = new float[3];
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mMagnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
}
protected void onResume() {
super.onResume();
mLastAccelerometerSet = false;
mLastMagnetometerSet = false;
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
mSensorManager.registerListener(this, mMagnetometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
#Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor == mAccelerometer) {
System.arraycopy(event.values, 0, mLastAccelerometer, 0, event.values.length);
mLastAccelerometerSet = true;
} else if (event.sensor == mMagnetometer) {
System.arraycopy(event.values, 0, mLastMagnetometer, 0, event.values.length);
mLastMagnetometerSet = true;
}
if (mLastAccelerometerSet && mLastMagnetometerSet) {
SensorManager.getRotationMatrix(mR, null, mLastAccelerometer, mLastMagnetometer);
SensorManager.getOrientation(mR, mOrientation);
Log.i("OrientationTestActivity", String.format("Orientation: %f, %f, %f",
mOrientation[0], mOrientation[1], mOrientation[2]));
}
}
}