无法读取Beurer BF950蓝牙体重秤体重测量特征值的问题求助
无法读取Beurer BF950蓝牙体重秤体重测量特征值的问题求助
我手头有一台支持BLE的Beurer BF950体重秤,现在遇到了个棘手的问题:没法从设备上读取体重数据。厂商给了一份包含UUID的Excel表,我照着写了代码,但体重测量的回调函数根本不会被触发——虽然能成功订阅服务,但就是一直等不到数据。奇怪的是,用官方APP就能正常获取体重,说明设备本身是能正常传输数据的。
先给大家看看我最开始写的代码:
import asyncio from bleak import BleakClient from struct import unpack BEURER_BF950_ADDRESS = "5C:CA:D3:F8:1C:C6" WEIGHT_SCALE_SERVICE_UUID = "0000181d-0000-1000-8000-00805f9b34fb" WEIGHT_MEASUREMENT_CHARACTERISTIC_UUID = "00002a9d-0000-1000-8000-00805f9b34fb" async def connect_and_read_weight(address): try: async with BleakClient(address) as client: print(f"Connected: {client.is_connected}") await client.start_notify(WEIGHT_MEASUREMENT_CHARACTERISTIC_UUID, weight_measurement_callback) print("Please step on the scale...") await asyncio.sleep(30) await client.stop_notify(WEIGHT_MEASUREMENT_CHARACTERISTIC_UUID) except Exception as e: print(f"Error: {e}") def weight_measurement_callback(sender, data): # print("here") flags = data[0] weight = unpack("<H", data[1:3])[0] * 0.005 print(f"Weight: {weight:.2f} kg") async def main(): print("Attempting to connect and read weight...") await connect_and_read_weight(BEURER_BF950_ADDRESS) print("Operation completed.") asyncio.run(main())
这段代码里,我能成功连接设备,也执行了start_notify,但weight_measurement_callback就是没反应——哪怕我站到秤上也没用。
不过我确认设备连接是正常的,因为用下面这段代码能读取到设备的静态特征值,比如电池电量、厂商信息这些:
import asyncio from bleak import BleakClient BEURER_BF950_ADDRESS = "5C:CA:D3:F8:1C:C6" # List of characteristics to read CHARACTERISTICS_TO_READ = [ ("00002a9e-0000-1000-8000-00805f9b34fb", "Weight Scale Feature"), ("00002a19-0000-1000-8000-00805f9b34fb", "Battery Level"), ("00002a29-0000-1000-8000-00805f9b34fb", "Manufacturer Name"), ("00002a24-0000-1000-8000-00805f9b34fb", "Model Number"), ("00002a26-0000-1000-8000-00805f9b34fb", "Firmware Revision"), ("00002a27-0000-1000-8000-00805f9b34fb", "Hardware Revision"), ("00002a28-0000-1000-8000-00805f9b34fb", "Software Revision"), ("00002a23-0000-1000-8000-00805f9b34fb", "System ID"), ("0000000b-0000-1000-8000-00805f9b34fb", "Custom Characteristic (Reference Weight/BF)"), ("00000000-0000-1000-8000-00805f9b34fb", "Custom Characteristic (Scale Setting)"), ] async def read_characteristics(address): try: async with BleakClient(address) as client: print(f"Connected: {client.is_connected}") for uuid, name in CHARACTERISTICS_TO_READ: try: value = await client.read_gatt_char(uuid) print(f"{name} ({uuid}):") print(f" Raw: {value.hex()}") if name == "Battery Level": print(f" Parsed: {value[0]}%") elif name == "Manufacturer Name" or name == "Model Number": print(f" Parsed: {value.decode('utf-8')}") else: print(f" Parsed: {value}") except Exception as e: print(f"Error reading {name} ({uuid}): {e}") except Exception as e: print(f"Connection error: {e}") async def main(): print("Attempting to connect and read characteristics...") await read_characteristics(BEURER_BF950_ADDRESS) print("Operation completed.") asyncio.run(main())
后来我还试了用自定义服务UUID,加上触发测量的特征,还用asyncio.Event来等待数据,但还是没用:
import asyncio from bleak import BleakClient from struct import unpack BEURER_BF950_ADDRESS = "5C:CA:D3:F8:1C:C6" CUSTOM_SERVICE_UUID = "0000ffff-0000-1000-8000-00805f9b34fb" TAKE_MEASUREMENT_CHARACTERISTIC_UUID = "00000006-0000-1000-8000-00805f9b34fb" MEASUREMENT_NOTIFY_CHARACTERISTIC_UUID = "00000007-0000-1000-8000-00805f9b34fb" weight_received = asyncio.Event() async def connect_and_read_weight(address): try: async with BleakClient(address) as client: print(f"Connected: {client.is_connected}") print("Enabling notifications for custom measurement characteristic...") await client.start_notify(MEASUREMENT_NOTIFY_CHARACTERISTIC_UUID, measurement_callback) print("Triggering measurement...") await client.write_gatt_char(TAKE_MEASUREMENT_CHARACTERISTIC_UUID, b'\x00') print("Please step on the scale...") await asyncio.wait_for(weight_received.wait(), timeout=60.0) print("Disabling notifications...") await client.stop_notify(MEASUREMENT_NOTIFY_CHARACTERISTIC_UUID) except asyncio.TimeoutError: print("Timeout: No measurement received within 60 seconds.") except Exception as e: print(f"Error: {e}") def measurement_callback(sender, data): print(f"Received data: {data.hex()}") try: # The exact data format may vary, this is a guess based on common formats if len(data) >= 2: weight = unpack("<H", data[:2])[0] * 0.1 # Assuming weight is first 2 bytes, in 0.1 kg units print(f"Weight: {weight:.1f} kg") weight_received.set() else: print("Received data is too short to contain a weight measurement") except Exception as e: print(f"Error parsing data: {e}") async def main(): print("Attempting to connect and read weight...") await connect_and_read_weight(BEURER_BF950_ADDRESS) print("Operation completed.") asyncio.run(main())
这段代码最后只会超时,measurement_callback也没被触发过。
有没有大佬能帮我看看问题出在哪?是不是我漏了什么步骤?比如是不是需要先写某个特征值来唤醒秤的测量模式?或者UUID用错了?毕竟官方APP能正常工作,肯定是我代码里有没考虑到的地方。
备注:内容来源于stack exchange,提问作者uzair khan
相关产品推荐
相关产品推荐

