fluttercommunity / plus_plugins

Flutter Community Plus Plugins
BSD 3-Clause "New" or "Revised" License
1.62k stars 985 forks source link

[Bug]: Sampling period is not being honored #3319

Closed HrafnPrescriby closed 1 month ago

HrafnPrescriby commented 1 month ago

Platform

Android 14

Plugin

sensor_plus

Version

6.0.1

Flutter SDK

3.19.5

Steps to reproduce

Issue it reproducible by subscribing to any of the sensor streams. Some streams report faster than others, but all do not seem to use the supplied samplingPeriod input variable. See code example below.

All devices used were resting on a surface, not moving. Device that were used were Pixel 6A and Motorolla G22.

Code Sample

import 'dart:async';

import 'package:flutter/widgets.dart';
import 'package:sensors_plus/sensors_plus.dart';

void main() async {
  runApp(
    TestWidget(),
  );
}

class TestWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _TestWidgetState();
}

class _TestWidgetState extends State<TestWidget> {
  late StreamSubscription _subscription;
  @override
  void initState() {
    _subscription = gyroscopeEventStream(
      samplingPeriod: const Duration(
        seconds: 10,
      ),
    ).listen(print);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return const Column();
  }
}

Logs

Gyro events showing timestamps between events when running the code above:

I/flutter (13803): [GyroscopeEvent (x: -0.0038499999791383743, y: -0.0012374999932944775, z: 0.0010999999940395355, timestamp: 2024-10-15 15:00:57.564196)]
I/flutter (13803): [GyroscopeEvent (x: -0.0006874999962747097, y: -0.00013749999925494194, z: 0.0010999999940395355, timestamp: 2024-10-15 15:00:58.561576)]
I/flutter (13803): [GyroscopeEvent (x: 0.0013749999925494194, y: -0.00013749999925494194, z: -0.0009624999947845936, timestamp: 2024-10-15 15:00:59.558956)]
I/flutter (13803): [GyroscopeEvent (x: -0.0006874999962747097, y: -0.00013749999925494194, z: -0.0009624999947845936, timestamp: 2024-10-15 15:01:00.556336)]
I/flutter (13803): [GyroscopeEvent (x: 0.0013749999925494194, y: -0.00013749999925494194, z: -0.0009624999947845936, timestamp: 2024-10-15 15:01:01.553717)]
I/flutter (13803): [GyroscopeEvent (x: 0.0004124999977648258, y: -0.00013749999925494194, z: 0.0010999999940395355, timestamp: 2024-10-15 15:01:02.551097)]
I/flutter (13803): [GyroscopeEvent (x: 0.0004124999977648258, y: -0.00013749999925494194, z: 0.0, timestamp: 2024-10-15 15:01:03.548478)]
I/flutter (13803): [GyroscopeEvent (x: -0.0006874999962747097, y: -0.00013749999925494194, z: -0.0009624999947845936, timestamp: 2024-10-15 15:01:04.545858)]
I/flutter (13803): [GyroscopeEvent (x: -0.0006874999962747097, y: -0.0012374999932944775, z: 0.0, timestamp: 2024-10-15 15:01:05.543238)]
I/flutter (13803): [GyroscopeEvent (x: -0.0006874999962747097, y: -0.00013749999925494194, z: -0.0009624999947845936, timestamp: 2024-10-15 15:01:06.540618)]
I/flutter (13803): [GyroscopeEvent (x: 0.0004124999977648258, y: -0.00013749999925494194, z: 0.0, timestamp: 2024-10-15 15:01:08.076582)]
I/flutter (13803): [GyroscopeEvent (x: 0.0004124999977648258, y: 0.0008249999955296516, z: 0.0010999999940395355, timestamp: 2024-10-15 15:01:09.073962)]
I/flutter (13803): [GyroscopeEvent (x: 0.0013749999925494194, y: -0.00013749999925494194, z: 0.002199999988079071, timestamp: 2024-10-15 15:01:10.071341)]
I/flutter (13803): [GyroscopeEvent (x: -0.0006874999962747097, y: 0.0008249999955296516, z: 0.0, timestamp: 2024-10-15 15:01:11.068720)]
I/flutter (13803): [GyroscopeEvent (x: -0.0006874999962747097, y: -0.00013749999925494194, z: 0.0, timestamp: 2024-10-15 15:01:12.066099)]
I/flutter (13803): [GyroscopeEvent (x: -0.0, y: -0.0002749999985098839, z: 0.0012374999932944775, timestamp: 2024-10-15 15:01:13.063478)]

