diff --git a/bumble/device.py b/bumble/device.py index 7a43061..031c071 100644 --- a/bumble/device.py +++ b/bumble/device.py @@ -2851,18 +2851,22 @@ class Device(CompositeEventEmitter): method = methods[peer_io_capability][io_capability] async def reply() -> None: - if await connection.abort_on('disconnection', method()): - await self.host.send_command( - HCI_User_Confirmation_Request_Reply_Command( # type: ignore[call-arg] - bd_addr=connection.peer_address - ) - ) - else: - await self.host.send_command( - HCI_User_Confirmation_Request_Negative_Reply_Command( # type: ignore[call-arg] - bd_addr=connection.peer_address + try: + if await connection.abort_on('disconnection', method()): + await self.host.send_command( + HCI_User_Confirmation_Request_Reply_Command( # type: ignore[call-arg] + bd_addr=connection.peer_address + ) ) + return + except Exception as error: + logger.warning(f'exception while confirming: {error}') + + await self.host.send_command( + HCI_User_Confirmation_Request_Negative_Reply_Command( # type: ignore[call-arg] + bd_addr=connection.peer_address ) + ) AsyncRunner.spawn(reply()) @@ -2874,21 +2878,25 @@ class Device(CompositeEventEmitter): pairing_config = self.pairing_config_factory(connection) async def reply() -> None: - number = await connection.abort_on( - 'disconnection', pairing_config.delegate.get_number() + try: + number = await connection.abort_on( + 'disconnection', pairing_config.delegate.get_number() + ) + if number is not None: + await self.host.send_command( + HCI_User_Passkey_Request_Reply_Command( # type: ignore[call-arg] + bd_addr=connection.peer_address, numeric_value=number + ) + ) + return + except Exception as error: + logger.warning(f'exception while asking for pass-key: {error}') + + await self.host.send_command( + HCI_User_Passkey_Request_Negative_Reply_Command( # type: ignore[call-arg] + bd_addr=connection.peer_address + ) ) - if number is not None: - await self.host.send_command( - HCI_User_Passkey_Request_Reply_Command( # type: ignore[call-arg] - bd_addr=connection.peer_address, numeric_value=number - ) - ) - else: - await self.host.send_command( - HCI_User_Passkey_Request_Negative_Reply_Command( # type: ignore[call-arg] - bd_addr=connection.peer_address - ) - ) AsyncRunner.spawn(reply()) diff --git a/bumble/smp.py b/bumble/smp.py index 3cdcae1..947fd69 100644 --- a/bumble/smp.py +++ b/bumble/smp.py @@ -858,10 +858,13 @@ class Session: self.tk = self.passkey.to_bytes(16, byteorder='little') logger.debug(f'TK from passkey = {self.tk.hex()}') - self.connection.abort_on( - 'disconnection', - self.pairing_config.delegate.display_number(self.passkey, digits=6), - ) + try: + self.connection.abort_on( + 'disconnection', + self.pairing_config.delegate.display_number(self.passkey, digits=6), + ) + except Exception as error: + logger.warning(f'exception while displaying number: {error}') def input_passkey(self, next_steps: Optional[Callable[[], None]] = None) -> None: # Prompt the user for the passkey displayed on the peer @@ -1300,7 +1303,11 @@ class Session: self, command: SMP_Pairing_Request_Command ) -> None: # Check if the request should proceed - accepted = await self.pairing_config.delegate.accept() + try: + accepted = await self.pairing_config.delegate.accept() + except Exception as error: + logger.warning(f'exception while accepting: {error}') + accepted = False if not accepted: logger.debug('pairing rejected by delegate') self.send_pairing_failed(SMP_PAIRING_NOT_SUPPORTED_ERROR)