基于BLED112的Python BLE扫描读写脚本开发求助
I totally get it—BGAPI and pygatt can feel overly clunky for basic BLE tasks. Let's use the bleak library instead: it's lightweight, has a clean API, and works seamlessly with the BLED112 adapter. Here's a step-by-step implementation that covers scanning, connecting, and reading/writing to your BLE device's characteristics.
Step 1: Install Bleak
First, install the library via pip:
pip install bleak
Step 2: Full Python Script Example
This script will scan for nearby BLE devices, connect to your target device (match by name or MAC address), locate your target service and characteristics, then perform read/write operations.
import asyncio from bleak import BleakScanner, BleakClient # Replace these with your device's actual details TARGET_DEVICE = "Your-BLE-Device-Name" # Or use MAC address like "AA:BB:CC:DD:EE:FF" TARGET_SERVICE_UUID = "0000xxxx-0000-1000-8000-00805f9b34fb" # Your service UUID TARGET_READ_CHAR_UUID = "0000yyyy-0000-1000-8000-00805f9b34fb" # Read characteristic UUID TARGET_WRITE_CHAR_UUID = "0000zzzz-0000-1000-8000-00805f9b34fb" # Write characteristic UUID async def scan_for_target_device(): """Scan nearby BLE devices and return the target if found""" print("Scanning for BLE devices...") devices = await BleakScanner.discover() for device in devices: if device.name == TARGET_DEVICE or device.address == TARGET_DEVICE: print(f"Found target device: {device.name} ({device.address})") return device print("Target device not found.") return None async def interact_with_device(device): """Connect to device, read from and write to specified characteristics""" async with BleakClient(device.address) as client: print(f"Connected to {device.name} successfully") # Locate the target service target_service = next( (s for s in client.services if s.uuid == TARGET_SERVICE_UUID), None ) if not target_service: print("Target service not found on the device.") return # Grab the read and write characteristics read_char = next( (c for c in target_service.characteristics if c.uuid == TARGET_READ_CHAR_UUID), None ) write_char = next( (c for c in target_service.characteristics if c.uuid == TARGET_WRITE_CHAR_UUID), None ) if not read_char or not write_char: print("One or both target characteristics are missing.") return # Perform read operation read_result = await client.read_gatt_char(read_char.uuid) print(f"Read data (raw bytes): {read_result}") print(f"Read data (hex): {read_result.hex()}") # If your data is an integer, uncomment this: # print(f"Read data (integer): {int.from_bytes(read_result, byteorder='little')}") # Perform write operation (example: send a string converted to bytes) write_payload = b"Hello from Python!" # Replace with your desired byte data await client.write_gatt_char(write_char.uuid, write_payload) print(f"Successfully sent: {write_payload.decode('utf-8')}") async def main(): target_device = await scan_for_target_device() if target_device: await interact_with_device(target_device) if __name__ == "__main__": asyncio.run(main())
Key Setup Tips:
- Device Matching: Using the MAC address instead of the device name is more reliable if multiple devices share the same name.
- UUIDs: Double-check that you're using the exact UUIDs for your device's service and characteristics (they're usually provided in the device's documentation).
- BLED112 Driver: On Windows, you may need to use Zadig to install the WinUSB driver for the BLED112. On Linux, add your user to the
bluetoothgroup withsudo usermod -aG bluetooth $USERto avoid permission issues. - Data Handling: BLE uses byte streams, so convert your data to bytes for writes (e.g.,
b"my data"or123.to_bytes(2, byteorder='little')) and parse bytes back to your desired format for reads.
This script skips the low-level BGAPI complexity while covering all your required operations—scanning, connecting, reading, and writing.
内容的提问来源于stack exchange,提问作者Sandrocottus

