diff --git "a/\344\273\243\347\240\201/.keep" "b/\344\273\243\347\240\201/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 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 0000000000000000000000000000000000000000..9a86692095adae5b5313c8e721609c35ada14ad9 --- /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 0000000000000000000000000000000000000000..1aff78e2d308f3e6a9e8a787fdcfde5d0d8c4c20 --- /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)) 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 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 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 0000000000000000000000000000000000000000..1a905c431ad944c79a07a1a5ba81807fcd9f20b3 --- /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 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 0000000000000000000000000000000000000000..5af29c123fcde86f714aea01fb5a8c6fe7613dc0 --- /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