From 2fc3848cfa08243cb581b0eece06738c6e643442 Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Tue, 30 Jul 2024 05:48:44 +0000 Subject: [PATCH 1/9] =?UTF-8?q?update=20=E4=BB=A3=E7=A0=81/client.py.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> --- "\344\273\243\347\240\201/client.py" | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git "a/\344\273\243\347\240\201/client.py" "b/\344\273\243\347\240\201/client.py" index 4ccdb2f..6155bd8 100644 --- "a/\344\273\243\347\240\201/client.py" +++ "b/\344\273\243\347\240\201/client.py" @@ -4,7 +4,6 @@ from the_uuids import UUID as UUIDs uuids = UUIDs() - def match_service_uuid(device: BLEDevice, adv: AdvertisementData): if uuids.service_uuid.lower() in adv.service_uuids: return True @@ -13,7 +12,7 @@ def match_service_uuid(device: BLEDevice, adv: AdvertisementData): async def main(): # 搜索设备, 查看是否匹配UUID,找到后可尝试建立连接,进行读写。 - device = await BleakScanner.find_device_by_filter(match_service_uuid, timeout=1000) + device = await BleakScanner.find_device_by_filter(match_service_uuid, timeout=10) # 创建BleakClient客户端,连接后进行串口操作 async with BleakClient(device) as client: @@ -25,5 +24,11 @@ async def main(): char = service.get_characteristic(uuids.characteristic_uuid) await client.write_gatt_char(char, "吃了吗".encode("utf-8")) + # 新增notify功能 2024/07/30 + await client.start_notify(char, handle_notification) + +# 接收蓝牙串口信息回调函数 2024/07/30 +def handle_notification(sender, data): + print(f"Notification received: {data.decode('utf-8')}") asyncio.run(main()) -- Gitee From 169ec09ae9bf4c0aaa5a05e77bcfd554f2a6b252 Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Tue, 30 Jul 2024 05:49:40 +0000 Subject: [PATCH 2/9] =?UTF-8?q?update=20=E4=BB=A3=E7=A0=81/server.py.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> --- "\344\273\243\347\240\201/server.py" | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git "a/\344\273\243\347\240\201/server.py" "b/\344\273\243\347\240\201/server.py" index 64bfe55..b0f4672 100644 --- "a/\344\273\243\347\240\201/server.py" +++ "b/\344\273\243\347\240\201/server.py" @@ -21,16 +21,18 @@ else: trigger = asyncio.Event() -def write_request(characteristic: BlessGATTCharacteristic, value: Any, **kwargs): - characteristic.value = value - print(value.decode('utf-8')) - - async def run(loop): trigger.clear() # Instantiate the server my_service_name = "Test Service" server = BlessServer(name=my_service_name, loop=loop) + + def write_request(characteristic: BlessGATTCharacteristic, value: Any, **kwargs): + characteristic.value = value + print(value.decode('utf-8')) + characteristic.value = '我吃了'.encode('utf-8') + server.update_value(uuids.service_uuid, uuids.characteristic_uuid) + server.write_request_func = write_request # Add Service @@ -39,7 +41,7 @@ async def run(loop): # Add a Characteristic to the service char_flags = ( GATTCharacteristicProperties.write - | GATTCharacteristicProperties.indicate + | GATTCharacteristicProperties.notify ) permissions = GATTAttributePermissions.readable | GATTAttributePermissions.writeable await server.add_new_characteristic( @@ -48,6 +50,7 @@ async def run(loop): await server.start() if trigger.__module__ == "threading": + # noinspection PyAsyncCall trigger.wait() else: await trigger.wait() -- Gitee From 934e98712cc31e71059026da5d1ea332e61defa6 Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Tue, 30 Jul 2024 05:50:10 +0000 Subject: [PATCH 3/9] =?UTF-8?q?update=20=E4=BB=A3=E7=A0=81/client.py.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> --- "\344\273\243\347\240\201/client.py" | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git "a/\344\273\243\347\240\201/client.py" "b/\344\273\243\347\240\201/client.py" index 6155bd8..79923bb 100644 --- "a/\344\273\243\347\240\201/client.py" +++ "b/\344\273\243\347\240\201/client.py" @@ -4,31 +4,30 @@ from the_uuids import UUID as UUIDs uuids = UUIDs() + def match_service_uuid(device: BLEDevice, adv: AdvertisementData): if uuids.service_uuid.lower() in adv.service_uuids: return True return False +def handle_notification(sender, data): + print(f"Notification received: {data.decode('utf-8')}") + + async def main(): # 搜索设备, 查看是否匹配UUID,找到后可尝试建立连接,进行读写。 device = await BleakScanner.find_device_by_filter(match_service_uuid, timeout=10) # 创建BleakClient客户端,连接后进行串口操作 async with BleakClient(device) as client: - print("Connected") service = client.services.get_service(uuids.service_uuid) # 接收蓝牙串口信息 char = service.get_characteristic(uuids.characteristic_uuid) - await client.write_gatt_char(char, "吃了吗".encode("utf-8")) - - # 新增notify功能 2024/07/30 await client.start_notify(char, handle_notification) + await client.write_gatt_char(char, "吃了吗".encode("utf-8")) -# 接收蓝牙串口信息回调函数 2024/07/30 -def handle_notification(sender, data): - print(f"Notification received: {data.decode('utf-8')}") asyncio.run(main()) -- Gitee From b3cfbf84a63bff5b8de75af74a93026f89791481 Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Tue, 30 Jul 2024 05:51:12 +0000 Subject: [PATCH 4/9] =?UTF-8?q?update=20=E4=BB=A3=E7=A0=81/=E8=8B=B1?= =?UTF-8?q?=E8=AF=AD=E6=9C=AF=E8=AF=AD=E5=AF=B9=E7=85=A7=E8=A1=A8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> --- ...46\234\257\350\257\255\345\257\271\347\205\247\350\241\250" | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git "a/\344\273\243\347\240\201/\350\213\261\350\257\255\346\234\257\350\257\255\345\257\271\347\205\247\350\241\250" "b/\344\273\243\347\240\201/\350\213\261\350\257\255\346\234\257\350\257\255\345\257\271\347\205\247\350\241\250" index 8966ee7..021389c 100644 --- "a/\344\273\243\347\240\201/\350\213\261\350\257\255\346\234\257\350\257\255\345\257\271\347\205\247\350\241\250" +++ "b/\344\273\243\347\240\201/\350\213\261\350\257\255\346\234\257\350\257\255\345\257\271\347\205\247\350\241\250" @@ -16,4 +16,5 @@ my_service_name | 我的服务名称 my_service_uuid | 我的服务UUID my_char_uuid | 我的特征UUID char_flags | 特征标志 -permissions | 权限 \ No newline at end of file +permissions | 权限 +notify | 通知 \ No newline at end of file -- Gitee From 6129a3cbe02008663557720657b1ee715eee4b33 Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Wed, 31 Jul 2024 02:45:09 +0000 Subject: [PATCH 5/9] =?UTF-8?q?update=20=E4=BB=A3=E7=A0=81/README.md.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> --- "\344\273\243\347\240\201/README.md" | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git "a/\344\273\243\347\240\201/README.md" "b/\344\273\243\347\240\201/README.md" index 4938bb5..e9e9556 100644 --- "a/\344\273\243\347\240\201/README.md" +++ "b/\344\273\243\347\240\201/README.md" @@ -9,13 +9,14 @@ 1、2两步可互换顺序。 -1. 甲机server启动 -2. 乙机client启动 -3. 乙机client连接成功后打印connected -4. 甲机server打印“吃了吗” -5. 乙机client自动退出 -6. 甲机server保持开启,乙机再次启动client -7. 回到第3步 +1、甲机server启动 +2、乙机client启动 +3、乙机client连接成功后打印connected +4、甲机server打印“吃了吗” +5、乙机client打印“我吃了” +6、乙机client自动退出 +7、甲机server保持开启,乙机再次启动client +8、回到第3步 ### 测试结果 @@ -26,6 +27,7 @@ | Win11 | Mac 14.5 | 成功 | | Win11 | Win11 外设属性否 | 成功发送一次,之后客户端启动发送失败 | | Win11 外设属性否 | | 服务端启动失败,报错“设备不支持命令功能” | +| Win11 | Win10 | 成功 | ## 运行机制 -- Gitee From b098cf9da32b1c3a3b9958c07d9f77fec2113b0d Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Wed, 31 Jul 2024 10:18:13 +0000 Subject: [PATCH 6/9] =?UTF-8?q?update=20=E4=BB=A3=E7=A0=81/README.md.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> --- "\344\273\243\347\240\201/README.md" | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git "a/\344\273\243\347\240\201/README.md" "b/\344\273\243\347\240\201/README.md" index e9e9556..c42e78f 100644 --- "a/\344\273\243\347\240\201/README.md" +++ "b/\344\273\243\347\240\201/README.md" @@ -9,14 +9,14 @@ 1、2两步可互换顺序。 -1、甲机server启动 -2、乙机client启动 -3、乙机client连接成功后打印connected -4、甲机server打印“吃了吗” -5、乙机client打印“我吃了” -6、乙机client自动退出 -7、甲机server保持开启,乙机再次启动client -8、回到第3步 +1. 甲机server启动 +2. 乙机client启动 +3. 乙机client连接成功后打印connected +4. 甲机server打印“吃了吗” +5. 乙机client打印“我吃了” +6. 乙机client自动退出 +7. 甲机server保持开启,乙机再次启动client +8. 回到第3步 ### 测试结果 -- Gitee From ee60b8f2bb0a5e553e057b261160c59eb32ddde0 Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Wed, 31 Jul 2024 11:11:21 +0000 Subject: [PATCH 7/9] =?UTF-8?q?update=20=E4=BB=A3=E7=A0=81/server.py.=20?= =?UTF-8?q?=E5=8E=9Fwrite=5Frequest=E7=AC=AC=E4=B8=80=E8=A1=8C=20=E5=A4=9A?= =?UTF-8?q?=E4=BD=99=20=E5=B7=B2=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> --- "\344\273\243\347\240\201/server.py" | 1 - 1 file changed, 1 deletion(-) diff --git "a/\344\273\243\347\240\201/server.py" "b/\344\273\243\347\240\201/server.py" index b0f4672..cd15252 100644 --- "a/\344\273\243\347\240\201/server.py" +++ "b/\344\273\243\347\240\201/server.py" @@ -28,7 +28,6 @@ async def run(loop): server = BlessServer(name=my_service_name, loop=loop) def write_request(characteristic: BlessGATTCharacteristic, value: Any, **kwargs): - characteristic.value = value print(value.decode('utf-8')) characteristic.value = '我吃了'.encode('utf-8') server.update_value(uuids.service_uuid, uuids.characteristic_uuid) -- Gitee From d673dead1f48f6844d9a4a95019b5e77c9c85e5b Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Thu, 1 Aug 2024 12:27:12 +0000 Subject: [PATCH 8/9] =?UTF-8?q?update=20=E4=BB=A3=E7=A0=81/client.py.=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0logging=20=E4=BD=BF=E5=85=B6=E8=83=BD?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E8=AF=A6=E7=BB=86=E7=9A=84=E8=B0=83=E8=AF=95?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=20=E6=B7=BB=E5=8A=A0=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E5=A4=84=E7=90=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> --- "\344\273\243\347\240\201/client.py" | 59 +++++++++++++++++++--------- 1 file changed, 41 insertions(+), 18 deletions(-) diff --git "a/\344\273\243\347\240\201/client.py" "b/\344\273\243\347\240\201/client.py" index 79923bb..bdd0ff9 100644 --- "a/\344\273\243\347\240\201/client.py" +++ "b/\344\273\243\347\240\201/client.py" @@ -1,33 +1,56 @@ import asyncio +import logging from bleak import BleakScanner, BleakClient, BLEDevice, AdvertisementData from the_uuids import UUID as UUIDs -uuids = UUIDs() +# Configure logging +logging.basicConfig( + level=logging.INFO, + datefmt='%H:%M:%S', + format='%(asctime)s [%(levelname)s] %(message)s' +) + +logger = logging.getLogger(__name__) +uuids = UUIDs() def match_service_uuid(device: BLEDevice, adv: AdvertisementData): if uuids.service_uuid.lower() in adv.service_uuids: + logger.info(f"Found matching device: {device.name} ({device.address})") return True return False def handle_notification(sender, data): - print(f"Notification received: {data.decode('utf-8')}") - + logger.info(f"Received data: {data.decode('utf-8')}") async def main(): - # 搜索设备, 查看是否匹配UUID,找到后可尝试建立连接,进行读写。 - device = await BleakScanner.find_device_by_filter(match_service_uuid, timeout=10) - - # 创建BleakClient客户端,连接后进行串口操作 - async with BleakClient(device) as client: - print("Connected") - - service = client.services.get_service(uuids.service_uuid) - # 接收蓝牙串口信息 - char = service.get_characteristic(uuids.characteristic_uuid) - await client.start_notify(char, handle_notification) - await client.write_gatt_char(char, "吃了吗".encode("utf-8")) - - -asyncio.run(main()) + try: + # Search for devices and check if they match the UUID + device = await BleakScanner.find_device_by_filter(match_service_uuid, timeout=20) + logger.info("Device search completed.") + + # Create a BleakClient instance and connect, then perform read/write operations + async with BleakClient(device) as client: + logger.info("Connected to the device.") + service = client.services.get_service(uuids.service_uuid) + logger.info(f"Service {service} retrieved.") + + # Receive Bluetooth serial port information + char = service.get_characteristic(uuids.characteristic_uuid) + logger.info(f"Characteristic {char} retrieved.") + + await client.start_notify(char, handle_notification) + logger.info("Notification started.") + + await client.write_gatt_char(char, "吃了吗".encode("utf-8")) + logger.info("Data written to characteristic.") + + except AttributeError: + logger.error("No matching device found.") + except Exception as e: + logger.error(f"An unexpected error occurred: {e}") + + +if __name__ == "__main__": + asyncio.run(main()) \ No newline at end of file -- Gitee From fea2945f7b94749b8c40756b2d64a7aee8088393 Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Thu, 1 Aug 2024 12:28:06 +0000 Subject: [PATCH 9/9] =?UTF-8?q?update=20=E4=BB=A3=E7=A0=81/server.py.=20?= =?UTF-8?q?=E4=BD=BF=E7=94=A8logging=20=E4=BD=BF=E5=85=B6=E8=83=BD?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E6=9B=B4=E8=AF=A6=E7=BB=86=E7=9A=84=E8=B0=83?= =?UTF-8?q?=E8=AF=95=E4=BF=A1=E6=81=AF=20=E4=B8=8E=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> --- "\344\273\243\347\240\201/server.py" | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git "a/\344\273\243\347\240\201/server.py" "b/\344\273\243\347\240\201/server.py" index cd15252..1c8ca6d 100644 --- "a/\344\273\243\347\240\201/server.py" +++ "b/\344\273\243\347\240\201/server.py" @@ -1,6 +1,7 @@ import sys import asyncio import threading +import logging from typing import Any, Union @@ -11,6 +12,16 @@ from bless import GATTAttributePermissions from the_uuids import UUID as UUIDs +# Configure logging +logging.basicConfig( + level = logging.DEBUG, + datefmt='%H:%M:%S', + format = '%(asctime)s [%(levelname)s] %(message)s', + handlers=[logging.FileHandler("debug.log"),logging.StreamHandler()] +) + +logger = logging.getLogger(__name__) + uuids = UUIDs() # NOTE: Some systems require different synchronization methods. @@ -23,12 +34,13 @@ else: async def run(loop): trigger.clear() + logger.info("Starting server...") # Instantiate the server my_service_name = "Test Service" server = BlessServer(name=my_service_name, loop=loop) def write_request(characteristic: BlessGATTCharacteristic, value: Any, **kwargs): - print(value.decode('utf-8')) + logger.info(f"Received data: {value.decode('utf-8')}") characteristic.value = '我吃了'.encode('utf-8') server.update_value(uuids.service_uuid, uuids.characteristic_uuid) @@ -36,6 +48,7 @@ async def run(loop): # Add Service await server.add_new_service(uuids.service_uuid) + logger.info("Service added.") # Add a Characteristic to the service char_flags = ( @@ -46,8 +59,11 @@ async def run(loop): await server.add_new_characteristic( uuids.service_uuid, uuids.characteristic_uuid, char_flags, None, permissions ) + logger.info("Characteristic added.") await server.start() + logger.info("Server started.") + if trigger.__module__ == "threading": # noinspection PyAsyncCall trigger.wait() @@ -55,7 +71,11 @@ async def run(loop): await trigger.wait() await server.stop() + logger.info("Server stopped.") +try: + loop = asyncio.get_event_loop() + loop.run_until_complete(run(loop)) -loop = asyncio.get_event_loop() -loop.run_until_complete(run(loop)) +except Exception as e: + logger.error(f"An error occurred: {e}") \ No newline at end of file -- Gitee