有没有办法从Apple Watch访问加速计?

19

看起来今天发布的WatchKit中没有包含这样的API。

4个回答

27

传感器数据信息现在可在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()
    }
    
}

2
感谢您引用我的代码:https://github.com/shu223/watchOS-2-Sampler/blob/master/watchOS2Sampler%20WatchKit%20Extension/AccelerometerInterfaceController.swift 如果您能将此链接作为参考介绍给其他人,我将不胜感激 :) - shu223
不,我们已经尝试过在watch OS 2.1上运行此代码,但是该代码仅显示配对iPhone的加速度计数据。当您移动手表时,它不会反映或更改任何数据。但是如果您移动手机,则它将开始反映。 - Nikh1414
我尝试了加速度计代码,它完美地工作了(显示了Apple Watch加速度计的数据)! - Osmani
你必须使用 Error 而不是 NSError。 - Jhonatan S. Souza

9

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

如果您需要此功能,请在https://bugreport.apple.com上提交请求。


8
现在这个已经过时了,请查看casillas提供的其他答案以获取关于WatchOS 2更新的信息。 - woody121

7

针对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分离到一个单独的模型中等- 在我看来非常有用。


1
@Victor'Chris'Cabral 很遗憾,它只在锻炼模式下起作用:/ 这限制了可能的使用情况(我是通过艰难的方式发现的)。 - tech4242

2

我们很可能会在明年获得这个功能,届时苹果将允许我们构建完整的应用程序。目前只有UI、Glances和通知。

更新: 现在苹果已经为开发者提供了API。请查看 casillas 的回答。


2
现在可用,我已经分享了下面的代码和资源。 - casillas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接