fc75f31b by huangyf2

named

1 parent 0e90a8e9
Showing 1 changed file with 36 additions and 5 deletions
...@@ -128,6 +128,7 @@ class BLEClient: ...@@ -128,6 +128,7 @@ class BLEClient:
128 self._notification_callbacks = {} # 存储通知回调,用于清理 128 self._notification_callbacks = {} # 存储通知回调,用于清理
129 self._shutdown = False # 添加关闭标志 129 self._shutdown = False # 添加关闭标志
130 self._scanner = None # 当前扫描器实例 130 self._scanner = None # 当前扫描器实例
131 self._device_name_from_advertisement = None # 从广播包响应中获取的设备名称
131 132
132 def on_disconnect(self, client): 133 def on_disconnect(self, client):
133 print("BLE连接断开,关闭WebSocket") 134 print("BLE连接断开,关闭WebSocket")
...@@ -160,14 +161,21 @@ class BLEClient: ...@@ -160,14 +161,21 @@ class BLEClient:
160 161
161 def detection_callback(self, device, advertisement_data): 162 def detection_callback(self, device, advertisement_data):
162 if any(service_uuid in advertisement_data.service_uuids for service_uuid in self.services): 163 if any(service_uuid in advertisement_data.service_uuids for service_uuid in self.services):
163 # 优先使用广播包中的local_name 164 # 优先从广播包响应中获取设备名称(local_name字段)
164 device_name = advertisement_data.local_name if advertisement_data.local_name else (device.name if device.name else "") 165 # 这是广播包响应中的设备名称,比device.name更准确
166 device_name_from_adv = advertisement_data.local_name if advertisement_data.local_name else None
167
168 # 如果广播包中没有名称,再使用device.name作为备用
169 device_name = device_name_from_adv if device_name_from_adv else (device.name if device.name else "")
165 170
166 # 丢弃名字为空的设备 171 # 丢弃名字为空的设备
167 if not device_name or device_name.strip() == "": 172 if not device_name or device_name.strip() == "":
168 print(f"跳过名字为空的设备: {device.address}") 173 print(f"跳过名字为空的设备: {device.address}")
169 return None 174 return None
170 175
176 # 保存从广播包响应中获取的设备名称
177 self._device_name_from_advertisement = device_name_from_adv if device_name_from_adv else None
178
171 self.target_device = (device, advertisement_data) 179 self.target_device = (device, advertisement_data)
172 if not self.target_device: 180 if not self.target_device:
173 print("未找到匹配设备") 181 print("未找到匹配设备")
...@@ -175,7 +183,9 @@ class BLEClient: ...@@ -175,7 +183,9 @@ class BLEClient:
175 else: 183 else:
176 device, adv_data = self.target_device 184 device, adv_data = self.target_device
177 print("\n找到目标设备:") 185 print("\n找到目标设备:")
178 print(f"设备名称(从广播包): {device_name}") 186 print(f"设备名称(从广播包响应): {device_name_from_adv if device_name_from_adv else 'N/A'}")
187 print(f"设备名称(备用来源): {device.name if device.name else 'N/A'}")
188 print(f"最终使用名称: {device_name}")
179 print(f"设备地址: {device.address}") 189 print(f"设备地址: {device.address}")
180 print(f"信号强度: {device.rssi} dBm") 190 print(f"信号强度: {device.rssi} dBm")
181 print("\n广播信息:") 191 print("\n广播信息:")
...@@ -230,6 +240,7 @@ class BLEClient: ...@@ -230,6 +240,7 @@ class BLEClient:
230 for filt in params.get("filters", [{}]): 240 for filt in params.get("filters", [{}]):
231 self.services.extend(filt.get("services", [])) 241 self.services.extend(filt.get("services", []))
232 self.optional_services = params.get("optionalServices", []) 242 self.optional_services = params.get("optionalServices", [])
243 self._device_name_from_advertisement = None # 重置保存的设备名称
233 244
234 # 双重扫描:快速扫描后若未发现,再进行扩展扫描;发现即停 245 # 双重扫描:快速扫描后若未发现,再进行扩展扫描;发现即停
235 phases = [("active", 3.0), ("passive", 6.0)] 246 phases = [("active", 3.0), ("passive", 6.0)]
...@@ -260,8 +271,27 @@ class BLEClient: ...@@ -260,8 +271,27 @@ class BLEClient:
260 271
261 if found: 272 if found:
262 device, adv_data = self.target_device 273 device, adv_data = self.target_device
263 # 优先使用广播包中的local_name,如果没有则使用device.name 274 # 优先使用从广播包响应中获取的设备名称(local_name字段)
264 device_name = adv_data.local_name if adv_data.local_name else (device.name if device.name else "") 275 # 这是扫描广播包响应时获取的名称,比device.name更准确可靠
276 device_name = adv_data.local_name if adv_data.local_name else None
277
278 # 如果广播包响应中没有名称,使用之前保存的名称或device.name作为备用
279 if not device_name:
280 device_name = self._device_name_from_advertisement
281 if not device_name:
282 device_name = device.name if device.name else None
283
284 # 补充检查:如果设备名称仍为空,使用设备地址作为名称
285 if not device_name or device_name.strip() == "":
286 print(f"警告: 设备名称为空,使用设备地址作为名称: {device.address}")
287 device_name = device.address # 使用设备地址作为备用名称
288
289 # 记录名称来源
290 name_source = "广播包响应(local_name)" if adv_data.local_name else \
291 ("已保存的广播包名称" if self._device_name_from_advertisement else \
292 ("device.name" if device.name else "设备地址"))
293 print(f"响应包中使用的设备名称来源: {name_source}")
294
265 discover_response = json.dumps({ 295 discover_response = json.dumps({
266 "jsonrpc": "2.0", 296 "jsonrpc": "2.0",
267 "method": "didDiscoverPeripheral", 297 "method": "didDiscoverPeripheral",
...@@ -532,6 +562,7 @@ class BLEClient: ...@@ -532,6 +562,7 @@ class BLEClient:
532 finally: 562 finally:
533 self.client = None 563 self.client = None
534 self.target_device = None 564 self.target_device = None
565 self._device_name_from_advertisement = None # 清理保存的设备名称
535 self._notification_callbacks.clear() 566 self._notification_callbacks.clear()
536 self.notification_records.clear() 567 self.notification_records.clear()
537 568
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!