5#include <QtCore/QCoreApplication>
6#include <QtCore/QPermissions>
7#include <QtCore/QThread>
16 , _workerThread(new QThread(this))
18 Q_ASSERT(_bluetoothConfig);
19 if (!_bluetoothConfig) {
20 qCCritical(BluetoothLinkLog) <<
"Invalid BluetoothConfiguration";
26 qCDebug(BluetoothLinkLog) <<
this;
29 ? QStringLiteral(
"BLE_%1") : QStringLiteral(
"Bluetooth_%1");
30 _workerThread->setObjectName(threadName.arg(_bluetoothConfig->name()));
32 _worker->moveToThread(_workerThread.data());
35 (void) connect(_workerThread.data(), &QThread::finished, _worker.data(), &QObject::deleteLater);
52 if (_workerThread->isRunning() && _connectedCache && _worker) {
53 (void) QMetaObject::invokeMethod(_worker,
"disconnectLink", Qt::QueuedConnection);
59 qCDebug(BluetoothLinkLog) <<
this;
64 return _connectedCache;
67bool BluetoothLink::_connect()
72 if (_bluetoothConfig) {
75 return QMetaObject::invokeMethod(_worker.data(),
"connectLink", Qt::QueuedConnection);
81 (void) QMetaObject::invokeMethod(_worker.data(),
"disconnectLink", Qt::QueuedConnection);
85void BluetoothLink::_onConnected()
87 _connectedCache =
true;
88 _disconnectedEmitted =
false;
92void BluetoothLink::_onDisconnected()
94 _connectedCache =
false;
95 if (!_disconnectedEmitted.exchange(
true)) {
100void BluetoothLink::_onErrorOccurred(
const QString &
errorString)
102 qCWarning(BluetoothLinkLog) <<
"Communication error:" <<
errorString;
104 if (!_bluetoothConfig) {
110 ? tr(
"Bluetooth Low Energy") : tr(
"Bluetooth");
113 tr(
"Link %1: (Device: %2) %3").arg(_bluetoothConfig->
name(),
114 _bluetoothConfig->
device().name(),
118void BluetoothLink::_onDataReceived(
const QByteArray &data)
123void BluetoothLink::_onDataSent(
const QByteArray &data)
128void BluetoothLink::_onRssiUpdated(qint16 rssi)
130 if (_bluetoothConfig) {
135void BluetoothLink::_writeBytes(
const QByteArray &bytes)
138 (void) QMetaObject::invokeMethod(_worker.data(),
"writeData", Qt::QueuedConnection, Q_ARG(QByteArray, bytes));
142void BluetoothLink::_checkPermission()
144 QBluetoothPermission permission;
145 permission.setCommunicationModes(QBluetoothPermission::Access);
147 const Qt::PermissionStatus permissionStatus = QCoreApplication::instance()->checkPermission(permission);
148 if (permissionStatus == Qt::PermissionStatus::Undetermined) {
149 QCoreApplication::instance()->requestPermission(permission,
this, [
this](
const QPermission &perm) {
150 _handlePermissionStatus(perm.status());
153 _handlePermissionStatus(permissionStatus);
157void BluetoothLink::_handlePermissionStatus(Qt::PermissionStatus permissionStatus)
159 if (permissionStatus != Qt::PermissionStatus::Granted) {
160 qCWarning(BluetoothLinkLog) <<
"Bluetooth Permission Denied";
161 _onErrorOccurred(tr(
"Bluetooth Permission Denied"));
166 qCDebug(BluetoothLinkLog) <<
"Bluetooth Permission Granted";
168 if (_workerThread && !_workerThread->isRunning()) {
169 _workerThread->start();
170 if (!_workerThread->isRunning()) {
171 qCCritical(BluetoothLinkLog) <<
"Failed to start worker thread";
172 _onErrorOccurred(tr(
"Failed to start Bluetooth worker thread"));
173 _worker->deleteLater();
std::shared_ptr< LinkConfiguration > SharedLinkConfigurationPtr
#define QGC_LOGGING_CATEGORY(name, categoryStr)
void errorOccurred(const QString &errorString)
const QBluetoothDeviceInfo & device() const
void setConnectedRssi(qint16 rssi)
BluetoothMode mode() const
Q_INVOKABLE void stopScan()
bool isConnected() const override
void disconnect() override
~BluetoothLink() override
void dataReceived(const QByteArray &data)
void dataSent(const QByteArray &data)
void errorOccurred(const QString &errorString)
void rssiUpdated(qint16 rssi)
static BluetoothWorker * create(const BluetoothConfiguration *config, QObject *parent=nullptr)
The link interface defines the interface for all links used to communicate with the ground station ap...
void bytesReceived(LinkInterface *link, const QByteArray &data)
void _shutdownWorkerThread(QThread *thread, const QLoggingCategory &category, bool allowTerminate=true)
void communicationError(const QString &title, const QString &error)
void bytesSent(LinkInterface *link, const QByteArray &data)