问题
I'm trying to get access to the gyroscope of the apple watch. From what I read it is available in watchos 3. Unfortunately I cannot get it to work. It keeps coming back with "Gyro not available" so motionManager.isGyroAvailable is always false. Here is my code. Any help would be appreciated.
import WatchKit
import Foundation
import CoreMotion
class InterfaceController: WKInterfaceController {
let motionManager = CMMotionManager()
override func awake(withContext context: Any?) {
super.awake(withContext: context)
motionManager.gyroUpdateInterval = 0.1
motionManager.accelerometerUpdateInterval = 0.1
// Configure interface objects here.
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
if (motionManager.isGyroAvailable == true) {
motionManager.startGyroUpdates(to: OperationQueue.current!, withHandler: { (data, error) -> Void in
guard let data = data else { return }
let rotationX = data.rotationRate.x
let rotationY = data.rotationRate.y
let rotationZ = data.rotationRate.z
// do you want to want to do with the data
print(rotationX)
print(rotationY)
print(rotationZ)
})
} else {
print("Gyro not available")
}
回答1:
From my experience (although I can't find it documented anywhere) raw gyroscope data isn't available on the watch, only the processed data. You can access the processed data using the CMMotionManager method:
startDeviceMotionUpdates(to queue: OperationQueue, withHandler handler: @escaping CMDeviceMotionHandler)
The CMDeviceMotion
object in the handler has detailed rotation data, for instance the rotation rate, the documentation for this states that it's processed data from the gyroscope. There is also attitude data.
来源:https://stackoverflow.com/questions/42650961/coremotion-gyroscope-apple-watch