Pair with a Bluetooth device Args: address: MAC address of the device to pair Returns: (success, message)
(self, address: str)
| 1090 | return enriched_devices |
| 1091 | |
| 1092 | def pair_device(self, address: str) -> Tuple[bool, str]: |
| 1093 | """ |
| 1094 | Pair with a Bluetooth device |
| 1095 | Args: |
| 1096 | address: MAC address of the device to pair |
| 1097 | Returns: (success, message) |
| 1098 | """ |
| 1099 | try: |
| 1100 | self.logger.info(f"Attempting to pair with device {address}...") |
| 1101 | |
| 1102 | # First check if device is discoverable |
| 1103 | devices = self.get_discovered_devices(refresh=False) |
| 1104 | if address not in devices: |
| 1105 | return False, f"Device {address} not found. Start a scan first." |
| 1106 | |
| 1107 | result = subprocess.run(['bluetoothctl', 'pair', address], |
| 1108 | capture_output=True, text=True, timeout=30) |
| 1109 | |
| 1110 | if result.returncode == 0 or 'Pairing successful' in result.stdout: |
| 1111 | self.logger.info(f"Successfully paired with {address}") |
| 1112 | return True, f"Successfully paired with {address}" |
| 1113 | else: |
| 1114 | error_msg = result.stderr.strip() or f'Failed to pair with {address}' |
| 1115 | if 'already paired' in error_msg.lower(): |
| 1116 | return True, f"Device {address} is already paired" |
| 1117 | |
| 1118 | self.logger.error(f"Failed to pair with {address}: {error_msg}") |
| 1119 | return False, error_msg |
| 1120 | |
| 1121 | except subprocess.TimeoutExpired: |
| 1122 | return False, f"Pairing with {address} timed out" |
| 1123 | except Exception as e: |
| 1124 | self.logger.error(f"Error pairing with {address}: {e}") |
| 1125 | return False, f"Error pairing with {address}: {str(e)}" |
| 1126 | |
| 1127 | def unpair_device(self, address: str) -> Tuple[bool, str]: |
| 1128 | """ |
no test coverage detected