tessl install tessl/pypi-bleak@1.1.0Cross-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%
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.
Connection Management
Notification Management
Data Collection
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."""
passFile: 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())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")Cross-platform Bluetooth Low Energy library for Python. Provides BLE device connection, GATT operations, and notification management.