English ver. | 中文版
Bluetooth Low Energy (BLE) heart rate monitor toolkit with two interfaces:
- A FastAPI-powered REST server that discovers, connects to, and streams heart rate metrics from BLE devices such as smart watches or chest straps.
- An MCP (Model Context Protocol) server exposing the same capabilities for AI copilots.
- A smartwatch or other devices that supports BLE heart rate broadcasting.
- Python 3.10 or newer
- Windows with Bluetooth Low Energy support (the project also runs on Linux and macOS if BLE is available)
uv(recommended) orpipfor dependency management
-
Install dependencies:
uv sync
If you prefer
pip, runpip install -r requirements.txtafter generating the lockfile withuv pip compile pyproject.toml -o requirements.txt. -
Review
.envand adjust the options to match your runtime environment. The application will create the file automatically on first launch if it is missing.SERVER_HOST=127.0.0.1 SERVER_PORT=8000 SERVER_RELOAD=false AUTO_RECONNECT=false BLE_DEVICE_ADDRESSES=
SERVER_HOST/SERVER_PORTcontrol how the API server listens.SERVER_RELOADtoggles Uvicorn's auto-reload; set totruefor iterative dev loops.AUTO_RECONNECTautomatically connects to the first known device during startup when set totrue.BLE_DEVICE_ADDRESSESstores a comma-separated history of devices you have connected to. The application appends to this list automatically.
uv run main.pyOnce running, open http://<SERVER_HOST>:<SERVER_PORT>/docs to explore the interactive Swagger UI.
| Method | Path | Description |
|---|---|---|
GET |
/health |
Simple liveness probe |
GET |
/devices/known |
List of previously connected BLE addresses |
POST |
/devices/scan |
Scan for nearby BLE devices |
POST |
/devices/connect |
Connect to a device by address (preferred) or name fragment |
POST |
/devices/disconnect |
Stop monitoring and release the connection |
GET |
/metrics/heart-rate |
Fetch the latest heart rate metrics |
All responses and logs are in English for easier integration with other tools.
Please make sure you enabled "heart rate broadcasting" function on your smart devices.
Send the device address you obtained from /devices/scan or /devices/known:
POST /devices/connect
{
"address": "E4:96:52:7F:BA:C3"
}If an address is not available, you can still provide a name field with a partial device name, and the API will attempt to find the closest match.
When AUTO_RECONNECT is enabled, the server tries to connect automatically to the first address stored in BLE_DEVICE_ADDRESSES during startup.
The MCP server exposes high-level tools for AI agents.
uv run mcp_server.pyAvailable tools:
scan_ble_devices()– enumerate nearby BLE devices.connect_ble_device(device_address: str | None, device_name: str | None)– connect to a device.disconnect_ble_device()– release the active connection.get_current_heart_rate()– retrieve the most recent heart rate values.
- Run
python -m compileall .to catch syntax issues quickly. - Enable
uvicorndebug logging with the--log-level debugflag during development. - The application persists connected BLE addresses back to
.env; ensure the file is writable.
- BLE permissions – Windows requires the Bluetooth radio to be switched on and accessible to the Python process.
- Multiple connections – If you connect to the already active device, the server reuses the connection instead of reopening it.
- Address persistence – Delete the
BLE_DEVICE_ADDRESSESvalue in.envif you need to reset the history.