From 251ea971c5f3639a84f56bfb22237f93b4f817d2 Mon Sep 17 00:00:00 2001 From: Rob Emery Date: Fri, 4 Oct 2024 04:25:51 +0000 Subject: [PATCH] Make bluetooth work if enabled after kdeconnectd is started Previously, if kdeconnectd was started with bluetooth disabled/unplugged it would continue to not work until kdeconnectd is restarted. --- .../bluetooth/bluetoothlinkprovider.cpp | 27 +++++++++++-------- .../bluetooth/bluetoothlinkprovider.h | 1 + core/daemon.cpp | 1 + 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/core/backends/bluetooth/bluetoothlinkprovider.cpp b/core/backends/bluetooth/bluetoothlinkprovider.cpp index 4c587d505..c021ae2c5 100644 --- a/core/backends/bluetooth/bluetoothlinkprovider.cpp +++ b/core/backends/bluetooth/bluetoothlinkprovider.cpp @@ -30,6 +30,12 @@ BluetoothLinkProvider::BluetoothLinkProvider() } void BluetoothLinkProvider::onStart() +{ + qCDebug(KDECONNECT_CORE) << "BluetoothLinkProvider::onStart executed"; + tryToInitialise(); +} + +void BluetoothLinkProvider::tryToInitialise() { QBluetoothLocalDevice localDevice; if (!localDevice.isValid()) { @@ -37,18 +43,16 @@ void BluetoothLinkProvider::onStart() return; } - qCDebug(KDECONNECT_CORE) << "BluetoothLinkProvider::onStart executed"; + if (!mBluetoothServer) { + qCDebug(KDECONNECT_CORE) << "BluetoothLinkProvider::onNetworkChange re-setting up mBluetoothServer"; - mBluetoothServer = new QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, this); - mBluetoothServer->setSecurityFlags(QBluetooth::Security::Encryption | QBluetooth::Security::Secure); - connect(mBluetoothServer, &QBluetoothServer::newConnection, this, &BluetoothLinkProvider::serverNewConnection); + mBluetoothServer = new QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, this); + mBluetoothServer->setSecurityFlags(QBluetooth::Security::Encryption | QBluetooth::Security::Secure); + connect(mBluetoothServer, &QBluetoothServer::newConnection, this, &BluetoothLinkProvider::serverNewConnection); - qCDebug(KDECONNECT_CORE) << "BluetoothLinkProvider::About to start server listen"; - mKdeconnectService = mBluetoothServer->listen(mServiceUuid, QStringLiteral("KDE Connect")); - - // Disabled for the moment as once the server is listening, the client will not be able to connect anyway - // mServiceDiscoveryAgent->start(); - // connectTimer->start(); + qCDebug(KDECONNECT_CORE) << "BluetoothLinkProvider::onNetworkChange About to start server listen"; + mKdeconnectService = mBluetoothServer->listen(mServiceUuid, QStringLiteral("KDE Connect")); + } } void BluetoothLinkProvider::onStop() @@ -65,9 +69,10 @@ void BluetoothLinkProvider::onStop() mBluetoothServer->deleteLater(); } -// I'm in a new network, let's be polite and introduce myself void BluetoothLinkProvider::onNetworkChange() { + qCDebug(KDECONNECT_CORE) << "BluetoothLinkProvider::onNetworkChange executed"; + tryToInitialise(); } void BluetoothLinkProvider::connectError() diff --git a/core/backends/bluetooth/bluetoothlinkprovider.h b/core/backends/bluetooth/bluetoothlinkprovider.h index b831c926b..cf35d0ae2 100644 --- a/core/backends/bluetooth/bluetoothlinkprovider.h +++ b/core/backends/bluetooth/bluetoothlinkprovider.h @@ -62,6 +62,7 @@ private Q_SLOTS: private: void addLink(BluetoothDeviceLink *deviceLink, const QString &deviceId); QList getPairedDevices(); + void tryToInitialise(); QBluetoothUuid mServiceUuid; QPointer mBluetoothServer; diff --git a/core/daemon.cpp b/core/daemon.cpp index ee76b507a..863216d4b 100644 --- a/core/daemon.cpp +++ b/core/daemon.cpp @@ -127,6 +127,7 @@ void Daemon::forceOnNetworkChange() { qCDebug(KDECONNECT_CORE) << "Sending onNetworkChange to" << d->m_linkProviders.size() << "LinkProviders"; for (LinkProvider *a : std::as_const(d->m_linkProviders)) { + qCDebug(KDECONNECT_CORE) << "Sending onNetworkChange to: " << a->name(); a->onNetworkChange(); } }