【问题标题】:Is there any way to access the accelerometer from the Apple Watch?有什么方法可以从 Apple Watch 访问加速度计?
【发布时间】:2015-01-16 03:34:09
【问题描述】:

今天发布的 WatchKit 好像没有包含这样的 API。

【问题讨论】:

    标签: ios watchkit accelerometer


    【解决方案1】:

    传感器数据信息现已在Watchkit for watchOS 2.0 中提供

    您可以在接下来的30分钟演示中查看此信息。如果您不想观看整个会话,则直接跳转到CoreMotionHealthKit功能,在22-28之间分钟:

    WatchKit for watchOS 2.0 Session in WWDC 2015

    心率实施

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

    加速度计实施

    这里是WatchKit Extension中加速度计的实现,这里是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()
        }
    }
    

    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()
        }
        
    }
    

    【讨论】:

    • 感谢引用我的代码:github.com/shu223/watchOS-2-Sampler/blob/master/… 如果您将此链接作为参考,我会很高兴:)
    • 否,我们已经在 watch OS 2.1 上尝试过此代码,但此代码显示已配对 iPhone 的加速度计数据。当您移动手表时,它不会反映或更改任何数据。但是如果你移动手机,它就会开始反射。
    • 我试过加速度计代码,它工作得很好(显示苹果手表加速度计的数据)!
    • 你必须使用 Error 而不是 NSError。
    【解决方案2】:

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

    更具体地说,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 获取一些实时数据,但在此 official Apple tutorial 中等待着一个更好且肯定更适合生产的版本,它解释了如何分离传感器逻辑来自单独模型中的 InterfaceController 等 - 在我看来非常有用。

    【讨论】:

    • @Victor'Chris'Cabral 遗憾的是它只适用于锻炼模式:/def。限制可能的用例(我发现很难)
    【解决方案3】:

    我们很可能会在明年获得它,届时 Apple 将允许我们构建完整的应用程序。到目前为止,它只是 UI、Glances 和 Notifications。

    更新:Apple 现已为其提供开发者 API。检查卡西利亚斯的答案。

    【讨论】:

    • 现在可以使用了,下面我分享了代码和资源。
    【解决方案4】:

    没有。无法直接访问 Apple Watch 传感器(包括加速度计)。

    与往常一样,如果您需要此功能,请通过https://bugreport.apple.com 提出请求。

    【讨论】:

    • 这已经过时了,请参阅 casillas 对 WatchOS 2 更新的其他回答
    猜你喜欢
    • 1970-01-01
    • 2015-08-25
    • 2011-10-22
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    • 1970-01-01
    相关资源
    最近更新 更多