Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,34 +1,59 @@
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)

/// 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)
}

}
18 changes: 18 additions & 0 deletions packages/pedometer/ios/Classes/SwiftPedometerPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
})
}
}

Expand Down
11 changes: 9 additions & 2 deletions packages/pedometer/lib/pedometer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<PedestrianStatus> _androidPedestrianController =
StreamController.broadcast();

Expand All @@ -23,7 +23,14 @@ class Pedometer {
if (Platform.isAndroid) return _androidStream(stream);
return stream;
}

static Future<bool?> get isStepDetectionSupported {
Future<bool?> result = _platform.invokeMethod<bool>("isStepDetectionSupported");
return result;
}
static Future<bool?> get isStepCountSupported {
Future<bool?> result = _platform.invokeMethod<bool>("isStepCountSupported");
return result;
}
/// Transformed stream for the Android platform
static Stream<PedestrianStatus> _androidStream(
Stream<PedestrianStatus> stream) {
Expand Down