fc75f31b by huangyf2

named

1 parent 0e90a8e9
Showing 1 changed file with 36 additions and 5 deletions
......@@ -128,6 +128,7 @@ class BLEClient:
self._notification_callbacks = {} # 存储通知回调,用于清理
self._shutdown = False # 添加关闭标志
self._scanner = None # 当前扫描器实例
self._device_name_from_advertisement = None # 从广播包响应中获取的设备名称
def on_disconnect(self, client):
print("BLE连接断开,关闭WebSocket")
......@@ -160,14 +161,21 @@ class BLEClient:
def detection_callback(self, device, advertisement_data):
if any(service_uuid in advertisement_data.service_uuids for service_uuid in self.services):
# 优先使用广播包中的local_name
device_name = advertisement_data.local_name if advertisement_data.local_name else (device.name if device.name else "")
# 优先从广播包响应中获取设备名称(local_name字段)
# 这是广播包响应中的设备名称,比device.name更准确
device_name_from_adv = advertisement_data.local_name if advertisement_data.local_name else None
# 如果广播包中没有名称,再使用device.name作为备用
device_name = device_name_from_adv if device_name_from_adv else (device.name if device.name else "")
# 丢弃名字为空的设备
if not device_name or device_name.strip() == "":
print(f"跳过名字为空的设备: {device.address}")
return None
# 保存从广播包响应中获取的设备名称
self._device_name_from_advertisement = device_name_from_adv if device_name_from_adv else None
self.target_device = (device, advertisement_data)
if not self.target_device:
print("未找到匹配设备")
......@@ -175,7 +183,9 @@ class BLEClient:
else:
device, adv_data = self.target_device
print("\n找到目标设备:")
print(f"设备名称(从广播包): {device_name}")
print(f"设备名称(从广播包响应): {device_name_from_adv if device_name_from_adv else 'N/A'}")
print(f"设备名称(备用来源): {device.name if device.name else 'N/A'}")
print(f"最终使用名称: {device_name}")
print(f"设备地址: {device.address}")
print(f"信号强度: {device.rssi} dBm")
print("\n广播信息:")
......@@ -230,6 +240,7 @@ class BLEClient:
for filt in params.get("filters", [{}]):
self.services.extend(filt.get("services", []))
self.optional_services = params.get("optionalServices", [])
self._device_name_from_advertisement = None # 重置保存的设备名称
# 双重扫描:快速扫描后若未发现,再进行扩展扫描;发现即停
phases = [("active", 3.0), ("passive", 6.0)]
......@@ -260,8 +271,27 @@ class BLEClient:
if found:
device, adv_data = self.target_device
# 优先使用广播包中的local_name,如果没有则使用device.name
device_name = adv_data.local_name if adv_data.local_name else (device.name if device.name else "")
# 优先使用从广播包响应中获取的设备名称(local_name字段)
# 这是扫描广播包响应时获取的名称,比device.name更准确可靠
device_name = adv_data.local_name if adv_data.local_name else None
# 如果广播包响应中没有名称,使用之前保存的名称或device.name作为备用
if not device_name:
device_name = self._device_name_from_advertisement
if not device_name:
device_name = device.name if device.name else None
# 补充检查:如果设备名称仍为空,使用设备地址作为名称
if not device_name or device_name.strip() == "":
print(f"警告: 设备名称为空,使用设备地址作为名称: {device.address}")
device_name = device.address # 使用设备地址作为备用名称
# 记录名称来源
name_source = "广播包响应(local_name)" if adv_data.local_name else \
("已保存的广播包名称" if self._device_name_from_advertisement else \
("device.name" if device.name else "设备地址"))
print(f"响应包中使用的设备名称来源: {name_source}")
discover_response = json.dumps({
"jsonrpc": "2.0",
"method": "didDiscoverPeripheral",
......@@ -532,6 +562,7 @@ class BLEClient:
finally:
self.client = None
self.target_device = None
self._device_name_from_advertisement = None # 清理保存的设备名称
self._notification_callbacks.clear()
self.notification_records.clear()
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!