WinRT Windows.Devices.Bluetooth.GenericAttributeProfile

3.2.1 · active · verified Fri Apr 17

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `pywinrt` to list available Bluetooth LE devices on a Windows system. It shows the basic pattern for importing WinRT classes and calling asynchronous methods. To interact with GATT services (e.g., GattDeviceService), you would extend this by getting a specific `BluetoothLEDevice` and calling its `GetGattServicesAsync` method, which is conceptually shown in comments.

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())

view raw JSON →