Accelerometer events showing timestamps between events when running the code above:

I/flutter (14005): [AccelerometerEvent (x: 0.09795000404119492, y: 8.097000122070312, z: 5.632950305938721, timestamp: 2024-10-15 15:02:35.041096)]
I/flutter (14005): [AccelerometerEvent (x: 0.10005000233650208, y: 8.092050552368164, z: 5.64300012588501, timestamp: 2024-10-15 15:02:35.061041)]
I/flutter (14005): [AccelerometerEvent (x: 0.09495000541210175, y: 8.097000122070312, z: 5.638050079345703, timestamp: 2024-10-15 15:02:35.080987)]
I/flutter (14005): [AccelerometerEvent (x: 0.09495000541210175, y: 8.097000122070312, z: 5.619000434875488, timestamp: 2024-10-15 15:02:35.100931)]
I/flutter (14005): [AccelerometerEvent (x: 0.10200000554323196, y: 8.104050636291504, z: 5.6449503898620605, timestamp: 2024-10-15 15:02:35.120874)]
I/flutter (14005): [AccelerometerEvent (x: 0.09000000357627869, y: 8.104050636291504, z: 5.628000259399414, timestamp: 2024-10-15 15:02:35.140817)]
I/flutter (14005): [AccelerometerEvent (x: 0.09300000220537186, y: 8.10105037689209, z: 5.620950222015381, timestamp: 2024-10-15 15:02:35.160761)]
I/flutter (14005): [AccelerometerEvent (x: 0.10005000233650208, y: 8.104050636291504, z: 5.63100004196167, timestamp: 2024-10-15 15:02:35.180706)]
I/flutter (14005): [AccelerometerEvent (x: 0.0880500078201294, y: 8.098950386047363, z: 5.628000259399414, timestamp: 2024-10-15 15:02:35.200650)]
I/flutter (14005): [AccelerometerEvent (x: 0.09000000357627869, y: 8.097000122070312, z: 5.640000343322754, timestamp: 2024-10-15 15:02:35.220596)]
I/flutter (14005): [AccelerometerEvent (x: 0.09000000357627869, y: 8.10105037689209, z: 5.650050163269043, timestamp: 2024-10-15 15:02:35.240542)]
I/flutter (14005): [AccelerometerEvent (x: 0.09795000404119492, y: 8.097000122070312, z: 5.638050079345703, timestamp: 2024-10-15 15:02:35.260487)]
I/flutter (14005): [AccelerometerEvent (x: 0.09000000357627869, y: 8.098950386047363, z: 5.620950222015381, timestamp: 2024-10-15 15:02:35.280430)]
I/flutter (14005): [AccelerometerEvent (x: 0.10695000737905502, y: 8.094000816345215, z: 5.632950305938721, timestamp: 2024-10-15 15:02:35.300375)]
I/flutter (14005): [AccelerometerEvent (x: 0.09795000404119492, y: 8.110950469970703, z: 5.635050296783447, timestamp: 2024-10-15 15:02:35.320320)]
I/flutter (14005): [AccelerometerEvent (x: 0.09495000541210175, y: 8.098950386047363, z: 5.63100004196167, timestamp: 2024-10-15 15:02:35.340265)]
I/flutter (14005): [AccelerometerEvent (x: 0.09795000404119492, y: 8.104050636291504, z: 5.638050079345703, timestamp: 2024-10-15 15:02:35.360211)]
I/flutter (14005): [AccelerometerEvent (x: 0.09300000220537186, y: 8.104050636291504, z: 5.628000259399414, timestamp: 2024-10-15 15:02:35.380157)]
I/flutter (14005): [AccelerometerEvent (x: 0.09300000220537186, y: 8.104050636291504, z: 5.63100004196167, timestamp: 2024-10-15 15:02:35.400102)]
I/flutter (14005): [AccelerometerEvent (x: 0.10005000233650208, y: 8.10105037689209, z: 5.64300012588501, timestamp: 2024-10-15 15:02:35.420047)]
I/flutter (14005): [AccelerometerEvent (x: 0.08595000207424164, y: 8.098950386047363, z: 5.623050212860107, timestamp: 2024-10-15 15:02:35.439990)]
I/flutter (14005): [AccelerometerEvent (x: 0.0880500078201294, y: 8.110950469970703, z: 5.614050388336182, timestamp: 2024-10-15 15:02:35.459935)]
I/flutter (14005): [AccelerometerEvent (x: 0.09000000357627869, y: 8.105999946594238, z: 5.63100004196167, timestamp: 2024-10-15 15:02:35.479878)]
I/flutter (14005): [AccelerometerEvent (x: 0.09300000220537186, y: 8.098950386047363, z: 5.619000434875488, timestamp: 2024-10-15 15:02:35.499823)]
I/flutter (14005): [AccelerometerEvent (x: 0.08595000207424164, y: 8.104050636291504, z: 5.638050079345703, timestamp: 2024-10-15 15:02:35.519769)]
I/flutter (14005): [AccelerometerEvent (x: 0.0880500078201294, y: 8.089950561523438, z: 5.638050079345703, timestamp: 2024-10-15 15:02:35.539712)]
I/flutter (14005): [AccelerometerEvent (x: 0.10005000233650208, y: 8.092050552368164, z: 5.63100004196167, timestamp: 2024-10-15 15:02:35.559657)]

