-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Closed
Labels
Description
Platform
12
Plugin
sensors_plus
Version
4.0.2
Flutter SDK
3.16.4
Steps to reproduce
While calling the accelerometerEventStream() method it is throwing this exception in android devices.
In the below code syntax while calling the mehtod in initState it is throwing this issue.
ShakeDetector.autoStart(onPhoneShake: () {
if (accessToken.isNotEmpty) {
if (showTips) {
context.pop();
}
BlocProvider.of<HealthInfoContentBloc>(context)
.add(GetRandomTipEvent());
handleShowTips(true);
getWellnessTips();
}
});
Code Sample
import 'dart:async';
import 'dart:math';
import 'package:sensors_plus/sensors_plus.dart';
/// Callback for phone shakes
typedef PhoneShakeCallback = void Function();
/// ShakeDetector class for phone shake functionality
class ShakeDetector {
/// User callback for phone shake
final PhoneShakeCallback onPhoneShake;
/// Shake detection threshold
final double shakeThresholdGravity;
/// Minimum time between shake
final int shakeSlopTimeMS;
/// Time before shake count resets
final int shakeCountResetTime;
/// Number of shakes required before shake is triggered
final int minimumShakeCount;
int mShakeTimestamp = DateTime.now().millisecondsSinceEpoch;
int mShakeCount = 0;
/// StreamSubscription for Accelerometer events
StreamSubscription? streamSubscription;
/// This constructor waits until [startListening] is called
ShakeDetector.waitForStart({
required this.onPhoneShake,
this.shakeThresholdGravity = 2.7,
this.shakeSlopTimeMS = 500,
this.shakeCountResetTime = 3000,
this.minimumShakeCount = 1,
});
/// This constructor automatically calls [startListening] and starts detection and callbacks.
ShakeDetector.autoStart({
required this.onPhoneShake,
this.shakeThresholdGravity = 2.7,
this.shakeSlopTimeMS = 500,
this.shakeCountResetTime = 3000,
this.minimumShakeCount = 1,
}) {
startListening();
}
/// Starts listening to accelerometer events
void startListening() {
streamSubscription = accelerometerEventStream().listen(
(AccelerometerEvent event) {
double x = event.x;
double y = event.y;
double z = event.z;
double gX = x / 9.80665;
double gY = y / 9.80665;
double gZ = z / 9.80665;
// gForce will be close to 1 when there is no movement.
double gForce = sqrt(gX * gX + gY * gY + gZ * gZ);
if (gForce > shakeThresholdGravity) {
var now = DateTime.now().millisecondsSinceEpoch;
// ignore shake events too close to each other (500ms)
if (mShakeTimestamp + shakeSlopTimeMS > now) {
return;
}
// reset the shake count after 3 seconds of no shakes
if (mShakeTimestamp + shakeCountResetTime < now) {
mShakeCount = 0;
}
mShakeTimestamp = now;
mShakeCount++;
if (mShakeCount >= minimumShakeCount) {
onPhoneShake();
}
}
},
cancelOnError: true,
onError: (error) {
print('the error ${error.message}');
});
}
/// Stops listening to accelerometer events
void stopListening() {
streamSubscription?.cancel();
}
}Logs
V/SystemSensorManagerExtImpl(14781): RegisterListener bmi26x Accelerometer Non-wakeup type:1 delay:200000us by dev.fluttercommunity.plus.sensors.StreamHandlerImpl$createSensorEventListener$1
E/flutter (14781): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: MissingPluginException(No implementation found for method setAccelerationSamplingPeriod on channel dev.fluttercommunity.plus/sensors/method)
E/flutter (14781): #0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:320:7)
E/flutter (14781): <asynchronous suspension>Flutter Doctor
[✓] Flutter (Channel stable, 3.16.4, on macOS 14.1.1 23B81 darwin-arm64, locale en-IN)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0-rc1)
[✓] Xcode - develop for iOS and macOS (Xcode 15.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.2)
[✓] VS Code (version 1.85.1)
[✓] Connected device (4 available)
[✓] Network resources
• No issues found!Checklist before submitting a bug
- I searched issues in this repository and couldn't find such bug/problem
- I Google'd a solution and I couldn't find it
- I searched on StackOverflow for a solution and I couldn't find it
- I read the README.md file of the plugin
- I'm using the latest version of the plugin
- All dependencies are up to date with
flutter pub upgrade - I did a
flutter clean - I tried running the example project
AlexSeednov, GerogeLeon, sonphamtrung17, zifeo, hanisoft2022 and 2 more