and so I needed to know when the Android phone was rotated in the x, y, z directions for a tilted program, and then I used a well circulating code that uses magnetism and acceleration to find the orientation, since the orientation sensor is unreliable, and it was good and good in the x, z planes, but it can be said that the person was on a circularly moving bus holding his phones in a fixed terrain position, and then the android detects movement in the y direction, even if the user is not moving It gets its phones in the y direction, and therefore the program crashes, but just using acceleration in itself isn / t accurate too, and I wonder how to solve this problem of detecting y-movement relative to the user?
public class MotionListener { String service_name = Context.SENSOR_SERVICE; SensorManager sensorManager; Sensor sensor; public MotionListener(Context context) { sensorManager = (SensorManager) context.getSystemService(service_name); SensorManager sm = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); Sensor aSensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); Sensor mfSensor = sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); sm.registerListener(myAccelerometerListener, aSensor, SensorManager.SENSOR_DELAY_GAME); sm.registerListener(myMagneticFieldListener, mfSensor, SensorManager.SENSOR_DELAY_GAME); } float[] accelerometerValues; float[] magneticFieldValues; final SensorEventListener myAccelerometerListener = new SensorEventListener() { public void onSensorChanged(SensorEvent sensorEvent) { if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) accelerometerValues = sensorEvent.values; float[] values = new float[3]; float[] R = new float[9]; try { SensorManager.getRotationMatrix(R, null, accelerometerValues, magneticFieldValues); SensorManager.getOrientation(R, values); values[0] = (float) ((float) values[0] * 180 / Math.PI); values[1] = (float) ((float) values[1] * 180 / Math.PI); values[2] = (float) ((float) values[2] * 180 / Math.PI); System.out.println((int)values[0] + " " + (int)values[1] + " " + (int)values[2]); setTiltCoordinates(values); } catch (NullPointerException e) { } public void onAccuracyChanged(Sensor sensor, int accuracy) { } };
, , , , - x, y z. , , "".
, , , , , , , "", , , , , .
, x, y z , -. "y" ( ) , zx. y .
, , . , , Android SDK "" , .