Add bluetooth available and enabled properties

This commit is contained in:
Simon Stürz 2018-06-26 11:38:04 +02:00
parent ab02d54d2f
commit e652c16436
2 changed files with 94 additions and 10 deletions

View File

@ -23,18 +23,58 @@
#include "bluetoothdiscovery.h" #include "bluetoothdiscovery.h"
#include <QDebug> #include <QDebug>
#include <QBluetoothLocalDevice>
BluetoothDiscovery::BluetoothDiscovery(QObject *parent) : BluetoothDiscovery::BluetoothDiscovery(QObject *parent) :
QObject(parent), QObject(parent),
m_discoveryAgent(new QBluetoothDeviceDiscoveryAgent(this)), m_deviceInfos(new BluetoothDeviceInfos(this))
m_deviceInfos(new BluetoothDeviceInfos(this)),
m_discovering(false)
{ {
// Check if bluetooth is available
QBluetoothLocalDevice localDevice;
if (!localDevice.isValid()) {
qWarning() << "BluetoothDiscovery: there is no bluetooth device available.";
setBluetoothAvailable(false);
return;
}
setBluetoothAvailable(true);
if (localDevice.allDevices().count() > 1) {
// FIXME: check the device with the most capabilities and check if low energy is available
} else {
QBluetoothHostInfo adapterHostInfo = localDevice.allDevices().first();
qDebug() << "BluetoothDiscovery: using bluetooth adapter" << adapterHostInfo.name() << adapterHostInfo.address().toString();
m_localDevice = new QBluetoothLocalDevice(adapterHostInfo.address(), this);
connect(m_localDevice, &QBluetoothLocalDevice::hostModeStateChanged, this, &BluetoothDiscovery::onBluetoothHostModeChanged);
onBluetoothHostModeChanged(m_localDevice->hostMode());
}
m_discoveryAgent = new QBluetoothDeviceDiscoveryAgent(m_localDevice->address(), this);
connect(m_discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &BluetoothDiscovery::deviceDiscovered); connect(m_discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &BluetoothDiscovery::deviceDiscovered);
connect(m_discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &BluetoothDiscovery::discoveryFinished); connect(m_discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &BluetoothDiscovery::discoveryFinished);
connect(m_discoveryAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)), this, SLOT(onError(QBluetoothDeviceDiscoveryAgent::Error))); connect(m_discoveryAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)), this, SLOT(onError(QBluetoothDeviceDiscoveryAgent::Error)));
} }
bool BluetoothDiscovery::bluetoothAvailable() const
{
return m_bluetoothAvailable;
}
bool BluetoothDiscovery::bluetoothEnabled() const
{
return m_bluetoothEnabled;
}
void BluetoothDiscovery::setBluetoothEnabled(bool enabled)
{
if (enabled) {
m_localDevice->powerOn();
} else {
m_localDevice->setHostMode(QBluetoothLocalDevice::HostPoweredOff);
}
}
bool BluetoothDiscovery::discovering() const bool BluetoothDiscovery::discovering() const
{ {
return m_discovering; return m_discovering;
@ -45,27 +85,53 @@ BluetoothDeviceInfos *BluetoothDiscovery::deviceInfos()
return m_deviceInfos; return m_deviceInfos;
} }
void BluetoothDiscovery::setBluetoothAvailable(bool available)
{
if (m_bluetoothAvailable == available)
return;
m_bluetoothAvailable = available;
emit bluetoothAvailableChanged(m_bluetoothAvailable);
}
void BluetoothDiscovery::setDiscovering(bool discovering) void BluetoothDiscovery::setDiscovering(bool discovering)
{ {
if (m_discovering == discovering)
return;
m_discovering = discovering; m_discovering = discovering;
emit discoveringChanged(); emit discoveringChanged(m_discovering);
}
void BluetoothDiscovery::onBluetoothHostModeChanged(const QBluetoothLocalDevice::HostMode &hostMode)
{
qDebug() << "BluetoothDiscovery: host mode changed" << hostMode;
switch (hostMode) {
case QBluetoothLocalDevice::HostPoweredOff:
setBluetoothEnabled(false);
break;
default:
// Note: discovery works in all other modes
setBluetoothEnabled(true);
break;
}
} }
void BluetoothDiscovery::deviceDiscovered(const QBluetoothDeviceInfo &deviceInfo) void BluetoothDiscovery::deviceDiscovered(const QBluetoothDeviceInfo &deviceInfo)
{ {
qDebug() << "Discovery: [+]" << deviceInfo.name() << "(" << deviceInfo.address().toString() << ")" << (deviceInfo.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration ? "LE" : ""); qDebug() << "BluetoothDiscovery: [+]" << deviceInfo.name() << "(" << deviceInfo.address().toString() << ")" << (deviceInfo.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration ? "LE" : "");
m_deviceInfos->addBluetoothDeviceInfo(new BluetoothDeviceInfo(deviceInfo)); m_deviceInfos->addBluetoothDeviceInfo(new BluetoothDeviceInfo(deviceInfo));
} }
void BluetoothDiscovery::discoveryFinished() void BluetoothDiscovery::discoveryFinished()
{ {
qDebug() << "Discovery finished"; qDebug() << "BluetoothDiscovery: Discovery finished";
setDiscovering(false); setDiscovering(false);
} }
void BluetoothDiscovery::onError(const QBluetoothDeviceDiscoveryAgent::Error &error) void BluetoothDiscovery::onError(const QBluetoothDeviceDiscoveryAgent::Error &error)
{ {
qWarning() << "Discovery error:" << error << m_discoveryAgent->errorString(); qWarning() << "BluetoothDiscovery: Discovery error:" << error << m_discoveryAgent->errorString();
setDiscovering(false); setDiscovering(false);
} }
@ -76,12 +142,14 @@ void BluetoothDiscovery::start()
m_deviceInfos->clearModel(); m_deviceInfos->clearModel();
qDebug() << "BluetoothDiscovery: Start discovering.";
m_discoveryAgent->start(); m_discoveryAgent->start();
setDiscovering(true); setDiscovering(true);
} }
void BluetoothDiscovery::stop() void BluetoothDiscovery::stop()
{ {
qDebug() << "BluetoothDiscovery: Stop discovering.";
m_discoveryAgent->stop(); m_discoveryAgent->stop();
setDiscovering(false); setDiscovering(false);
} }

