WinRT Windows.Devices.Bluetooth.GenericAttributeProfile
This package is a Python projection of the Windows Runtime (WinRT) APIs specific to `Windows.Devices.Bluetooth.GenericAttributeProfile`. It allows Python applications to interact with Bluetooth LE devices and their Generic Attribute Profile (GATT) services and characteristics on Windows. Part of the `pywinrt` project, it is currently at version 3.2.1 and receives regular updates to align with Windows SDK changes and project enhancements.
Common errors
-
ModuleNotFoundError: No module named 'windows.devices.bluetooth'
cause Incorrect top-level import. WinRT projection namespaces are always prefixed with `winrt.`.fixChange `import windows.devices.bluetooth` to `import winrt.windows.devices.bluetooth`. -
AttributeError: 'IAsyncOperation_1__GattDeviceServicesResult' object has no attribute 'get'
cause Attempting to call `.get()` or `.wait()` on an asynchronous WinRT operation (`IAsyncOperation` or `IAsyncAction`) on a `pywinrt` version older than 3.2.0, where these methods were not available or stable.fixUpgrade `pywinrt` to version 3.2.0 or later. Alternatively, use `await` on the async operation within an `async` function if synchronous blocking behavior is not strictly required. -
ImportError: cannot import name 'GattDeviceService' from 'winrt_windows_devices_bluetooth_genericattributeprofile'
cause Importing directly from the PyPI package name (using underscores) instead of the structured WinRT namespace (using dots) within the `winrt` module.fixCorrect the import path to `from winrt.windows.devices.bluetooth.genericattributeprofile import GattDeviceService`. -
AttributeError: module 'winrt.system' has no attribute 'hresult'
cause Accessing the `winrt.system.hresult` module, which contains WinRT error codes, on a `pywinrt` version older than 3.2.0.fixUpgrade `pywinrt` to version 3.2.0 or later to enable access to the `winrt.system.hresult` module.
Warnings
- breaking Version 3.0.0 introduced significant breaking changes, including a redesigned API, new `winrt.runtime` module, and changes to `winrt.system` functionalities (e.g., `box_...`/`unbox_...` functions, `Object.as_()`). Applications targeting `pywinrt` v2.x.x will require migration.
- gotcha Prior to version 3.2.0, synchronously calling WinRT async operations was less straightforward, primarily relying on `asyncio.run()` or `await` within an async context. Directly calling `.get()` or `.wait()` on async operations was not officially supported or stable.
- gotcha The `winrt.system.hresult` module, which provides access to common WinRT error codes, was introduced in version 3.2.0. Attempting to access this module or its contents on older versions will result in an `AttributeError`.
- gotcha Fixed a possible circular import exception in version 3.2.1. Older versions might encounter issues related to module loading order, especially in complex application structures.
Install
-
pip install winrt-windows-devices-bluetooth-genericattributeprofile -
pip install winrt
Imports
- GattDeviceService
from winrt_windows_devices_bluetooth_genericattributeprofile import GattDeviceService
from winrt.windows.devices.bluetooth.genericattributeprofile import GattDeviceService
- BluetoothLEDevice
from winrt.windows.devices.bluetooth.genericattributeprofile import BluetoothLEDevice
from winrt.windows.devices.bluetooth import BluetoothLEDevice
- DeviceInformation
from winrt.windows.devices.bluetooth import DeviceInformation
from winrt.windows.devices.enumeration import DeviceInformation
Quickstart
import asyncio
from winrt.windows.devices.bluetooth import BluetoothLEDevice
from winrt.windows.devices.enumeration import DeviceInformation
async def list_bluetooth_le_devices():
# AQS filter for Bluetooth LE devices
aqs_le_devices = BluetoothLEDevice.get_device_selector()
print("Searching for Bluetooth LE devices (this may take a few seconds)...")
# DeviceInformation is in winrt.windows.devices.enumeration
devices = await DeviceInformation.find_all_async(aqs_le_devices)
if not devices:
print("No Bluetooth LE devices found.")
print("Ensure Bluetooth is enabled and devices are discoverable.")
return
print(f"Found {len(devices)} Bluetooth LE devices:")
for i, device in enumerate(devices):
print(f" [{i}] Name: {device.name}, Id: {device.id}")
# To get GATT services, you would then obtain a BluetoothLEDevice instance
# and call its GetGattServicesAsync method.
# Example (conceptual):
# from winrt.windows.devices.bluetooth.genericattributeprofile import GattCommunicationStatus
# le_device = await BluetoothLEDevice.from_id_async(device.id)
# if le_device:
# result = await le_device.get_gatt_services_async()
# if result.status == GattCommunicationStatus.SUCCESS:
# for service in result.services:
# print(f" GATT Service: {service.uuid}")
# # Further GATT operations would use objects from
# # winrt.windows.devices.bluetooth.genericattributeprofile
# # e.g., GattDeviceService, GattCharacteristic, etc.
if __name__ == "__main__":
# This code must run on a Windows machine with Bluetooth enabled and permissions granted.
asyncio.run(list_bluetooth_le_devices())