|
|
@@ -3,6 +3,8 @@ import time
|
|
|
from bleak import BleakScanner, BleakClient
|
|
|
import threading
|
|
|
from collections import deque
|
|
|
+
|
|
|
+from networkx import is_connected
|
|
|
from utils.SingletonType import SingletonType
|
|
|
from .RemoteControlV2 import RemoteControlV2
|
|
|
from .BaseClass import BaseClass
|
|
|
@@ -45,6 +47,8 @@ class BlueToothMode(BaseClass,metaclass=SingletonType):
|
|
|
|
|
|
async def scan_for_esp32(self):
|
|
|
"""扫描附近的BLE设备,并寻找ESP32"""
|
|
|
+ if self.connect_state == True:
|
|
|
+ return
|
|
|
print("Scanning for ESP32 devices...*****************")
|
|
|
try:
|
|
|
devices = await BleakScanner.discover()
|
|
|
@@ -84,6 +88,13 @@ class BlueToothMode(BaseClass,metaclass=SingletonType):
|
|
|
if "remote" in name:
|
|
|
self.print_error("71 关闭蓝牙连接{}-{}".format(address, name))
|
|
|
self.remote_control_v2.close_bluetooth_connect()
|
|
|
+ self.connect_state = False
|
|
|
+ self.receive_data = b""
|
|
|
+ self.devices = {}
|
|
|
+ self.devices_name = {}
|
|
|
+ self.last_value = None
|
|
|
+ self.retry_num = 0
|
|
|
+ self.last_error = ""
|
|
|
|
|
|
async def handle_disconnect(self, client):
|
|
|
"""处理断开连接事件"""
|
|
|
@@ -121,71 +132,76 @@ class BlueToothMode(BaseClass,metaclass=SingletonType):
|
|
|
self.print_error("""连接到指定地址的ESP32设备并监听通知""")
|
|
|
while True:
|
|
|
# try:
|
|
|
- async with BleakClient(address) as client:
|
|
|
- if not client.is_connected:
|
|
|
- self.print_error("Failed to connect to the device.")
|
|
|
- self.devices[address]["connect_state"] = False
|
|
|
- self.disconnect_device(
|
|
|
+ if len(self.devices) == 0:
|
|
|
+ break
|
|
|
+ async with BleakClient(address) as client:
|
|
|
+ if not client.is_connected:
|
|
|
+ self.print_error("Failed to connect to the device.")
|
|
|
+ self.devices[address]["connect_state"] = False
|
|
|
+ self.disconnect_device(
|
|
|
address=address, name=self.devices[address]["name"]
|
|
|
)
|
|
|
- continue
|
|
|
- self.devices[address]["connect_state"] = True
|
|
|
- self.print_error(f"Connected to {address}")
|
|
|
- # 获取服务和特征(假设你已经知道要监听的特征UUID)
|
|
|
- services = await client.get_services()
|
|
|
- for service in services:
|
|
|
- for char in service.characteristics:
|
|
|
- if "notify" in char.properties:
|
|
|
- self.print_error(
|
|
|
+ continue
|
|
|
+ if len(self.devices) == 0:
|
|
|
+ break
|
|
|
+ self.devices[address]["connect_state"] = True
|
|
|
+ self.print_error(f"Connected to {address}")
|
|
|
+ # 获取服务和特征(假设你已经知道要监听的特征UUID)
|
|
|
+ services = await client.get_services()
|
|
|
+ for service in services:
|
|
|
+ for char in service.characteristics:
|
|
|
+ if "notify" in char.properties:
|
|
|
+ self.print_error(
|
|
|
f"Subscribing to characteristic: {char.uuid}"
|
|
|
)
|
|
|
- await client.start_notify(
|
|
|
+ await client.start_notify(
|
|
|
char,
|
|
|
lambda char, data: asyncio.create_task(
|
|
|
self.notification_handler(address, char, data)
|
|
|
),
|
|
|
)
|
|
|
|
|
|
- # 进入一个简单的循环,保持连接
|
|
|
- self.print_error(
|
|
|
+ # 进入一个简单的循环,保持连接
|
|
|
+ self.print_error(
|
|
|
"进入一个简单的循环 保持连接", self.devices[address]["name"]
|
|
|
)
|
|
|
- self.connect_device(
|
|
|
+ self.connect_device(
|
|
|
address=address, name=self.devices[address]["name"]
|
|
|
)
|
|
|
- self.retry_num += 1
|
|
|
- while True:
|
|
|
- if not client.is_connected:
|
|
|
- self.print_error(
|
|
|
+ self.retry_num += 1
|
|
|
+ while True:
|
|
|
+ if not client.is_connected:
|
|
|
+ self.print_error(
|
|
|
f"Device {address} disconnected unexpectedly."
|
|
|
)
|
|
|
- with self._lock:
|
|
|
- self.disconnect_device(
|
|
|
+ with self._lock:
|
|
|
+ self.disconnect_device(
|
|
|
address=address, name=self.devices[address]["name"]
|
|
|
)
|
|
|
- self.devices[address]["connect_state"] = False
|
|
|
- break
|
|
|
-
|
|
|
- if self.devices[address]["send_queue"]:
|
|
|
- with self._lock:
|
|
|
- send_data = self.devices[address][
|
|
|
+ self.devices[address]["connect_state"] = False
|
|
|
+ break
|
|
|
+ if len(self.devices) == 0:
|
|
|
+ break
|
|
|
+ if self.devices[address]["send_queue"]:
|
|
|
+ with self._lock:
|
|
|
+ send_data = self.devices[address][
|
|
|
"send_queue"
|
|
|
].popleft()
|
|
|
- # print("-----------> send_data:", self.change_hex_to_10_int(send_data))
|
|
|
- await self.write_characteristic(
|
|
|
+ # print("-----------> send_data:", self.change_hex_to_10_int(send_data))
|
|
|
+ await self.write_characteristic(
|
|
|
client, char.uuid, send_data
|
|
|
)
|
|
|
- await asyncio.sleep(0.01)
|
|
|
-
|
|
|
- await asyncio.sleep(0.02)
|
|
|
- # except Exception as e:
|
|
|
- # with self._lock:
|
|
|
- # self.disconnect_device(
|
|
|
- # address=address, name=self.devices[address]["name"]
|
|
|
- # )
|
|
|
- # self.devices[address]["connect_state"] = False
|
|
|
- # print(f"Error during connection or listening: {e}")
|
|
|
- # await asyncio.sleep(2) # 发生错误时等待一段时间再重试
|
|
|
+ await asyncio.sleep(0.01)
|
|
|
+
|
|
|
+ await asyncio.sleep(0.02)
|
|
|
+ # except Exception as e:
|
|
|
+ # with self._lock:
|
|
|
+ # self.disconnect_device(
|
|
|
+ # address=address, name=self.devices[address]["name"]
|
|
|
+ # )
|
|
|
+ # self.devices[address]["connect_state"] = False
|
|
|
+ # print(f"Error during connection or listening: {e}")
|
|
|
+ # await asyncio.sleep(2) # 发生错误时等待一段时间再重试
|
|
|
|
|
|
async def main_func(self):
|
|
|
"""主函数"""
|
|
|
@@ -200,6 +216,15 @@ class BlueToothMode(BaseClass,metaclass=SingletonType):
|
|
|
# }
|
|
|
# self.devices_name[_name] = address
|
|
|
# asyncio.create_task(self.connect_and_listen(address))
|
|
|
+ if self.connect_state == True:
|
|
|
+ print('蓝牙已连接,不可重新连接')
|
|
|
+ message = {
|
|
|
+ "_type": "show_info",
|
|
|
+ "plugins_mode": "remote_control",
|
|
|
+ "data": "遥控设备V2 打开蓝牙成功",
|
|
|
+ }
|
|
|
+ self.sendSocketMessage(code=0, msg="遥控设备V2 打开蓝牙成功", data=message)
|
|
|
+ return
|
|
|
await self.scan_for_esp32()
|
|
|
# 定期重新扫描以发现新设备
|
|
|
while True:
|
|
|
@@ -208,7 +233,6 @@ class BlueToothMode(BaseClass,metaclass=SingletonType):
|
|
|
else:
|
|
|
await asyncio.sleep(3)
|
|
|
await self.scan_for_esp32()
|
|
|
-
|
|
|
def run(self):
|
|
|
self.print_error("开启蓝牙扫描")
|
|
|
asyncio.run(self.main_func())
|