commits
tags
import QtQuick
import TvLauncher
// Bluetooth via BlueZ (bluetoothctl): sätt på, sök, parkoppla och anslut
// t.ex. handkontroller, tangentbord och hörlurar.
Item {
id: page
readonly property bool available: shell.has("bluetoothctl")
property bool serviceOk: true
property bool powered: false
property bool scanning: false
property var devices: []
property string busyMac: ""
property string status: ""
function focusFirst() { list.focusFirst() }
function parseDevices(text) {
const out = []
for (const line of text.split("\n")) {
const m = line.trim().match(/^Device\s+([0-9A-F:]{17})\s+(.*)$/i)
if (m) out.push({ mac: m[1].toUpperCase(), name: m[2].trim() })
}
return out
}
function refresh() {
if (!available) return
shell.run("bluetoothctl", ["show"], (code, out) => {
serviceOk = code === 0 && out.indexOf("Controller") >= 0
powered = /Powered:\s*yes/.test(out)
}, 8000)
shell.run("bluetoothctl", ["devices"], (c1, all) => {
shell.run("bluetoothctl", ["devices", "Paired"], (c2, paired) => {
shell.run("bluetoothctl", ["devices", "Connected"], (c3, conn) => {
const p = new Set(parseDevices(paired).map(d => d.mac))
const c = new Set(parseDevices(conn).map(d => d.mac))
const list = parseDevices(all).map(d => ({
mac: d.mac, name: d.name, paired: p.has(d.mac), connected: c.has(d.mac)
})).filter(d => d.paired || d.name.replace(/-/g, ":").toUpperCase() !== d.mac)
list.sort((a, b) => (b.connected - a.connected) || (b.paired - a.paired)
|| a.name.localeCompare(b.name))
devices = list
}, 8000)
}, 8000)
}, 8000)
}
function setPower(on) {
shell.run("bluetoothctl", ["power", on ? "on" : "off"], () => refresh(), 8000)
}
function scan() {
if (scanning) return
if (!powered) setPower(true)
scanning = true
status = "Söker efter enheter i 20 sekunder. Sätt enheten i parkopplingsläge."
shell.run("bluetoothctl", ["--timeout", "20", "scan", "on"], () => {
scanning = false
status = ""
refresh()
}, 30000)
}
function iconFor(name) {
if (/8bitdo|controller|gamepad|pad|xbox|dualsense|dualshock|joy-?con|pro con/i.test(name)) return "gamepad"
if (/keyboard|keys|tangentbord/i.test(name)) return "keyboard"
if (/head|buds|airpods|wh-|wf-|earbud|hörlur/i.test(name)) return "headphones"
if (/speaker|soundbar|jbl|sonos/i.test(name)) return "speaker"
return "device"
}
function activate(d) {
if (busyMac !== "") return
busyMac = d.mac
const done = (msg) => { busyMac = ""; status = msg; refresh() }
if (d.connected) {
shell.run("bluetoothctl", ["disconnect", d.mac], (c) => done(c === 0 ? "Frånkopplad: " + d.name : "Kunde inte koppla från " + d.name), 15000)
} else if (d.paired) {
status = "Ansluter till " + d.name + " ..."
shell.run("bluetoothctl", ["connect", d.mac], (c, out) =>
done(/successful/i.test(out) ? "Ansluten: " + d.name : "Kunde inte ansluta till " + d.name + ". Är den påslagen?"), 20000)
} else {
status = "Parkopplar " + d.name + " ..."
shell.run("bluetoothctl", ["--agent", "NoInputNoOutput", "--timeout", "25", "pair", d.mac], (c, out) => {
if (!/Pairing successful|AlreadyExists/i.test(out)) {
done("Parkopplingen misslyckades. Sätt " + d.name + " i parkopplingsläge och försök igen.")
return
}
shell.run("bluetoothctl", ["trust", d.mac], () => {
shell.run("bluetoothctl", ["connect", d.mac], (c3, out3) =>
done(/successful/i.test(out3) ? "Klar! " + d.name + " är ansluten." : "Parkopplad, men kunde inte ansluta ännu."), 20000)
}, 8000)
}, 35000)
}
}
function forget(d) {
shell.run("bluetoothctl", ["remove", d.mac], () => { status = "Glömde " + d.name; refresh() }, 8000)
}
Component.onCompleted: refresh()
Timer { interval: page.scanning ? 2000 : 6000; running: page.available && page.visible; repeat: true; onTriggered: page.refresh() }
CardList {
id: list
anchors.fill: parent
SettingsCard {
visible: !page.available
icon: "bluetooth"
title: "Bluetooth saknas"
subtitle: "Installera BlueZ:\nsudo apt install bluez\nsudo systemctl enable --now bluetooth"
}
SettingsCard {
visible: page.available && !page.serviceOk
icon: "bluetooth"
title: "Bluetooth-tjänsten svarar inte"
subtitle: "Starta den med: sudo systemctl enable --now bluetooth"
onActivated: page.refresh()
}
SettingsCard {
visible: page.available && page.serviceOk
icon: "bluetooth"
title: "Bluetooth"
subtitle: page.powered ? "På" : "Av"
toggle: page.powered
onActivated: page.setPower(!page.powered)
}
SettingsCard {
visible: page.available && page.serviceOk
icon: "search"
title: page.scanning ? "Söker ..." : "Sök efter nya enheter"
subtitle: "Sätt handkontrollen eller tangentbordet i parkopplingsläge först"
busy: page.scanning
onActivated: page.scan()
}
Text {
width: parent.width
visible: page.status !== ""
text: page.status
color: Theme.accent
wrapMode: Text.Wrap
topPadding: Theme.dp(10)
font.family: Theme.fontFamily
font.pixelSize: Theme.dp(24)
}
SectionTitle {
visible: page.devices.length > 0
text: "Enheter"
}
Repeater {
model: page.devices
delegate: SettingsCard {
required property var modelData
icon: page.iconFor(modelData.name)
title: modelData.name
subtitle: modelData.connected ? "Ansluten · tryck för att koppla från"
: modelData.paired ? "Parkopplad · tryck för att ansluta · Y: glöm"
: "Ny enhet · tryck för att parkoppla"
checked: modelData.connected
busy: page.busyMac === modelData.mac
onActivated: page.activate(modelData)
onMenuRequested: if (modelData.paired) page.forget(modelData)
}
}
}
}