Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
add methods to check if sensors are available
  • Loading branch information
Codel1417 committed Jan 25, 2025
commit 9509e13d09bdb36d3cd016c2d66c78dda14d6451
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
29 changes: 29 additions & 0 deletions packages/pedometer/ios/Classes/SwiftPedometerPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}

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