4#include <QtCore/QLoggingCategory>
13Q_DECLARE_LOGGING_CATEGORY(QT_BT_BLUEZ)
15using namespace QtBluetoothPrivate;
18
19
20
21
22
23
24
25RemoteDeviceManager::RemoteDeviceManager(
26 const QBluetoothAddress &address, QObject *parent)
27 : QObject(parent), localAddress(address)
32 adapterPath = findAdapterForAddress(address, &ok);
33 if (!ok || adapterPath.isEmpty()) {
34 qCWarning(QT_BT_BLUEZ) <<
"Cannot initialize RemoteDeviceManager";
38bool RemoteDeviceManager::scheduleJob(JobType job,
const QList<QBluetoothAddress> &remoteDevices)
40 if (adapterPath.isEmpty())
43 for (
const auto& remote : remoteDevices)
44 jobQueue.push_back(std::make_pair(job, remote));
46 QTimer::singleShot(0,
this, [
this](){ runQueue(); });
50void RemoteDeviceManager::runQueue()
52 if (jobInProgress || adapterPath.isEmpty())
59 switch (jobQueue.front().first) {
60 case JobType::JobDisconnectDevice:
61 disconnectDevice(jobQueue.front().second);
68void RemoteDeviceManager::prepareNextJob()
70 Q_ASSERT(!jobQueue.empty());
73 jobInProgress =
false;
75 qCDebug(QT_BT_BLUEZ) <<
"RemoteDeviceManager job queue status:" << jobQueue.empty();
82void RemoteDeviceManager::disconnectDevice(
const QBluetoothAddress &remote)
85 OrgFreedesktopDBusObjectManagerInterface managerBluez5(
86 QStringLiteral(
"org.bluez"),
88 QDBusConnection::systemBus(),
this);
89 QDBusPendingReply<ManagedObjectList> reply = managerBluez5.GetManagedObjects();
90 reply.waitForFinished();
91 if (reply.isError()) {
92 QTimer::singleShot(0,
this, [
this](){ prepareNextJob(); });
96 bool jobStarted =
false;
98 for (
auto it = managedObjectList.constBegin(); it != managedObjectList.constEnd(); ++it) {
99 const QDBusObjectPath &path = it.key();
100 const InterfaceList &ifaceList = it.value();
102 for (
auto jt = ifaceList.constBegin(); jt != ifaceList.constEnd(); ++jt) {
103 const QString &iface = jt.key();
105 if (path.path().indexOf(adapterPath) != 0)
108 if (iface != QStringLiteral(
"org.bluez.Device1"))
111 const QBluetoothAddress foundAddress(ifaceList.value(iface).value(QStringLiteral(
"Address")).toString());
112 if (foundAddress != remote)
116 OrgBluezDevice1Interface* device1 =
new OrgBluezDevice1Interface(QStringLiteral(
"org.bluez"),
118 QDBusConnection::systemBus(),
120 QDBusPendingReply<> asyncReply = device1->Disconnect();
121 QDBusPendingCallWatcher* watcher =
new QDBusPendingCallWatcher(asyncReply,
this);
122 const auto watcherFinished = [
this, device1](QDBusPendingCallWatcher* call) {
124 device1->deleteLater();
127 connect(watcher, &QDBusPendingCallWatcher::finished,
this, watcherFinished);
134 qCDebug(QT_BT_BLUEZ) <<
"RemoteDeviceManager JobDisconnectDevice failed";
135 QTimer::singleShot(0,
this, [
this](){ prepareNextJob(); });
141#include "moc_remotedevicemanager_p.cpp"
QMap< QDBusObjectPath, InterfaceList > ManagedObjectList