A Python server that saves files from a Fitbit OS app.
A phone-based server is used to receive the files. This avoids the need to use https, which can be difficult to set up on a LAN.
This demonstration transfers accelerometer data, which is probably the most demanding use case. The same architecture should be fine for streaming heart rate, step count, etc.
This repository consists of two main components:
fitbit. This is a Fitbit OS app comprising a device (watch) component and a companion (phone) component. Accelerometer data is passed from the device to the companion using file transfer. The companion then usesfetch()toPOSTeach file to theservercomponent.GETrequests are used to pass server control commands.server. An HTTP server (written in Python) is used to receive filesPOSTed from the Fitbit companion and save them on the phone.GETrequests are also decoded but are not used.
- Download or clone the
fitbitapp’s files. - In
companion/index.js, verify thathttpURLis appropriate to your phone. (The default should be fine on Android.) - Build the app (named
File Saver) using the Fitbit development CLI. - Install the app onto a watch (although it can also run on the Fitbit Simulator).
- Install a Python 3.5+ execution environment on the phone on which the Fitbit companion component (ie, Fitbit mobile app) will run. For Android, you can use pydroid 3. For iOS, Pythonista 3 might work (untested).
- In
file-saver.py, setSERVER_LOCAL_PORTto match that specified incompanion/index.js’shttpURL. - Copy
server/file-saver.pyto a directory on your phone from which Python can run it.
- Using Python on your phone, run
file-saver.py. - On your watch, run the
File Saverapp.
- Use the
File Saverapp on the watch to record some accelerometer data, and then transfer it to the phone. - You can monitor the transfers on the watch or the companion settings page.
- After transfer, files should be available on the phone in the same directory as
file-saver.py.
- Python’s
HTTP.serverand this repository’sfile-saver.pyare insecure. Use at your own risk. - There is minimal error checking or recovery.
- Python’s
HTTP.serveris slow (but should be able to keep up with Fitbit’s file transfer). - If you restart either component, connection(s) may not automatically reopen. You may need to restart the other component.
- In this demonstration, binary messages are used for watch-to-companion file transfers, because the watch-to-companion connection (in particular) can be slow. Using text (string) files should be possible but will take longer to transfer (conceivably by a factor of 10). If you use text files, be aware that the server won’t attempt to convert end-of-line characters.
- The
servershould save files without changing their content or format, so the use of binary or text files should be possible (untested). - File transfer and
fetch()aren't well-suited to streaming data in real-time. For streaming, use WebSockets. See the fitbit-stream-bridge repository for an example. - The Fitbit component is based on fitbit-accel-fetcher, and probably contains some irrelevant artefacts. Please see the
READMEthere for more information. - The server doesn't attempt to combine (ie, merge or append) files even when they represent successive sets of readings. You could implement this on receipt of an HTTP
GETcommand. - If you don't want to install Python, android-fitbit-fetcher provides source code for a native Java server than runs on Android.
file-saver.pycontains snippets from realpython.File Saver’s icon was adapted from friconix.