From 5e7c27001b69caf295469eb5507f6ef610e0f6bb Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Sun, 4 Aug 2024 02:55:41 +0000 Subject: [PATCH 01/22] =?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" | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git "a/\344\273\243\347\240\201/client.py" "b/\344\273\243\347\240\201/client.py" index bc00cf6..0a748c9 100644 --- "a/\344\273\243\347\240\201/client.py" +++ "b/\344\273\243\347\240\201/client.py" @@ -22,7 +22,11 @@ def 匹配服务识别码(设备: BLEDevice, adv: AdvertisementData): def 处理通知(sender, 数据): - 日志.info(f"Received data: {数据.decode('utf-8')}") + try: + 日志.info(f"Received data: {数据.decode('utf-8')}") + + except Exception as e: + 日志.error(f"An error occurred: {e}") async def 中(): try: @@ -43,14 +47,18 @@ async def 中(): await 客户端.start_notify(特征, 处理通知) 日志.info("Notification started.") - await 客户端.write_gatt_char(特征, "吃了吗".encode("utf-8")) - 日志.info("Data written to characteristic.") - + while True: + find_path = input("请输入路径(输入exit退出): ") + if find_path == "exit": + break + await 客户端.write_gatt_char(特征, find_path.encode("utf-8")) + 日志.info("Data written to characteristic.") + except AttributeError: 日志.error("No matching device found.") except Exception as e: - 日志.error(f"An unexpected error occurred: {e}") - + 日志.error(f"An error occurred: {e}") if __name__ == "__main__": - asyncio.run(中()) \ No newline at end of file + asyncio.run(中()) + -- Gitee From 1b54ecad2e0591871a489276a6677086494aa215 Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Sun, 4 Aug 2024 02:56:06 +0000 Subject: [PATCH 02/22] =?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" | 105 +++++++++++++++------------ 1 file changed, 58 insertions(+), 47 deletions(-) diff --git "a/\344\273\243\347\240\201/server.py" "b/\344\273\243\347\240\201/server.py" index b6d77f1..95c8d57 100644 --- "a/\344\273\243\347\240\201/server.py" +++ "b/\344\273\243\347\240\201/server.py" @@ -1,3 +1,4 @@ +import os import sys import asyncio import threading @@ -31,55 +32,65 @@ if sys.platform in ["darwin", "win32"]: else: 触发器 = asyncio.Event() +# [蓝牙核心规范(V5.2)7.6-深入详解之GATT(1)](https://bbs.huaweicloud.com/blogs/detail/309098) +# “属性协议”部分描述了GATT协议包的各个部分与长度限制,其中属性值的大小限制为 512b +def write_request(characteristic: BlessGATTCharacteristic, value: Any, **kwargs): + try: + find_path = value.decode('utf-8') + if os.path.exists(find_path): + characteristic.value = "存在".encode('utf-8') + else: + characteristic.value = "不存在".encode('utf-8') -async def 中(loop): - 触发器.clear() - 日志.info("Starting server...") - # Instantiate the server - my_service_name = "Test Service" - 服务器 = BlessServer(name=my_service_name, loop=loop) - - def write_request(characteristic: BlessGATTCharacteristic, value: Any, **kwargs): - 日志.info(f"Received data: {value.decode('utf-8')}") - - # [蓝牙核心规范(V5.2)7.6-深入详解之GATT(1)](https://bbs.huaweicloud.com/blogs/detail/309098) - # “属性协议”部分描述了GATT协议包的各个部分与长度限制,其中属性值的大小限制为 512b - characteristic.value = '我吃了'.encode('utf-8') 服务器.update_value(各识别码.服务识别码, 各识别码.特征识别码) - 日志.info("Updated!") - - 服务器.write_request_func = write_request - - # Add Service - await 服务器.add_new_service(各识别码.服务识别码) - 日志.info("Service added.") - - # Add a Characteristic to the service - char_flags = ( - GATTCharacteristicProperties.write - | GATTCharacteristicProperties.notify - ) - permissions = GATTAttributePermissions.readable | GATTAttributePermissions.writeable - await 服务器.add_new_characteristic( - 各识别码.服务识别码, 各识别码.特征识别码, char_flags, None, permissions - ) - 日志.info("Characteristic added.") - - await 服务器.start() - 日志.info("Server started.") - - if 触发器.__module__ == "threading": - # noinspection PyAsyncCall - 触发器.wait() - else: - await 触发器.wait() - - await 服务器.stop() - 日志.info("Server stopped.") - -try: + 日志.info(f"Updated! Find:{find_path} {characteristic.value.decode()}") + + except Exception as e: + 日志.error(f"An error occurred: {e}") +async def 中(loop): + try: + 触发器.clear() + 日志.info("Starting server...") + + # Instantiate the server + global 服务器 + my_service_name = "Test Service" + 服务器 = BlessServer(name=my_service_name, loop=loop) + 服务器.write_request_func = write_request + + # Add Service + await 服务器.add_new_service(各识别码.服务识别码) + 日志.info("Service added.") + + # Add a Characteristic to the service + char_flags = ( + GATTCharacteristicProperties.write + | GATTCharacteristicProperties.notify + ) + permissions = GATTAttributePermissions.readable | GATTAttributePermissions.writeable + await 服务器.add_new_characteristic( + 各识别码.服务识别码, 各识别码.特征识别码, char_flags, None, permissions + ) + 日志.info("Characteristic added.") + + await 服务器.start() + 日志.info("Server started.") + + + + if 触发器.__module__ == "threading": + # noinspection PyAsyncCall + 触发器.wait() + else: + await 触发器.wait() + + await 服务器.stop() + 日志.info("Server stopped.") + + except Exception as e: + 日志.error(f"An error occurred: {e}") + +if __name__ == "__main__": loop = asyncio.get_event_loop() loop.run_until_complete(中(loop)) -except Exception as e: - 日志.error(f"An error occurred: {e}") \ No newline at end of file -- Gitee From d3aa9a7786960d98557b06cd5c633ac8fb51202d Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Sun, 4 Aug 2024 02:57:21 +0000 Subject: [PATCH 03/22] =?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" | 1 + 1 file changed, 1 insertion(+) diff --git "a/\344\273\243\347\240\201/README.md" "b/\344\273\243\347\240\201/README.md" index c5bb300..19a356a 100644 --- "a/\344\273\243\347\240\201/README.md" +++ "b/\344\273\243\347\240\201/README.md" @@ -27,6 +27,7 @@ | Win11 | Mac 14.5 | 成功 | | Win11 | Win11 外设属性否 | 【待测】成功发送一次,之后客户端启动发送失败 | | Win11 外设属性否 | | 【待测】服务端启动失败,报错“设备不支持命令功能” | +| Win11 | Win10 | 成功 | ## 运行机制 -- Gitee From 62f1a51a865062fb80bdf40c669b63354ccb7008 Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Thu, 8 Aug 2024 05:02:55 +0000 Subject: [PATCH 04/22] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20?= =?UTF-8?q?=E4=BB=A3=E7=A0=81/client.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\344\273\243\347\240\201/client.py" | 64 ---------------------------- 1 file changed, 64 deletions(-) delete mode 100644 "\344\273\243\347\240\201/client.py" diff --git "a/\344\273\243\347\240\201/client.py" "b/\344\273\243\347\240\201/client.py" deleted file mode 100644 index 0a748c9..0000000 --- "a/\344\273\243\347\240\201/client.py" +++ /dev/null @@ -1,64 +0,0 @@ -import asyncio -import logging -from bleak import BleakScanner, BleakClient, BLEDevice, AdvertisementData -from the_uuids import 唯一识别码 - -# Configure logging -logging.basicConfig( - level=logging.INFO, - datefmt='%H:%M:%S', - format='%(asctime)s [%(levelname)s] %(message)s' -) - -日志 = logging.getLogger(__name__) - -各识别码 = 唯一识别码() - -def 匹配服务识别码(设备: BLEDevice, adv: AdvertisementData): - if 各识别码.服务识别码.lower() in adv.service_uuids: - 日志.info(f"Found matching device: {设备.name} ({设备.address})") - return True - return False - - -def 处理通知(sender, 数据): - try: - 日志.info(f"Received data: {数据.decode('utf-8')}") - - except Exception as e: - 日志.error(f"An error occurred: {e}") - -async def 中(): - try: - # Search for devices and check if they match the UUID - 设备 = await BleakScanner.find_device_by_filter(匹配服务识别码, timeout=20) - 日志.info("Device search completed.") - - # Create a BleakClient instance and connect, then perform read/write operations - async with BleakClient(设备) as 客户端: - 日志.info("Connected to the device.") - 服务 = 客户端.services.get_service(各识别码.服务识别码) - 日志.info(f"Service {服务} retrieved.") - - # Receive Bluetooth serial port information - 特征 = 服务.get_characteristic(各识别码.特征识别码) - 日志.info(f"Characteristic {特征} retrieved.") - - await 客户端.start_notify(特征, 处理通知) - 日志.info("Notification started.") - - while True: - find_path = input("请输入路径(输入exit退出): ") - if find_path == "exit": - break - await 客户端.write_gatt_char(特征, find_path.encode("utf-8")) - 日志.info("Data written to characteristic.") - - except AttributeError: - 日志.error("No matching device found.") - except Exception as e: - 日志.error(f"An error occurred: {e}") - -if __name__ == "__main__": - asyncio.run(中()) - -- Gitee From 44a7e107c19c7dae219c2213f1a87c6594549573 Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Thu, 8 Aug 2024 05:03:05 +0000 Subject: [PATCH 05/22] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20?= =?UTF-8?q?=E4=BB=A3=E7=A0=81/server.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\344\273\243\347\240\201/server.py" | 96 ---------------------------- 1 file changed, 96 deletions(-) delete mode 100644 "\344\273\243\347\240\201/server.py" diff --git "a/\344\273\243\347\240\201/server.py" "b/\344\273\243\347\240\201/server.py" deleted file mode 100644 index 95c8d57..0000000 --- "a/\344\273\243\347\240\201/server.py" +++ /dev/null @@ -1,96 +0,0 @@ -import os -import sys -import asyncio -import threading -import logging - -from typing import Any, Union - -from bless import BlessServer -from bless import BlessGATTCharacteristic -from bless import GATTCharacteristicProperties -from bless import GATTAttributePermissions - -from the_uuids import 唯一识别码 - -# 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()] -) - -日志 = logging.getLogger(__name__) - -各识别码 = 唯一识别码() - -# NOTE: Some systems require different synchronization methods. -触发器: Union[asyncio.Event, threading.Event] -if sys.platform in ["darwin", "win32"]: - 触发器 = threading.Event() -else: - 触发器 = asyncio.Event() - -# [蓝牙核心规范(V5.2)7.6-深入详解之GATT(1)](https://bbs.huaweicloud.com/blogs/detail/309098) -# “属性协议”部分描述了GATT协议包的各个部分与长度限制,其中属性值的大小限制为 512b -def write_request(characteristic: BlessGATTCharacteristic, value: Any, **kwargs): - try: - find_path = value.decode('utf-8') - if os.path.exists(find_path): - characteristic.value = "存在".encode('utf-8') - else: - characteristic.value = "不存在".encode('utf-8') - - 服务器.update_value(各识别码.服务识别码, 各识别码.特征识别码) - 日志.info(f"Updated! Find:{find_path} {characteristic.value.decode()}") - - except Exception as e: - 日志.error(f"An error occurred: {e}") -async def 中(loop): - try: - 触发器.clear() - 日志.info("Starting server...") - - # Instantiate the server - global 服务器 - my_service_name = "Test Service" - 服务器 = BlessServer(name=my_service_name, loop=loop) - 服务器.write_request_func = write_request - - # Add Service - await 服务器.add_new_service(各识别码.服务识别码) - 日志.info("Service added.") - - # Add a Characteristic to the service - char_flags = ( - GATTCharacteristicProperties.write - | GATTCharacteristicProperties.notify - ) - permissions = GATTAttributePermissions.readable | GATTAttributePermissions.writeable - await 服务器.add_new_characteristic( - 各识别码.服务识别码, 各识别码.特征识别码, char_flags, None, permissions - ) - 日志.info("Characteristic added.") - - await 服务器.start() - 日志.info("Server started.") - - - - if 触发器.__module__ == "threading": - # noinspection PyAsyncCall - 触发器.wait() - else: - await 触发器.wait() - - await 服务器.stop() - 日志.info("Server stopped.") - - except Exception as e: - 日志.error(f"An error occurred: {e}") - -if __name__ == "__main__": - loop = asyncio.get_event_loop() - loop.run_until_complete(中(loop)) - -- Gitee From 043a9c6bd2daf73c97bb6094cd0c9d447f848ac9 Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Thu, 8 Aug 2024 05:03:14 +0000 Subject: [PATCH 06/22] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20?= =?UTF-8?q?=E4=BB=A3=E7=A0=81/the=5Fuuids.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\344\273\243\347\240\201/the_uuids.py" | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 "\344\273\243\347\240\201/the_uuids.py" diff --git "a/\344\273\243\347\240\201/the_uuids.py" "b/\344\273\243\347\240\201/the_uuids.py" deleted file mode 100644 index 964b79e..0000000 --- "a/\344\273\243\347\240\201/the_uuids.py" +++ /dev/null @@ -1,4 +0,0 @@ -class 唯一识别码: - def __init__(self): - self.服务识别码 = "A07498CA-AD5B-474E-940D-16F1FBE7E8CD" - self.特征识别码 = "51FF12BB-3ED8-46E5-B4F9-D64E2FEC021B" -- Gitee From 2612fd379288e1986fda89149434de1dd40366a6 Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Thu, 8 Aug 2024 05:03:32 +0000 Subject: [PATCH 07/22] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E7=94=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\344\273\243\347\240\201/\347\224\262/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\344\273\243\347\240\201/\347\224\262/.keep" diff --git "a/\344\273\243\347\240\201/\347\224\262/.keep" "b/\344\273\243\347\240\201/\347\224\262/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 2b01e411c702dfe8490b66829ded1cde17a8cd9c Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Thu, 8 Aug 2024 05:03:46 +0000 Subject: [PATCH 08/22] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E4=B9=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\344\273\243\347\240\201/\344\271\231/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\344\273\243\347\240\201/\344\271\231/.keep" diff --git "a/\344\273\243\347\240\201/\344\271\231/.keep" "b/\344\273\243\347\240\201/\344\271\231/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 3b618d0fc31f4bccab1f27d7a9f24d5627a7f851 Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Thu, 8 Aug 2024 05:05:43 +0000 Subject: [PATCH 09/22] =?UTF-8?q?add=20=E4=BB=A3=E7=A0=81/=E7=94=B2/client?= =?UTF-8?q?.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> --- .../\347\224\262/client.py" | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 "\344\273\243\347\240\201/\347\224\262/client.py" diff --git "a/\344\273\243\347\240\201/\347\224\262/client.py" "b/\344\273\243\347\240\201/\347\224\262/client.py" new file mode 100644 index 0000000..64d4b3a --- /dev/null +++ "b/\344\273\243\347\240\201/\347\224\262/client.py" @@ -0,0 +1,54 @@ +import sys +import asyncio +import aioconsole + +from bleak import BLEDevice +from bleak import BleakClient +from bleak import BleakScanner +from bleak import AdvertisementData + + +service_uuid = "defacaba-71f8-4322-a1a9-4c4ba271bcb5" +characteristic_uuid = "69d050c1-13cf-4d14-9b43-ae7c3a928282" + +def match_service_uuid(device: BLEDevice, adv: AdvertisementData): + if service_uuid.lower() in adv.service_uuids: + return True + return False + +def handle_notify(sender, data): + try: + data = data.decode("utf-8") + print(f"查询结果: {data}") + + except Exception as e: + print(f"发生错误: {e}") + +async def main(loop): + try: + device = await BleakScanner.find_device_by_filter(match_service_uuid, timeout=10) + + global client + async with BleakClient(device) as client: + + global service, characteristic + service = client.services.get_service(service_uuid) + characteristic = service.get_characteristic(characteristic_uuid) + + await client.start_notify(characteristic, handle_notify) + + print("连接乙方服务器成功") + print("输入路径进行查询(输入exit退出): ") + + while True: + messgae = await aioconsole.ainput("> ") + if (messgae == "exit"): + break + await client.write_gatt_char(characteristic, messgae.encode("utf-8")) + + except Exception as e: + print(f"发生错误:: {e}") + +if __name__ == "__main__": + loop = asyncio.get_event_loop() + loop.run_until_complete(main(loop)) -- Gitee From 5bb15006b007bbafff1fdfee034d164f6298d65e Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Thu, 8 Aug 2024 05:06:05 +0000 Subject: [PATCH 10/22] =?UTF-8?q?add=20=E4=BB=A3=E7=A0=81/=E7=94=B2/server?= =?UTF-8?q?.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> --- .../\347\224\262/server.py" | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 "\344\273\243\347\240\201/\347\224\262/server.py" diff --git "a/\344\273\243\347\240\201/\347\224\262/server.py" "b/\344\273\243\347\240\201/\347\224\262/server.py" new file mode 100644 index 0000000..6b88899 --- /dev/null +++ "b/\344\273\243\347\240\201/\347\224\262/server.py" @@ -0,0 +1,73 @@ +import os +import logging +import asyncio + +from bless import BlessServer +from bless import BlessGATTCharacteristic +from bless import GATTAttributePermissions +from bless import GATTCharacteristicProperties + + +server_name = "Server_甲" +logfile_path = "server_甲.log" +service_uuid = "5e517536-814a-4f77-803c-04f1729dac3e" +characteristic_uuid = "89bc2069-6c32-48d6-b666-56aff9d272d4" + +def handle_write(characteristic, value): + try: + value = value.decode("utf-8") + if (os.path.exists(value)): + characteristic.value = "存在".encode("utf-8") + else: + characteristic.value = "不存在".encode("utf-8") + + server.update_value(service_uuid, characteristic_uuid) + logger.info(f"来自客户端的查询: {value}") + + except Exception as e: + logger.error(e) + + +async def main(loop): + try: + global server + server = BlessServer(server_name, loop=loop) + + await server.add_new_service(service_uuid) + + await server.add_new_characteristic( + service_uuid, + characteristic_uuid, + GATTCharacteristicProperties.write | GATTCharacteristicProperties.notify, + None, + GATTAttributePermissions.readable | GATTAttributePermissions.writeable + ) + + server.write_request_func = handle_write + + await server.start() + logger.info("服务器开启") + + while True: + await asyncio.sleep(1) + + await server.stop() + logger.info("服务器关闭") + + except Exception as e: + logger.error(e) + + +if __name__ == "__main__": + + logging.basicConfig( + level = logging.DEBUG, + datefmt = "%H:%M:%S", + format = "%(asctime)s [%(levelname)s] %(message)s", + filename = logfile_path + ) + logger = logging.getLogger(__name__) + + loop = asyncio.get_event_loop() + loop.run_until_complete(main(loop)) + -- Gitee From fc6094ee152dae100dc3d1ddebe7e4be4bd2a739 Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Thu, 8 Aug 2024 05:06:34 +0000 Subject: [PATCH 11/22] =?UTF-8?q?add=20=E4=BB=A3=E7=A0=81/=E4=B9=99/client?= =?UTF-8?q?.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\271\231/client.py" | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 "\344\273\243\347\240\201/\344\271\231/client.py" diff --git "a/\344\273\243\347\240\201/\344\271\231/client.py" "b/\344\273\243\347\240\201/\344\271\231/client.py" new file mode 100644 index 0000000..7718edf --- /dev/null +++ "b/\344\273\243\347\240\201/\344\271\231/client.py" @@ -0,0 +1,54 @@ +import sys +import asyncio +import aioconsole + +from bleak import BLEDevice +from bleak import BleakClient +from bleak import BleakScanner +from bleak import AdvertisementData + + +service_uuid = "5e517536-814a-4f77-803c-04f1729dac3e" +characteristic_uuid = "89bc2069-6c32-48d6-b666-56aff9d272d4" + +def match_service_uuid(device: BLEDevice, adv: AdvertisementData): + if service_uuid.lower() in adv.service_uuids: + return True + return False + +def handle_notify(sender, data): + try: + data = data.decode("utf-8") + print(f"查询结果: {data}") + + except Exception as e: + print(f"发生错误: {e}") + +async def main(loop): + try: + device = await BleakScanner.find_device_by_filter(match_service_uuid, timeout=10) + + global client + async with BleakClient(device) as client: + + global service, characteristic + service = client.services.get_service(service_uuid) + characteristic = service.get_characteristic(characteristic_uuid) + + await client.start_notify(characteristic, handle_notify) + + print("连接甲方服务器成功") + print("输入路径进行查询(输入exit退出): ") + + while True: + messgae = await aioconsole.ainput("> ") + if (messgae == "exit"): + break + await client.write_gatt_char(characteristic, messgae.encode("utf-8")) + + except Exception as e: + print(f"发生错误:: {e}") + +if __name__ == "__main__": + loop = asyncio.get_event_loop() + loop.run_until_complete(main(loop)) -- Gitee From ec1cc6104246dcafd30eacd4826f67562e0d422e Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Thu, 8 Aug 2024 05:07:12 +0000 Subject: [PATCH 12/22] =?UTF-8?q?add=20=E4=BB=A3=E7=A0=81/=E4=B9=99/server?= =?UTF-8?q?.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\271\231/server.py" | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 "\344\273\243\347\240\201/\344\271\231/server.py" diff --git "a/\344\273\243\347\240\201/\344\271\231/server.py" "b/\344\273\243\347\240\201/\344\271\231/server.py" new file mode 100644 index 0000000..70c41b9 --- /dev/null +++ "b/\344\273\243\347\240\201/\344\271\231/server.py" @@ -0,0 +1,74 @@ +import os +import logging +import asyncio + +from bless import BlessServer +from bless import BlessGATTCharacteristic +from bless import GATTAttributePermissions +from bless import GATTCharacteristicProperties + + +server_name = "Server_乙" +logfile_path = "server_乙.log" +service_uuid = "defacaba-71f8-4322-a1a9-4c4ba271bcb5" +characteristic_uuid = "69d050c1-13cf-4d14-9b43-ae7c3a928282" + +def handle_write(characteristic, value): + try: + value = value.decode("utf-8") + if (os.path.exists(value)): + characteristic.value = "存在".encode("utf-8") + else: + characteristic.value = "不存在".encode("utf-8") + + server.update_value(service_uuid, characteristic_uuid) + logger.info(f"来自客户端的查询: {value}") + + except Exception as e: + logger.error(e) + + + +async def main(loop): + try: + global server + server = BlessServer(server_name, loop=loop) + + await server.add_new_service(service_uuid) + + await server.add_new_characteristic( + service_uuid, + characteristic_uuid, + GATTCharacteristicProperties.write | GATTCharacteristicProperties.notify, + None, + GATTAttributePermissions.readable | GATTAttributePermissions.writeable + ) + + server.write_request_func = handle_write + + await server.start() + logger.info("服务器开启") + + while True: + await asyncio.sleep(1) + + await server.stop() + logger.info("服务器关闭") + + except Exception as e: + logger.error(e) + + +if __name__ == "__main__": + + logging.basicConfig( + level = logging.DEBUG, + datefmt = "%H:%M:%S", + format = "%(asctime)s [%(levelname)s] %(message)s", + filename = logfile_path + ) + logger = logging.getLogger(__name__) + + loop = asyncio.get_event_loop() + loop.run_until_complete(main(loop)) + -- Gitee From 5414d8bcdd93231e9031723b3fa8b9ccf60ab12c Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Thu, 8 Aug 2024 05:07:31 +0000 Subject: [PATCH 13/22] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20?= =?UTF-8?q?=E4=BB=A3=E7=A0=81/=E4=B9=99/.keep?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\344\273\243\347\240\201/\344\271\231/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 "\344\273\243\347\240\201/\344\271\231/.keep" diff --git "a/\344\273\243\347\240\201/\344\271\231/.keep" "b/\344\273\243\347\240\201/\344\271\231/.keep" deleted file mode 100644 index e69de29..0000000 -- Gitee From 3543ddbf94995ee4f0dc8a2e061c326bee4dec9e Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Thu, 8 Aug 2024 05:07:44 +0000 Subject: [PATCH 14/22] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20?= =?UTF-8?q?=E4=BB=A3=E7=A0=81/=E7=94=B2/.keep?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\344\273\243\347\240\201/\347\224\262/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 "\344\273\243\347\240\201/\347\224\262/.keep" diff --git "a/\344\273\243\347\240\201/\347\224\262/.keep" "b/\344\273\243\347\240\201/\347\224\262/.keep" deleted file mode 100644 index e69de29..0000000 -- Gitee From 5128e1725d1a457a63ee2f189775df9993f804b9 Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Thu, 8 Aug 2024 05:12:37 +0000 Subject: [PATCH 15/22] =?UTF-8?q?update=20=E4=BB=A3=E7=A0=81/=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E6=9C=BA=E5=88=B6/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> --- .../README.md" | 90 ++++++++++++------- 1 file changed, 57 insertions(+), 33 deletions(-) diff --git "a/\344\273\243\347\240\201/\350\277\220\350\241\214\346\234\272\345\210\266/README.md" "b/\344\273\243\347\240\201/\350\277\220\350\241\214\346\234\272\345\210\266/README.md" index 70207d7..f1f8c4b 100644 --- "a/\344\273\243\347\240\201/\350\277\220\350\241\214\346\234\272\345\210\266/README.md" +++ "b/\344\273\243\347\240\201/\350\277\220\350\241\214\346\234\272\345\210\266/README.md" @@ -1,35 +1,59 @@ 注:此文档为调研初期对代码的详细阐释,仅作入门参考之用。后续原则上仅对中文版作更新,英文版如无特别需要不再更新。 - **甲机server.py详细运行行为:** -1. 导入必要的模块和库:包括 sys, asyncio, threading 以及 bless 库中的相关类和函数。 -2. 定义同步触发器:根据操作系统类型选择使用 threading.Event 或 asyncio.Event 作为触发器。 -3. 定义写请求处理函数: -write_request:设置特征的新值,并打印接收到的字符串。 -4. 定义异步运行函数 run: - 清除触发器。 - 实例化 BlessServer 并设置写请求处理函数。 - 添加服务和特征。 - 启动服务器并等待触发器事件。 - 停止服务器。 -5. 运行事件循环:获取事件循环并运行 run 函数直到完成。 - **这些代码的主要目的是创建一个BLE服务器,能够在收到写请求时运行定义过的处理函数,这个处理函数会打印接收到的字符串** - - **乙机client.py详细运行行为:** -1. 导入必要的库:导入 asyncio 和 bleak 库中的相关模块。 -2. 定义常量:定义了服务 UUID 和特征 UUID。 -3. 定义匹配函数:match_service_uuid 函数用于检查扫描到的设备是否包含指定的服务 UUID。 -4. 定义主函数 main: - 使用 BleakScanner 扫描蓝牙设备,并通过 match_service_uuid 过滤设备。 - 找到匹配设备后,使用 BleakClient 连接设备。 - 连接成功后,获取服务和特征,并向特征写入数据("吃了吗")。 - **代码的主要目的是通过service的uuid扫描并连接特定的蓝牙设备,然后向甲设备发送数据。** - - **甲机行为概括:** -创建并配置server对象(主要为服务uuid、特征uuid和写权限) -启动server线程并使用trigger阻塞掉主线程防止主线程执行完后自动关闭导致server线程被强制退出 -打印所有收到的字符串 - - **乙机行为概括:** -创建并配置client对象(这步包含了连接甲机,通过service的uuid搜索并连接) -一旦连上就立即向甲机写入“吃了吗” -程序结束 \ No newline at end of file +# 甲,乙 client端代码阐释 + +## 概述 +该程序是一个基于 `bleak` 库的 BLE(蓝牙低功耗)客户端程序,用于连接并交互一个特定的 BLE 设备。程序通过扫描、连接、发送和接收数据来实现与 BLE 设备的通信。 + +## 主要功能 +1. **导入必要的库**: + - `sys`, `asyncio`, `aioconsole`:用于异步编程和控制台输入。 + - `bleak` 库的相关模块:用于 BLE 设备的扫描、连接和数据交互。 + +2. **定义 UUID**: + - `service_uuid` 和 `characteristic_uuid`:用于标识特定的 BLE 服务和特征。 + +3. **匹配服务 UUID**: + - `match_service_uuid` 函数:用于过滤和匹配扫描到的 BLE 设备,确保只连接到具有指定服务 UUID 的设备。 + +4. **通知处理函数**: + - `handle_notify` 函数:当接收到 BLE 设备的通知时,解码并打印接收到的数据。 + +5. **主异步函数**: + - `main` 函数:负责扫描设备、连接设备、启动通知、发送和接收数据。 + - 使用 `BleakScanner` 扫描设备并找到匹配的设备。 + - 使用 `BleakClient` 连接到设备,获取服务和特征。 + - 启动通知以接收数据,并通过控制台输入发送数据。 + - 循环等待用户输入,发送查询请求,直到用户输入 `exit` 退出。 + +6. **主程序入口**: + - 获取事件循环并运行 `main` 函数。 + + +# 甲,乙 server端代码阐释 + +## 概述 +该程序是一个基于 `bless` 库的 BLE(蓝牙低功耗)服务器程序,用于提供 BLE 服务和处理客户端的请求。服务器通过广播服务、处理写请求和更新特征值来与 BLE 客户端进行通信。 + +## 主要功能 +1. **导入必要的库**: + - `os`, `logging`, `asyncio`:用于日志记录和异步编程。 + - `bless` 库的相关模块:用于创建和管理 BLE 服务器、服务和特征。 + +2. **定义常量**: + - `server_name`:服务器的名称。 + - `logfile_path`:日志文件的路径。 + - `service_uuid` 和 `characteristic_uuid`:用于标识特定的 BLE 服务和特征。 + +3. **写请求处理函数**: + - `handle_write` 函数:当接收到客户端的写请求时,检查指定的路径是否存在,并更新特征值以通知客户端结果。 + +4. **主异步函数**: + - `main` 函数:负责初始化 BLE 服务器、添加服务和特征、设置写请求处理函数,并启动服务器。 + - 使用 `BlessServer` 创建 BLE 服务器实例。 + - 添加新的服务和特征,并设置特征的属性和权限。 + - 启动服务器并进入主循环,持续处理客户端请求。 + +5. **主程序入口**: + - 配置日志记录,设置日志级别和格式。 + - 获取事件循环并运行 `main` 函数。 \ No newline at end of file -- Gitee From 341a65af2bd6a976d5c031674df83e408c29e736 Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Thu, 8 Aug 2024 05:13:28 +0000 Subject: [PATCH 16/22] =?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.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> --- ...\234\257\350\257\255\345\257\271\347\205\247\350\241\250.md" | 2 +- 1 file changed, 1 insertion(+), 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.md" "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.md" index 65530a8..9bdad87 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.md" +++ "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.md" @@ -19,7 +19,7 @@ permissions | 权限 read | 读取 read_request | 读取请求 request | 请求 -sender | ? +sender | 发送者 server | 服务器 service | 服务 trigger | 触发器 -- Gitee From 62443d4332be89e8e127aed3f47ce240378e8733 Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Thu, 8 Aug 2024 05:39:30 +0000 Subject: [PATCH 17/22] =?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" | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git "a/\344\273\243\347\240\201/README.md" "b/\344\273\243\347\240\201/README.md" index 19a356a..40edd34 100644 --- "a/\344\273\243\347\240\201/README.md" +++ "b/\344\273\243\347\240\201/README.md" @@ -7,16 +7,9 @@ ### 步骤 -1、2两步可互换顺序。 - -1. 甲机server启动 -2. 乙机client启动 -3. 乙机client连接成功后打印connected -4. 甲机server打印“吃了吗” -5. 乙机client打印“我吃了” -6. 乙机client自动退出 -7. 甲机server保持开启,乙机再次启动client -8. 回到第3步 +1. 启动甲乙服务端 +2. 启动甲乙客户端 +3. 进行测试 ### 测试结果 -- Gitee From 01b80ac48c156a3e7f6fef3495a9964dd879234e Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Thu, 8 Aug 2024 05:40:49 +0000 Subject: [PATCH 18/22] =?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" | 4 ++++ 1 file changed, 4 insertions(+) diff --git "a/\344\273\243\347\240\201/README.md" "b/\344\273\243\347\240\201/README.md" index 40edd34..b1620f8 100644 --- "a/\344\273\243\347\240\201/README.md" +++ "b/\344\273\243\347\240\201/README.md" @@ -22,6 +22,10 @@ | Win11 外设属性否 | | 【待测】服务端启动失败,报错“设备不支持命令功能” | | Win11 | Win10 | 成功 | +| 甲运行系统 | 乙运行系统 | 测试结果 | +| ---------- | ----------| -----------| +| Win10 | Win11 | 成功| + ## 运行机制 见 [此文档](运行机制/README.md)。 \ No newline at end of file -- Gitee From bb4c673fe6571b53267fd62ba066c72f25704ecc Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Thu, 8 Aug 2024 05:43:55 +0000 Subject: [PATCH 19/22] =?UTF-8?q?update=20=E4=BB=A3=E7=A0=81/=E7=94=B2/cli?= =?UTF-8?q?ent.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> --- .../\347\224\262/client.py" | 43 +++++++++---------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git "a/\344\273\243\347\240\201/\347\224\262/client.py" "b/\344\273\243\347\240\201/\347\224\262/client.py" index 64d4b3a..669a878 100644 --- "a/\344\273\243\347\240\201/\347\224\262/client.py" +++ "b/\344\273\243\347\240\201/\347\224\262/client.py" @@ -7,48 +7,47 @@ from bleak import BleakClient from bleak import BleakScanner from bleak import AdvertisementData +服务UUID = "defacaba-71f8-4322-a1a9-4c4ba271bcb5" +特征UUID = "69d050c1-13cf-4d14-9b43-ae7c3a928282" -service_uuid = "defacaba-71f8-4322-a1a9-4c4ba271bcb5" -characteristic_uuid = "69d050c1-13cf-4d14-9b43-ae7c3a928282" - -def match_service_uuid(device: BLEDevice, adv: AdvertisementData): - if service_uuid.lower() in adv.service_uuids: +def 匹配服务UUID(设备: BLEDevice, 广告: AdvertisementData): + if 服务UUID.lower() in 广告.service_uuids: return True return False -def handle_notify(sender, data): +def 处理通知(发送者, 数据): try: - data = data.decode("utf-8") - print(f"查询结果: {data}") + 数据 = 数据.decode("utf-8") + print(f"查询结果: {数据}") except Exception as e: print(f"发生错误: {e}") -async def main(loop): +async def 主函数(循环): try: - device = await BleakScanner.find_device_by_filter(match_service_uuid, timeout=10) + 设备 = await BleakScanner.find_device_by_filter(匹配服务UUID, timeout=10) - global client - async with BleakClient(device) as client: + global 客户端 + async with BleakClient(设备) as 客户端: - global service, characteristic - service = client.services.get_service(service_uuid) - characteristic = service.get_characteristic(characteristic_uuid) + global 服务, 特征 + 服务 = 客户端.services.get_service(服务UUID) + 特征 = 服务.get_characteristic(特征UUID) - await client.start_notify(characteristic, handle_notify) + await 客户端.start_notify(特征, 处理通知) print("连接乙方服务器成功") print("输入路径进行查询(输入exit退出): ") while True: - messgae = await aioconsole.ainput("> ") - if (messgae == "exit"): + 消息 = await aioconsole.ainput("> ") + if (消息 == "exit"): break - await client.write_gatt_char(characteristic, messgae.encode("utf-8")) + await 客户端.write_gatt_char(特征, 消息.encode("utf-8")) except Exception as e: - print(f"发生错误:: {e}") + print(f"发生错误: {e}") if __name__ == "__main__": - loop = asyncio.get_event_loop() - loop.run_until_complete(main(loop)) + 循环 = asyncio.get_event_loop() + 循环.run_until_complete(主函数(循环)) -- Gitee From 20f87f8c3abf7136f5820c1cbe3cc28df50a7da1 Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Thu, 8 Aug 2024 05:47:30 +0000 Subject: [PATCH 20/22] =?UTF-8?q?update=20=E4=BB=A3=E7=A0=81/=E7=94=B2/ser?= =?UTF-8?q?ver.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> --- .../\347\224\262/server.py" | 60 +++++++++---------- 1 file changed, 29 insertions(+), 31 deletions(-) diff --git "a/\344\273\243\347\240\201/\347\224\262/server.py" "b/\344\273\243\347\240\201/\347\224\262/server.py" index 6b88899..789c858 100644 --- "a/\344\273\243\347\240\201/\347\224\262/server.py" +++ "b/\344\273\243\347\240\201/\347\224\262/server.py" @@ -7,55 +7,54 @@ from bless import BlessGATTCharacteristic from bless import GATTAttributePermissions from bless import GATTCharacteristicProperties +服务器名称 = "Server_甲" +日志文件路径 = "server_甲.log" +服务UUID = "5e517536-814a-4f77-803c-04f1729dac3e" +特征UUID = "89bc2069-6c32-48d6-b666-56aff9d272d4" -server_name = "Server_甲" -logfile_path = "server_甲.log" -service_uuid = "5e517536-814a-4f77-803c-04f1729dac3e" -characteristic_uuid = "89bc2069-6c32-48d6-b666-56aff9d272d4" - -def handle_write(characteristic, value): +def 处理写入(特征, 值): try: - value = value.decode("utf-8") - if (os.path.exists(value)): - characteristic.value = "存在".encode("utf-8") + 值 = 值.decode("utf-8") + if (os.path.exists(值)): + 特征.value = "存在".encode("utf-8") else: - characteristic.value = "不存在".encode("utf-8") + 特征.value = "不存在".encode("utf-8") - server.update_value(service_uuid, characteristic_uuid) - logger.info(f"来自客户端的查询: {value}") + 服务器.update_value(服务UUID, 特征UUID) + 日志记录器.info(f"来自客户端的查询: {值}") except Exception as e: - logger.error(e) + 日志记录器.error(e) -async def main(loop): +async def 主函数(循环): try: - global server - server = BlessServer(server_name, loop=loop) + global 服务器 + 服务器 = BlessServer(服务器名称, loop=循环) - await server.add_new_service(service_uuid) + await 服务器.add_new_service(服务UUID) - await server.add_new_characteristic( - service_uuid, - characteristic_uuid, + await 服务器.add_new_characteristic( + 服务UUID, + 特征UUID, GATTCharacteristicProperties.write | GATTCharacteristicProperties.notify, None, GATTAttributePermissions.readable | GATTAttributePermissions.writeable ) - server.write_request_func = handle_write + 服务器.write_request_func = 处理写入 - await server.start() - logger.info("服务器开启") + await 服务器.start() + 日志记录器.info("服务器开启") while True: await asyncio.sleep(1) - await server.stop() - logger.info("服务器关闭") + await 服务器.stop() + 日志记录器.info("服务器关闭") except Exception as e: - logger.error(e) + 日志记录器.error(e) if __name__ == "__main__": @@ -64,10 +63,9 @@ if __name__ == "__main__": level = logging.DEBUG, datefmt = "%H:%M:%S", format = "%(asctime)s [%(levelname)s] %(message)s", - filename = logfile_path + filename = 日志文件路径 ) - logger = logging.getLogger(__name__) - - loop = asyncio.get_event_loop() - loop.run_until_complete(main(loop)) + 日志记录器 = logging.getLogger(__name__) + 循环 = asyncio.get_event_loop() + 循环.run_until_complete(主函数(循环)) -- Gitee From c880632609d6b49d87e97996dfeb07dd14ee3785 Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Thu, 8 Aug 2024 05:49:34 +0000 Subject: [PATCH 21/22] =?UTF-8?q?update=20=E4=BB=A3=E7=A0=81/=E4=B9=99/cli?= =?UTF-8?q?ent.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\271\231/client.py" | 41 +++++++++---------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git "a/\344\273\243\347\240\201/\344\271\231/client.py" "b/\344\273\243\347\240\201/\344\271\231/client.py" index 7718edf..1365b2b 100644 --- "a/\344\273\243\347\240\201/\344\271\231/client.py" +++ "b/\344\273\243\347\240\201/\344\271\231/client.py" @@ -7,48 +7,47 @@ from bleak import BleakClient from bleak import BleakScanner from bleak import AdvertisementData +服务UUID = "5e517536-814a-4f77-803c-04f1729dac3e" +特征UUID = "89bc2069-6c32-48d6-b666-56aff9d272d4" -service_uuid = "5e517536-814a-4f77-803c-04f1729dac3e" -characteristic_uuid = "89bc2069-6c32-48d6-b666-56aff9d272d4" - -def match_service_uuid(device: BLEDevice, adv: AdvertisementData): - if service_uuid.lower() in adv.service_uuids: +def 匹配服务UUID(设备: BLEDevice, 广告: AdvertisementData): + if 服务UUID.lower() in 广告.service_uuids: return True return False -def handle_notify(sender, data): +def 处理通知(发送者, 数据): try: - data = data.decode("utf-8") - print(f"查询结果: {data}") + 数据 = 数据.decode("utf-8") + print(f"查询结果: {数据}") except Exception as e: print(f"发生错误: {e}") -async def main(loop): +async def 主函数(循环): try: - device = await BleakScanner.find_device_by_filter(match_service_uuid, timeout=10) + 设备 = await BleakScanner.find_device_by_filter(匹配服务UUID, timeout=10) - global client - async with BleakClient(device) as client: + global 客户端 + async with BleakClient(设备) as 客户端: - global service, characteristic - service = client.services.get_service(service_uuid) - characteristic = service.get_characteristic(characteristic_uuid) + global 服务, 特征 + 服务 = 客户端.services.get_service(服务UUID) + 特征 = 服务.get_characteristic(特征UUID) - await client.start_notify(characteristic, handle_notify) + await 客户端.start_notify(特征, 处理通知) print("连接甲方服务器成功") print("输入路径进行查询(输入exit退出): ") while True: - messgae = await aioconsole.ainput("> ") - if (messgae == "exit"): + 消息 = await aioconsole.ainput("> ") + if (消息 == "exit"): break - await client.write_gatt_char(characteristic, messgae.encode("utf-8")) + await 客户端.write_gatt_char(特征, 消息.encode("utf-8")) except Exception as e: print(f"发生错误:: {e}") if __name__ == "__main__": - loop = asyncio.get_event_loop() - loop.run_until_complete(main(loop)) + 循环 = asyncio.get_event_loop() + 循环.run_until_complete(主函数(循环)) -- Gitee From 19b1008091d59339c11f26018979ad5ff2c6663e Mon Sep 17 00:00:00 2001 From: ycy20090311 <12263881+ycy20090311@user.noreply.gitee.com> Date: Thu, 8 Aug 2024 05:51:37 +0000 Subject: [PATCH 22/22] =?UTF-8?q?update=20=E4=BB=A3=E7=A0=81/=E4=B9=99/ser?= =?UTF-8?q?ver.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\271\231/server.py" | 61 +++++++++---------- 1 file changed, 29 insertions(+), 32 deletions(-) diff --git "a/\344\273\243\347\240\201/\344\271\231/server.py" "b/\344\273\243\347\240\201/\344\271\231/server.py" index 70c41b9..2fb94c5 100644 --- "a/\344\273\243\347\240\201/\344\271\231/server.py" +++ "b/\344\273\243\347\240\201/\344\271\231/server.py" @@ -7,56 +7,54 @@ from bless import BlessGATTCharacteristic from bless import GATTAttributePermissions from bless import GATTCharacteristicProperties +服务器名称 = "Server_乙" +日志文件路径 = "server_乙.log" +服务UUID = "defacaba-71f8-4322-a1a9-4c4ba271bcb5" +特征UUID = "69d050c1-13cf-4d14-9b43-ae7c3a928282" -server_name = "Server_乙" -logfile_path = "server_乙.log" -service_uuid = "defacaba-71f8-4322-a1a9-4c4ba271bcb5" -characteristic_uuid = "69d050c1-13cf-4d14-9b43-ae7c3a928282" - -def handle_write(characteristic, value): +def 处理写入(特征, 值): try: - value = value.decode("utf-8") - if (os.path.exists(value)): - characteristic.value = "存在".encode("utf-8") + 值 = 值.decode("utf-8") + if (os.path.exists(值)): + 特征.value = "存在".encode("utf-8") else: - characteristic.value = "不存在".encode("utf-8") + 特征.value = "不存在".encode("utf-8") - server.update_value(service_uuid, characteristic_uuid) - logger.info(f"来自客户端的查询: {value}") + 服务器.update_value(服务UUID, 特征UUID) + 日志记录器.info(f"来自客户端的查询: {值}") except Exception as e: - logger.error(e) - + 日志记录器.error(e) -async def main(loop): +async def 主函数(循环): try: - global server - server = BlessServer(server_name, loop=loop) + global 服务器 + 服务器 = BlessServer(服务器名称, loop=循环) - await server.add_new_service(service_uuid) + await 服务器.add_new_service(服务UUID) - await server.add_new_characteristic( - service_uuid, - characteristic_uuid, + await 服务器.add_new_characteristic( + 服务UUID, + 特征UUID, GATTCharacteristicProperties.write | GATTCharacteristicProperties.notify, None, GATTAttributePermissions.readable | GATTAttributePermissions.writeable ) - server.write_request_func = handle_write + 服务器.write_request_func = 处理写入 - await server.start() - logger.info("服务器开启") + await 服务器.start() + 日志记录器.info("服务器开启") while True: await asyncio.sleep(1) - await server.stop() - logger.info("服务器关闭") + await 服务器.stop() + 日志记录器.info("服务器关闭") except Exception as e: - logger.error(e) + 日志记录器.error(e) if __name__ == "__main__": @@ -65,10 +63,9 @@ if __name__ == "__main__": level = logging.DEBUG, datefmt = "%H:%M:%S", format = "%(asctime)s [%(levelname)s] %(message)s", - filename = logfile_path + filename = 日志文件路径 ) - logger = logging.getLogger(__name__) - - loop = asyncio.get_event_loop() - loop.run_until_complete(main(loop)) + 日志记录器 = logging.getLogger(__name__) + 循环 = asyncio.get_event_loop() + 循环.run_until_complete(主函数(循环)) -- Gitee