Flutter Doctor

[✓] Flutter (Channel stable, 3.19.6, on Ubuntu 22.04.5 LTS 6.8.0-40-generic, locale en_US.UTF-8)
    • Flutter version 3.19.6 on channel stable at /home/hrafn/opt/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 54e66469a9 (6 months ago), 2024-04-17 13:08:03 -0700
    • Engine revision c4cd48e186
    • Dart version 3.3.4
    • DevTools version 2.31.1

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /usr/lib/android/sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_HOME = /usr/lib/android/sdk
    • Java binary at: /opt/android-studio/2023.3.1.7/jbr/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.9-11255266)
    • All Android licenses accepted.

[✗] Chrome - develop for the web (Cannot find Chrome executable at google-chrome)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.

[✗] Linux toolchain - develop for Linux desktop
    ✗ clang++ is required for Linux development.
      It is likely available from your distribution (e.g.: apt install clang), or can be downloaded from https://releases.llvm.org/
    ✗ CMake is required for Linux development.
      It is likely available from your distribution (e.g.: apt install cmake), or can be downloaded from https://cmake.org/download/
    ✗ ninja is required for Linux development.
      It is likely available from your distribution (e.g.: apt install ninja-build), or can be downloaded from https://github.com/ninja-build/ninja/releases
    • pkg-config version 0.29.2
    ✗ GTK 3.0 development libraries are required for Linux development.
      They are likely available from your distribution (e.g.: apt install libgtk-3-dev)

[✓] Android Studio (version 2022.3)
    • Android Studio at /opt/android-studio/2022.3.1.20
    • Flutter plugin version 76.2.2
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)

[✓] Android Studio (version 2023.3)
    • Android Studio at /opt/android-studio/2023.3.1.7
    • Flutter plugin version 77.3.1
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.9-11255266)

[!] Android Studio (version unknown)
    • Android Studio at /opt/android-studio
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    ✗ Unable to determine Android Studio version.
    ✗ Unable to find bundled Java version.
    • Try updating or re-installing Android Studio.

[✓] IntelliJ IDEA Community Edition (version 2022.3)
    • IntelliJ at /opt/jetbrains/intellij/community/2022.3.2
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] VS Code (version 1.94.1)
    • VS Code at /usr/share/code
    • Flutter extension version 3.98.0

[✓] Connected device (2 available)
    • moto g22 (mobile) • ZT322B7G97 • android-arm64 • Android 12 (API 31)
    • Linux (desktop)   • linux      • linux-x64     • Ubuntu 22.04.5 LTS 6.8.0-40-generic

[✓] Network resources
    • All expected network resources are available.

Checklist before submitting a bug

vbuberen commented 1 month ago

Please read README of the plugin. It is explained there that Android doesn't guarantee following the specified sampling rate.

There is a note right under this code snippet: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/sensors_plus/sensors_plus#example