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)
50 enum class BluetoothMode {
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);
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;
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;
82 LinkType type()
const override {
return LinkConfiguration::TypeBluetooth; }
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;
89 BluetoothMode mode()
const {
return _mode; }
90 void setMode(BluetoothMode mode);
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;
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);
109 const QBluetoothUuid& readCharacteristicUuid()
const {
return _readCharacteristicUuid; }
110 const QBluetoothUuid& writeCharacteristicUuid()
const {
return _writeCharacteristicUuid; }
112 [[nodiscard]]
static bool isBluetoothAvailable();
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")};
136 void _deviceDiscovered(
const QBluetoothDeviceInfo &info);
137 void _onDiscoveryErrorOccurred(QBluetoothDeviceDiscoveryAgent::Error
error);
138 void _onDiscoveryFinished();
139 void _deviceUpdated(
const QBluetoothDeviceInfo &info, QBluetoothDeviceInfo::Fields updatedFields);
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);
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;
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;
177 QBluetoothUuid _serviceUuid{NORDIC_UART_SERVICE};
178 QBluetoothUuid _readCharacteristicUuid{NORDIC_UART_RX_CHAR};
179 QBluetoothUuid _writeCharacteristicUuid{NORDIC_UART_TX_CHAR};