Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending heart rate data from apple watch #62

Open
dhruvadhia1 opened this issue Jan 27, 2022 · 0 comments
Open

Sending heart rate data from apple watch #62

dhruvadhia1 opened this issue Jan 27, 2022 · 0 comments

Comments

@dhruvadhia1
Copy link

Hi There,

I am trying to send Heart Rate Data and the entire code is here. I dont get any errors and the app opens showing my hr data but on the OS monitor I enter the right address and port. And they are on the same wifi. I still cant see any messages coming in.

`import SwiftUI
import HealthKit
import SwiftOSC

struct ContentView: View {
private var healthStore = HKHealthStore()
let heartRateQuantity = HKUnit(from: "count/min")
var client = OSCClient(address: "192.168.1.70", port: 8080)

@State private var value = 0

var body: some View {
    VStack{
        HStack{
            Text("❤️")
                .font(.system(size: 50))
            Spacer()

        }
        
        HStack{
            Text("\(value)")
                .fontWeight(.regular)
                .font(.system(size: 70))
            
            Text("BPM")
                .font(.headline)
                .fontWeight(.bold)
                .foregroundColor(Color.red)
                .padding(.bottom, 28.0)
            
            Spacer()
            
        }

    }
    .padding()
    .onAppear(perform: start)
}


func start() {
    autorizeHealthKit()
    startHeartRateQuery(quantityTypeIdentifier: .heartRate)
    
    
}

func autorizeHealthKit() {
    let healthKitTypes: Set = [
    HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)!]

    healthStore.requestAuthorization(toShare: healthKitTypes, read: healthKitTypes) { _, _ in }
}

private func startHeartRateQuery(quantityTypeIdentifier: HKQuantityTypeIdentifier) {
    
    // 1
    let devicePredicate = HKQuery.predicateForObjects(from: [HKDevice.local()])
    // 2
    let updateHandler: (HKAnchoredObjectQuery, [HKSample]?, [HKDeletedObject]?, HKQueryAnchor?, Error?) -> Void = {
        query, samples, deletedObjects, queryAnchor, error in
        
        // 3
    guard let samples = samples as? [HKQuantitySample] else {
        return
    }
        
    self.process(samples, type: quantityTypeIdentifier)

    }
    
    // 4
    let query = HKAnchoredObjectQuery(type: HKObjectType.quantityType(forIdentifier: quantityTypeIdentifier)!, predicate: devicePredicate, anchor: nil, limit: HKObjectQueryNoLimit, resultsHandler: updateHandler)
    
    query.updateHandler = updateHandler
    
    // 5
    
    healthStore.execute(query)
}

private func process(_ samples: [HKQuantitySample], type: HKQuantityTypeIdentifier) {
    var lastHeartRate = 0.0
    
    for sample in samples {
        if type == .heartRate {
            lastHeartRate = sample.quantity.doubleValue(for: heartRateQuantity)
        }
        
        self.value = Int(lastHeartRate)
        let message = OSCMessage(
            OSCAddressPattern("/mynd"),
            self.value
        )
        
        client.client.enableBroadcast()
        client.send(message)
    }
}

}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant