It doesn't look like WatchKit released today has such API included.
问题:
回答1:
No. Direct access to the Apple Watch sensors (which include the accelerometer) is not possible.
As always, if this is something you'd like, please file a request for it at https://bugreport.apple.com.
回答2:
Sensor Data information is now available in Watchkit for watchOS 2.0
.
You could check this information in the following session which is total 30 minutes presentation.If you do not want to watch entire session, then you directly jump to the CoreMotion
and HealthKit
features which is in between 22-28 min:
WatchKit for watchOS 2.0 Session in WWDC 2015
Heart Rate Implementation
https://developer.apple.com/documentation/healthkit/hkworkout
Accelerometer Implementation
Here is the implementation of accelerometer in WatchKit Extension, Here is the reference:
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()
}
}
回答3:
Update for watchOS 4 & iOS 11: Gyroscope data (rotation rate) is now also available and all of the sensor data from the watch can be accessed via the updated CoreMotion interface.
More specifically CMDeviceMotion gets you:
- attitude & rotation rate
- gravity & user acceleration
- calibrated magnetic field
- ...
Implementation of the accelerometer with 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()
}
}
Notes on the implementation above:
While this method will get you from A to B in terms of getting some real time data from the Apple Watch, a much nicer and definitely more production ready version awaits in this official Apple tutorial, which explains how to separate the sensor logic from the InterfaceController in a separate model etc. - extremely useful, in my opinion.
回答4:
We will get it most likely next year, when Apple will allow us to build full applications. Until now it's only UI, Glances and Notifications.
Update: Apple has provided developer APIs for it now. Check casillas's answer.