Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to access the accelerometer from the Apple Watch?

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

like image 909
mobileideafactory Avatar asked Nov 18 '14 19:11

mobileideafactory


People also ask

How do you use an accelerometer on Apple Watch?

Open the app on your Apple Watch and tap Outdoor Walk. The Activity app relies on arm motion and an accelerometer to track movement, but the Workout app can use the accelerometer, the heart rate sensor, and GPS.

Does Apple Watch have accelerometers?

The hardwareThe watch has an accelerometer, a gyroscope and optical heart-rate sensors, which measure your heart rate from your wrist.

Does a Apple Watch 7 have accelerometer?

This safety feature makes use of an improved three-axis gyroscope and a completely new accelerometer to record 3,000 samples per second. Combined with the Apple Watch's other sensors, the new hardware is designed to allow the device to almost instantly detect vehicle crashes.

How many accelerometers are in an Apple Watch?

The Apple watch contains 3-axis accelerometers, 3-axis gyroscopes, and 3-axis magnetometers.


3 Answers

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()
    }
    
}
like image 65
casillas Avatar answered Oct 19 '22 07:10

casillas


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.

like image 34
Dave DeLong Avatar answered Oct 19 '22 07:10

Dave DeLong


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.

like image 43
tech4242 Avatar answered Oct 19 '22 05:10

tech4242