Sending heart rate data from apple watch
dhruvadhia1 opened this issue · 0 comments
dhruvadhia1 commented
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()
}
}
`