5#include <QtCore/QCoreApplication>
6#include <QtCore/QPermissions>
7#include <QtCore/QThread>
16 , _workerThread(new QThread(this))
18 if (!_bluetoothConfig) {
19 qCCritical(BluetoothLinkLog) <<
"Invalid BluetoothConfiguration";
25 qCDebug(BluetoothLinkLog) <<
this;
28 ? QStringLiteral(
"BLE_%1") : QStringLiteral(
"Bluetooth_%1");
29 _workerThread->setObjectName(threadName.arg(_bluetoothConfig->name()));
31 _worker->moveToThread(_workerThread.data());
34 (void) connect(_workerThread.data(), &QThread::finished, _worker.data(), &QObject::deleteLater);
51 if (_workerThread->isRunning() && _connectedCache && _worker) {
52 (void) QMetaObject::invokeMethod(_worker,
"disconnectLink", Qt::QueuedConnection);
58 qCDebug(BluetoothLinkLog) <<
this;
63 return _connectedCache;
66bool BluetoothLink::_connect()
71 if (_bluetoothConfig) {
72 _bluetoothConfig->stopScan();
74 return QMetaObject::invokeMethod(_worker.data(),
"connectLink", Qt::QueuedConnection);
80 (void) QMetaObject::invokeMethod(_worker.data(),
"disconnectLink", Qt::QueuedConnection);
84void BluetoothLink::_onConnected()
86 _connectedCache =
true;
87 _disconnectedEmitted =
false;
91void BluetoothLink::_onDisconnected()
93 _connectedCache =
false;
94 if (!_disconnectedEmitted.exchange(
true)) {
99void BluetoothLink::_onErrorOccurred(
const QString &errorString)
101 qCWarning(BluetoothLinkLog) <<
"Communication error:" << errorString;
103 if (!_bluetoothConfig) {
109 ? tr(
"Bluetooth Low Energy") : tr(
"Bluetooth");
112 tr(
"Link %1: (Device: %2) %3").arg(_bluetoothConfig->name(),
113 _bluetoothConfig->device().name(),
117void BluetoothLink::_onDataReceived(
const QByteArray &data)
122void BluetoothLink::_onDataSent(
const QByteArray &data)
127void BluetoothLink::_onRssiUpdated(qint16 rssi)
129 if (_bluetoothConfig) {
130 _bluetoothConfig->setConnectedRssi(rssi);
134void BluetoothLink::_writeBytes(
const QByteArray &bytes)
137 (void) QMetaObject::invokeMethod(_worker.data(),
"writeData", Qt::QueuedConnection, Q_ARG(QByteArray, bytes));
141void BluetoothLink::_checkPermission()
143 QBluetoothPermission permission;
144 permission.setCommunicationModes(QBluetoothPermission::Access);
146 const Qt::PermissionStatus permissionStatus = QCoreApplication::instance()->checkPermission(permission);
147 if (permissionStatus == Qt::PermissionStatus::Undetermined) {
148 QCoreApplication::instance()->requestPermission(permission,
this, [
this](
const QPermission &perm) {
149 _handlePermissionStatus(perm.status());
152 _handlePermissionStatus(permissionStatus);
156void BluetoothLink::_handlePermissionStatus(Qt::PermissionStatus permissionStatus)
158 if (permissionStatus != Qt::PermissionStatus::Granted) {
159 qCWarning(BluetoothLinkLog) <<
"Bluetooth Permission Denied";
160 _onErrorOccurred(tr(
"Bluetooth Permission Denied"));
165 qCDebug(BluetoothLinkLog) <<
"Bluetooth Permission Granted";
167 if (_workerThread && !_workerThread->isRunning()) {
168 _workerThread->start();
169 if (!_workerThread->isRunning()) {
170 qCCritical(BluetoothLinkLog) <<
"Failed to start worker thread";
171 _onErrorOccurred(tr(
"Failed to start Bluetooth worker thread"));
172 _worker->deleteLater();
std::shared_ptr< LinkConfiguration > SharedLinkConfigurationPtr
#define QGC_LOGGING_CATEGORY(name, categoryStr)
void errorOccurred(const QString &errorString)
BluetoothLink(SharedLinkConfigurationPtr &config, QObject *parent=nullptr)
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)
void bytesReceived(LinkInterface *link, const QByteArray &data)
void _shutdownWorkerThread(QThread *thread, const QLoggingCategory &category, bool allowTerminate=true)
LinkInterface(SharedLinkConfigurationPtr &config, QObject *parent=nullptr)
Links are only created by LinkManager so constructor is not public.
void communicationError(const QString &title, const QString &error)
void bytesSent(LinkInterface *link, const QByteArray &data)