Deepak
Deepak

Reputation: 41

Is there a WatchKit sensor API?

Is there any API available for Apple Watch Kit sensors such as the accelerometer, heart rate monitor, haptic sensor ?

How can I access those sensors ?

Upvotes: 3

Views: 1145

Answers (2)

casillas
casillas

Reputation: 16793

Sensor Data (accelerometer, heart rate monitor, haptic sensor) information is now available in Watchkit for watchOS 2.0.

You could check this information in the following session which is total 30 minutes presentation.If you do not want to watch entire session, then you directly jump to the CoreMotion and HealthKit features which is in between 22-28 min:

WatchKit for watchOS 2.0 Session in WWDC 2015

Heart Rate Code

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

Accelerometer code

Here is the implementation of Accelerometer in WatchKit Extension, I have added three labels (LabelX, LabelY, and LabelZ) on watch storyboard.

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

Upvotes: 5

BalestraPatrick
BalestraPatrick

Reputation: 10144

There is no current available option to access any sensor of the Apple Watch with the current version of the WatchKit SDK.

Upvotes: 1

Related Questions