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