How to Access the Accelerometer from the Apple Watch

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

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.

Can iPhone retrieve accelerometer data from the Watch without a Watch App?

To access the Apple Watch sensors, you will need an Apple Watch app
And due to limited background code execution capability on the Apple Watch, the user will have to explicitly interact with the app to start the sensors.

Basically, an iPhone app can access only it's own sensors. Same goes for an Apple Watch app.

There's no way right now in CMMotionManager to tap into another device's sensors.


The reason heart rate is available is because of HealthKit.
That data is sent to the iPhone periodically.

You are basically communicating with HealthKit to get the latest heart rate, which... it recently got from the Apple Watch.

As for sensors, there's no central store that keeps the motion data of both devices.

Reasons probably being:

  • It doesn't make much sense to store this data
  • It's just too much data to sync!
  • Motion is something that is expected for extreme responsiveness so the sensor data from Apple Watch would have to be sent to the iPhone, in worst case, atleast every second. That would be a massive battery hog!

Now this part is a hypothetical solution but sorry, it's not possible.

Your next challenge would be to create a placeholder Apple Watch App Extension whose sole task will be to receive a message from the iPhone, via WatchConnectivity, to start it's sensors to get data, and send the samples back to the iPhone via WatchConnectivity again.

Sadly, Apple Watch Apps are really limited when it comes to executing code in the background so nope.

Get accelerometer and gyroscope data from apple watch (and not the iphone)?

The problem was that I wasn't running watchOS2. I assumed I was but it's still in beta and I hadn't installed it. The data I was getting was accelerometer data from the phone. Also, currently, you can only get acc data from the watch using watchOS2 and not gyro data.

How to work with accelerometer data from the Apple Watch?

I found that using a CMMotionManager on the watch works just as good as it does on iPhone. This way you can implement startAccelerometerUpdates in awake and receive real time feed back on watch positions for X Y Z coordinates so that you can get a better grasp of the data;

var motionManager = CMMotionManager() 

override func awake(withContext context: Any?) {
super.awake(withContext: context)

manager.accelerometerUpdateInterval = 0.2

manager.startAccelerometerUpdates(to: OperationQueue.current!) { (data, error) in
if let myData = data {

print("x: \(myData.acceleration.x) y: \(myData.acceleration.y) z: \(myData.acceleration.z)")
if myData.acceleration.x > 1.5 && myData.acceleration.y > 1.5 {

}
}
}

}

Collecting Apple Watch accelerometer data in background

That is possible, but rather complex since watchOS3 apps are considered mostly foreground.

  1. Once your watchapp will request CoreMotion sensor to record accelerometer data, CMSensorRecorder().recordAccelerometer(forDuration: ...), this data will be recordered by sensor on it's own without any additional impact form your app and stored in system for 3 days.
  2. When your app will have execution time (opened in foreground by user or given background refresh task), you can get data by CMSensorRecorder().accelerometerData(from: startDate, to: endDate) and process it.

The most difficult thing is to get enough execution time to process and send huge amount of data that will be recordered. Depending on complexity of your analyze methods, you can face problem that OS does not give enough execution time in background refresh task and OS watchdog terminates the process. On the other side, when your app is foreground, it is active only while screen is on and goes to suspended state once screen is off.

If your app is in Dock, it will receive up to 1 background refresh task per hour. If your app will have complication on active watchface, you can get up to 2 background refresh tasks per hour. This number depends on battery state, on how you've been using your previous tasks and on how much tasks are currently used by other apps.

Except background refresh tasks, watchapp can work in background only during workout. In this case duration is not limited, but you can not use more than 15% CPU.

Bottomline:
* you can schedule background refresh tasks once an hour
* once background task starts, start workout so app will be active in background
* return task back to OS asap once workout successfully started
* request accelerometer data from sensor
* process it minding CPU usage
* send to wherever you want and finish workout

WWDC16 sessions to check: 227, 235, 218



Related Topics



Leave a reply



Submit