Skip to content

bluetooth: add rssi property #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/bluetooth/device.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "device.hpp"
#include <algorithm>

#include <qcontainerfwd.h>
#include <qdbusconnection.h>
Expand Down Expand Up @@ -46,12 +47,35 @@ BluetoothDevice::BluetoothDevice(const QString& path, QObject* parent): QObject(
}

this->properties.setInterface(this->mInterface);

this->bRssi.subscribe([this]() { emit this->signalStrengthChanged(); });
}

BluetoothAdapter* BluetoothDevice::adapter() const {
return Bluez::instance()->adapter(this->bAdapterPath.value().path());
}

qint32 BluetoothDevice::signalStrength() const {
// Convert RSSI (dBm) to a normalized 0-100 scale
// Based on practical Bluetooth RSSI ranges:
// -30 to -40 dBm = 85-100
// -40 to -55 dBm = 65-85
// -55 to -65 dBm = 45-65
// -65 to -75 dBm = 25-45
// -75 to -85 dBm = 10-25
// <= -85 dBm = 0-10

auto rssiValue = this->bRssi.value();
if (rssiValue == 0) {
return 0;
}

auto rssi = std::max(static_cast<qint16>(-100), std::min(static_cast<qint16>(-30), rssiValue));
auto normalized = static_cast<qint32>(((rssi + 100) / 70.0) * 100.0);

return std::max(0, std::min(100, normalized));
}

void BluetoothDevice::setConnected(bool connected) {
if (connected == this->bConnected) return;

Expand Down
9 changes: 9 additions & 0 deletions src/bluetooth/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ class BluetoothDevice: public QObject {
Q_PROPERTY(bool batteryAvailable READ batteryAvailable NOTIFY batteryAvailableChanged);
/// Battery level of the connected device, from `0.0` to `1.0`. Only valid if @@batteryAvailable is true.
Q_PROPERTY(qreal battery READ default NOTIFY batteryChanged BINDABLE bindableBattery);
/// Signal strength as a normalized score from 0 to 100, where higher is better (stronger signal).
Q_PROPERTY(qint32 signalStrength READ signalStrength NOTIFY signalStrengthChanged BINDABLE bindableSignalStrength);
/// The Bluetooth adapter this device belongs to.
Q_PROPERTY(BluetoothAdapter* adapter READ adapter NOTIFY adapterChanged);
/// DBus path of the device under the `org.bluez` system service.
Expand Down Expand Up @@ -145,6 +147,8 @@ class BluetoothDevice: public QObject {
[[nodiscard]] bool wakeAllowed() const { return this->bWakeAllowed; }
void setWakeAllowed(bool wakeAllowed);

[[nodiscard]] qint32 signalStrength() const;

[[nodiscard]] bool pairing() const { return this->bPairing; }

[[nodiscard]] QBindable<QString> bindableAddress() { return &this->bAddress; }
Expand All @@ -159,6 +163,7 @@ class BluetoothDevice: public QObject {
[[nodiscard]] QBindable<QString> bindableIcon() { return &this->bIcon; }
[[nodiscard]] QBindable<qreal> bindableBattery() { return &this->bBattery; }
[[nodiscard]] QBindable<BluetoothDeviceState::Enum> bindableState() { return &this->bState; }
[[nodiscard]] QBindable<qint32> bindableSignalStrength() { return &this->bSignalStrength; }

void addInterface(const QString& interface, const QVariantMap& properties);
void removeInterface(const QString& interface);
Expand All @@ -178,6 +183,7 @@ class BluetoothDevice: public QObject {
void iconChanged();
void batteryAvailableChanged();
void batteryChanged();
void signalStrengthChanged();
void adapterChanged();

private:
Expand All @@ -201,6 +207,8 @@ class BluetoothDevice: public QObject {
Q_OBJECT_BINDABLE_PROPERTY(BluetoothDevice, qreal, bBattery, &BluetoothDevice::batteryChanged);
Q_OBJECT_BINDABLE_PROPERTY(BluetoothDevice, BluetoothDeviceState::Enum, bState, &BluetoothDevice::stateChanged);
Q_OBJECT_BINDABLE_PROPERTY(BluetoothDevice, bool, bPairing, &BluetoothDevice::pairingChanged);
Q_OBJECT_BINDABLE_PROPERTY(BluetoothDevice, qint32, bSignalStrength, &BluetoothDevice::signalStrengthChanged);
Q_OBJECT_BINDABLE_PROPERTY(BluetoothDevice, qint16, bRssi);

QS_DBUS_BINDABLE_PROPERTY_GROUP(BluetoothDevice, properties);
QS_DBUS_PROPERTY_BINDING(BluetoothDevice, pAddress, bAddress, properties, "Address");
Expand All @@ -214,6 +222,7 @@ class BluetoothDevice: public QObject {
QS_DBUS_PROPERTY_BINDING(BluetoothDevice, pWakeAllowed, bWakeAllowed, properties, "WakeAllowed");
QS_DBUS_PROPERTY_BINDING(BluetoothDevice, pIcon, bIcon, properties, "Icon");
QS_DBUS_PROPERTY_BINDING(BluetoothDevice, pAdapterPath, bAdapterPath, properties, "Adapter");
QS_DBUS_PROPERTY_BINDING(BluetoothDevice, pRssi, bRssi, properties, "RSSI", false);

QS_DBUS_BINDABLE_PROPERTY_GROUP(BluetoothDevice, batteryProperties);
QS_DBUS_PROPERTY_BINDING(BluetoothDevice, BatteryPercentage, pBattery, bBattery, batteryProperties, "Percentage", true);
Expand Down