Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
bluezperipheralconnectionmanager.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include "device1_bluez5_p.h"
6
7#include <QtBluetooth/QBluetoothLocalDevice>
8#include <QtDBus/QDBusConnection>
9#include <QtCore/QLoggingCategory>
10
12
14
15using namespace Qt::StringLiterals;
16
18 const QBluetoothAddress& localAddress, QObject* parent)
19 : QObject(parent),
20 m_localDevice(new QBluetoothLocalDevice(localAddress, this))
21{
23 this, &QtBluezPeripheralConnectionManager::remoteDeviceDisconnected);
24}
25
27 const QString& remoteDeviceObjectPath, quint16 mtu)
28{
29 if (m_clients.contains(remoteDeviceObjectPath))
30 return; // Already aware of the client
31
32 std::unique_ptr<OrgBluezDevice1Interface> device{new OrgBluezDevice1Interface(
33 "org.bluez"_L1, remoteDeviceObjectPath,
35
36 qCDebug(QT_BT_BLUEZ) << "New LE Gatt client connected: " << remoteDeviceObjectPath
37 << device->address() << device->name() << "mtu:" << mtu;
38
39 RemoteDeviceDetails details{QBluetoothAddress{device->address()}, device->name(), mtu};
40
41 m_clients.insert(remoteDeviceObjectPath, details);
42 if (!m_connected) {
43 m_connected = true;
45 }
46 emit remoteDeviceChanged(details.address, details.name, details.mtu);
47}
48
49void QtBluezPeripheralConnectionManager::remoteDeviceDisconnected(const QBluetoothAddress& address)
50{
51 // Find if the disconnected device was gatt client
52 bool remoteDetailsChanged{false};
53 for (auto it = m_clients.begin(); it != m_clients.end(); it++) {
54 if (it.value().address == address) {
55 qCDebug(QT_BT_BLUEZ) << "LE Gatt client disconnected:" << address;
56 remoteDetailsChanged = true;
57 m_clients.remove(it.key());
58 break;
59 }
60 }
61
62 if (!remoteDetailsChanged)
63 return;
64
65 if (m_clients.isEmpty() && m_connected) {
66 m_connected = false;
68 }
69
70 // If a client disconnected but there are others, pick any other.
71 // Qt API doesn't distinguish between clients
72 if (!m_clients.isEmpty()) {
73 emit remoteDeviceChanged(m_clients.last().address,
74 m_clients.last().name, m_clients.last().mtu);
75 }
76}
77
79{
80 for (auto it = m_clients.begin(); it != m_clients.end(); it++) {
81 std::unique_ptr<OrgBluezDevice1Interface> device{new OrgBluezDevice1Interface(
82 "org.bluez"_L1, it.key(), QDBusConnection::systemBus())};
83 device->Disconnect();
84 }
85 reset();
86}
87
89{
90 m_connected = false;
91 m_clients.clear();
92}
93
95
96#include "moc_bluezperipheralconnectionmanager_p.cpp"
IOBluetoothDevice * device
\inmodule QtBluetooth
\inmodule QtBluetooth
void deviceDisconnected(const QBluetoothAddress &address)
static QDBusConnection systemBus()
Returns a QDBusConnection object opened with the system bus.
iterator insert(const Key &key, const T &value)
Definition qmap.h:688
T & last()
Definition qmap.h:421
bool contains(const Key &key) const
Definition qmap.h:341
size_type remove(const Key &key)
Definition qmap.h:300
void clear()
Definition qmap.h:289
bool isEmpty() const
Definition qmap.h:269
iterator begin()
Definition qmap.h:598
iterator end()
Definition qmap.h:602
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
void connectivityStateChanged(bool connected)
void remoteDeviceAccessEvent(const QString &remoteDeviceObjectPath, quint16 mtu)
QtBluezPeripheralConnectionManager(const QBluetoothAddress &localAddress, QObject *parent=nullptr)
void remoteDeviceChanged(const QBluetoothAddress &address, const QString &name, quint16 mtu)
#define this
Definition dialogs.cpp:9
QSet< QString >::iterator it
Combined button and popup list for selecting options.
#define qCDebug(category,...)
#define Q_DECLARE_LOGGING_CATEGORY(name)
GLuint GLuint64EXT address
#define emit
unsigned short quint16
Definition qtypes.h:48