【问题标题】:Is there a WatchKit sensor API?是否有 WatchKit 传感器 API?
【发布时间】:2015-02-25 10:40:30
【问题描述】:

是否有任何 API 可用于 Apple Watch Kit 传感器,例如加速度计、心率监测器、触觉传感器?

如何访问这些传感器?

【问题讨论】:

    标签: iphone watchkit apple-watch


    【解决方案1】:

    传感器数据(加速度计、心率监测器、触觉传感器)信息现已在Watchkit for watchOS 2.0 中提供

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

    WatchKit for watchOS 2.0 Session in WWDC 2015

    心率代码

    https://developer.apple.com/library/prerelease/watchos/documentation/HealthKit/Reference/HKWorkout_Class/

    加速度计代码

    这是 Accelerometer 在 WatchKit Extension 中的实现,我在手表故事板上添加了三个标签(LabelXLabelYLabelZ)。

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

    【讨论】:

      【解决方案2】:

      当前没有可用的选项来使用当前版本的 WatchKit SDK 访问 Apple Watch 的任何传感器。

      【讨论】:

        猜你喜欢
        • 2012-01-12
        • 1970-01-01
        • 1970-01-01
        • 2014-11-17
        • 1970-01-01
        • 2017-09-22
        • 1970-01-01
        • 1970-01-01
        • 2015-05-08
        相关资源
        最近更新 更多