我正在开发一个应用程序,它需要加速度计、陀螺仪和计步器数据以及心率。我正在将这些数据从iwatch传输到iPhone,然后从iPhone,我需要通过MQTT协议同步这些数据。现在我的问题是,一旦iwatch窗口关闭我的应用程序。我正在使用核心运动和现场锻炼会议。有人能帮助我如何保持iwatch应用程序活动或从非活动模式传输上述数据吗?
发布于 2022-09-06 09:12:00
以下是我的解决方案,可以帮助您:
HKWorkoutSession和CoreMotion listenerimport WatchKit
import Foundation
import CoreMotion
import HealthKit
enum VelocityVector: Int {
case x, y, z
}
class InterfaceController: WKInterfaceController {
@IBOutlet weak var labelVelocity: WKInterfaceLabel!
let coreMotion = CMMotionManager.init()
let pool = OperationQueue.init()
let currentSession: HKWorkoutSession?
let healthKit = HKHealthStore()
override func awake(withContext context: Any?) {
coreMotion.accelerometerUpdateInterval = 0.1
coreMotion.startAccelerometerUpdates(to: pool) { data, err in
guard let _data = data else { return }
DispatchQueue.main.async {
self.labelVelocity.setText(String.init(format: "G-Force (x:y:z) %.3f:%.3f:%.3f", arguments: [_data.acceleration.x, _data.acceleration.y, _data.acceleration.z]))
}
}
let config = HKWorkoutConfiguration.init()
config.activityType = .other
config.locationType = .unknown
do {
self.currentSession = try HKWorkoutSession.init(healthStore: self.healthKit, configuration: config)
self.currentSession?.startActivity(with: Date())
} catch error {
print(error?.localizedDescription)
}
}
private func stopHKWorkoutSession() {
self.currentSession?.stopActivity(with: Date())
self.currentSession?.end()
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
}
}
https://stackoverflow.com/questions/73530229
复制相似问题