1// Copyright (C) 2016 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
4#ifndef QLOWENERGYCONTROLLER_H
5#define QLOWENERGYCONTROLLER_H
6
7#include <QtCore/QObject>
8#include <QtBluetooth/QBluetoothAddress>
9#include <QtBluetooth/QBluetoothDeviceInfo>
10#include <QtBluetooth/QBluetoothUuid>
11#include <QtBluetooth/QLowEnergyAdvertisingData>
12#include <QtBluetooth/QLowEnergyConnectionParameters>
13#include <QtBluetooth/QLowEnergyService>
14
15QT_BEGIN_NAMESPACE
16
17class QLowEnergyAdvertisingParameters;
18class QLowEnergyControllerPrivate;
19class QLowEnergyServiceData;
20
21class Q_BLUETOOTH_EXPORT QLowEnergyController : public QObject
22{
23 Q_OBJECT
24public:
25 enum Error {
26 NoError,
27 UnknownError,
28 UnknownRemoteDeviceError,
29 NetworkError,
30 InvalidBluetoothAdapterError,
31 ConnectionError,
32 AdvertisingError,
33 RemoteHostClosedError,
34 AuthorizationError,
35 MissingPermissionsError,
36 RssiReadError
37 };
38 Q_ENUM(Error)
39
40 enum ControllerState {
41 UnconnectedState = 0,
42 ConnectingState,
43 ConnectedState,
44 DiscoveringState,
45 DiscoveredState,
46 ClosingState,
47 AdvertisingState,
48 };
49 Q_ENUM(ControllerState)
50
51 enum RemoteAddressType {
52 PublicAddress = 0,
53 RandomAddress
54 };
55 Q_ENUM(RemoteAddressType)
56
57 enum Role { CentralRole, PeripheralRole };
58 Q_ENUM(Role)
59
60 static QLowEnergyController *createCentral(const QBluetoothDeviceInfo &remoteDevice,
61 QObject *parent = nullptr);
62 static QLowEnergyController *createCentral(const QBluetoothDeviceInfo &remoteDevice,
63 const QBluetoothAddress &localDevice,
64 QObject *parent = nullptr);
65 static QLowEnergyController *createPeripheral(const QBluetoothAddress &localDevice,
66 QObject *parent = nullptr);
67 static QLowEnergyController *createPeripheral(QObject *parent = nullptr);
68
69 // TODO: Allow to set connection timeout (disconnect when no data has been exchanged for n seconds).
70
71 ~QLowEnergyController();
72
73 QBluetoothAddress localAddress() const;
74 QBluetoothAddress remoteAddress() const;
75 QBluetoothUuid remoteDeviceUuid() const;
76
77 QString remoteName() const;
78
79 ControllerState state() const;
80
81 // TODO Qt6 remove this property. It is not longer needed when using Bluez DBus backend
82 RemoteAddressType remoteAddressType() const;
83 void setRemoteAddressType(RemoteAddressType type);
84
85 void connectToDevice();
86 void disconnectFromDevice();
87
88 void discoverServices();
89 QList<QBluetoothUuid> services() const;
90 QLowEnergyService *createServiceObject(const QBluetoothUuid &service, QObject *parent = nullptr);
91
92 void startAdvertising(const QLowEnergyAdvertisingParameters &parameters,
93 const QLowEnergyAdvertisingData &advertisingData,
94 const QLowEnergyAdvertisingData &scanResponseData = QLowEnergyAdvertisingData());
95 void stopAdvertising();
96
97 QLowEnergyService *addService(const QLowEnergyServiceData &service, QObject *parent = nullptr);
98
99 void requestConnectionUpdate(const QLowEnergyConnectionParameters &parameters);
100
101 Error error() const;
102 QString errorString() const;
103
104 Role role() const;
105
106 int mtu() const;
107 void readRssi();
108
109Q_SIGNALS:
110 void connected();
111 void disconnected();
112 void stateChanged(QLowEnergyController::ControllerState state);
113 void errorOccurred(QLowEnergyController::Error newError);
114 void mtuChanged(int mtu);
115 void rssiRead(qint16 rssi);
116
117 void serviceDiscovered(const QBluetoothUuid &newService);
118 void discoveryFinished();
119 void connectionUpdated(const QLowEnergyConnectionParameters &parameters);
120
121
122private:
123 // peripheral role ctor
124 explicit QLowEnergyController(const QBluetoothAddress &localDevice, QObject *parent = nullptr);
125
126 // central role ctors
127 explicit QLowEnergyController(const QBluetoothDeviceInfo &remoteDevice,
128 const QBluetoothAddress &localDevice,
129 QObject *parent = nullptr);
130
131
132 Q_DECLARE_PRIVATE(QLowEnergyController)
133 QLowEnergyControllerPrivate *d_ptr;
134};
135
136QT_END_NAMESPACE
137
138QT_DECL_METATYPE_EXTERN_TAGGED(QLowEnergyController::Error, QLowEnergyController__Error,
139 Q_BLUETOOTH_EXPORT)
140QT_DECL_METATYPE_EXTERN_TAGGED(QLowEnergyController::ControllerState,
141 QLowEnergyController__ControllerState,
142 Q_BLUETOOTH_EXPORT)
143QT_DECL_METATYPE_EXTERN_TAGGED(QLowEnergyController::RemoteAddressType,
144 QLowEnergyController__RemoteAddressType,
145 Q_BLUETOOTH_EXPORT)
146QT_DECL_METATYPE_EXTERN_TAGGED(QLowEnergyController::Role, QLowEnergyController__Role,
147 Q_BLUETOOTH_EXPORT)
148
149#endif // QLOWENERGYCONTROLLER_H
150

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