skip to Main Content

I am working on a application which needs accelerometer, gyroscope & pedometer data & also the heart rate. I am transferring this data from iwatch to iPhone then from iPhone I need to sync this data via MQTT protocol. Now my problem is that, once the iwatch window goes disable my application terminated. I am using core motion and live workout session.
Can anyone help me on how can I keep the iwatch app active or transfer the above data from inactive mode ?

2

Answers


  1. Here is my solution that can help you:

    Ref: https://developer.apple.com/documentation/healthkit/workouts_and_activity_rings/running_workout_sessions

    1. Setup HKWorkoutSession and CoreMotion listener
    import WatchKit
    import Foundation
    import CoreMotion
    import HealthKit
    
    enum VelocityVector: Int {
        case x, y, z
    }
    
    class InterfaceController: WKInterfaceController {
    
        @IBOutlet weak var labelVelocity: WKInterfaceLabel!
        let coreMotion = CMMotionManager.init()
        let pool = OperationQueue.init()
        let currentSession: HKWorkoutSession?
        let healthKit = HKHealthStore()
        
        override func awake(withContext context: Any?) {
            coreMotion.accelerometerUpdateInterval = 0.1
            coreMotion.startAccelerometerUpdates(to: pool) { data, err in
                guard let _data = data else { return }
                DispatchQueue.main.async {
                    self.labelVelocity.setText(String.init(format: "G-Force (x:y:z) %.3f:%.3f:%.3f", arguments: [_data.acceleration.x, _data.acceleration.y, _data.acceleration.z]))
                }
            }
            
            let config = HKWorkoutConfiguration.init()
            config.activityType = .other
            config.locationType = .unknown
            
            do {
                self.currentSession = try HKWorkoutSession.init(healthStore: self.healthKit, configuration: config)
                self.currentSession?.startActivity(with: Date())
            } catch error {
                print(error?.localizedDescription)
            }        
        }
    
        private func stopHKWorkoutSession() {
            self.currentSession?.stopActivity(with: Date())
            self.currentSession?.end()
        }
        
        override func willActivate() {
            // This method is called when watch view controller is about to be visible to user
        }
        
        override func didDeactivate() {
            // This method is called when watch view controller is no longer visible
        }
    
    }
    
    1. Enable "Workout processing" in Background Mode:
      enter image description here
    Login or Signup to reply.
  2. An addition to good answer of Neklas, should open capability of Healhkit and add permission of healthkit to info plist of watchos project like below images. Otherwise, you may encounter an error.

    enter image description here

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search