コード例 #1
0
 public override void Pause()
 {
     // Disable all sensors when application is paused
     accelerometerListener?.Disable();
     gyroscopeListener?.Disable();
     linearAccelerationListener?.Disable();
     orientationListener?.Disable();
     gravityListener?.Disable();
 }
コード例 #2
0
        /// <summary>
        /// Tries to create a sensor listener if it is supported
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public AndroidSensorListener TryGetSensorListener(SensorType type)
        {
            var listener = new AndroidSensorListener(type);

            if (!listener.Enable())
            {
                listener.Dispose();
                return(null);
            }

            listener.Disable();
            return(listener);
        }
コード例 #3
0
        public void UpdateSensorPair <TSensor>(AndroidSensorListener listener, TSensor sensor, Action <TSensor, AndroidSensorListener> updater) where TSensor : Sensor
        {
            if (listener != null)
            {
                bool enable = sensor.IsEnabled;
                if (enable != listener.Enabled)
                {
                    if (enable)
                    {
                        listener.Enable();
                    }
                    else
                    {
                        listener.Disable();
                    }
                }

                if (enable)
                {
                    updater.Invoke(sensor, listener);
                }
            }
        }
コード例 #4
0
        public override void Update()
        {
            base.Update();

            var quaternionArray     = new float[4];
            var rotationMatrixArray = new float[9];
            var yawPitchRollArray   = new float[3];

            // Update sensors
            // Enable/disable supported sensors and update enabled sensors
            UpdateSensorPair(accelerometerListener, accelerometerSensor,
                             (sensor, listener) => sensor.Acceleration = listener.GetCurrentValuesAsVector());
            UpdateSensorPair(gyroscopeListener, gyroscopeSensor,
                             (sensor, listener) => sensor.RotationRate = -listener.GetCurrentValuesAsVector());
            UpdateSensorPair(linearAccelerationListener, userAccelerationSensor,
                             (sensor, listener) => sensor.Acceleration = listener.GetCurrentValuesAsVector());
            UpdateSensorPair(gravityListener, gravitySensor,
                             (sensor, listener) => sensor.Vector = listener.GetCurrentValuesAsVector());

            // Enabled/Disable/Update Orientation
            if (orientationListener != null)
            {
                bool enable = orientationSensor.IsEnabled || compassSensor.IsEnabled; // Orientation is used for compass as well
                if (enable != orientationListener.Enabled)
                {
                    if (enable)
                    {
                        orientationListener.Enable();
                    }
                    else
                    {
                        orientationListener.Disable();
                    }
                }

                if (enable)
                {
                    // Update orientation
                    base.Update();

                    var rotationVector = orientationListener.GetValues()?.ToArray() ?? new [] { 0.0f, 0.0f, 0.0f };
                    if (rotationVector.Length < 3)
                    {
                        return;
                    }

                    SensorManager.GetQuaternionFromVector(quaternionArray, rotationVector);
                    SensorManager.GetRotationMatrixFromVector(rotationMatrixArray, rotationVector);
                    SensorManager.GetOrientation(rotationMatrixArray, yawPitchRollArray);

                    var quaternion = Quaternion.Identity;
                    quaternion.W = +quaternionArray[0];
                    quaternion.X = +quaternionArray[1];
                    quaternion.Y = +quaternionArray[3];
                    quaternion.Z = -quaternionArray[2];
                    quaternion   = Quaternion.RotationY(MathUtil.Pi) * quaternion; // align the orientation with north.
                    orientationSensor.FromQuaternion(quaternion);

                    // Update compass
                    compassSensor.Heading = yawPitchRollArray[0] + MathUtil.Pi;
                }
            }
        }