55import 'dart:async' ;
66
77import 'package:flutter/services.dart' ;
8+ import 'package:logging/logging.dart' ;
89import '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 >();
0 commit comments