ishibuilds

← All posts

Challenges with getting data from my Garmin

I couldn't find my lowest heart rate or my average daily heart rate anywhere in the Garmin Connect app. I've always been curious about my health data, and this limitation, along with others in the Garmin app, led me to build my own app. It gets data from my watch and gives me better insight into how my body is recovering.

Garmin provides two official ways to get data:

  1. The Garmin developer APIs
  2. The Garmin Health SDKs

The developer API program was paused when I started, and it only reopened in October. Even so, I wouldn't use it. The data would go from my watch to the Garmin app, then to the Garmin cloud, and only then to me, and I want everything running locally on my device. The Health SDKs are only available to enterprises, so I couldn't get access to them.

Since neither option suited me, I had to look elsewhere. I asked Claude to research every way to get my data off the watch. It found a few routes:

  • garmin-ble: a Python package that only streams live data. It can't download files from the watch, so it could never get my sleep or workouts.
  • Gadgetbridge: an Android app, and its AGPL licence means copying its code would force me to publish Gradatim's code. So it was only ever a reference. Even so, it showed us how Garmin devices talk over Bluetooth, and with that understanding Claude could write its own implementation for what I needed.
  • Apple Health: ruled out, because Garmin doesn't write HRV, stress or Body Battery there.

With that understanding of how the watch works, it was time to write some code and install it on my phone so I could test it. My phone was already connected to the watch through the Garmin Connect app, so I could use that open connection to pull my activity data. Weeks of walks and rides were still on the watch, and they all came across.

A few bugs came up, but I resolved them with a debug build. It saves the raw Bluetooth packets and files from the watch, and I copy them from my phone to my Mac. Any time I noticed an issue, I would tell Claude what it was and connect my phone. It could then look at the captured data, investigate and fix the issue.

The sleep and HRV data was a different story. Every morning, my sleep and HRV data wouldn't show up in my own app, but it would show up in the Garmin app.

On investigation, it looks like Garmin watches wipe the sleep and HRV data from the watch once it has been synced. I'm assuming there is a flag somewhere that says when a file can be deleted.

The diagram below is my guess at how it works, based on what I saw. The exact times and the rules for each file type haven't been confirmed.

graph TD
  A([Start]) --> B(Check the file metadata)
  B --> I{Does an expiry time exist on the file?}
  I -->|Yes| J{Has the expiry time passed?}
  J -->|Yes| K[(Delete the file)]
  J -->|No| D(Leave the file as it is)
  I -->|No| C{Has the file been synced?}
  C -->|No| D
  D --> B
  C -->|Yes| E{What type of file is it?}
  E -->|Sleep file| F[Set the expiry time to a few minutes]
  E -->|Health file| G[Set the expiry time to a few hours]
  E -->|Activity file| H[Set the expiry time to a few days]
  F --> B
  G --> B
  H --> B

This led to a few issues, but the biggest was the sleep data. When I woke up, Garmin would get the sleep data (and set the delete flag) before my app managed to sync it. Even with the Garmin app closed, it was getting my sleep data before my own app.

At first I blamed the Garmin Connect app. I thought it was grabbing the sleep data the moment it was recorded, so I switched the watch's connection to it off to test this. The same issue happened the next day. It turned out my watch was connected to Wi-Fi, so it was sending the sleep data to the Garmin cloud and then deleting it from the watch. Turning Wi-Fi off fixed it. Since then, I've left Wi-Fi off and turned off the Garmin Connect connection every night before I sleep (in the app settings, under accessories, there's a switch that allows the Garmin app to connect to the watch). It works, but it's a nuisance.

I also had a theory that iOS gives the Garmin Connect app priority for background refresh, so I tried turning off Background App Refresh for it, and I looked into Bluetooth accessory syncing. Neither made any difference.

I submitted my app for review a few weeks ago and got a rejection with a list of things to fix. One of them was the pairing method, which led me to implement a proper one: Apple's accessory pairing. To set it up, I first had to disconnect the watch from Garmin Connect, then go through the pairing flow in Gradatim. When I opened Garmin Connect again, it showed a popup asking whether I wanted to share the watch connection. So the watch can stay connected to both apps.

The sleep problem is still there, though. I now mostly leave the Garmin Connect connection off and only turn it on when I want to sync something to Strava. Why Garmin Connect wins the race for the sleep file is still an open question. My guess is that Apple gives it priority because it's been installed longer and comes from the App Store rather than TestFlight, but I haven't been able to confirm that.