Skip to content

Commit 1c62271

Browse files
Sahil Totalavatsaltanna-simformsolutions
authored andcommitted
fix: 🐛Fixed gyroscope initialization issue
1 parent 4929441 commit 1c62271

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# [4.0.2](https://github.com/SimformSolutionsPvtLtd/flutter_credit_card/tree/4.0.2) [UNRELEASED]
22

33
- Fixed floating event stream bad state exception [#157](https://github.com/SimformSolutionsPvtLtd/flutter_credit_card/issues/157).
4+
- Fixed Gyroscope initialization issue [#173](https://github.com/SimformSolutionsPvtLtd/flutter_credit_card/issues/173).
5+
- Fixed Namespace Not Found issue [#176](https://github.com/SimformSolutionsPvtLtd/flutter_credit_card/issues/176).
46

57
# [4.0.1](https://github.com/SimformSolutionsPvtLtd/flutter_credit_card/tree/4.0.1)
68

‎android/src/main/kotlin/com/simform/flutter_credit_card/gyroscope/GyroscopeStreamHandler.kt‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,26 @@ internal class GyroscopeStreamHandler(
1515
) : EventChannel.StreamHandler {
1616
private var sensorEventListener: SensorEventListener? = null
1717

18-
private val sensor: Sensor by lazy {
18+
private val sensor: Sensor? by lazy {
1919
sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE)
2020
}
2121

2222
override fun onListen(arguments: Any?, events: EventSink) {
23+
if (sensor == null) {
24+
events.error("SENSOR_UNAVAILABLE", "Gyroscope sensor is not available on this device.", null)
25+
return
26+
}
2327
sensorEventListener = createSensorEventListener(events)
2428
// Gyroscope Event sample period set at 60 fps, specified in microseconds.
2529
sensorManager.registerListener(sensorEventListener, sensor, 16666)
2630
}
2731

28-
override fun onCancel(arguments: Any?) = sensorManager.unregisterListener(sensorEventListener)
32+
override fun onCancel(arguments: Any?) {
33+
if (sensorEventListener != null) {
34+
sensorManager.unregisterListener(sensorEventListener)
35+
sensorEventListener = null
36+
}
37+
}
2938

3039
private fun createSensorEventListener(events: EventSink): SensorEventListener {
3140
return object : SensorEventListener {

‎lib/src/plugin/flutter_credit_card_method_channel.dart‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ class MethodChannelFlutterCreditCard extends FlutterCreditCardPlatform {
5151
);
5252

5353
if (Platform.isIOS || Platform.isAndroid) {
54-
await initiateEvents();
5554
_isGyroscopeAvailable = await _methodChannel!.invokeMethod<dynamic>(
5655
AppConstants.isGyroAvailableMethod,
5756
) ??
@@ -60,6 +59,10 @@ class MethodChannelFlutterCreditCard extends FlutterCreditCardPlatform {
6059
// Other platforms should not use the gyroscope events.
6160
_isGyroscopeAvailable = false;
6261
}
62+
// We will only initialize event if gyroScope is available.
63+
if (_isGyroscopeAvailable) {
64+
await initiateEvents();
65+
}
6366
}
6467

6568
@override

0 commit comments

Comments
 (0)