From f1575be985d21ee9d24cfec85ddc1d2cf1f3e33e Mon Sep 17 00:00:00 2001 From: b1nmar <> Date: Mon, 25 May 2026 13:10:50 +0200 Subject: [PATCH] fix: make BLE compatible with macOS by handling missing BlueZ import BlueZ is Linux-only. On macOS, importing BleakClientBlueZDBus raises ImportError. Wrap the import in a try/except and guard the isinstance check so the printer works on both Linux and macOS. Co-Authored-By: Claude Sonnet 4.6 --- catprinter/ble.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/catprinter/ble.py b/catprinter/ble.py index c3ae9d0..2a5893f 100644 --- a/catprinter/ble.py +++ b/catprinter/ble.py @@ -6,7 +6,11 @@ from bleak import BleakClient, BleakScanner from bleak.backends.scanner import AdvertisementData from bleak.backends.device import BLEDevice -from bleak.backends.bluezdbus.client import BleakClientBlueZDBus +# In MacOS capture exception trying Linux library import and set a flag +try: + from bleak.backends.bluezdbus.client import BleakClientBlueZDBus +except ImportError: + BleakClientBlueZDBus = None from catprinter import logger # For some reason, bleak reports the 0xaf30 service on my macOS, while it reports @@ -99,7 +103,8 @@ async def run_ble(data, device: Optional[str]): async with BleakClient(address) as client: # XXX: BlueZ incorrectly reports a fixed MTU of 23; force MTU negotiation manually. # https://bleak.readthedocs.io/en/latest/api/client.html#bleak.BleakClient.mtu_size - if isinstance(client, BleakClientBlueZDBus): + # Only use this library on Linux, not MacOS + if BleakClientBlueZDBus and isinstance(client, BleakClientBlueZDBus): await client._acquire_mtu() logger.info(f"✅ Connected: {client.is_connected}; MTU: {client.mtu_size}")