Bluetooth: revamped a lot of code
This commit is contained in:
parent
b2e9058a2f
commit
c3956c5894
4 changed files with 379 additions and 319 deletions
|
|
@ -13,17 +13,17 @@ Singleton {
|
|||
readonly property bool discovering: (adapter && adapter.discovering) ?? false
|
||||
readonly property var devices: adapter ? adapter.devices : null
|
||||
readonly property var pairedDevices: {
|
||||
if (!adapter || !adapter.devices)
|
||||
return []
|
||||
|
||||
if (!adapter || !adapter.devices) {
|
||||
return []
|
||||
}
|
||||
return adapter.devices.values.filter(dev => {
|
||||
return dev && (dev.paired || dev.trusted)
|
||||
})
|
||||
}
|
||||
readonly property var allDevicesWithBattery: {
|
||||
if (!adapter || !adapter.devices)
|
||||
return []
|
||||
|
||||
if (!adapter || !adapter.devices) {
|
||||
return []
|
||||
}
|
||||
return adapter.devices.values.filter(dev => {
|
||||
return dev && dev.batteryAvailable && dev.battery > 0
|
||||
})
|
||||
|
|
@ -49,34 +49,36 @@ Singleton {
|
|||
}
|
||||
|
||||
function getDeviceIcon(device) {
|
||||
if (!device)
|
||||
if (!device) {
|
||||
return "bluetooth"
|
||||
}
|
||||
|
||||
var name = (device.name || device.deviceName || "").toLowerCase()
|
||||
var icon = (device.icon || "").toLowerCase()
|
||||
if (icon.includes("headset") || icon.includes("audio") || name.includes("headphone") || name.includes("airpod")
|
||||
|| name.includes("headset") || name.includes("arctis"))
|
||||
|| name.includes("headset") || name.includes("arctis")) {
|
||||
return "headset"
|
||||
}
|
||||
|
||||
if (icon.includes("mouse") || name.includes("mouse"))
|
||||
if (icon.includes("mouse") || name.includes("mouse")) {
|
||||
return "mouse"
|
||||
|
||||
if (icon.includes("keyboard") || name.includes("keyboard"))
|
||||
}
|
||||
if (icon.includes("keyboard") || name.includes("keyboard")) {
|
||||
return "keyboard"
|
||||
|
||||
}
|
||||
if (icon.includes("phone") || name.includes("phone") || name.includes("iphone") || name.includes("android")
|
||||
|| name.includes("samsung"))
|
||||
|| name.includes("samsung")) {
|
||||
return "smartphone"
|
||||
|
||||
if (icon.includes("watch") || name.includes("watch"))
|
||||
}
|
||||
if (icon.includes("watch") || name.includes("watch")) {
|
||||
return "watch"
|
||||
|
||||
if (icon.includes("speaker") || name.includes("speaker"))
|
||||
}
|
||||
if (icon.includes("speaker") || name.includes("speaker")) {
|
||||
return "speaker"
|
||||
|
||||
if (icon.includes("display") || name.includes("tv"))
|
||||
}
|
||||
if (icon.includes("display") || name.includes("tv")) {
|
||||
return "tv"
|
||||
|
||||
}
|
||||
return "bluetooth"
|
||||
}
|
||||
|
||||
|
|
@ -88,60 +90,91 @@ Singleton {
|
|||
}
|
||||
|
||||
function getSignalStrength(device) {
|
||||
if (!device || device.signalStrength === undefined || device.signalStrength <= 0)
|
||||
return "Unknown"
|
||||
|
||||
if (device.pairing) {
|
||||
return "Pairing..."
|
||||
}
|
||||
if (device.blocked) {
|
||||
return "Blocked"
|
||||
}
|
||||
if (!device || device.signalStrength === undefined || device.signalStrength <= 0) {
|
||||
return "Signal: Unknown"
|
||||
}
|
||||
var signal = device.signalStrength
|
||||
if (signal >= 80)
|
||||
return "Excellent"
|
||||
if (signal >= 80) {
|
||||
return "Signal: Excellent"
|
||||
}
|
||||
if (signal >= 60) {
|
||||
return "Signal: Good"
|
||||
}
|
||||
if (signal >= 40) {
|
||||
return "Signal: Fair"
|
||||
}
|
||||
if (signal >= 20) {
|
||||
return "Signal: Poor"
|
||||
}
|
||||
return "Signal: Very Poor"
|
||||
}
|
||||
|
||||
if (signal >= 60)
|
||||
return "Good"
|
||||
|
||||
if (signal >= 40)
|
||||
return "Fair"
|
||||
|
||||
if (signal >= 20)
|
||||
return "Poor"
|
||||
|
||||
return "Very Poor"
|
||||
function getBattery(device) {
|
||||
return `Battery: ${Math.round(device.battery * 100)}`
|
||||
}
|
||||
|
||||
function getSignalIcon(device) {
|
||||
if (!device || device.signalStrength === undefined || device.signalStrength <= 0)
|
||||
if (!device || device.signalStrength === undefined || device.signalStrength <= 0) {
|
||||
return "signal_cellular_null"
|
||||
|
||||
}
|
||||
var signal = device.signalStrength
|
||||
if (signal >= 80)
|
||||
if (signal >= 80) {
|
||||
return "signal_cellular_4_bar"
|
||||
|
||||
if (signal >= 60)
|
||||
}
|
||||
if (signal >= 60) {
|
||||
return "signal_cellular_3_bar"
|
||||
|
||||
if (signal >= 40)
|
||||
}
|
||||
if (signal >= 40) {
|
||||
return "signal_cellular_2_bar"
|
||||
|
||||
if (signal >= 20)
|
||||
}
|
||||
if (signal >= 20) {
|
||||
return "signal_cellular_1_bar"
|
||||
|
||||
}
|
||||
return "signal_cellular_0_bar"
|
||||
}
|
||||
|
||||
function isDeviceBusy(device) {
|
||||
if (!device)
|
||||
if (!device) {
|
||||
return false
|
||||
}
|
||||
|
||||
return device.pairing || device.state === BluetoothDeviceState.Disconnecting
|
||||
|| device.state === BluetoothDeviceState.Connecting
|
||||
}
|
||||
|
||||
function connectDeviceWithTrust(device) {
|
||||
if (!device)
|
||||
if (!device) {
|
||||
return
|
||||
}
|
||||
|
||||
device.trusted = true
|
||||
device.connect()
|
||||
}
|
||||
|
||||
function disconnectDevice(device) {
|
||||
if (!device) {
|
||||
return
|
||||
}
|
||||
|
||||
device.trusted = false
|
||||
device.disconnect()
|
||||
}
|
||||
|
||||
function forgetDevice(device) {
|
||||
if (!device) {
|
||||
return
|
||||
}
|
||||
|
||||
device.trusted = false
|
||||
device.forget()
|
||||
}
|
||||
|
||||
function setBluetoothEnabled(enabled) {
|
||||
if (!adapter) {
|
||||
Logger.warn("Bluetooth", "No adapter available")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue