1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2014 Denis Shienkov <denis.shienkov@gmail.com>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#ifndef QBLUETOOTHLOCALDEVICE_P_H
6#define QBLUETOOTHLOCALDEVICE_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtBluetooth/qtbluetoothglobal.h>
20
21#include "qbluetoothlocaldevice.h"
22
23#if QT_CONFIG(bluez)
24#include <QObject>
25#include <QDBusObjectPath>
26#include <QDBusMessage>
27#include <QSet>
28#include "bluez/bluez5_helper_p.h"
29
30class OrgBluezAdapter1Interface;
31class OrgFreedesktopDBusPropertiesInterface;
32class OrgFreedesktopDBusObjectManagerInterface;
33class OrgBluezDevice1Interface;
34
35QT_BEGIN_NAMESPACE
36class QDBusPendingCallWatcher;
37QT_END_NAMESPACE
38#endif
39
40#ifdef QT_WINRT_BLUETOOTH
41#include "qbluetoothutils_winrt_p.h"
42#include <winrt/Windows.Devices.Bluetooth.h>
43QT_BEGIN_NAMESPACE
44struct PairingWorker;
45QT_END_NAMESPACE
46#endif
47
48#ifdef QT_ANDROID_BLUETOOTH
49#include <jni.h>
50#include <QtCore/QJniEnvironment>
51#include <QtCore/QJniObject>
52#include <QtCore/QPair>
53#endif
54
55QT_BEGIN_NAMESPACE
56
57extern void registerQBluetoothLocalDeviceMetaType();
58
59class QBluetoothAddress;
60
61#ifdef QT_ANDROID_BLUETOOTH
62class LocalDeviceBroadcastReceiver;
63class QBluetoothLocalDevicePrivate : public QObject
64{
65 Q_OBJECT
66public:
67 QBluetoothLocalDevicePrivate(
68 QBluetoothLocalDevice *q, const QBluetoothAddress &address = QBluetoothAddress());
69 ~QBluetoothLocalDevicePrivate();
70
71 QJniObject *adapter();
72 void initialize(const QBluetoothAddress &address);
73 static bool startDiscovery();
74 static bool cancelDiscovery();
75 static bool isDiscovering();
76 bool isValid() const;
77
78private slots:
79 void processHostModeChange(QBluetoothLocalDevice::HostMode newMode);
80 void processPairingStateChanged(const QBluetoothAddress &address,
81 QBluetoothLocalDevice::Pairing pairing);
82 void processConnectDeviceChanges(const QBluetoothAddress &address, bool isConnectEvent);
83
84private:
85 QBluetoothLocalDevice *q_ptr;
86 QJniObject *obj = nullptr;
87
88 int pendingPairing(const QBluetoothAddress &address);
89
90public:
91 LocalDeviceBroadcastReceiver *receiver;
92 bool pendingConnectableHostModeTransition = false;
93 QList<QPair<QBluetoothAddress, bool> > pendingPairings;
94
95 QList<QBluetoothAddress> connectedDevices;
96};
97
98#elif QT_CONFIG(bluez)
99class QBluetoothLocalDevicePrivate : public QObject
100{
101 Q_OBJECT
102 Q_DECLARE_PUBLIC(QBluetoothLocalDevice)
103public:
104 QBluetoothLocalDevicePrivate(QBluetoothLocalDevice *q,
105 QBluetoothAddress localAddress = QBluetoothAddress());
106 ~QBluetoothLocalDevicePrivate();
107
108 QSet<QBluetoothAddress> connectedDevicesSet;
109 OrgBluezAdapter1Interface *adapter = nullptr;
110 OrgFreedesktopDBusPropertiesInterface *adapterProperties = nullptr;
111 OrgFreedesktopDBusObjectManagerInterface *manager = nullptr;
112 QMap<QString, OrgFreedesktopDBusPropertiesInterface *> deviceChangeMonitors;
113
114 QList<QBluetoothAddress> connectedDevices() const;
115
116 QBluetoothAddress localAddress;
117 QBluetoothAddress address;
118 QBluetoothLocalDevice::Pairing pairing;
119 OrgBluezDevice1Interface *pairingTarget = nullptr;
120 QTimer *pairingDiscoveryTimer = nullptr;
121 QBluetoothLocalDevice::HostMode currentMode;
122 int pendingHostModeChange;
123 bool pairingRequestCanceled = false;
124
125public slots:
126 void pairingCompleted(QDBusPendingCallWatcher *);
127
128 bool isValid() const;
129
130 void requestPairing(const QBluetoothAddress &address,
131 QBluetoothLocalDevice::Pairing targetPairing);
132
133private Q_SLOTS:
134 void PropertiesChanged(const QString &interface,
135 const QVariantMap &changed_properties,
136 const QStringList &invalidated_properties,
137 const QDBusMessage &signal);
138 void InterfacesAdded(const QDBusObjectPath &object_path,
139 InterfaceList interfaces_and_properties);
140 void InterfacesRemoved(const QDBusObjectPath &object_path,
141 const QStringList &interfaces);
142 void processPairing(const QString &objectPath, QBluetoothLocalDevice::Pairing target);
143 void pairingDiscoveryTimedOut();
144
145private:
146 void connectDeviceChanges();
147
148 QString deviceAdapterPath;
149
150 QBluetoothLocalDevice *q_ptr;
151
152 void initializeAdapter();
153};
154
155#elif defined(QT_WINRT_BLUETOOTH)
156class QBluetoothLocalDevicePrivate : public QObject
157{
158 Q_OBJECT
159 Q_DECLARE_PUBLIC(QBluetoothLocalDevice)
160public:
161 QBluetoothLocalDevicePrivate(QBluetoothLocalDevice *q,
162 QBluetoothAddress = QBluetoothAddress());
163 ~QBluetoothLocalDevicePrivate();
164
165 bool isValid() const;
166
167 void updateAdapterState(QBluetoothLocalDevice::HostMode mode);
168 Q_SLOT void onAdapterRemoved(winrt::hstring id);
169 Q_SLOT void onAdapterAdded(winrt::hstring id);
170 Q_SLOT void radioModeChanged(winrt::hstring id, QBluetoothLocalDevice::HostMode mode);
171 Q_SLOT void onDeviceAdded(const QBluetoothAddress &address);
172 Q_SLOT void onDeviceRemoved(const QBluetoothAddress &address);
173
174 QBluetoothLocalDevice *q_ptr;
175 winrt::com_ptr<PairingWorker> mPairingWorker;
176 winrt::Windows::Devices::Bluetooth::BluetoothAdapter mAdapter;
177 winrt::hstring mDeviceId;
178 QString mAdapterName;
179 QBluetoothLocalDevice::HostMode mMode;
180 winrt::event_token mModeChangeToken;
181
182signals:
183 void updateMode(winrt::hstring id, QBluetoothLocalDevice::HostMode mode);
184};
185#elif !defined(QT_OSX_BLUETOOTH) // dummy backend
186class QBluetoothLocalDevicePrivate : public QObject
187{
188public:
189 QBluetoothLocalDevicePrivate(QBluetoothLocalDevice * = nullptr,
190 QBluetoothAddress = QBluetoothAddress())
191 {
192 }
193
194 bool isValid() const
195 {
196 return false;
197 }
198};
199#endif
200
201QT_END_NAMESPACE
202
203#endif // QBLUETOOTHLOCALDEVICE_P_H
204

Provided by KDAB

Privacy Policy
Learn Advanced QML with KDAB
Find out more

source code of qtconnectivity/src/bluetooth/qbluetoothlocaldevice_p.h