fix(bridge): connect 改扫描解析 BLEDevice 再连(WinRT 兼容)+ SERVER_BUILD=2
This commit is contained in:
1 parent
49b2010088
commit
89335e0e82
1 file changed
+18
-3
+18
-3
@@ -27,6 +27,7 @@ import platform
|
||||
import sys
|
||||
|
||||
PROTOCOL_VERSION = 1
|
||||
SERVER_BUILD = 2 # 2026-07-28: connect 改为扫描解析 BLEDevice 后连接(WinRT 兼容)
|
||||
|
||||
EPD_SERVICE_UUID = "13187b10-eba9-a3ba-044e-83d3217d9a38"
|
||||
EPD_CHAR_UUID = "4b646063-6264-f3a7-8941-e65356ea82fe"
|
||||
@@ -76,12 +77,25 @@ class BleSession:
|
||||
return {"devices": out}
|
||||
|
||||
async def connect(self, address: str, timeout: float = 15.0):
|
||||
from bleak import BleakClient
|
||||
from bleak import BleakClient, BleakScanner
|
||||
await self.close()
|
||||
self.client = BleakClient(address, timeout=timeout)
|
||||
# Windows/WinRT 按地址直连常解析失败——先扫描拿到 BLEDevice(含地址类型/广播数据)再连
|
||||
deadline = asyncio.get_event_loop().time() + timeout
|
||||
dev = None
|
||||
while True:
|
||||
remain = deadline - asyncio.get_event_loop().time()
|
||||
if remain <= 0:
|
||||
break
|
||||
dev = await BleakScanner.find_device_by_address(
|
||||
address, timeout=min(5.0, max(1.0, remain)))
|
||||
if dev is not None:
|
||||
break
|
||||
if dev is None:
|
||||
raise RuntimeError(f"设备解析失败(扫描未见 {address})")
|
||||
self.client = BleakClient(dev, timeout=timeout)
|
||||
await self.client.connect()
|
||||
await self.client.start_notify(RXTX_CHAR_UUID, self._on_notify)
|
||||
return {"address": address}
|
||||
return {"address": address, "name": dev.name}
|
||||
|
||||
def _on_notify(self, _sender, data: bytearray):
|
||||
self._id_buf.extend(data)
|
||||
@@ -199,6 +213,7 @@ class BridgeServer:
|
||||
async def _dispatch(self, op: str, req: dict):
|
||||
if op == "ping":
|
||||
return {"pong": True, "version": PROTOCOL_VERSION,
|
||||
"build": SERVER_BUILD,
|
||||
"platform": platform.system()}
|
||||
if op == "scan":
|
||||
return await self.ble.scan(float(req.get("timeout", 5.0)))
|
||||
|
||||
Reference in new issue
Block a user