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

mob*_*com 18 accelerometer ios watchkit

它看起来并不像今天发布的WatchKit包含这样的API.

cas*_*las 25

传感器数据信息现已可用于Watchkit for watchOS 2.0.

你可以查看下面的会议是总30分钟presentation.If你不想看整个会话信息,那么您可以直接跳转到CoreMotionHealthKit这是在22-28分钟之间的特点:

WatchKit for WWOS 2015中的watchOS 2.0 Session

心率实施

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

加速度计实施

以下是WatchKit Extension中加速度计的实现,以下是参考:

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()
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 感谢引用我的代码:https://github.com/shu223/watchOS-2-Sampler/blob/master/watchOS2Sampler%20WatchKit%20Extension/AccelerometerInterfaceController.swift如果你引入这个链接作为参考我会很高兴:) (2认同)

Dav*_*ong 9

不可以直接访问Apple Watch传感器(包括加速度计).

与往常一样,如果这是您想要的,请通过https://bugreport.apple.com提交申请.

  • 这已经过时了,请参阅casillas对WatchOS 2更新的其他答案 (7认同)

tec*_*242 5

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()
}
}
Run Code Online (Sandbox Code Playgroud)

关于上述实现的注意事项:

虽然从Apple Watch获得一些实时数据方面,这种方法可以使您从A到B,但是在此官方Apple教程中,还有一个更好,而且肯定可以生产的版本,它解释了如何将传感器逻辑与InterfaceController分开。我认为这非常有用。