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
124public slots:
125 void pairingCompleted(QDBusPendingCallWatcher *);
126
127 bool isValid() const;
128
129 void requestPairing(const QBluetoothAddress &address,
130 QBluetoothLocalDevice::Pairing targetPairing);
131
132private Q_SLOTS:
133 void PropertiesChanged(const QString &interface,
134 const QVariantMap &changed_properties,
135 const QStringList &invalidated_properties,
136 const QDBusMessage &signal);
137 void InterfacesAdded(const QDBusObjectPath &object_path,
138 InterfaceList interfaces_and_properties);
139 void InterfacesRemoved(const QDBusObjectPath &object_path,
140 const QStringList &interfaces);
141 void processPairing(const QString &objectPath, QBluetoothLocalDevice::Pairing target);
142 void pairingDiscoveryTimedOut();
143
144private:
145 void connectDeviceChanges();
146
147 QString deviceAdapterPath;
148
149 QBluetoothLocalDevice *q_ptr;
150
151 void initializeAdapter();
152};
153
154#elif defined(QT_WINRT_BLUETOOTH)
155class QBluetoothLocalDevicePrivate : public QObject
156{
157 Q_OBJECT
158 Q_DECLARE_PUBLIC(QBluetoothLocalDevice)
159public:
160 QBluetoothLocalDevicePrivate(QBluetoothLocalDevice *q,
161 QBluetoothAddress = QBluetoothAddress());
162 ~QBluetoothLocalDevicePrivate();
163
164 bool isValid() const;
165
166 void updateAdapterState(QBluetoothLocalDevice::HostMode mode);
167 Q_SLOT void onAdapterRemoved(winrt::hstring id);
168 Q_SLOT void onAdapterAdded(winrt::hstring id);
169 Q_SLOT void radioModeChanged(winrt::hstring id, QBluetoothLocalDevice::HostMode mode);
170 Q_SLOT void onDeviceAdded(const QBluetoothAddress &address);
171 Q_SLOT void onDeviceRemoved(const QBluetoothAddress &address);
172
173 QBluetoothLocalDevice *q_ptr;
174 winrt::com_ptr<PairingWorker> mPairingWorker;
175 winrt::Windows::Devices::Bluetooth::BluetoothAdapter mAdapter;
176 winrt::hstring mDeviceId;
177 QString mAdapterName;
178 QBluetoothLocalDevice::HostMode mMode;
179 winrt::event_token mModeChangeToken;
180
181signals:
182 void updateMode(winrt::hstring id, QBluetoothLocalDevice::HostMode mode);
183};
184#elif !defined(QT_OSX_BLUETOOTH) // dummy backend
185class QBluetoothLocalDevicePrivate : public QObject
186{
187public:
188 QBluetoothLocalDevicePrivate(QBluetoothLocalDevice * = nullptr,
189 QBluetoothAddress = QBluetoothAddress())
190 {
191 }
192
193 bool isValid() const
194 {
195 return false;
196 }
197};
198#endif
199
200QT_END_NAMESPACE
201
202#endif // QBLUETOOTHLOCALDEVICE_P_H
203

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