19

It doesn't look like WatchKit released today has such API included.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
mobileideafactory
  • 1,640
  • 1
  • 22
  • 30

4 Answers4

27

Sensor Data 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 Implementation

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

Accelerometer Implementation

Here is the implementation of accelerometer in WatchKit Extension, Here is the 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()
    }
}

Code for 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()
    }
    
}
DeyaEldeen
  • 10,847
  • 10
  • 42
  • 75
casillas
  • 16,351
  • 19
  • 115
  • 215
  • 2
    Thanks to quote my code: https://github.com/shu223/watchOS-2-Sampler/blob/master/watchOS2Sampler%20WatchKit%20Extension/AccelerometerInterfaceController.swift I would be glad if you introduce this link as a reference :) – shu223 Aug 13 '15 at 10:59
  • No, We have tried this code with watch OS 2.1 but this code is displaying Accelerometer data of paired iPhone. When you move watch it is not reflecting or changing any data. But if you move Phone then it will start reflecting. – Nikh1414 Dec 21 '15 at 12:38
  • I tried the accelerometer code and it works perfectly (shows data of apple watch accelerometer)! – Osmani Apr 06 '16 at 03:17
  • You must use Error instead NSError. – Jhonatan S. Souza May 23 '20 at 19:06
9

No. Direct access to the Apple Watch sensors (which include the accelerometer) is not possible.

As always, if this is something you'd like, please file a request for it at https://bugreport.apple.com.

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
7

Update for watchOS 4 & iOS 11: Gyroscope data (rotation rate) is now also available and all of the sensor data from the watch can be accessed via the updated CoreMotion interface.

More specifically CMDeviceMotion gets you:

  • attitude & rotation rate
  • gravity & user acceleration
  • calibrated magnetic field
  • ...

Implementation of the accelerometer with 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()
}
}

Notes on the implementation above:

While this method will get you from A to B in terms of getting some real time data from the Apple Watch, a much nicer and definitely more production ready version awaits in this official Apple tutorial, which explains how to separate the sensor logic from the InterfaceController in a separate model etc. - extremely useful, in my opinion.

tech4242
  • 2,348
  • 2
  • 23
  • 33
  • 1
    @Victor'Chris'Cabral sadly it only works in workout mode : / def. limits the possible use cases (which I found out the hard way) – tech4242 May 27 '18 at 14:49
2

We will get it most likely next year, when Apple will allow us to build full applications. Until now it's only UI, Glances and Notifications.

Update: Apple has provided developer APIs for it now. Check casillas's answer.

Sebyddd
  • 4,305
  • 2
  • 39
  • 43