Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamsorcerer committed Jan 1, 2025
1 parent 58d33e5 commit 0038c20
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
6 changes: 3 additions & 3 deletions adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ async def wait_till_adapter_present_then_init(self) -> None:
await asyncio.sleep(2)

self.alias = DEVICE_NAME
self.bluetooth_devices.remove_devices()
await self.bluetooth_devices.remove_devices()
self.bluetooth_devices.add_devices()
self.initialising_adapter = False

Expand All @@ -137,11 +137,11 @@ def interfaces_added(self, obj_name: str, interfaces: Container[str]) -> None:
def interfaces_removed(self, obj_name: str, interfaces: Container[str]) -> None:
if (obj_name==ADAPTER_OBJECT or obj_name==ROOT_OBJECT):
self.adapter = None
self.bluetooth_devices.remove_devices()
asyncio.run_coroutine_threadsafe(self.bluetooth_devices.remove_devices(), loop=self.loop)
print("Bluetooth adapter removed. Stopping")
asyncio.run_coroutine_threadsafe(self.init(), loop=self.loop)
elif INPUT_HOST_INTERFACE in interfaces or INPUT_DEVICE_INTERFACE in interfaces:
self.bluetooth_devices.remove_device(obj_name)
asyncio.run_coroutine_threadsafe(self.bluetooth_devices.remove_device(obj_name), loop=self.loop)
self.on_interface_changed()

def register_agent(self) -> None:
Expand Down
20 changes: 10 additions & 10 deletions bluetooth_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async def reconcile_connected_state(self, delay: int) -> None:
if self.connected and not self.sockets_connected:
await self.connect_sockets()
elif not self.connected and self.sockets_connected:
self.disconnect_sockets()
await self.disconnect_sockets()
except Exception as exc:
print("Possibly dbus error during reconcile_connected_state ",exc)

Expand Down Expand Up @@ -89,7 +89,7 @@ async def connect_sockets(self) -> None:
await asyncio.sleep(1)
asyncio.run_coroutine_threadsafe(self.connect_sockets(), loop=self.loop)

def disconnect_sockets(self) -> None:
async def disconnect_sockets(self) -> None:
for t in self._tasks:
t.cancel()
with suppress(asyncio.CancelledError):
Expand Down Expand Up @@ -119,7 +119,7 @@ async def loop_of_fun(self, is_ctrl: bool) -> None:
print("Cannot read data from socket. ", self.object_path ,"Closing sockets")
if self is not None:
try:
self.disconnect_sockets()
await self.disconnect_sockets()
except:
print("Error while disconnecting sockets")
print("Arranging reconnect")
Expand Down Expand Up @@ -154,12 +154,12 @@ def device_connected_state_changed(self, _arg1: object, _arg2: object, _arg3: ob
if self.device_registry.on_devices_changed_handler is not None:
asyncio.run_coroutine_threadsafe(self.device_registry.on_devices_changed_handler(), loop=self.loop)

def finalise(self) -> None:
async def finalise(self) -> None:
self.props.PropertiesChanged.disconnect(self.device_connected_state_changed)
self.control_socket_path = None
self.interrupt_socket_path = None
# Close sockets
self.disconnect_sockets()
await self.disconnect_sockets()
print("BT Device ",self.object_path," finalised")


Expand Down Expand Up @@ -223,21 +223,21 @@ async def is_slave(self, device_address: str) -> bool:
stdout, stderr = await proc.communicate()
return any("SLAVE" in l and device_address in l for l in stdout.decode().split("\n"))

def remove_devices(self) -> None:
async def remove_devices(self) -> None:
print("Removing all BT devices")
while len(self.all) >0:
self.remove_device(list(self.all)[0])
await self.remove_device(list(self.all)[0])


def remove_device(self, device_object_path: str) -> None:
async def remove_device(self, device_object_path: str) -> None:
if device_object_path not in self.all:
return # No such device
device = self.all[device_object_path]
del self.all[device_object_path]
list = self.connected_hosts if device.is_host else self.connected_devices
if device in list:
list.remove(device)
device.finalise()
await device.finalise()
del device

def switch_host(self) -> None:
Expand All @@ -262,7 +262,7 @@ def send_message(self, msg: bytes, send_to_hosts: bool, is_control_channel: bool
print("Cannot send data to socket of ",target.object_path,". Closing")
if target is not None:
try:
target.disconnect_sockets()
asyncio.run_coroutine_threadsafe(target.disconnect_sockets(), loop=self.loop)
except:
print("Error while trying to disconnect sockets")
asyncio.run_coroutine_threadsafe(target.reconcile_connected_state(1), loop=self.loop)

0 comments on commit 0038c20

Please sign in to comment.