or run

tessl search
Log in

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/bleak@1.1.x
tile.json

tessl/pypi-bleak

tessl install tessl/pypi-bleak@1.1.0

Cross-platform Bluetooth Low Energy GATT client library for asynchronous BLE communication

Agent Success

Agent success rate when using this tile

97%

Improvement

Agent success rate improvement when using this tile compared to baseline

1.07x

Baseline

Agent success rate without this tile

91%

task.mdevals/scenario-1/

BLE Heart Rate Monitor Controller

Overview

Build a heart rate monitor controller that connects to a simulated BLE heart rate sensor device and manages notification subscriptions. The controller should allow users to start monitoring heart rate data, stop monitoring when no longer needed, and handle cleanup properly.

Requirements

Core Functionality

  1. Connection Management

    • Connect to a BLE device by address
    • Verify connection status
    • Disconnect when done
  2. Notification Management

    • Subscribe to heart rate measurement notifications from a characteristic
    • Handle incoming heart rate data via a callback function
    • Unsubscribe from notifications when monitoring should stop
    • Support multiple start/stop cycles on the same connection
  3. Data Collection

    • Store received heart rate measurements in a list
    • Each measurement should include timestamp and heart rate value
    • Provide a method to retrieve collected measurements

Implementation Details

File: heart_rate_controller.py

Create a HeartRateController class with the following interface:

class HeartRateController:
    async def connect(self, device_address: str) -> bool:
        """Connect to the BLE heart rate device.

        Args:
            device_address: The Bluetooth address of the device

        Returns:
            True if connection successful, False otherwise
        """
        pass

    async def start_monitoring(self, characteristic_uuid: str) -> bool:
        """Start receiving heart rate notifications.

        Args:
            characteristic_uuid: UUID of the heart rate measurement characteristic

        Returns:
            True if notifications started successfully, False otherwise
        """
        pass

    async def stop_monitoring(self, characteristic_uuid: str) -> bool:
        """Stop receiving heart rate notifications.

        Args:
            characteristic_uuid: UUID of the heart rate measurement characteristic

        Returns:
            True if notifications stopped successfully, False otherwise
        """
        pass

    def get_measurements(self) -> list:
        """Retrieve all collected heart rate measurements.

        Returns:
            List of measurements, each as a dict with 'timestamp' and 'heart_rate' keys
        """
        pass

    async def disconnect(self):
        """Disconnect from the device."""
        pass

Test Cases

Test 1: Start and Stop Notifications @test

File: test_heart_rate_controller.py

import asyncio
from heart_rate_controller import HeartRateController

async def test_start_stop_notifications():
    controller = HeartRateController()

    # Mock device address and characteristic UUID
    device_address = "00:11:22:33:44:55"
    hr_characteristic = "00002a37-0000-1000-8000-00805f9b34fb"

    # Connect
    connected = await controller.connect(device_address)
    assert connected, "Should connect successfully"

    # Start monitoring
    started = await controller.start_monitoring(hr_characteristic)
    assert started, "Should start notifications successfully"

    # Wait briefly to collect some data
    await asyncio.sleep(2)

    # Stop monitoring
    stopped = await controller.stop_monitoring(hr_characteristic)
    assert stopped, "Should stop notifications successfully"

    # Verify we collected some measurements
    measurements = controller.get_measurements()
    assert len(measurements) > 0, "Should have collected measurements"

    # Disconnect
    await controller.disconnect()

    print("Test passed: Successfully started and stopped notifications")

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

Test 2: Multiple Start-Stop Cycles @test

File: test_heart_rate_controller.py

async def test_multiple_cycles():
    controller = HeartRateController()

    device_address = "00:11:22:33:44:55"
    hr_characteristic = "00002a37-0000-1000-8000-00805f9b34fb"

    await controller.connect(device_address)

    # First cycle
    await controller.start_monitoring(hr_characteristic)
    await asyncio.sleep(1)
    await controller.stop_monitoring(hr_characteristic)
    count_after_first = len(controller.get_measurements())

    # Wait to ensure notifications truly stopped
    await asyncio.sleep(1)
    count_after_wait = len(controller.get_measurements())
    assert count_after_wait == count_after_first, "Should not receive data after stop"

    # Second cycle
    await controller.start_monitoring(hr_characteristic)
    await asyncio.sleep(1)
    await controller.stop_monitoring(hr_characteristic)
    count_after_second = len(controller.get_measurements())

    assert count_after_second > count_after_first, "Should collect more data in second cycle"

    await controller.disconnect()

    print("Test passed: Multiple start-stop cycles work correctly")

Dependencies { .dependencies }

bleak { .dependency }

Cross-platform Bluetooth Low Energy library for Python. Provides BLE device connection, GATT operations, and notification management.

Notes

  • The solution should use asynchronous programming patterns
  • Proper error handling should be implemented for connection and notification operations
  • The controller should handle cleanup automatically when stopping notifications
  • For testing purposes, you may use a mock BLE device or simulator