How to get WCSession to continue streaming data after WatchKit app falls asleep?

51 views Asked by At

We are working on a WatchKit app that streams heart rate data to a companion iOS app. This works very well with WCSession, however, when the WatchKit app sleeps, the data does not continue to stream. The only way to get the data to stream again is by moving/touching the Apple Watch and waking it up again.

We tried implementing a WKExtendedRuntimeSession, but we were unable to send data with this session. Is there a way to have data to continue to stream continuously, even if the watchkit app falls asleep?

WatchOS: 8.8.1 iOS: 17.0 XCode: 15.0.1

2

There are 2 answers

0
gui_dos On

My app https://github.com/gui-dos/DiaBLE is independent and I am succeeding in keeping it in the background while communicating with the glucose sensor Libre 3 which reconnects very regularly every minute.

I am setting a smart alarm at 55 seconds even though I am not supporting real workout activities yet... B-)

0
chuanpham On

Make sure you have enabled background mode both in Apple watch and iPhone

Example

And you can try the HKLiveWorkoutBuilderDelegate like this:

extension WorkoutManager: HKLiveWorkoutBuilderDelegate {
    func workoutBuilderDidCollectEvent(_ workoutBuilder: HKLiveWorkoutBuilder) {
        
    }
    
    func workoutBuilder(_ workoutBuilder: HKLiveWorkoutBuilder, didCollectDataOf collectedTypes: Set<HKSampleType>) {
        for type in collectedTypes {
            guard let quantityType = type as? HKQuantityType else {
                return // Nothing to do.
            }
            
            let statistics = workoutBuilder.statistics(for: quantityType)
            
            // Update the published values.
            if workoutState == .running {
                updateForStatistics(statistics)
            }
        }
    }
}