From 61b0528fac218618a3eb4f7e04a948501fd9f0b8 Mon Sep 17 00:00:00 2001 From: GivenGu Date: Sun, 21 Jul 2024 04:13:15 +0000 Subject: [PATCH 1/9] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\344\273\243\347\240\201/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\344\273\243\347\240\201/.keep" diff --git "a/\344\273\243\347\240\201/.keep" "b/\344\273\243\347\240\201/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From d9c7b7cbdf9c2a6bfda1da16e9c354f5d970ca19 Mon Sep 17 00:00:00 2001 From: GivenGu Date: Sun, 21 Jul 2024 04:13:27 +0000 Subject: [PATCH 2/9] =?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/.keep?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\344\273\243\347\240\201/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 "\344\273\243\347\240\201/.keep" diff --git "a/\344\273\243\347\240\201/.keep" "b/\344\273\243\347\240\201/.keep" deleted file mode 100644 index e69de29..0000000 -- Gitee From 81f1a8babd2258c59e9e7b91418545d447295297 Mon Sep 17 00:00:00 2001 From: GivenGu Date: Sun, 21 Jul 2024 04:13:45 +0000 Subject: [PATCH 3/9] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\344\273\243\347\240\201/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\344\273\243\347\240\201/.keep" diff --git "a/\344\273\243\347\240\201/.keep" "b/\344\273\243\347\240\201/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From b3466f227dcb300fe51ef7d6e01b1ead4f8f2966 Mon Sep 17 00:00:00 2001 From: GivenGu Date: Sun, 21 Jul 2024 04:14:36 +0000 Subject: [PATCH 4/9] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: GivenGu --- "\344\273\243\347\240\201/client.py" | 31 +++++++++++++ "\344\273\243\347\240\201/server.py" | 65 ++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 "\344\273\243\347\240\201/client.py" create mode 100644 "\344\273\243\347\240\201/server.py" diff --git "a/\344\273\243\347\240\201/client.py" "b/\344\273\243\347\240\201/client.py" new file mode 100644 index 0000000..9a86692 --- /dev/null +++ "b/\344\273\243\347\240\201/client.py" @@ -0,0 +1,31 @@ +import asyncio +from bleak import BleakScanner, BleakClient, BLEDevice, AdvertisementData + +SERVICE_UUID = "A07498CA-AD5B-474E-940D-16F1FBE7E8CD" +CHAR_UUID = "51FF12BB-3ED8-46E5-B4F9-D64E2FEC021B" + + +def match_service_uuid(device: BLEDevice, adv: AdvertisementData): + if SERVICE_UUID.lower() in adv.service_uuids: + return True + return False + + +async def main(): + # 搜索设备, 查看是否匹配NUS UUID,找到后可尝试建立连接,进行读写。 + device = await BleakScanner.find_device_by_filter(match_service_uuid, timeout=1000) + + # 创建BleakClient客户端,连接后进行串口操作 + async with BleakClient(device) as client: + # await client.start_notify(CHAR_UUID,) + + print("Connected") + + # loop = asyncio.get_running_loop() + service = client.services.get_service(SERVICE_UUID) + # 接收蓝牙串口信息 + char = service.get_characteristic(CHAR_UUID) + await client.write_gatt_char(char, "吃了吗".encode("utf-8")) + + +asyncio.run(main()) diff --git "a/\344\273\243\347\240\201/server.py" "b/\344\273\243\347\240\201/server.py" new file mode 100644 index 0000000..1aff78e --- /dev/null +++ "b/\344\273\243\347\240\201/server.py" @@ -0,0 +1,65 @@ +import sys +import asyncio +import threading + +from typing import Any, Union + +from bless import BlessServer +from bless import BlessGATTCharacteristic +from bless import GATTCharacteristicProperties +from bless import GATTAttributePermissions + +# NOTE: Some systems require different synchronization methods. +trigger: Union[asyncio.Event, threading.Event] +if sys.platform in ["darwin", "win32"]: + trigger = threading.Event() +else: + trigger = asyncio.Event() + + +def read_request(characteristic: BlessGATTCharacteristic, **kwargs) -> bytearray: + return characteristic.value + + +def write_request(characteristic: BlessGATTCharacteristic, value: Any, **kwargs): + characteristic.value = value + print(value.decode('utf-8')) + if value.decode('utf-8') == "吃了吗": + trigger.set() + + +async def run(loop): + trigger.clear() + # Instantiate the server + my_service_name = "Test Service" + server = BlessServer(name=my_service_name, loop=loop) + server.read_request_func = read_request + server.write_request_func = write_request + + # Add Service + my_service_uuid = "A07498CA-AD5B-474E-940D-16F1FBE7E8CD" + await server.add_new_service(my_service_uuid) + + # Add a Characteristic to the service + my_char_uuid = "51FF12BB-3ED8-46E5-B4F9-D64E2FEC021B" + char_flags = ( + GATTCharacteristicProperties.read + | GATTCharacteristicProperties.write + | GATTCharacteristicProperties.indicate + ) + permissions = GATTAttributePermissions.readable | GATTAttributePermissions.writeable + await server.add_new_characteristic( + my_service_uuid, my_char_uuid, char_flags, None, permissions + ) + + await server.start() + if trigger.__module__ == "threading": + trigger.wait() + else: + await trigger.wait() + + await server.stop() + + +loop = asyncio.get_event_loop() +loop.run_until_complete(run(loop)) -- Gitee From d69acd72369ee24260a8cdd74dc46f1ca20121b5 Mon Sep 17 00:00:00 2001 From: GivenGu Date: Sun, 21 Jul 2024 04:15:17 +0000 Subject: [PATCH 5/9] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E7=8E=AF=E5=A2=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\344\273\243\347\240\201/\347\216\257\345\242\203/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\344\273\243\347\240\201/\347\216\257\345\242\203/.keep" diff --git "a/\344\273\243\347\240\201/\347\216\257\345\242\203/.keep" "b/\344\273\243\347\240\201/\347\216\257\345\242\203/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From c47be31b8f121316d17e5e4c5106e4b85f37c85c Mon Sep 17 00:00:00 2001 From: GivenGu Date: Sun, 21 Jul 2024 04:16:32 +0000 Subject: [PATCH 6/9] =?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=8E=AF=E5=A2=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\344\273\243\347\240\201/\347\216\257\345\242\203/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 "\344\273\243\347\240\201/\347\216\257\345\242\203/.keep" diff --git "a/\344\273\243\347\240\201/\347\216\257\345\242\203/.keep" "b/\344\273\243\347\240\201/\347\216\257\345\242\203/.keep" deleted file mode 100644 index e69de29..0000000 -- Gitee From 1eeb829b19df8e08594e8330df86a99ee1a4eec9 Mon Sep 17 00:00:00 2001 From: GivenGu Date: Sun, 21 Jul 2024 04:21:52 +0000 Subject: [PATCH 7/9] =?UTF-8?q?add=20=E4=BB=A3=E7=A0=81/=E8=A6=81=E5=AE=89?= =?UTF-8?q?=E8=A3=85=E7=9A=84=E7=8E=AF=E5=A2=83.txt.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: GivenGu --- ...\243\205\347\232\204\347\216\257\345\242\203.txt" | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 "\344\273\243\347\240\201/\350\246\201\345\256\211\350\243\205\347\232\204\347\216\257\345\242\203.txt" diff --git "a/\344\273\243\347\240\201/\350\246\201\345\256\211\350\243\205\347\232\204\347\216\257\345\242\203.txt" "b/\344\273\243\347\240\201/\350\246\201\345\256\211\350\243\205\347\232\204\347\216\257\345\242\203.txt" new file mode 100644 index 0000000..5af29c1 --- /dev/null +++ "b/\344\273\243\347\240\201/\350\246\201\345\256\211\350\243\205\347\232\204\347\216\257\345\242\203.txt" @@ -0,0 +1,12 @@ +python-3.9.13 https://www.python.org/ftp/python/3.9.13/python-3.9.13-amd64.exe +Git-2.45.2 https://git-scm.com/download/win + +其中python安装时需要勾选添加环境变量 + +重启 + +重启完之后在终端安装python库: +pip install bleak bless -i https://pypi.tuna.tsinghua.edu.cn/simple +pip install git+https://github.com/gwangyi/pysetupdi + +#有些网址可能需要梯子才能上去 \ No newline at end of file -- Gitee From 99f11de48b874272b8949e0cb05f2fdc215e497c Mon Sep 17 00:00:00 2001 From: GivenGu Date: Sun, 21 Jul 2024 04:22:43 +0000 Subject: [PATCH 8/9] =?UTF-8?q?add=20=E4=BB=A3=E7=A0=81/=E5=8F=91=E9=80=81?= =?UTF-8?q?=E2=80=9C=E5=90=83=E4=BA=86=E5=90=97=E2=80=9D=E7=9A=84=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E5=9C=A8=E2=80=9Cclient.py=E2=80=9D=E9=87=8C.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: GivenGu --- ...\217\345\234\250\342\200\234client.py\342\200\235\351\207\214" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\344\273\243\347\240\201/\345\217\221\351\200\201\342\200\234\345\220\203\344\272\206\345\220\227\342\200\235\347\232\204\347\250\213\345\272\217\345\234\250\342\200\234client.py\342\200\235\351\207\214" diff --git "a/\344\273\243\347\240\201/\345\217\221\351\200\201\342\200\234\345\220\203\344\272\206\345\220\227\342\200\235\347\232\204\347\250\213\345\272\217\345\234\250\342\200\234client.py\342\200\235\351\207\214" "b/\344\273\243\347\240\201/\345\217\221\351\200\201\342\200\234\345\220\203\344\272\206\345\220\227\342\200\235\347\232\204\347\250\213\345\272\217\345\234\250\342\200\234client.py\342\200\235\351\207\214" new file mode 100644 index 0000000..e69de29 -- Gitee From e575a2840efa4f3fc0214c3d7e3cda6d03f67b2b Mon Sep 17 00:00:00 2001 From: GivenGu Date: Sun, 21 Jul 2024 04:28:45 +0000 Subject: [PATCH 9/9] =?UTF-8?q?add=20=E4=BB=A3=E7=A0=81/=E8=8B=B1=E8=AF=AD?= =?UTF-8?q?=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: GivenGu --- ...6\234\257\350\257\255\345\257\271\347\205\247\350\241\250" | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 "\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" 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" new file mode 100644 index 0000000..1a905c4 --- /dev/null +++ "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" @@ -0,0 +1,4 @@ +bluetooth | 蓝牙 +service | 服务 +permissions | 权限 +characteristic | 特征 \ No newline at end of file -- Gitee