Skip to content

Commit

Permalink
fix(sensors_plus): Add protection of reserved samplingPeriod value (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tlserver committed Nov 24, 2023
1 parent d175102 commit 559faa1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
Expand Up @@ -5,6 +5,7 @@
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:logging/logging.dart';
import 'package:sensors_plus_platform_interface/sensors_plus_platform_interface.dart';

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

final logger = Logger('MethodChannelSensors');
Stream<AccelerometerEvent>? _accelerometerEvents;
Stream<GyroscopeEvent>? _gyroscopeEvents;
Stream<UserAccelerometerEvent>? _userAccelerometerEvents;
Expand All @@ -35,8 +37,17 @@ class MethodChannelSensors extends SensorsPlatform {
Stream<AccelerometerEvent> accelerometerEventStream({
Duration samplingPeriod = SensorInterval.normalInterval,
}) {
_methodChannel.invokeMethod(
'setAccelerationSamplingPeriod', samplingPeriod.inMicroseconds);
var microseconds = samplingPeriod.inMicroseconds;
if (microseconds >= 1 && microseconds <= 3) {
logger.warning('The SamplingPeriod is currently set to $microsecondsμs, '
'which is a reserved value in Android. Please consider changing it '
'to either 0 or 4μs. See https://developer.android.com/reference/'
'android/hardware/SensorManager#registerListener(android.hardware.'
'SensorEventListener,%20android.hardware.Sensor,%20int) for more '
'information');
microseconds = 0;
}
_methodChannel.invokeMethod('setAccelerationSamplingPeriod', microseconds);
_accelerometerEvents ??= _accelerometerEventChannel
.receiveBroadcastStream()
.map((dynamic event) {
Expand All @@ -52,8 +63,17 @@ class MethodChannelSensors extends SensorsPlatform {
Stream<GyroscopeEvent> gyroscopeEventStream({
Duration samplingPeriod = SensorInterval.normalInterval,
}) {
_methodChannel.invokeMethod(
'setGyroscopeSamplingPeriod', samplingPeriod.inMicroseconds);
var microseconds = samplingPeriod.inMicroseconds;
if (microseconds >= 1 && microseconds <= 3) {
logger.warning('The SamplingPeriod is currently set to $microsecondsμs, '
'which is a reserved value in Android. Please consider changing it '
'to either 0 or 4μs. See https://developer.android.com/reference/'
'android/hardware/SensorManager#registerListener(android.hardware.'
'SensorEventListener,%20android.hardware.Sensor,%20int) for more '
'information');
microseconds = 0;
}
_methodChannel.invokeMethod('setGyroscopeSamplingPeriod', microseconds);
_gyroscopeEvents ??=
_gyroscopeEventChannel.receiveBroadcastStream().map((dynamic event) {
final list = event.cast<double>();
Expand All @@ -68,8 +88,18 @@ class MethodChannelSensors extends SensorsPlatform {
Stream<UserAccelerometerEvent> userAccelerometerEventStream({
Duration samplingPeriod = SensorInterval.normalInterval,
}) {
var microseconds = samplingPeriod.inMicroseconds;
if (microseconds >= 1 && microseconds <= 3) {
logger.warning('The SamplingPeriod is currently set to $microsecondsμs, '
'which is a reserved value in Android. Please consider changing it '
'to either 0 or 4μs. See https://developer.android.com/reference/'
'android/hardware/SensorManager#registerListener(android.hardware.'
'SensorEventListener,%20android.hardware.Sensor,%20int) for more '
'information');
microseconds = 0;
}
_methodChannel.invokeMethod(
'setUserAccelerometerSamplingPeriod', samplingPeriod.inMicroseconds);
'setUserAccelerometerSamplingPeriod', microseconds);
_userAccelerometerEvents ??= _userAccelerometerEventChannel
.receiveBroadcastStream()
.map((dynamic event) {
Expand All @@ -85,8 +115,17 @@ class MethodChannelSensors extends SensorsPlatform {
Stream<MagnetometerEvent> magnetometerEventStream({
Duration samplingPeriod = SensorInterval.normalInterval,
}) {
_methodChannel.invokeMethod(
'setMagnetometerSamplingPeriod', samplingPeriod.inMicroseconds);
var microseconds = samplingPeriod.inMicroseconds;
if (microseconds >= 1 && microseconds <= 3) {
logger.warning('The SamplingPeriod is currently set to $microsecondsμs, '
'which is a reserved value in Android. Please consider changing it '
'to either 0 or 4μs. See https://developer.android.com/reference/'
'android/hardware/SensorManager#registerListener(android.hardware.'
'SensorEventListener,%20android.hardware.Sensor,%20int) for more '
'information');
microseconds = 0;
}
_methodChannel.invokeMethod('setMagnetometerSamplingPeriod', microseconds);
_magnetometerEvents ??=
_magnetometerEventChannel.receiveBroadcastStream().map((dynamic event) {
final list = event.cast<double>();
Expand Down
Expand Up @@ -7,6 +7,7 @@ repository: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/
dependencies:
flutter:
sdk: flutter
logging: ^1.2.0
meta: ^1.8.0
plugin_platform_interface: ^2.1.4

Expand Down

0 comments on commit 559faa1

Please sign in to comment.