Merge pull request #1981 from cbxcvl/feature/bluetooth-autoconnect

feat(bluetooth): auto-connect paired & trusted devices with toggle control
This commit is contained in:
Lemmy
2026-03-03 19:09:19 -05:00
committed by GitHub
6 changed files with 80 additions and 0 deletions
+48
View File
@@ -68,6 +68,17 @@ Singleton {
property bool _discoveryWasRunning: false
property bool _ctlInit: false
Connections {
target: Settings.data.network
function onBluetoothAutoConnectChanged() {
if ((Settings?.data?.network?.bluetoothAutoConnect ?? true) && adapter && adapter.enabled) {
autoConnectTimer.restart();
} else {
autoConnectTimer.stop();
}
}
}
Timer {
id: initDelayTimer
interval: 3000
@@ -75,6 +86,13 @@ Singleton {
repeat: false
}
Timer {
id: autoConnectTimer
interval: 2000
repeat: false
onTriggered: root.attemptAutoConnect()
}
function init() {
Logger.i("Bluetooth", "Service started");
}
@@ -86,6 +104,10 @@ Singleton {
Quickshell.execDetached(["rfkill", "block", "wifi"]);
Quickshell.execDetached(["rfkill", "block", "bluetooth"]);
}
// Auto-connect on startup if BT is already enabled
if ((Settings?.data?.network?.bluetoothAutoConnect ?? true) && adapter && adapter.enabled) {
autoConnectTimer.restart();
}
}
// Handle system wakeup to force-poll and ensure state is up-to-date
@@ -106,6 +128,11 @@ Singleton {
}
checkAirplaneMode.running = true;
}
function onEnabledChanged() {
if (adapter && adapter.enabled && (Settings?.data?.network?.bluetoothAutoConnect ?? true)) {
autoConnectTimer.restart();
}
}
}
onAdapterChanged: {
@@ -597,6 +624,27 @@ Singleton {
forgetDevice(device);
}
function attemptAutoConnect() {
if (airplaneModeEnabled) return;
if (!adapter || !adapter.enabled) return;
if (!(Settings?.data?.network?.bluetoothAutoConnect ?? true)) return;
var devList = adapter.devices.values.filter(function(dev) {
return dev && dev.paired && !dev.connected && !dev.blocked;
});
for (var i = 0; i < devList.length; i++) {
Logger.i("Bluetooth", "Auto-connecting to:", devList[i].name || devList[i].deviceName);
connectDeviceWithTrust(devList[i]);
}
if (devList.length > 0) {
ToastService.showNotice(I18n.tr("common.bluetooth"), I18n.tr("toast.bluetooth.auto-connecting", {
count: devList.length
}), "bluetooth");
}
}
function connectDeviceWithTrust(device) {
if (!device) {
return;