Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sensors_plus): Add protection of reserved samplingPeriod value #2390

Merged
merged 1 commit into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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