Files
rog_app/lib/services/motion_service.dart
2024-09-08 18:16:51 +09:00

42 lines
1.3 KiB
Dart

import 'package:flutter/services.dart';
import 'dart:io';
class MotionService {
static const MethodChannel _channel = MethodChannel('net.sumasen.gifunavi/motion');
static Future<void> startMotionUpdates() async {
if (Platform.isIOS) {
try {
await _channel.invokeMethod('startMotionUpdates');
} on PlatformException catch (e) {
print("Failed to start motion updates: '${e.message}'.");
}
} else{
// Android の場合は何もしない、またはAndroid向けの代替実装を行う
print("Motion updates not supported on this platform");
}
}
Future<dynamic> _handleMotionData(MethodCall call) async {
switch (call.method) {
case 'onMotionData':
final Map<String, dynamic> motionData = call.arguments;
// ここでモーションデータを処理します
print('Received motion data: $motionData');
// 例: データを状態管理システムに渡す、UIを更新する等
break;
default:
print('Unknown method ${call.method}');
}
}
static Future<void> stopMotionUpdates() async {
if (Platform.isIOS) {
try {
await _channel.invokeMethod('stopMotionUpdates');
} on PlatformException catch (e) {
print("Failed to stop motion updates: '${e.message}'.");
}
}
}
}