🐛 fix(bluetooth): add fallback to bluetoothctl for enabling/disabling bluetooth
This commit is contained in:
2 files changed
+32
-9
No files matched your search
@@ -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
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in new issue
Block a user