请教python和蓝牙相关!
我通过pycahrm的bleak库和蓝牙设备绑定,为什么句柄是对的显示找不到该句柄呢,而且找不到该句柄后就无法将PIN码写入特征值;这是什么原因啊,有没有懂得教教我。代码在下面:
遇到的现象和发生背景,请写出第一个错误信息
import asyncio
from bleak import BleakScanner, BleakClient
target_device_name = "F038051J 0181"
target_device_address = None
# 自定义扫描回调函数,在发现设备时进行匹配
def scan_callback(device, advertisement_data):
global target_device_address
target_device_address = device.address
async def scan_devices():
scanner = BleakScanner()
scanner.register_detection_callback(scan_callback)
await scanner.start()
await asyncio.sleep(10) # 扫描时间(秒)
await scanner.stop()
async def main():
await scan_devices()
if target_device_address:
print("找到血糖测试仪设备,名称:", target_device_name, "地址:", target_device_address)
async with BleakClient(target_device_address) as client:
service_uuid = "0000fff0-0000-1000-8000-00805f9b34fb"
characteristic_uuid = "0000fff1-0000-1000-8000-00805f9b34fb"
descriptor_uuid = "00002902-0000-1000-8000-00805f9b34fb"
# 写入句柄为46的描述符
descriptor_handle_1 = 46
descriptor_handle = 44
descriptor_value_1 = bytearray([0x01, 0x00])
descriptor_value = bytearray([0x01, 0x01])
await client.write_gatt_descriptor(descriptor_handle_1, descriptor_value_1)
await client.write_gatt_descriptor(descriptor_handle, descriptor_value)
# 特征句柄
await asyncio.sleep(2)
# 执行写操作
# 输入PIN码
pin_code = input("请输入PIN码: ")
# 要写入的PIN码数据
pin_data = bytearray(pin_code.encode())
await client.write_gatt_char(characteristic_uuid, bytearray(pin_code.encode()))
else:
print("未找到血糖测试仪设备:", target_device_name)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
```python
运行结果及详细报错内容
找到血糖测试仪设备,名称: F038051J 0181 地址: F8:8A:5E:9E:A3:69
Traceback (most recent call last):
File "D:\python_QT\BLE_BOND.py", line 54, in
loop.run_until_complete(main())
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 653, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "D:\python_QT\BLE_BOND.py", line 38, in main
await client.write_gatt_descriptor(descriptor_handle_1, descriptor_value_1)
File "D:\python_QT\Lib\site-packages\bleak_init_.py", line 763, in write_gatt_descriptor
await self._backend.write_gatt_descriptor(handle, data)
File "D:\python_QT\Lib\site-packages\bleak\backends\winrt\client.py", line 900, in write_gatt_descriptor
_ensure_success(
File "D:\python_QT\Lib\site-packages\bleak\backends\winrt\client.py", line 115, in _ensure_success
raise BleakError(
bleak.exc.BleakError: Could not write value bytearray(b'\x01\x00') to descriptor 002E: Protocol Error 0x05: Insufficient Authentication
```报错内容是这些