代码拉取完成,页面将自动刷新
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()
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:
loop = asyncio.get_event_loop()
loop.run_until_complete(中(loop))
except Exception as e:
日志.error(f"An error occurred: {e}")
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。