PyWinRT - Windows.Devices.Bluetooth Projection
PyWinRT is a set of Python projections for Windows Runtime (WinRT) APIs, allowing Python developers to access native Windows features. This specific package, `winrt-windows-devices-bluetooth`, provides access to Bluetooth-related functionalities from the `Windows.Devices.Bluetooth` namespace. It is currently at version 3.2.1 and follows a frequent release cadence, often aligned with Windows SDK and App SDK updates.
Common errors
-
ImportError: cannot import name 'BluetoothAdapter' from 'winrt.windows.devices.bluetooth'
cause The `winrt-windows-devices-bluetooth` package is not installed, or its core `winrt` dependency is missing/corrupted.fixRun `pip install winrt-windows-devices-bluetooth` to ensure both the projection and its core runtime are installed. -
AttributeError: module 'winrt' has no attribute 'windows'
cause Only the core `winrt` package is installed, but not the specific projection package for `Windows.Devices.Bluetooth` (i.e., `winrt-windows-devices-bluetooth`). The `windows` namespace is part of the projection, not the core.fixInstall the specific projection package: `pip install winrt-windows-devices-bluetooth`. -
TypeError: 'coroutine' object is not awaited
cause An asynchronous WinRT method (typically ending in `Async`) was called but not `await`ed in an `async` function, or the calling context is not asynchronous.fixPrefix the call with `await` (e.g., `await BluetoothAdapter.GetDefaultAsync()`). If synchronous execution is required (from v3.2.0+), call `.get()` or `.wait()` on the returned async object: `BluetoothAdapter.GetDefaultAsync().get()`. -
System.Exception: Element not found.
cause This low-level WinRT error often occurs when running PyWinRT on a non-Windows operating system, or when a required Windows feature (like Bluetooth) is disabled or not present.fixPyWinRT is Windows-only. Ensure the application is running on Windows and that the necessary hardware/features (e.g., Bluetooth) are enabled and functioning correctly in Windows settings.
Warnings
- breaking Significant breaking changes were introduced in v3.0.0, including a new `winrt.runtime` module, changes to `winrt.system.Object` behavior (e.g., `as_()`), and regeneration of all projections. Code written for v2.x will likely not work without modification.
- gotcha PyWinRT is a projection of Windows Runtime APIs, meaning it only functions on Windows operating systems. Attempting to use it on Linux, macOS, or other platforms will result in `ImportError` or runtime errors because the underlying WinRT APIs are unavailable.
- gotcha Most WinRT APIs exposed through PyWinRT are asynchronous and return `IAsyncOperation` or `IAsyncAction` objects. Forgetting to `await` these objects will lead to `TypeError: 'coroutine' object is not awaited` or the async operation never completing.
- gotcha While `pip install winrt-windows-devices-bluetooth` will typically pull in the core `winrt` package as a dependency, issues can arise if the dependency is not correctly resolved or if an incompatible version of `winrt` is already installed. The specific projection package itself does not contain the core runtime logic.
Install
-
pip install winrt-windows-devices-bluetooth -
pip install winrt
Imports
- BluetoothAdapter
from winrt_windows_devices_bluetooth import BluetoothAdapter
from winrt.windows.devices.bluetooth import BluetoothAdapter
- BluetoothDevice
import winrt.windows.devices.bluetooth.BluetoothDevice
from winrt.windows.devices.bluetooth import BluetoothDevice
Quickstart
import asyncio
from winrt.windows.devices.bluetooth import BluetoothAdapter
async def get_bluetooth_adapter_info():
print("Attempting to get default Bluetooth adapter...")
adapter = await BluetoothAdapter.GetDefaultAsync()
if adapter:
print(f"\nBluetooth Adapter Found:")
print(f" Device ID: {adapter.DeviceId}")
print(f" Is Low Energy Supported: {adapter.IsLowEnergySupported}")
print(f" Is Central Role Supported: {adapter.IsCentralRoleSupported}")
print(f" Is Peripheral Role Supported: {adapter.IsPeripheralRoleSupported}")
print(f" Address: {adapter.BluetoothAddress:X}")
else:
print("No Bluetooth adapter found or enabled on this system.")
print("Ensure Bluetooth is turned on in Windows settings.")
if __name__ == "__main__":
# Ensure asyncio event loop is running to await WinRT async operations
asyncio.run(get_bluetooth_adapter_info())