Skip to content

Commit 559faa1

Browse files
authored
fix(sensors_plus): Add protection of reserved samplingPeriod value (#2390)
1 parent d175102 commit 559faa1

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

packages/sensors_plus/sensors_plus_platform_interface/lib/src/method_channel_sensors.dart

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:async';
66

77
import 'package:flutter/services.dart';
8+
import 'package:logging/logging.dart';
89
import 'package:sensors_plus_platform_interface/sensors_plus_platform_interface.dart';
910

1011
/// A method channel -based implementation of the SensorsPlatform interface.
@@ -24,6 +25,7 @@ class MethodChannelSensors extends SensorsPlatform {
2425
static const EventChannel _magnetometerEventChannel =
2526
EventChannel('dev.fluttercommunity.plus/sensors/magnetometer');
2627

28+
final logger = Logger('MethodChannelSensors');
2729
Stream<AccelerometerEvent>? _accelerometerEvents;
2830
Stream<GyroscopeEvent>? _gyroscopeEvents;
2931
Stream<UserAccelerometerEvent>? _userAccelerometerEvents;
@@ -35,8 +37,17 @@ class MethodChannelSensors extends SensorsPlatform {
3537
Stream<AccelerometerEvent> accelerometerEventStream({
3638
Duration samplingPeriod = SensorInterval.normalInterval,
3739
}) {
38-
_methodChannel.invokeMethod(
39-
'setAccelerationSamplingPeriod', samplingPeriod.inMicroseconds);
40+
var microseconds = samplingPeriod.inMicroseconds;
41+
if (microseconds >= 1 && microseconds <= 3) {
42+
logger.warning('The SamplingPeriod is currently set to $microsecondsμs, '
43+
'which is a reserved value in Android. Please consider changing it '
44+
'to either 0 or 4μs. See https://developer.android.com/reference/'
45+
'android/hardware/SensorManager#registerListener(android.hardware.'
46+
'SensorEventListener,%20android.hardware.Sensor,%20int) for more '
47+
'information');
48+
microseconds = 0;
49+
}
50+
_methodChannel.invokeMethod('setAccelerationSamplingPeriod', microseconds);
4051
_accelerometerEvents ??= _accelerometerEventChannel
4152
.receiveBroadcastStream()
4253
.map((dynamic event) {
@@ -52,8 +63,17 @@ class MethodChannelSensors extends SensorsPlatform {
5263
Stream<GyroscopeEvent> gyroscopeEventStream({
5364
Duration samplingPeriod = SensorInterval.normalInterval,
5465
}) {
55-
_methodChannel.invokeMethod(
56-
'setGyroscopeSamplingPeriod', samplingPeriod.inMicroseconds);
66+
var microseconds = samplingPeriod.inMicroseconds;
67+
if (microseconds >= 1 && microseconds <= 3) {
68+
logger.warning('The SamplingPeriod is currently set to $microsecondsμs, '
69+
'which is a reserved value in Android. Please consider changing it '
70+
'to either 0 or 4μs. See https://developer.android.com/reference/'
71+
'android/hardware/SensorManager#registerListener(android.hardware.'
72+
'SensorEventListener,%20android.hardware.Sensor,%20int) for more '
73+
'information');
74+
microseconds = 0;
75+
}
76+
_methodChannel.invokeMethod('setGyroscopeSamplingPeriod', microseconds);
5777
_gyroscopeEvents ??=
5878
_gyroscopeEventChannel.receiveBroadcastStream().map((dynamic event) {
5979
final list = event.cast<double>();
@@ -68,8 +88,18 @@ class MethodChannelSensors extends SensorsPlatform {
6888
Stream<UserAccelerometerEvent> userAccelerometerEventStream({
6989
Duration samplingPeriod = SensorInterval.normalInterval,
7090
}) {
91+
var microseconds = samplingPeriod.inMicroseconds;
92+
if (microseconds >= 1 && microseconds <= 3) {
93+
logger.warning('The SamplingPeriod is currently set to $microsecondsμs, '
94+
'which is a reserved value in Android. Please consider changing it '
95+
'to either 0 or 4μs. See https://developer.android.com/reference/'
96+
'android/hardware/SensorManager#registerListener(android.hardware.'
97+
'SensorEventListener,%20android.hardware.Sensor,%20int) for more '
98+
'information');
99+
microseconds = 0;
100+
}
71101
_methodChannel.invokeMethod(
72-
'setUserAccelerometerSamplingPeriod', samplingPeriod.inMicroseconds);
102+
'setUserAccelerometerSamplingPeriod', microseconds);
73103
_userAccelerometerEvents ??= _userAccelerometerEventChannel
74104
.receiveBroadcastStream()
75105
.map((dynamic event) {
@@ -85,8 +115,17 @@ class MethodChannelSensors extends SensorsPlatform {
85115
Stream<MagnetometerEvent> magnetometerEventStream({
86116
Duration samplingPeriod = SensorInterval.normalInterval,
87117
}) {
88-
_methodChannel.invokeMethod(
89-
'setMagnetometerSamplingPeriod', samplingPeriod.inMicroseconds);
118+
var microseconds = samplingPeriod.inMicroseconds;
119+
if (microseconds >= 1 && microseconds <= 3) {
120+
logger.warning('The SamplingPeriod is currently set to $microsecondsμs, '
121+
'which is a reserved value in Android. Please consider changing it '
122+
'to either 0 or 4μs. See https://developer.android.com/reference/'
123+
'android/hardware/SensorManager#registerListener(android.hardware.'
124+
'SensorEventListener,%20android.hardware.Sensor,%20int) for more '
125+
'information');
126+
microseconds = 0;
127+
}
128+
_methodChannel.invokeMethod('setMagnetometerSamplingPeriod', microseconds);
90129
_magnetometerEvents ??=
91130
_magnetometerEventChannel.receiveBroadcastStream().map((dynamic event) {
92131
final list = event.cast<double>();

packages/sensors_plus/sensors_plus_platform_interface/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ repository: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/
77
dependencies:
88
flutter:
99
sdk: flutter
10+
logging: ^1.2.0
1011
meta: ^1.8.0
1112
plugin_platform_interface: ^2.1.4
1213

0 commit comments

Comments
 (0)