From 2eba79b98eec8ed0b571b33a874f2c615d2f41e4 Mon Sep 17 00:00:00 2001 From: S4NKALP Date: Sat, 16 Aug 2025 13:15:21 +0545 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(bluetooth):=20add=20fallback?= =?UTF-8?q?=20to=20bluetoothctl=20for=20enabling/disabling=20bluetooth?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/controlcenter/bluetooth.py | 27 ++++++++++++++++++++++++--- modules/controlcenter/main.py | 14 ++++++++------ 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/modules/controlcenter/bluetooth.py b/modules/controlcenter/bluetooth.py index ce256b4..483621e 100644 --- a/modules/controlcenter/bluetooth.py +++ b/modules/controlcenter/bluetooth.py @@ -2,7 +2,7 @@ import subprocess import gi from fabric.bluetooth import BluetoothClient, BluetoothDevice -from fabric.utils import get_relative_path +from fabric.utils import exec_shell_command, get_relative_path from fabric.widgets.box import Box from fabric.widgets.button import Button from fabric.widgets.centerbox import CenterBox @@ -12,13 +12,32 @@ from fabric.widgets.revealer import Revealer from fabric.widgets.scrolledwindow import ScrolledWindow from fabric.widgets.separator import Separator from fabric.widgets.svg import Svg -from gi.repository import Gdk, Gtk, GLib +from gi.repository import Gdk, GLib, Gtk +from loguru import logger + from services.battery import Battery gi.require_version("Gtk", "3.0") gi.require_version("Gdk", "3.0") +def set_bluetooth_enabled_with_fallback(client, enabled: bool): + try: + # Try fabric bluetooth first + client.set_enabled(enabled) + except Exception as e: + logger.warning(f"Fabric bluetooth set_enabled({enabled}) failed: {e}") + + # Fallback to bluetoothctl using exec_shell_command + power_state = "on" if enabled else "off" + command = f"bluetoothctl power {power_state}" + + try: + exec_shell_command(command) + except Exception as fallback_e: + logger.error(f"bluetoothctl fallback failed: {fallback_e}") + + class BluetoothDeviceSlot(CenterBox): def __init__(self, device: BluetoothDevice, **kwargs): super().__init__(h_expand=True, name="device-button", **kwargs) @@ -206,7 +225,9 @@ class BluetoothConnections(Box): self.toggle_button.set_active(self.client.enabled) self.toggle_button.connect( "notify::active", - lambda *_: self.client.set_enabled(self.toggle_button.get_active()), + lambda *_: set_bluetooth_enabled_with_fallback( + self.client, self.toggle_button.get_active() + ), ) # Connect client signals diff --git a/modules/controlcenter/main.py b/modules/controlcenter/main.py index 5270f01..cfa00bc 100644 --- a/modules/controlcenter/main.py +++ b/modules/controlcenter/main.py @@ -12,7 +12,11 @@ from fabric.widgets.scale import Scale from fabric.widgets.svg import Svg from gi.repository import Gdk, GLib from loguru import logger -from modules.controlcenter.bluetooth import BluetoothConnections + +from modules.controlcenter.bluetooth import ( + BluetoothConnections, + set_bluetooth_enabled_with_fallback, +) from modules.controlcenter.expanded_player import EmbeddedExpandedPlayer from modules.controlcenter.nightlight import create_night_light_widget from modules.controlcenter.per_app_volume import PerAppVolumeControl @@ -21,9 +25,6 @@ from modules.controlcenter.wifi import WifiConnections from services.brightness import Brightness from services.mpris import MprisPlayerManager from services.network import NetworkClient -from utils.animator import Animator -from utils.animator import Animator -from utils.animator import Animator from utils.roam import audio_service, modus_service from widgets.wayland import WaylandWindow as Window @@ -824,12 +825,13 @@ class ModusControlCenter(Window): return True def toggle_bluetooth(self, *_): - """Toggle bluetooth on/off""" try: # Access the bluetooth client from the bluetooth manager if hasattr(self, "bluetooth_man") and hasattr(self.bluetooth_man, "client"): current_state = self.bluetooth_man.client.enabled - self.bluetooth_man.client.set_enabled(not current_state) + set_bluetooth_enabled_with_fallback( + self.bluetooth_man.client, not current_state + ) else: logger.warning("Bluetooth client not available for toggling") except Exception as e: