QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
BluetoothConfiguration.h
Go to the documentation of this file.
1#pragma once
2
3#include "LinkConfiguration.h"
4
5#include <QtBluetooth/QBluetoothAddress>
6#include <QtBluetooth/QBluetoothDeviceDiscoveryAgent>
7#include <QtBluetooth/QBluetoothDeviceInfo>
8#include <QtBluetooth/QBluetoothHostInfo>
9#include <QtBluetooth/QBluetoothLocalDevice>
10#include <QtCore/QFutureWatcher>
11#include <QtCore/QList>
12#include <QtCore/QPointer>
13#include <QtCore/QString>
14#include <QtCore/QVariantList>
15#include <QtQmlIntegration/QtQmlIntegration>
16
17/*===========================================================================*/
18
20{
21 Q_OBJECT
22 QML_ELEMENT
23 QML_UNCREATABLE("")
24 Q_PROPERTY(BluetoothMode mode READ mode WRITE setMode NOTIFY modeChanged)
25 Q_PROPERTY(QString deviceName READ deviceName NOTIFY deviceChanged)
26 Q_PROPERTY(QString address READ address NOTIFY deviceChanged)
27 Q_PROPERTY(QVariantList devicesModel READ devicesModel NOTIFY devicesModelChanged)
28 Q_PROPERTY(bool scanning READ scanning NOTIFY scanningChanged)
29 Q_PROPERTY(QString serviceUuid READ serviceUuid WRITE setServiceUuid NOTIFY serviceUuidChanged)
30 Q_PROPERTY(QString readUuid READ readUuid WRITE setReadUuid NOTIFY readUuidChanged)
31 Q_PROPERTY(QString writeUuid READ writeUuid WRITE setWriteUuid NOTIFY writeUuidChanged)
32 Q_PROPERTY(qint16 connectedRssi READ connectedRssi NOTIFY connectedRssiChanged)
33 Q_PROPERTY(qint16 selectedRssi READ selectedRssi NOTIFY selectedRssiChanged)
34 Q_PROPERTY(bool adapterAvailable READ isAdapterAvailable NOTIFY adapterStateChanged)
35 Q_PROPERTY(bool adapterPoweredOn READ isAdapterPoweredOn NOTIFY adapterStateChanged)
36 Q_PROPERTY(bool adapterDiscoverable READ isDiscoverable NOTIFY adapterStateChanged)
37 Q_PROPERTY(QString adapterName READ getAdapterName NOTIFY adapterStateChanged)
38 Q_PROPERTY(QString adapterAddress READ getAdapterAddress NOTIFY adapterStateChanged)
39 Q_PROPERTY(QString hostMode READ getHostMode NOTIFY adapterStateChanged)
40
41public:
42 explicit BluetoothConfiguration(const QString &name, QObject *parent = nullptr);
43 explicit BluetoothConfiguration(const BluetoothConfiguration *copy, QObject *parent = nullptr);
44 ~BluetoothConfiguration() override;
45
46 enum class BluetoothMode {
49 };
50 Q_ENUM(BluetoothMode)
51
52 Q_INVOKABLE void startScan();
53 Q_INVOKABLE void stopScan();
54 Q_INVOKABLE void setDevice(const QString &name);
55 Q_INVOKABLE void setDeviceByAddress(const QString &address);
56
57 // Pairing support (Classic Bluetooth only)
58 Q_INVOKABLE void requestPairing(const QString &address);
59 Q_INVOKABLE void removePairing(const QString &address);
60 Q_INVOKABLE QString getPairingStatus(const QString &address) const;
61 [[nodiscard]] Q_INVOKABLE bool isPaired(const QString &address) const;
62 [[nodiscard]] Q_INVOKABLE bool isDiscoverable() const;
63
64 // Adapter information
65 [[nodiscard]] Q_INVOKABLE bool isAdapterAvailable() const;
66 Q_INVOKABLE QString getAdapterAddress() const;
67 Q_INVOKABLE QString getAdapterName() const;
68 [[nodiscard]] Q_INVOKABLE bool isAdapterPoweredOn() const;
69 Q_INVOKABLE QVariantList getAllPairedDevices() const;
70 Q_INVOKABLE QVariantList getConnectedDevices() const;
71 Q_INVOKABLE void powerOnAdapter();
72 Q_INVOKABLE void powerOffAdapter();
73 Q_INVOKABLE void setAdapterDiscoverable(bool discoverable);
74 Q_INVOKABLE QVariantList getAllAvailableAdapters();
75 Q_INVOKABLE void selectAdapter(const QString &address);
76 Q_INVOKABLE QString getHostMode() const;
77
78 LinkType type() const override { return LinkConfiguration::TypeBluetooth; }
79 void copyFrom(const LinkConfiguration *source) override;
80 void loadSettings(QSettings &settings, const QString &root) override;
81 void saveSettings(QSettings &settings, const QString &root) const override;
82 QString settingsURL() const override { return QStringLiteral("BluetoothSettings.qml"); }
83 QString settingsTitle() const override;
84
85 BluetoothMode mode() const { return _mode; }
87
88 const QBluetoothDeviceInfo& device() const { return _device; }
89 QString deviceName() const { return _device.name(); }
90 QString address() const { return _device.address().toString(); }
91 QVariantList devicesModel() const;
92 bool scanning() const;
93 qint16 connectedRssi() const { return _connectedRssi; }
94 void setConnectedRssi(qint16 rssi);
95 qint16 selectedRssi() const;
96
97 // BLE specific settings
98 QString serviceUuid() const { return _serviceUuid.toString(); }
99 void setServiceUuid(const QString &uuid);
100 QString readUuid() const { return _readCharacteristicUuid.toString(); }
101 void setReadUuid(const QString &uuid);
102 QString writeUuid() const { return _writeCharacteristicUuid.toString(); }
103 void setWriteUuid(const QString &uuid);
104
105 const QBluetoothUuid& readCharacteristicUuid() const { return _readCharacteristicUuid; }
106 const QBluetoothUuid& writeCharacteristicUuid() const { return _writeCharacteristicUuid; }
107
108 [[nodiscard]] static bool isBluetoothAvailable();
109
110 // Known BLE UART-like service UUIDs
111 static inline const QBluetoothUuid NORDIC_UART_SERVICE{QStringLiteral("6e400001-b5a3-f393-e0a9-e50e24dcca9e")};
112 static inline const QBluetoothUuid NORDIC_UART_RX_CHAR{QStringLiteral("6e400003-b5a3-f393-e0a9-e50e24dcca9e")};
113 static inline const QBluetoothUuid NORDIC_UART_TX_CHAR{QStringLiteral("6e400002-b5a3-f393-e0a9-e50e24dcca9e")};
114 static inline const QBluetoothUuid TI_SENSORTAG_SERVICE{QStringLiteral("0000ffe0-0000-1000-8000-00805f9b34fb")};
115 static inline const QBluetoothUuid TI_SENSORTAG_CHAR{QStringLiteral("0000ffe1-0000-1000-8000-00805f9b34fb")};
116
117signals:
127 void errorOccurred(const QString &errorString);
130
131private slots:
132 void _deviceDiscovered(const QBluetoothDeviceInfo &info);
133 void _onDiscoveryErrorOccurred(QBluetoothDeviceDiscoveryAgent::Error error);
134 void _onDiscoveryFinished();
135 void _deviceUpdated(const QBluetoothDeviceInfo &info, QBluetoothDeviceInfo::Fields updatedFields);
136
137 // QBluetoothLocalDevice slots
138 void _onHostModeStateChanged(QBluetoothLocalDevice::HostMode mode);
139 void _onDeviceConnected(const QBluetoothAddress &address);
140 void _onDeviceDisconnected(const QBluetoothAddress &address);
141 void _onPairingFinished(const QBluetoothAddress &address, QBluetoothLocalDevice::Pairing pairing);
142 void _onLocalDeviceErrorOccurred(QBluetoothLocalDevice::Error error);
143
144private:
145 void _initDeviceDiscoveryAgent();
146 void _refreshAvailableAdaptersAsync();
147 void _updateAvailableAdapters(const QList<QBluetoothHostInfo> &hosts);
148 bool _ensureLocalDevice();
149 void _requestHostMode(QBluetoothLocalDevice::HostMode mode);
150 void _updateDeviceList();
151 void _applySelectedDevice(const QBluetoothDeviceInfo &info);
152 bool _createLocalDevice(const QBluetoothAddress &address);
153 void _connectLocalDeviceSignals();
154 bool _isDeviceCompatibleWithMode(const QBluetoothDeviceInfo &info) const;
155
157 QBluetoothDeviceInfo _device;
158 QList<QBluetoothDeviceInfo> _deviceList;
159 QVariantList _devicesModelCache;
160 QPointer<QBluetoothDeviceDiscoveryAgent> _deviceDiscoveryAgent;
161 QPointer<QBluetoothLocalDevice> _localDevice;
162 QVariantList _availableAdapters;
163 QPointer<QFutureWatcher<QList<QBluetoothHostInfo>>> _adapterWatcher;
164 bool _adapterEnumerationInProgress = false;
165 bool _hostModeRequestPending = false;
166 bool _deferredPowerOnFixPending = false;
167 QBluetoothLocalDevice::HostMode _requestedHostMode = QBluetoothLocalDevice::HostPoweredOff;
168 bool _hasQueuedHostModeRequest = false;
169 QBluetoothLocalDevice::HostMode _queuedHostMode = QBluetoothLocalDevice::HostPoweredOff;
170 qint16 _connectedRssi = 0;
171
172 // BLE specific - Default to Nordic UART Service
173 QBluetoothUuid _serviceUuid{NORDIC_UART_SERVICE};
174 QBluetoothUuid _readCharacteristicUuid{NORDIC_UART_RX_CHAR};
175 QBluetoothUuid _writeCharacteristicUuid{NORDIC_UART_TX_CHAR};
176};
QString errorString
Error error
static const QBluetoothUuid NORDIC_UART_SERVICE
static const QBluetoothUuid NORDIC_UART_RX_CHAR
void errorOccurred(const QString &errorString)
Q_INVOKABLE bool isDiscoverable() const
Q_INVOKABLE QString getAdapterAddress() const
Q_INVOKABLE void setDevice(const QString &name)
LinkType type() const override
Q_INVOKABLE QVariantList getAllPairedDevices() const
Q_INVOKABLE void requestPairing(const QString &address)
Q_INVOKABLE void setAdapterDiscoverable(bool discoverable)
Q_INVOKABLE bool isAdapterAvailable() const
Q_INVOKABLE void selectAdapter(const QString &address)
Q_INVOKABLE QVariantList getConnectedDevices() const
void loadSettings(QSettings &settings, const QString &root) override
Q_INVOKABLE QString getPairingStatus(const QString &address) const
const QBluetoothDeviceInfo & device() const
Q_INVOKABLE bool isPaired(const QString &address) const
void setMode(BluetoothMode mode)
QVariantList devicesModel() const
const QBluetoothUuid & writeCharacteristicUuid() const
const QBluetoothUuid & readCharacteristicUuid() const
static const QBluetoothUuid TI_SENSORTAG_SERVICE
BluetoothMode mode() const
static const QBluetoothUuid NORDIC_UART_TX_CHAR
QString settingsTitle() const override
Settings Title, Pure virtual method providing the Title for the (QML) settings dialog.
void saveSettings(QSettings &settings, const QString &root) const override
static const QBluetoothUuid TI_SENSORTAG_CHAR
void setWriteUuid(const QString &uuid)
Q_INVOKABLE bool isAdapterPoweredOn() const
QString settingsURL() const override
Settings URL, Pure virtual method providing the URL for the (QML) settings dialog.
Q_INVOKABLE QVariantList getAllAvailableAdapters()
Q_INVOKABLE void removePairing(const QString &address)
Q_INVOKABLE QString getHostMode() const
Q_INVOKABLE void setDeviceByAddress(const QString &address)
void setServiceUuid(const QString &uuid)
Q_INVOKABLE QString getAdapterName() const
void setReadUuid(const QString &uuid)
void copyFrom(const LinkConfiguration *source) override
Interface holding link specific settings.
@ TypeBluetooth
Bluetooth Link.
QString name() const