Add bluetooth available and enabled properties
This commit is contained in:
parent
ab02d54d2f
commit
e652c16436
@ -23,18 +23,58 @@
|
||||
#include "bluetoothdiscovery.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QBluetoothLocalDevice>
|
||||
|
||||
BluetoothDiscovery::BluetoothDiscovery(QObject *parent) :
|
||||
QObject(parent),
|
||||
m_discoveryAgent(new QBluetoothDeviceDiscoveryAgent(this)),
|
||||
m_deviceInfos(new BluetoothDeviceInfos(this)),
|
||||
m_discovering(false)
|
||||
m_deviceInfos(new BluetoothDeviceInfos(this))
|
||||
{
|
||||
|
||||
// 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::finished, this, &BluetoothDiscovery::discoveryFinished);
|
||||
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
|
||||
{
|
||||
return m_discovering;
|
||||
@ -45,27 +85,53 @@ BluetoothDeviceInfos *BluetoothDiscovery::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)
|
||||
{
|
||||
if (m_discovering == discovering)
|
||||
return;
|
||||
|
||||
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)
|
||||
{
|
||||
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));
|
||||
}
|
||||
|
||||
void BluetoothDiscovery::discoveryFinished()
|
||||
{
|
||||
qDebug() << "Discovery finished";
|
||||
qDebug() << "BluetoothDiscovery: Discovery finished";
|
||||
setDiscovering(false);
|
||||
}
|
||||
|
||||
void BluetoothDiscovery::onError(const QBluetoothDeviceDiscoveryAgent::Error &error)
|
||||
{
|
||||
qWarning() << "Discovery error:" << error << m_discoveryAgent->errorString();
|
||||
qWarning() << "BluetoothDiscovery: Discovery error:" << error << m_discoveryAgent->errorString();
|
||||
setDiscovering(false);
|
||||
}
|
||||
|
||||
@ -76,12 +142,14 @@ void BluetoothDiscovery::start()
|
||||
|
||||
m_deviceInfos->clearModel();
|
||||
|
||||
qDebug() << "BluetoothDiscovery: Start discovering.";
|
||||
m_discoveryAgent->start();
|
||||
setDiscovering(true);
|
||||
}
|
||||
|
||||
void BluetoothDiscovery::stop()
|
||||
{
|
||||
qDebug() << "BluetoothDiscovery: Stop discovering.";
|
||||
m_discoveryAgent->stop();
|
||||
setDiscovering(false);
|
||||
}
|
||||
|
||||
@ -24,6 +24,7 @@
|
||||
#define BLUETOOTHDISCOVERY_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QBluetoothLocalDevice>
|
||||
#include <QBluetoothDeviceDiscoveryAgent>
|
||||
|
||||
#include "bluetoothdeviceinfos.h"
|
||||
@ -31,28 +32,43 @@
|
||||
class BluetoothDiscovery : public QObject
|
||||
{
|
||||
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(BluetoothDeviceInfos *deviceInfos READ deviceInfos CONSTANT)
|
||||
|
||||
public:
|
||||
explicit BluetoothDiscovery(QObject *parent = 0);
|
||||
|
||||
bool bluetoothAvailable() const;
|
||||
|
||||
bool bluetoothEnabled() const;
|
||||
void setBluetoothEnabled(bool enabled);
|
||||
|
||||
bool discovering() const;
|
||||
|
||||
BluetoothDeviceInfos *deviceInfos();
|
||||
|
||||
private:
|
||||
QBluetoothDeviceDiscoveryAgent *m_discoveryAgent;
|
||||
QBluetoothLocalDevice *m_localDevice = nullptr;
|
||||
QBluetoothDeviceDiscoveryAgent *m_discoveryAgent = nullptr;
|
||||
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);
|
||||
|
||||
signals:
|
||||
void discoveringChanged();
|
||||
void bluetoothAvailableChanged(bool bluetoothAvailable);
|
||||
void bluetoothEnabledChanged(bool bluetoothEnabled);
|
||||
|
||||
void discoveringChanged(bool discovering);
|
||||
|
||||
private slots:
|
||||
void onBluetoothHostModeChanged(const QBluetoothLocalDevice::HostMode &hostMode);
|
||||
void deviceDiscovered(const QBluetoothDeviceInfo &deviceInfo);
|
||||
void discoveryFinished();
|
||||
void onError(const QBluetoothDeviceDiscoveryAgent::Error &error);
|
||||
|
||||
Reference in New Issue
Block a user