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..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 @@ -1,22 +1,40 @@ 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.embedding.engine.plugins.FlutterPlugin.FlutterPluginBinding 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 + private lateinit var methodChannel : MethodChannel; + private lateinit var sensorManager : SensorManager; - override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { + 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: 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) @@ -24,11 +42,18 @@ class PedometerPlugin : FlutterPlugin { /// Set handlers stepDetectionChannel.setStreamHandler(stepDetectionHandler) stepCountChannel.setStreamHandler(stepCountHandler) + + // setup method channel + val context = flutterPluginBinding.applicationContext + sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager + methodChannel = MethodChannel(flutterPluginBinding.binaryMessenger, "com.example.pedometer") + methodChannel.setMethodCallHandler(this) } override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) { stepDetectionChannel.setStreamHandler(null) stepCountChannel.setStreamHandler(null) + methodChannel.setMethodCallHandler(null) } } diff --git a/packages/pedometer/ios/Classes/SwiftPedometerPlugin.swift b/packages/pedometer/ios/Classes/SwiftPedometerPlugin.swift index b13c62af9..b95ac0c6a 100644 --- a/packages/pedometer/ios/Classes/SwiftPedometerPlugin.swift +++ b/packages/pedometer/ios/Classes/SwiftPedometerPlugin.swift @@ -14,6 +14,24 @@ 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: registrar.messenger()) + methodChannel.setMethodCallHandler({ + (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()) + } + }) } } 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) {