有没有办法从Apple Watch访问加速度计?

它看起来不像今天发布的WatchKit包含这样的API。

不可以。直接访问Apple Watch传感器(包括加速计)是不可能的。

一如既往,如果这是你想要的东西,请在https://bugreport.apple.com上提出请求。

传感器数据信息现已Watchkit for watchOS 2.0

您可以在下面的会话中查看这个信息,这是总共30分钟的介绍。如果您不想看整个会话,那么您直接跳到22-28分钟之间的CoreMotionHealthKitfunction:

在WWDC 2015的watchOS 2.0会议上的WatchKit

心率执行

https://developer.apple.com/documentation/healthkit/hkworkout

加速计实现

以下是WatchKit Extension中的加速度计的实现,下面是参考:

 import WatchKit import Foundation import CoreMotion class InterfaceController: WKInterfaceController { @IBOutlet weak var labelX: WKInterfaceLabel! @IBOutlet weak var labelY: WKInterfaceLabel! @IBOutlet weak var labelZ: WKInterfaceLabel! let motionManager = CMMotionManager() override func awakeWithContext(context: AnyObject?) { super.awakeWithContext(context) motionManager.accelerometerUpdateInterval = 0.1 } override func willActivate() { super.willActivate() if (motionManager.accelerometerAvailable == true) { let handler:CMAccelerometerHandler = {(data: CMAccelerometerData?, error: NSError?) -> Void in self.labelX.setText(String(format: "%.2f", data!.acceleration.x)) self.labelY.setText(String(format: "%.2f", data!.acceleration.y)) self.labelZ.setText(String(format: "%.2f", data!.acceleration.z)) } motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.currentQueue()!, withHandler: handler) } else { self.labelX.setText("not available") self.labelY.setText("not available") self.labelZ.setText("not available") } } override func didDeactivate() { super.didDeactivate() motionManager.stopAccelerometerUpdates() } } 

明年,苹果将允许我们构build完整的应用程序。 到目前为止,这只是UI,浏览和通知。

更新 :苹果现在已经为它提供了开发者API。 检查卡西利亚斯的答案。

watchOS 4和iOS 11的更新:陀螺仪数据(旋转速率)现在也可用,手表的所有传感器数据都可以通过更新的CoreMotion界面访问。

更具体的CMDeviceMotion让你:

  • 态度和旋转率
  • 引力和用户加速
  • 校准的磁场

使用CMDeviceMotion实现加速度计:

 class InterfaceController: WKInterfaceController { let motionManager = CMMotionManager() override func awake(withContext context: Any?) { super.awake(withContext: context) motionManager.deviceMotionUpdateInterval = 0.1 } override func willActivate() { super.willActivate() if motionManager.isDeviceMotionAvailable { let coreMotionHandler : CMDeviceMotionHandler = {(data: CMDeviceMotion?, error: Error?) -> Void in // do something with data!.userAcceleration // data!. can be used to access all the other properties mentioned above. Have a look in Xcode for the suggested variables or follow the link to CMDeviceMotion I have provided } motionManager.startDeviceMotionUpdates(to: OperationQueue.current!, withHandler: coreMotionHandler) } else { //notify user that no data is available } } override func didDeactivate() { super.didDeactivate() motionManager.stopDeviceMotionUpdates() } } 

上面的实施注意事项:

虽然这种方法可以让你从A到B从Apple Watch获得一些实时数据,但是在这个官方的Apple教程中有一个更好,更准确的产品版本,它解释了如何将传感器逻辑从InterfaceController在一个单独的模型等 – 非常有用,在我看来。