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

polar-python is a Python library designed for seamless integration with Polar devices using Bluetooth Low Energy (BLE) through the Bleak library. With polar-python, you can easily connect to Polar devices, query supported functionalities such as ECG, ACC, and PPG, explore configurable options and their possible values, and start data streaming to receive parsed binary data through callback functions.

Features

  • Connect to Polar Devices: Use BLE to connect to Polar devices.
  • Query Device Capabilities: Discover supported functionalities like ECG, ACC, and PPG.
  • Explore Configurable Options: Query and set measurement settings for each feature.
  • Stream Data: Start data streaming and receive parsed binary data via callback functions.

Installation

Since polar-python is not yet available on PyPI, you can install it locally by following these steps:

  1. Clone the repository:

    git clone https://github.com/yourusername/polar-python.git
    
  2. Navigate to the project directory:

    cd polar-python
    
  3. Install the package using pip:

    pip install .
    

Usage

Below is an example of how to use polar-python to connect to a Polar device, query its features, set measurement settings, and start data streaming.

Step 1: Import Libraries and Initialize Console

import asyncio
from bleak import BleakScanner
from polar_python import PolarDevice, MeasurementSettings, SettingType, ECGData, ACCData

Step 2: Define Data Callback Function

def data_callback(data: Union[ECGData, ACCData]):
    """
    Callback function to handle incoming data from the Polar device.

    Args:
        data (Union[ECGData, ACCData]): The data received from the Polar device.
    """

Step 3: Define Main Function to Connect to Polar Device

async def main():
    """
    Main function to connect to a Polar device, query its features,
    set measurement settings, and start data streaming.
    """
    # Find the Polar H10 device
    device = await BleakScanner.find_device_by_filter(
        lambda bd, ad: bd.name and "Polar H10" in bd.name, timeout=5
    )
    if device is None:
        return

Step 4: Connect to Polar Device and Query Features

    # Establish connection to the Polar device
    async with PolarDevice(device, data_callback) as polar_device:
        # Query available features
        available_features = await polar_device.available_features()

        # Query and print stream settings for each feature
        for feature in available_features:
            settings = await polar_device.request_stream_settings(feature)

Step 5: Define and Start Data Streams

        # Define ECG measurement settings
        ecg_settings = MeasurementSettings(
            measurement_type="ECG",
            settings=[
                SettingType(type="SAMPLE_RATE", array_length=1, values=[130]),
                SettingType(type="RESOLUTION", array_length=1, values=[14]),
            ],
        )

        # Define ACC measurement settings
        acc_settings = MeasurementSettings(
            measurement_type="ACC",
            settings=[
                SettingType(type="SAMPLE_RATE", array_length=1, values=[25]),
                SettingType(type="RESOLUTION", array_length=1, values=[16]),
                SettingType(type="RANGE", array_length=1, values=[2]),
            ],
        )

        # Start data streams for ECG and ACC
        await polar_device.start_stream(ecg_settings)
        await polar_device.start_stream(acc_settings)

        # Keep the stream running for 120 seconds
        await asyncio.sleep(120)

Step 6: Run the Main Function

if __name__ == "__main__":
    asyncio.run(main())

License

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

Acknowledgements

  • Bleak - BLE library for Python.
  • Rich - Python library for rich text and beautiful formatting in the terminal.
  • 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-0.0.1.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

polar_python-0.0.1-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: polar_python-0.0.1.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for polar_python-0.0.1.tar.gz
Algorithm Hash digest
SHA256 faac16f7191dc3f0162e6c5048430ea98c033c08c1a8574b4ce8329b3b9f8868
MD5 0a758f585890f79c40c118d9e292fc23
BLAKE2b-256 676e7d589d1613a51b084ec095907aed930119feab6ccf7e335fb01e2c9ca1d0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polar_python-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7a4ffca915e62f5511c3c24d67a9edfbdf8ba4cc73f69d0654d8476efbbfbd20
MD5 c0c3f45ceb04fc15a3407022055f6979
BLAKE2b-256 c63e45834c7983e6e908ead5fe7b9a82eb04a4160f0aff766a66a068f6c6a9d1

See more details on using hashes here.

Supported by

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