Add signal strength property to bluetooth devices

pull/503/head
Michael Zanetti 2021-01-14 00:20:36 +01:00
parent 94483e92c5
commit 46e4180bb0
5 changed files with 17 additions and 2 deletions

View File

@ -67,6 +67,11 @@ bool BluetoothDeviceInfo::isLowEnergy() const
return m_deviceInfo.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration; return m_deviceInfo.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration;
} }
int BluetoothDeviceInfo::signalStrength() const
{
return (m_deviceInfo.rssi() + 100) * 2;
}
QBluetoothDeviceInfo BluetoothDeviceInfo::bluetoothDeviceInfo() const QBluetoothDeviceInfo BluetoothDeviceInfo::bluetoothDeviceInfo() const
{ {
return m_deviceInfo; return m_deviceInfo;

View File

@ -41,6 +41,7 @@ class BluetoothDeviceInfo : public QObject
Q_OBJECT Q_OBJECT
Q_PROPERTY(QString name READ name NOTIFY deviceChanged) Q_PROPERTY(QString name READ name NOTIFY deviceChanged)
Q_PROPERTY(QString address READ address NOTIFY deviceChanged) Q_PROPERTY(QString address READ address NOTIFY deviceChanged)
Q_PROPERTY(int signalStrength READ signalStrength NOTIFY deviceChanged)
public: public:
BluetoothDeviceInfo(); BluetoothDeviceInfo();
@ -50,6 +51,7 @@ public:
QString address() const; QString address() const;
QString name() const; QString name() const;
bool isLowEnergy() const; bool isLowEnergy() const;
int signalStrength() const;
QBluetoothDeviceInfo bluetoothDeviceInfo() const; QBluetoothDeviceInfo bluetoothDeviceInfo() const;
void setBluetoothDeviceInfo(const QBluetoothDeviceInfo &deviceInfo); void setBluetoothDeviceInfo(const QBluetoothDeviceInfo &deviceInfo);

View File

@ -60,6 +60,8 @@ QVariant BluetoothDeviceInfos::data(const QModelIndex &index, int role) const
return deviceInfo->address(); return deviceInfo->address();
} else if (role == BluetoothDeviceInfoRoleLe) { } else if (role == BluetoothDeviceInfoRoleLe) {
return deviceInfo->isLowEnergy(); return deviceInfo->isLowEnergy();
} else if (role == BluetoothDeviceInfoRoleSignalStrength) {
return deviceInfo->signalStrength();
} }
return QVariant(); return QVariant();
@ -103,5 +105,6 @@ QHash<int, QByteArray> BluetoothDeviceInfos::roleNames() const
roles[BluetoothDeviceInfoRoleName] = "name"; roles[BluetoothDeviceInfoRoleName] = "name";
roles[BluetoothDeviceInfoRoleAddress] = "address"; roles[BluetoothDeviceInfoRoleAddress] = "address";
roles[BluetoothDeviceInfoRoleLe] = "lowEnergy"; roles[BluetoothDeviceInfoRoleLe] = "lowEnergy";
roles[BluetoothDeviceInfoRoleSignalStrength] = "signalStrength";
return roles; return roles;
} }

View File

@ -44,8 +44,10 @@ public:
enum BluetoothDeviceInfoRole { enum BluetoothDeviceInfoRole {
BluetoothDeviceInfoRoleName = Qt::DisplayRole, BluetoothDeviceInfoRoleName = Qt::DisplayRole,
BluetoothDeviceInfoRoleAddress, BluetoothDeviceInfoRoleAddress,
BluetoothDeviceInfoRoleLe BluetoothDeviceInfoRoleLe,
BluetoothDeviceInfoRoleSignalStrength
}; };
Q_ENUM(BluetoothDeviceInfoRole)
explicit BluetoothDeviceInfos(QObject *parent = nullptr); explicit BluetoothDeviceInfos(QObject *parent = nullptr);

View File

@ -176,16 +176,19 @@ void BluetoothDiscovery::deviceDiscovered(const QBluetoothDeviceInfo &deviceInfo
{ {
if (!deviceInfo.isValid() if (!deviceInfo.isValid()
|| !deviceInfo.coreConfigurations().testFlag(QBluetoothDeviceInfo::LowEnergyCoreConfiguration) || !deviceInfo.coreConfigurations().testFlag(QBluetoothDeviceInfo::LowEnergyCoreConfiguration)
|| deviceInfo.name().isEmpty()) { || deviceInfo.name().isEmpty()
|| deviceInfo.isCached()) {
return; return;
} }
foreach (BluetoothDeviceInfo *di, m_deviceInfos->deviceInfos()) { foreach (BluetoothDeviceInfo *di, m_deviceInfos->deviceInfos()) {
if (di->address() == deviceInfo.address().toString()) { if (di->address() == deviceInfo.address().toString()) {
di->setBluetoothDeviceInfo(deviceInfo);
return; return;
} }
} }
BluetoothDeviceInfo *deviceInformation = new BluetoothDeviceInfo(deviceInfo); BluetoothDeviceInfo *deviceInformation = new BluetoothDeviceInfo(deviceInfo);
// qDebug() << "BluetoothDiscovery: [+]" << deviceInformation->name() << "(" << deviceInformation->address() << ")" << (isLowEnergy ? "LE" : "") << deviceInfo.majorDeviceClass() << deviceInfo.minorDeviceClass() << deviceInfo.serviceClasses(); // qDebug() << "BluetoothDiscovery: [+]" << deviceInformation->name() << "(" << deviceInformation->address() << ")" << (isLowEnergy ? "LE" : "") << deviceInfo.majorDeviceClass() << deviceInfo.minorDeviceClass() << deviceInfo.serviceClasses();
m_deviceInfos->addBluetoothDeviceInfo(deviceInformation); m_deviceInfos->addBluetoothDeviceInfo(deviceInformation);