Skip to main content

polar-python is a Python library for connecting to Polar devices via Bluetooth Low Energy (BLE) using Bleak. It allows querying device capabilities (e.g., ECG, ACC, PPG), exploring configurable options, and streaming parsed data through callback functions.

Project description

polar-python

PyPI version Python versions License: MIT Build Status

polar-python is an asynchronous Python library designed for integration with Polar devices using Bluetooth Low Energy (BLE). Powered by the bleak library, polar-python abstracts the complexity of Polar's binary protocols, allowing developers to easily connect, configure, and stream real-time physiological and kinematic data directly into structured Python objects via callback functions. The codebase is fully type-hinted and passes strict static type checks. Additionally, all public functions and classes include comprehensive docstrings for better developer experience and IDE support.

[!CAUTION] Breaking Changes: Current versions (1.x.x+) are incompatible with older 0.0.x versions. The API has undergone significant architectural changes. New users can proceed normally; existing users should refer to the examples section to migrate their code.

Supported Devices & Data Streams

Currently, polar-python has been tested and is guaranteed to work with Polar H10 and Polar Verity Sense. Below are the supported data streams and their precise configuration limits for each device.

Polar H10

The Polar H10 is a heart rate sensor that also provides electrocardiogram and kinematics data.

  • Heart Rate (HR): Standard BLE heart rate stream with RR intervals.
  • Electrocardiogram (ECG):
    • Sample Rate: 130 Hz
    • Resolution: 14 bit
  • Accelerometer (ACC):
    • Sample Rate: 25, 50, 100, 200 Hz
    • Resolution: 16 bit
    • Range: 2, 4, 8 G

Polar Verity Sense

The Polar Verity Sense is an optical heart rate sensor providing a wide array of optical and kinematic data.

  • Heart Rate (HR): Standard BLE heart rate stream.
  • Photoplethysmography (PPG):
    • Sample Rate: 55 Hz
    • Resolution: 22 bit
    • Channels: 4
  • Peak-to-Peak Interval (PPI):
    • Requires no specific configuration. Streams raw PPI, error estimates, and calculated HR.
  • Accelerometer (ACC):
    • Sample Rate: 52 Hz
    • Resolution: 16 bit
    • Range: 8 G
    • Channels: 3
  • Gyroscope (Gyro):
    • Sample Rate: 52 Hz
    • Resolution: 16 bit
    • Range: 2000 dps (deg/s)
    • Channels: 3
  • Magnetometer (MAG):
    • Sample Rate: 10, 20, 50, 100 Hz
    • Resolution: 16 bit
    • Range: 50 Gauss
    • Channels: 3

Installation

You can install polar-python from PyPI using pip:

pip install polar-python

To use the interactive Command Line Interface (CLI) tool, install the extra dependencies:

pip install polar-python[cli]

Usage

Interactive CLI Tool

For a quick start or to test your hardware, use our built-in CLI tool. It will scan for nearby Polar devices, allow you to select one, inspect its supported measurement types, and configure/start data streams interactively.

python -m polar_python.cli

Examples

Here is a minimal, self-contained example demonstrating the core workflow: scanning for a Polar H10 device, establishing a connection, and streaming real-time data using callbacks.

import asyncio
from bleak import BleakScanner
from polar_python import PolarDevice
from polar_python.models import ACCData, ECGData, HRData

async def main():
    # 1. Find a Polar H10 device
    print("Scanning for Polar H10...")
    device = await BleakScanner.find_device_by_filter(lambda bd, ad: bd.name and "Polar H10" in bd.name, timeout=5)
    if not device:
        print("Device not found. Please ensure your Polar device is awake and nearby.")
        return
    print(f"Found {device.name}, connecting...")

    # 2. Connect and manage the device session
    async with PolarDevice(device) as polar_device:
        # 3. Define your callback functions
        def ecg_callback(data: ECGData):
            print(f"ECG Data: {data}")
        def acc_callback(data: ACCData):
            print(f"ACC Data: {data}")
        def hr_callback(data: HRData):
            print(f"HR Data: {data}")

        # 4. Start data streams with desired configurations
        await polar_device.start_ecg_stream(ecg_callback=ecg_callback, sample_rate=130, resolution=14)
        await polar_device.start_acc_stream(acc_callback=acc_callback, sample_rate=25, resolution=16, range=2)
        await polar_device.start_hr_stream(hr_callback=hr_callback)

        # 5. Keep the main loop running to receive data
        print("Streaming data for 10 seconds...")
        await asyncio.sleep(10)
if __name__ == "__main__":
    asyncio.run(main())

To understand how to integrate the library into your own scripts, please refer to the examples/ directory in the repository. These files demonstrate the complete usage of the library, including connection management, custom configurations, and data handling:

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome and highly encouraged! Whether you are fixing a bug, adding a new feature, or improving documentation, your help is appreciated.

  • Submit a Pull Request: Feel free to fork the repository, make your changes, and submit a PR.
  • Report Issues: If you encounter any bugs or have suggestions for improvements, please open an Issue to let us know.

We appreciate your support in making polar-python better!

Acknowledgements

  • Bleak - BLE library for Python.
  • bleakheart - For providing inspiration and valuable insights.
  • Polar BLE SDK - For providing official BLE SDK and documentation for Polar devices.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

polar_python-1.0.2.tar.gz (42.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

polar_python-1.0.2-py3-none-any.whl (26.1 kB view details)

Uploaded Python 3

File details

Details for the file polar_python-1.0.2.tar.gz.

File metadata

  • Download URL: polar_python-1.0.2.tar.gz
  • Upload date:
  • Size: 42.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for polar_python-1.0.2.tar.gz
Algorithm Hash digest
SHA256 921f91552de0cf459083c859152b5057a26d721c0cb6a73d95f61a9b9035f335
MD5 c0f9fd477427ed8c36489a762bca64ac
BLAKE2b-256 f8e1c825621e4f8d00d365f14e7d303804d6a2022032b11368bbde5c67d346e6

See more details on using hashes here.

File details

Details for the file polar_python-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: polar_python-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 26.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for polar_python-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f2c9f66e0722f9d0a33c8fc7716abf4f430c2ae88184daade3d5f592b70b1456
MD5 693fa9ab570c98f5227a78fd1487d398
BLAKE2b-256 7c3c40c27707e3a1e47b0cf1e08b82edcb2ce500328bab13d6c4de0fd25901e7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page