From b6fe59a67531232f5a213af8fede499d13fd214a Mon Sep 17 00:00:00 2001 From: James Montemagno Date: Wed, 11 Mar 2020 13:48:26 -0700 Subject: [PATCH] Check if 4th value is present in orienation sensor --- .../OrientationSensor.android.cs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/Xamarin.Essentials/OrientationSensor/OrientationSensor.android.cs b/Xamarin.Essentials/OrientationSensor/OrientationSensor.android.cs index 1ff995a88..f2eca3745 100644 --- a/Xamarin.Essentials/OrientationSensor/OrientationSensor.android.cs +++ b/Xamarin.Essentials/OrientationSensor/OrientationSensor.android.cs @@ -43,11 +43,21 @@ void ISensorEventListener.OnAccuracyChanged(Sensor sensor, [GeneratedEnum] Senso void ISensorEventListener.OnSensorChanged(SensorEvent e) { - if ((e?.Values?.Count ?? 0) < 4) + var count = e?.Values?.Count ?? 0; + if (count < 3) return; - var data = new OrientationSensorData(e.Values[0], e.Values[1], e.Values[2], e.Values[3]); - OrientationSensor.OnChanged(data); + OrientationSensorData? data; + + // Docs: https://developer.android.com/reference/android/hardware/SensorEvent#sensor.type_rotation_vector-: + // values[3], originally optional, will always be present from SDK Level 18 onwards. values[4] is a new value that has been added in SDK Level 18. + + if (count < 4) + data = new OrientationSensorData(e.Values[0], e.Values[1], e.Values[2], -1); + else + data = new OrientationSensorData(e.Values[0], e.Values[1], e.Values[2], e.Values[3]); + + OrientationSensor.OnChanged(data.Value); } } }