看起来今天发布的WatchKit中没有包含这样的API。
传感器数据信息现在可在watchOS 2.0的Watchkit中获取。
您可以在以下会议中查看此信息,该会议总共持续30分钟。如果您不想观看整个会议,则可以直接跳转到22-28分钟之间的CoreMotion和HealthKit功能:
WWDC 2015中的watchOS 2.0 WatchKit会议
心率实现
https://developer.apple.com/documentation/healthkit/hkworkout
加速计实现
以下是WatchKit扩展中加速计的实现方法,这里是参考链接:
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()
}
}
适用于 WatchOS 7.x 的代码
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 awake(withContext context: Any?) {
super.awake(withContext: context)
motionManager.accelerometerUpdateInterval = 0.1
}
override func willActivate() {
super.willActivate()
if (motionManager.isAccelerometerAvailable == true) {
let handler:CMAccelerometerHandler = {data,error 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.startAccelerometerUpdates(to: OperationQueue.current!, 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()
}
}
无法直接访问Apple Watch传感器(包括加速度计)。
如果您需要此功能,请在https://bugreport.apple.com上提交请求。
针对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()
}
}
上面所述实现的注意事项:
虽然这种方法能够让你从Apple Watch获取一些实时数据,但一个更好且肯定更适合生产的版本在这个官方Apple教程中等待着你。该教程将解释如何将传感器逻辑与InterfaceController分离到一个单独的模型中等- 在我看来非常有用。
我们很可能会在明年获得这个功能,届时苹果将允许我们构建完整的应用程序。目前只有UI、Glances和通知。
更新: 现在苹果已经为开发者提供了API。请查看 casillas 的回答。