View File

@ -24,6 +24,7 @@
#define BLUETOOTHDISCOVERY_H #define BLUETOOTHDISCOVERY_H
#include <QObject> #include <QObject>
#include <QBluetoothLocalDevice>
#include <QBluetoothDeviceDiscoveryAgent> #include <QBluetoothDeviceDiscoveryAgent>
#include "bluetoothdeviceinfos.h" #include "bluetoothdeviceinfos.h"
@ -31,28 +32,43 @@
class BluetoothDiscovery : public QObject class BluetoothDiscovery : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(bool bluetoothAvailable READ bluetoothAvailable NOTIFY bluetoothAvailableChanged)
Q_PROPERTY(bool bluetoothEnabled READ bluetoothEnabled NOTIFY bluetoothEnabledChanged)
Q_PROPERTY(bool discovering READ discovering NOTIFY discoveringChanged) Q_PROPERTY(bool discovering READ discovering NOTIFY discoveringChanged)
Q_PROPERTY(BluetoothDeviceInfos *deviceInfos READ deviceInfos CONSTANT) Q_PROPERTY(BluetoothDeviceInfos *deviceInfos READ deviceInfos CONSTANT)
public: public:
explicit BluetoothDiscovery(QObject *parent = 0); explicit BluetoothDiscovery(QObject *parent = 0);
bool bluetoothAvailable() const;
bool bluetoothEnabled() const;
void setBluetoothEnabled(bool enabled);
bool discovering() const; bool discovering() const;
BluetoothDeviceInfos *deviceInfos(); BluetoothDeviceInfos *deviceInfos();
private: private:
QBluetoothDeviceDiscoveryAgent *m_discoveryAgent; QBluetoothLocalDevice *m_localDevice = nullptr;
QBluetoothDeviceDiscoveryAgent *m_discoveryAgent = nullptr;
BluetoothDeviceInfos *m_deviceInfos; BluetoothDeviceInfos *m_deviceInfos;
bool m_discovering; bool m_discovering = false;
bool m_bluetoothAvailable = false;
bool m_bluetoothEnabled = false;
void setBluetoothAvailable(bool available);
void setDiscovering(bool discovering); void setDiscovering(bool discovering);
signals: signals:
void discoveringChanged(); void bluetoothAvailableChanged(bool bluetoothAvailable);
void bluetoothEnabledChanged(bool bluetoothEnabled);
void discoveringChanged(bool discovering);
private slots: private slots:
void onBluetoothHostModeChanged(const QBluetoothLocalDevice::HostMode &hostMode);
void deviceDiscovered(const QBluetoothDeviceInfo &deviceInfo); void deviceDiscovered(const QBluetoothDeviceInfo &deviceInfo);
void discoveryFinished(); void discoveryFinished();
void onError(const QBluetoothDeviceDiscoveryAgent::Error &error); void onError(const QBluetoothDeviceDiscoveryAgent::Error &error);