1
0
mirror of https://github.com/AvengeMedia/DankMaterialShell.git synced 2025-12-07 22:15:38 -05:00

bluetooth: pairing dialog

- Allows connecting pin/passkey/confirmation dialogs
- Fixes inability to connect to many devices
- Dependent on un-merged quickshell PR: https://github.com/quickshell-mirror/quickshell/pull/138
This commit is contained in:
bbedward
2025-07-22 19:20:10 -04:00
parent 02bd9bbc72
commit 3157622c8b
4 changed files with 555 additions and 29 deletions

View File

@@ -10,8 +10,10 @@ Singleton {
readonly property BluetoothAdapter adapter: Bluetooth.defaultAdapter
readonly property bool available: adapter !== null
readonly property bool enabled: (adapter && adapter.enabled) ?? false
readonly property bool discovering: (adapter && adapter.discovering) ?? false
readonly property bool enabled: available ? adapter.enabled ?? false : false
readonly property bool discovering: available ? adapter.discovering ?? false : false
property bool operationInProgress: false
readonly property var devices: adapter ? adapter.devices : null
readonly property var pairedDevices: {
if (!adapter || !adapter.devices)
@@ -30,6 +32,15 @@ Singleton {
});
}
// Pairing dialog properties
property bool pairingDialogVisible: false
property int pairingType: BluetoothPairingRequestType.Authorization
property string pendingDeviceAddress: ""
property string pendingDeviceName: ""
property int pendingPasskey: 0
property var pendingToken: null
property string inputText: ""
function sortDevices(devices) {
return devices.sort((a, b) => {
var aName = a.name || a.deviceName || "";
@@ -167,9 +178,147 @@ Singleton {
function connectDeviceWithTrust(device) {
if (!device) return;
device.trusted = true;
device.connect();
device.connect()
}
function toggleAdapter() {
if (!available || operationInProgress) {
console.warn("BluetoothService: Cannot toggle adapter - not available or operation in progress");
return false;
}
operationInProgress = true;
var targetState = !adapter.enabled;
try {
adapter.enabled = targetState;
return true;
} catch (error) {
console.error("BluetoothService: Failed to toggle adapter:", error);
operationInProgress = false;
return false;
}
}
function toggleDiscovery() {
if (!available || !adapter.enabled || operationInProgress) {
console.warn("BluetoothService: Cannot toggle discovery - adapter not ready or operation in progress");
return false;
}
operationInProgress = true;
var targetState = !adapter.discovering;
try {
adapter.discovering = targetState;
return true;
} catch (error) {
console.error("BluetoothService: Failed to toggle discovery:", error);
operationInProgress = false;
return false;
}
}
// Monitor adapter state changes to clear operation flags
Connections {
target: adapter
ignoreUnknownSignals: true
function onEnabledChanged() {
operationInProgress = false;
}
function onDiscoveringChanged() {
operationInProgress = false;
}
}
// Pairing agent signal handler
Connections {
target: Bluetooth.agent
ignoreUnknownSignals: true
function onPairingRequested(deviceAddress, type, passkey, token) {
console.log("BluetoothService: Pairing requested for", deviceAddress, "type:", type, "passkey:", passkey, "token:", token);
root.pairingType = type;
root.pendingDeviceAddress = deviceAddress;
root.pendingPasskey = passkey;
root.pendingToken = token;
root.inputText = "";
// Try to find and store the device name using MAC address
var device = root.getDeviceFromAddress(deviceAddress);
root.pendingDeviceName = device ? (device.name || device.deviceName || deviceAddress) : deviceAddress;
console.log("BluetoothService: Device name:", root.pendingDeviceName, "for address:", deviceAddress, "token:", token);
root.pairingDialogVisible = true;
}
}
function acceptPairing() {
console.log("BluetoothService: Accepting pairing for", root.pendingDeviceAddress, "type:", root.pairingType, "token:", root.pendingToken);
if (!Bluetooth.agent || root.pendingToken === null) return;
switch (root.pairingType) {
case BluetoothPairingRequestType.Authorization:
case BluetoothPairingRequestType.Confirmation:
case BluetoothPairingRequestType.ServiceAuthorization:
Bluetooth.agent.respondToRequest(root.pendingToken, true);
break;
case BluetoothPairingRequestType.PinCode:
if (root.inputText.length > 0) {
Bluetooth.agent.respondWithPinCode(root.pendingToken, root.inputText);
} else {
console.warn("BluetoothService: No PIN code entered");
return;
}
break;
case BluetoothPairingRequestType.Passkey:
var passkey = parseInt(root.inputText);
if (passkey >= 0 && passkey <= 999999) {
Bluetooth.agent.respondWithPasskey(root.pendingToken, passkey);
} else {
console.warn("BluetoothService: Invalid passkey:", root.inputText);
return;
}
break;
}
closePairingDialog();
}
function rejectPairing() {
console.log("BluetoothService: Rejecting pairing for", root.pendingDeviceAddress, "token:", root.pendingToken);
if (Bluetooth.agent && root.pendingToken !== null) {
Bluetooth.agent.respondToRequest(root.pendingToken, false);
}
closePairingDialog();
}
function closePairingDialog() {
root.pairingDialogVisible = false;
root.pendingDeviceAddress = "";
root.pendingDeviceName = "";
root.pendingPasskey = 0;
root.pendingToken = null;
root.inputText = "";
root.pairingType = BluetoothPairingRequestType.Authorization;
}
function getDeviceFromPath(devicePath) {
if (!adapter || !adapter.devices || !devicePath)
return null;
return adapter.devices.values.find(d => d && d.path === devicePath) || null;
}
function getDeviceFromAddress(deviceAddress) {
if (!adapter || !adapter.devices || !deviceAddress)
return null;
return adapter.devices.values.find(d => d && d.address === deviceAddress) || null;
}
}