2024-08-22 14:35:09 +09:00
|
|
|
|
import Flutter
|
|
|
|
|
|
import UIKit
|
2024-09-03 22:17:09 +09:00
|
|
|
|
import CoreMotion
|
2024-08-22 14:35:09 +09:00
|
|
|
|
|
|
|
|
|
|
@main
|
|
|
|
|
|
@objc class AppDelegate: FlutterAppDelegate {
|
2024-09-03 22:17:09 +09:00
|
|
|
|
private let motionManager = CMMotionManager()
|
|
|
|
|
|
|
2024-08-22 14:35:09 +09:00
|
|
|
|
override func application(
|
|
|
|
|
|
_ application: UIApplication,
|
|
|
|
|
|
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
|
|
|
|
|
|
) -> Bool {
|
2024-09-03 22:17:09 +09:00
|
|
|
|
//GeneratedPluginRegistrant.register(with: self)
|
|
|
|
|
|
//return super.application(application, didFinishLaunchingWithOptions: launchOptions)
|
2024-09-08 18:16:51 +09:00
|
|
|
|
|
2024-09-03 22:17:09 +09:00
|
|
|
|
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
|
2024-09-08 18:16:51 +09:00
|
|
|
|
let motionChannel = FlutterMethodChannel(name: "net.sumasen.gifunavi/motion",
|
2024-09-03 22:17:09 +09:00
|
|
|
|
binaryMessenger: controller.binaryMessenger)
|
|
|
|
|
|
motionChannel.setMethodCallHandler({
|
|
|
|
|
|
[weak self] (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
|
|
|
|
|
|
guard let self = self else { return }
|
|
|
|
|
|
|
|
|
|
|
|
switch call.method {
|
|
|
|
|
|
case "startMotionUpdates":
|
|
|
|
|
|
self.startMotionUpdates(result: result)
|
|
|
|
|
|
case "stopMotionUpdates":
|
|
|
|
|
|
self.stopMotionUpdates(result: result)
|
|
|
|
|
|
default:
|
|
|
|
|
|
result(FlutterMethodNotImplemented)
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2024-08-22 14:35:09 +09:00
|
|
|
|
GeneratedPluginRegistrant.register(with: self)
|
|
|
|
|
|
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
|
|
|
|
|
|
}
|
2024-09-03 22:17:09 +09:00
|
|
|
|
|
2024-09-08 18:16:51 +09:00
|
|
|
|
// この実装では以下のことを行っています:
|
|
|
|
|
|
// デバイスのモーションデータ(姿勢、重力、ユーザー加速度)を取得します。
|
|
|
|
|
|
// 取得したデータを辞書形式にまとめます。
|
|
|
|
|
|
// sendMotionDataメソッドを使って、Flutterアプリにデータを送信します。
|
|
|
|
|
|
|
2024-09-03 22:17:09 +09:00
|
|
|
|
private func startMotionUpdates(result: @escaping FlutterResult) {
|
|
|
|
|
|
if motionManager.isDeviceMotionAvailable {
|
|
|
|
|
|
motionManager.deviceMotionUpdateInterval = 0.1
|
2024-09-08 18:16:51 +09:00
|
|
|
|
motionManager.startDeviceMotionUpdates(to: .main) { [weak self] (motion, error) in
|
|
|
|
|
|
guard let self = self else { return }
|
|
|
|
|
|
|
|
|
|
|
|
if let error = error {
|
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
|
result(FlutterError(code: "MOTION_ERROR",
|
|
|
|
|
|
message: error.localizedDescription,
|
|
|
|
|
|
details: nil))
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
guard let motion = motion else {
|
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
|
result(FlutterError(code: "NO_MOTION_DATA",
|
|
|
|
|
|
message: "No motion data available",
|
|
|
|
|
|
details: nil))
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
|
let motionData: [String: Any] = [
|
|
|
|
|
|
"attitude": [
|
|
|
|
|
|
"roll": motion.attitude.roll,
|
|
|
|
|
|
"pitch": motion.attitude.pitch,
|
|
|
|
|
|
"yaw": motion.attitude.yaw
|
|
|
|
|
|
],
|
|
|
|
|
|
"gravity": [
|
|
|
|
|
|
"x": motion.gravity.x,
|
|
|
|
|
|
"y": motion.gravity.y,
|
|
|
|
|
|
"z": motion.gravity.z
|
|
|
|
|
|
],
|
|
|
|
|
|
"userAcceleration": [
|
|
|
|
|
|
"x": motion.userAcceleration.x,
|
|
|
|
|
|
"y": motion.userAcceleration.y,
|
|
|
|
|
|
"z": motion.userAcceleration.z
|
|
|
|
|
|
]
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
self.sendMotionData(motionData)
|
|
|
|
|
|
}
|
2024-09-03 22:17:09 +09:00
|
|
|
|
}
|
2024-09-08 18:16:51 +09:00
|
|
|
|
result(nil) // 初期化成功を示す
|
2024-09-03 22:17:09 +09:00
|
|
|
|
} else {
|
|
|
|
|
|
result(FlutterError(code: "UNAVAILABLE",
|
|
|
|
|
|
message: "Device motion is not available.",
|
|
|
|
|
|
details: nil))
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-08 18:16:51 +09:00
|
|
|
|
private func sendMotionData(_ data: [String: Any]) {
|
|
|
|
|
|
let motionChannel = FlutterMethodChannel(name: "net.sumasen.gifunavi/motion",
|
|
|
|
|
|
binaryMessenger: (window?.rootViewController as! FlutterViewController).binaryMessenger)
|
|
|
|
|
|
motionChannel.invokeMethod("onMotionData", arguments: data)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-03 22:17:09 +09:00
|
|
|
|
private func stopMotionUpdates(result: @escaping FlutterResult) {
|
|
|
|
|
|
motionManager.stopDeviceMotionUpdates()
|
|
|
|
|
|
result(nil)
|
|
|
|
|
|
}
|
2024-08-22 14:35:09 +09:00
|
|
|
|
}
|