From 9509e13d09bdb36d3cd016c2d66c78dda14d6451 Mon Sep 17 00:00:00 2001 From: Codel1417 Date: Sat, 25 Jan 2025 17:07:57 -0500 Subject: [PATCH 1/6] add methods to check if sensors are available --- .../com/example/pedometer/PedometerPlugin.kt | 19 +++++++++++- .../ios/Classes/SwiftPedometerPlugin.swift | 29 +++++++++++++++++++ packages/pedometer/lib/pedometer.dart | 11 +++++-- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/packages/pedometer/android/src/main/kotlin/com/example/pedometer/PedometerPlugin.kt b/packages/pedometer/android/src/main/kotlin/com/example/pedometer/PedometerPlugin.kt index 0c2bfd7ad..7a527e2ea 100644 --- a/packages/pedometer/android/src/main/kotlin/com/example/pedometer/PedometerPlugin.kt +++ b/packages/pedometer/android/src/main/kotlin/com/example/pedometer/PedometerPlugin.kt @@ -1,22 +1,39 @@ package com.example.pedometer +import android.content.Context import android.hardware.Sensor import android.hardware.SensorEventListener import android.hardware.SensorManager import androidx.annotation.NonNull import io.flutter.embedding.engine.plugins.FlutterPlugin import io.flutter.plugin.common.EventChannel +import io.flutter.plugin.common.MethodChannel /** PedometerPlugin */ class PedometerPlugin : FlutterPlugin { private lateinit var stepDetectionChannel: EventChannel private lateinit var stepCountChannel: EventChannel + override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) { + super.configureFlutterEngine(flutterEngine) + MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "com.example.pedometer").setMethodCallHandler { + call, result -> + val context = flutterPluginBinding.applicationContext + val sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager + if (call.method == "isStepDetectionSupported"){ + val stepDetectionSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR) + result.success(stepDetectionSensor != null) + } + else if (call.method == "isStepCountSupported"){ + val stepCountSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER) + result.success(stepCountSensor != null) + } + } + } override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { /// Create channels stepDetectionChannel = EventChannel(flutterPluginBinding.binaryMessenger, "step_detection") stepCountChannel = EventChannel(flutterPluginBinding.binaryMessenger, "step_count") - /// Create handlers val stepDetectionHandler = SensorStreamHandler(flutterPluginBinding, Sensor.TYPE_STEP_DETECTOR) val stepCountHandler = SensorStreamHandler(flutterPluginBinding, Sensor.TYPE_STEP_COUNTER) diff --git a/packages/pedometer/ios/Classes/SwiftPedometerPlugin.swift b/packages/pedometer/ios/Classes/SwiftPedometerPlugin.swift index b13c62af9..f090fe7d6 100644 --- a/packages/pedometer/ios/Classes/SwiftPedometerPlugin.swift +++ b/packages/pedometer/ios/Classes/SwiftPedometerPlugin.swift @@ -14,6 +14,35 @@ public class SwiftPedometerPlugin: NSObject, FlutterPlugin { let stepCountHandler = StepCounter() let stepCountChannel = FlutterEventChannel.init(name: "step_count", binaryMessenger: registrar.messenger()) stepCountChannel.setStreamHandler(stepCountHandler) + + let methodChannel = FlutterMethodChannel(name: "com.example.pedometer", + binaryMessenger: controller.binaryMessenger) + methodChannel.setMethodCallHandler({ + [weak self] (call: FlutterMethodCall, result: FlutterResult) -> Void in + // This method is invoked on the UI thread. + guard call.method == "isStepDetectionSupported" || call.method == "isStepCountSupported" else { + result(FlutterMethodNotImplemented) + return + } + if call.method == "isStepCountSupported"{ + result(CMPedometer.isStepCountingAvailable()) + + } + else if call.method = "isStepDetectionSupported" { + result(CMPedometer.isPedometerEventTrackingAvailable()) + } + }) + } + private func receiveBatteryLevel(result: FlutterResult) { + let device = UIDevice.current + device.isBatteryMonitoringEnabled = true + if device.batteryState == UIDevice.BatteryState.unknown { + result(FlutterError(code: "UNAVAILABLE", + message: "Battery level not available.", + details: nil)) + } else { + result(Int(device.batteryLevel * 100)) + } } } diff --git a/packages/pedometer/lib/pedometer.dart b/packages/pedometer/lib/pedometer.dart index d23494e86..df333da88 100644 --- a/packages/pedometer/lib/pedometer.dart +++ b/packages/pedometer/lib/pedometer.dart @@ -10,7 +10,7 @@ class Pedometer { const EventChannel('step_detection'); static const EventChannel _stepCountChannel = const EventChannel('step_count'); - + static const _platform = MethodChannel('com.example.pedometer'); static StreamController _androidPedestrianController = StreamController.broadcast(); @@ -23,7 +23,14 @@ class Pedometer { if (Platform.isAndroid) return _androidStream(stream); return stream; } - + static Future get isStepDetectionSupported { + Future result = _platform.invokeMethod("isStepDetectionSupported"); + return result; + } + static Future get isStepCountSupported { + Future result = _platform.invokeMethod("isStepCountSupported"); + return result; + } /// Transformed stream for the Android platform static Stream _androidStream( Stream stream) { From bd5324e0797b253e7b44591b7be7e7ba9183df44 Mon Sep 17 00:00:00 2001 From: Codel1417 Date: Sat, 25 Jan 2025 17:33:24 -0500 Subject: [PATCH 2/6] adjust android method to work like a plugin, not an app --- .../com/example/pedometer/PedometerPlugin.kt | 39 +++++++++++-------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/packages/pedometer/android/src/main/kotlin/com/example/pedometer/PedometerPlugin.kt b/packages/pedometer/android/src/main/kotlin/com/example/pedometer/PedometerPlugin.kt index 7a527e2ea..f6e1bbeba 100644 --- a/packages/pedometer/android/src/main/kotlin/com/example/pedometer/PedometerPlugin.kt +++ b/packages/pedometer/android/src/main/kotlin/com/example/pedometer/PedometerPlugin.kt @@ -8,26 +8,26 @@ import androidx.annotation.NonNull import io.flutter.embedding.engine.plugins.FlutterPlugin import io.flutter.plugin.common.EventChannel import io.flutter.plugin.common.MethodChannel +import io.flutter.plugin.common.MethodChannel.MethodCallHandler +import io.flutter.plugin.common.MethodChannel.Result +import io.flutter.plugin.common.MethodCall /** PedometerPlugin */ -class PedometerPlugin : FlutterPlugin { +class PedometerPlugin : FlutterPlugin, MethodCallHandler { private lateinit var stepDetectionChannel: EventChannel private lateinit var stepCountChannel: EventChannel - override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) { - super.configureFlutterEngine(flutterEngine) - MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "com.example.pedometer").setMethodCallHandler { - call, result -> - val context = flutterPluginBinding.applicationContext - val sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager - if (call.method == "isStepDetectionSupported"){ - val stepDetectionSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR) - result.success(stepDetectionSensor != null) - - } - else if (call.method == "isStepCountSupported"){ - val stepCountSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER) - result.success(stepCountSensor != null) - } + private lateinit var methodChannel : MethodChannel; + private lateinit var sensorManager : SensorManager; + + override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) { + if (call.method == "isStepDetectionSupported"){ + val stepDetectionSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR) + result.success(stepDetectionSensor != null) + + } + else if (call.method == "isStepCountSupported"){ + val stepCountSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER) + result.success(stepCountSensor != null) } } override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { @@ -41,11 +41,18 @@ class PedometerPlugin : FlutterPlugin { /// Set handlers stepDetectionChannel.setStreamHandler(stepDetectionHandler) stepCountChannel.setStreamHandler(stepCountHandler) + + // setup method channel + val context = FlutterPlugin.FlutterPluginBinding.applicationContext + sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager + methodChannel = MethodChannel(flutterPluginBinding), "com.example.pedometer") + methodChannel.setMethodCallHandler(this) } override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) { stepDetectionChannel.setStreamHandler(null) stepCountChannel.setStreamHandler(null) + methodChannel.setMethodCallHandler(null) } } From b45c8fc87325a48e0a9dfd049870c46f9899676e Mon Sep 17 00:00:00 2001 From: Codel1417 Date: Sat, 25 Jan 2025 17:49:06 -0500 Subject: [PATCH 3/6] dev with the ide fighting you is fun --- .../main/kotlin/com/example/pedometer/PedometerPlugin.kt | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/pedometer/android/src/main/kotlin/com/example/pedometer/PedometerPlugin.kt b/packages/pedometer/android/src/main/kotlin/com/example/pedometer/PedometerPlugin.kt index f6e1bbeba..c1e86f97b 100644 --- a/packages/pedometer/android/src/main/kotlin/com/example/pedometer/PedometerPlugin.kt +++ b/packages/pedometer/android/src/main/kotlin/com/example/pedometer/PedometerPlugin.kt @@ -6,6 +6,7 @@ import android.hardware.SensorEventListener import android.hardware.SensorManager import androidx.annotation.NonNull import io.flutter.embedding.engine.plugins.FlutterPlugin +import io.flutter.embedding.engine.plugins.FlutterPlugin.FlutterPluginBinding import io.flutter.plugin.common.EventChannel import io.flutter.plugin.common.MethodChannel import io.flutter.plugin.common.MethodChannel.MethodCallHandler @@ -30,7 +31,7 @@ class PedometerPlugin : FlutterPlugin, MethodCallHandler { result.success(stepCountSensor != null) } } - override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { + override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPluginBinding) { /// Create channels stepDetectionChannel = EventChannel(flutterPluginBinding.binaryMessenger, "step_detection") stepCountChannel = EventChannel(flutterPluginBinding.binaryMessenger, "step_count") @@ -43,9 +44,9 @@ class PedometerPlugin : FlutterPlugin, MethodCallHandler { stepCountChannel.setStreamHandler(stepCountHandler) // setup method channel - val context = FlutterPlugin.FlutterPluginBinding.applicationContext + val context = flutterPluginBinding.applicationContext sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager - methodChannel = MethodChannel(flutterPluginBinding), "com.example.pedometer") + methodChannel = MethodChannel(flutterPluginBinding.binaryMessenger, "com.example.pedometer") methodChannel.setMethodCallHandler(this) } From e8c6388bc876cb4971c3341aa8915a517ca68cfd Mon Sep 17 00:00:00 2001 From: Codel1417 Date: Sat, 25 Jan 2025 18:57:02 -0500 Subject: [PATCH 4/6] fix comparison on ios --- packages/pedometer/ios/Classes/SwiftPedometerPlugin.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pedometer/ios/Classes/SwiftPedometerPlugin.swift b/packages/pedometer/ios/Classes/SwiftPedometerPlugin.swift index f090fe7d6..022492c4a 100644 --- a/packages/pedometer/ios/Classes/SwiftPedometerPlugin.swift +++ b/packages/pedometer/ios/Classes/SwiftPedometerPlugin.swift @@ -28,7 +28,7 @@ public class SwiftPedometerPlugin: NSObject, FlutterPlugin { result(CMPedometer.isStepCountingAvailable()) } - else if call.method = "isStepDetectionSupported" { + else if call.method == "isStepDetectionSupported" { result(CMPedometer.isPedometerEventTrackingAvailable()) } }) From bf078ebe36b997dfdd543b570b7d7eb5d1c57bc2 Mon Sep 17 00:00:00 2001 From: Codel1417 Date: Sat, 25 Jan 2025 19:05:15 -0500 Subject: [PATCH 5/6] replace controller reference on ios --- packages/pedometer/ios/Classes/SwiftPedometerPlugin.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pedometer/ios/Classes/SwiftPedometerPlugin.swift b/packages/pedometer/ios/Classes/SwiftPedometerPlugin.swift index 022492c4a..4c67ab89d 100644 --- a/packages/pedometer/ios/Classes/SwiftPedometerPlugin.swift +++ b/packages/pedometer/ios/Classes/SwiftPedometerPlugin.swift @@ -16,7 +16,7 @@ public class SwiftPedometerPlugin: NSObject, FlutterPlugin { stepCountChannel.setStreamHandler(stepCountHandler) let methodChannel = FlutterMethodChannel(name: "com.example.pedometer", - binaryMessenger: controller.binaryMessenger) + binaryMessenger: registrar.messenger()) methodChannel.setMethodCallHandler({ [weak self] (call: FlutterMethodCall, result: FlutterResult) -> Void in // This method is invoked on the UI thread. From 205d83189004177a6cadac8478409c44612dee30 Mon Sep 17 00:00:00 2001 From: Codel1417 Date: Sat, 25 Jan 2025 19:19:31 -0500 Subject: [PATCH 6/6] remove [weak self] from method channel --- .../ios/Classes/SwiftPedometerPlugin.swift | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/packages/pedometer/ios/Classes/SwiftPedometerPlugin.swift b/packages/pedometer/ios/Classes/SwiftPedometerPlugin.swift index 4c67ab89d..b95ac0c6a 100644 --- a/packages/pedometer/ios/Classes/SwiftPedometerPlugin.swift +++ b/packages/pedometer/ios/Classes/SwiftPedometerPlugin.swift @@ -18,7 +18,7 @@ public class SwiftPedometerPlugin: NSObject, FlutterPlugin { let methodChannel = FlutterMethodChannel(name: "com.example.pedometer", binaryMessenger: registrar.messenger()) methodChannel.setMethodCallHandler({ - [weak self] (call: FlutterMethodCall, result: FlutterResult) -> Void in + (call: FlutterMethodCall, result: FlutterResult) -> Void in // This method is invoked on the UI thread. guard call.method == "isStepDetectionSupported" || call.method == "isStepCountSupported" else { result(FlutterMethodNotImplemented) @@ -33,17 +33,6 @@ public class SwiftPedometerPlugin: NSObject, FlutterPlugin { } }) } - private func receiveBatteryLevel(result: FlutterResult) { - let device = UIDevice.current - device.isBatteryMonitoringEnabled = true - if device.batteryState == UIDevice.BatteryState.unknown { - result(FlutterError(code: "UNAVAILABLE", - message: "Battery level not available.", - details: nil)) - } else { - result(Int(device.batteryLevel * 100)) - } - } } /// StepDetector, handles pedestrian status streaming