0

I want the realtime accelerometer and gyro data from the watch to be pushed to the corresponding iOS app in order to process the data as and when received. What are the different ways i can achieve this?

2 Answers2

2

1. Sensor Data

You would require CoreMotion to access the accelerometer & device motion data.

import CoreMotion    

Accelerometer

let motionManager = CMMotionManager()

if motionManager.isAccelerometerAvailable {
    motionManager.accelerometerUpdateInterval = 1

    motionManager.startAccelerometerUpdates(to: OperationQueue.current!, withHandler: { (data, error) in
        if let data = data {
            let x = data.acceleration.x
            let y = data.acceleration.y
            let z = data.acceleration.z

            print("x:\(x) y:\(y) z:\(z)")
        }
    })
}

MotionData

Gyroscope sensor alone is not available on Apple Watch but Motion Data is with more information.

let motionManager = CMMotionManager()

if motionManager.isDeviceMotionAvailable {      
    motionManager.deviceMotionUpdateInterval = 1

    motionManager.startDeviceMotionUpdates(to: OperationQueue.current!, withHandler: { (data, error) in
        /*
         data has many properties like: attitude, gravity, heading etc.
         explore, use what you need
        */
    })
}

Good Read: https://heartbeat.fritz.ai/introduction-to-apple-watchkit-with-core-motion-tracking-jumping-jacks-259ee80d1210


2. WatchConnectivity

For sending the information from Apple Watch app to iPhone app counterpart, you would require WatchConnectivity.

Good Tutorial: https://www.natashatherobot.com/watchconnectivity-say-hello-to-wcsession/

In a very rough manner, it would be something like:

import WatchConnectivity

//this needs to be done just once (on Apple Watch as well as iPhone)
func prepareForWatchConnectivity() {
    if (WCSession.isSupported()) {
        let session = WCSession.default
        session.delegate = self //requires `WCSessionDelegate` protocol, so implement the required delegates as well
        session.activate()
    }
}

Then you can send messages from Apple Watch to iPhone App simply by:

//Example: Sending Accelerometer Data from Apple Watch
WCSession.default.sendMessage(["x":x,
                               "y":y,
                               "z":z],
                              replyHandler: nil)

On the iPhone you do the same thing to set up WatchConnectivity but here your delegate should handle the message in:

func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
    //message received will be ["x":<value>,"y":<value>,"z":<value>] as sent from Apple Watch
}

The above WatchConnectivity example is rough and just for giving a general idea. It's dirty and can be structured and improved greatly.

staticVoidMan
  • 19,275
  • 6
  • 69
  • 98
  • when i try this, there is a lag in the value sent. Also if the watch screen is dimmed , this doesn't work in background mode. – Prabhakar Patil Feb 22 '18 at 13:15
  • is there a way by which i can access the motion data from the HealthKit data. By motion data i mean accelerometer and not steps. – Prabhakar Patil Feb 22 '18 at 13:16
  • @PrabhakarPatil lag is a possibility. tag the data with a timestamp. – staticVoidMan Feb 22 '18 at 13:18
  • @PrabhakarPatil if watch screen is dimmed, the app will stop running it's code. that's just how apple watch is unless you enable background modes. that too this has limitations. – staticVoidMan Feb 22 '18 at 13:19
  • 1
    @PrabhakarPatil HealthKit does not provide motion sensor data. It's designed only to read/write health data. – staticVoidMan Feb 22 '18 at 13:20
  • is there a way by which i can simultaneously share the accelerometer data from the watch to phone using Bluetooth. – Prabhakar Patil Feb 22 '18 at 13:52
  • @PrabhakarPatil I don't think you really have much choice here. Take a look here: https://forums.developer.apple.com/thread/19399 – staticVoidMan Feb 22 '18 at 16:59
  • @PrabhakarPatil You could ping that data to some server while another app is connected to this server by socket getting the live data? I dunno. Crazy, but `WatchConnectivity` is the fastest option you have right now. – staticVoidMan Feb 22 '18 at 17:00
  • also if watch screen is dimmed, the app will stop running it's code. that's just how apple watch is. ref: https://stackoverflow.com/a/32796823/2857130 – staticVoidMan Feb 22 '18 at 17:00
0

WatchConnectivity is the best way to share data. To avoid screen dimming, you should use background task with HKWorkoutSession. see: https://developer.apple.com/library/archive/samplecode/SwingWatch/Listings/SwingWatch_WatchKit_Extension_WorkoutManager_swift.html#//apple_ref/doc/uid/TP40017286-SwingWatch_WatchKit_Extension_WorkoutManager_swift-DontLinkElementID_11

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 12 '22 at 00:20