mirror of
https://github.com/smittix/intercept.git
synced 2026-07-15 04:58:10 -07:00
Improve Bluetooth adapter reset with multiple fallback methods
- Check if running as root, try sudo -n if not - Try rfkill unblock bluetooth first - Fall back to bluetoothctl power off/on if hciconfig fails - Better error messaging when adapter stays down - Suggest manual command when auto-reset fails 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
+32
-6
@@ -5959,27 +5959,53 @@ def reset_bt_adapter():
|
|||||||
# Reset the adapter
|
# Reset the adapter
|
||||||
try:
|
try:
|
||||||
import time
|
import time
|
||||||
|
import os
|
||||||
|
|
||||||
# Kill any processes that might be using the adapter
|
# Kill any processes that might be using the adapter
|
||||||
subprocess.run(['pkill', '-f', 'hcitool'], capture_output=True, timeout=2)
|
subprocess.run(['pkill', '-f', 'hcitool'], capture_output=True, timeout=2)
|
||||||
subprocess.run(['pkill', '-f', 'bluetoothctl'], capture_output=True, timeout=2)
|
subprocess.run(['pkill', '-f', 'bluetoothctl'], capture_output=True, timeout=2)
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
|
|
||||||
|
# Check if running as root
|
||||||
|
is_root = os.geteuid() == 0
|
||||||
|
|
||||||
|
# Try rfkill unblock first
|
||||||
|
subprocess.run(['rfkill', 'unblock', 'bluetooth'], capture_output=True, timeout=5)
|
||||||
|
|
||||||
# Reset the adapter with a delay between down and up
|
# Reset the adapter with a delay between down and up
|
||||||
subprocess.run(['hciconfig', interface, 'down'], capture_output=True, timeout=5)
|
if is_root:
|
||||||
time.sleep(1)
|
down_result = subprocess.run(['hciconfig', interface, 'down'], capture_output=True, text=True, timeout=5)
|
||||||
subprocess.run(['hciconfig', interface, 'up'], capture_output=True, timeout=5)
|
time.sleep(1)
|
||||||
|
up_result = subprocess.run(['hciconfig', interface, 'up'], capture_output=True, text=True, timeout=5)
|
||||||
|
else:
|
||||||
|
# Try with sudo
|
||||||
|
down_result = subprocess.run(['sudo', '-n', 'hciconfig', interface, 'down'], capture_output=True, text=True, timeout=5)
|
||||||
|
time.sleep(1)
|
||||||
|
up_result = subprocess.run(['sudo', '-n', 'hciconfig', interface, 'up'], capture_output=True, text=True, timeout=5)
|
||||||
|
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
|
|
||||||
# Check if adapter is up
|
# Check if adapter is up
|
||||||
result = subprocess.run(['hciconfig', interface], capture_output=True, text=True, timeout=5)
|
result = subprocess.run(['hciconfig', interface], capture_output=True, text=True, timeout=5)
|
||||||
is_up = 'UP RUNNING' in result.stdout
|
is_up = 'UP RUNNING' in result.stdout
|
||||||
|
|
||||||
bt_queue.put({'type': 'info', 'text': f'Bluetooth adapter {interface} reset'})
|
# If still not up, try bluetoothctl
|
||||||
|
if not is_up:
|
||||||
|
subprocess.run(['bluetoothctl', 'power', 'off'], capture_output=True, timeout=5)
|
||||||
|
time.sleep(1)
|
||||||
|
subprocess.run(['bluetoothctl', 'power', 'on'], capture_output=True, timeout=5)
|
||||||
|
time.sleep(0.5)
|
||||||
|
result = subprocess.run(['hciconfig', interface], capture_output=True, text=True, timeout=5)
|
||||||
|
is_up = 'UP RUNNING' in result.stdout
|
||||||
|
|
||||||
|
if is_up:
|
||||||
|
bt_queue.put({'type': 'info', 'text': f'Bluetooth adapter {interface} reset successfully'})
|
||||||
|
else:
|
||||||
|
bt_queue.put({'type': 'error', 'text': f'Adapter {interface} may need manual reset. Try: sudo hciconfig {interface} up'})
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
'status': 'success',
|
'status': 'success' if is_up else 'warning',
|
||||||
'message': f'Adapter {interface} reset',
|
'message': f'Adapter {interface} reset' if is_up else f'Reset attempted but adapter still down. Run: sudo hciconfig {interface} up',
|
||||||
'is_up': is_up
|
'is_up': is_up
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user