-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblecomm.cpp
171 lines (144 loc) · 5.26 KB
/
blecomm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "blecomm.hpp"
#include <algorithm>
BLEComm::BLEComm(QObject* parent) : QObject{parent} {}
BLEComm::BLEComm(QBluetoothUuid const& serviceUuid,
QBluetoothUuid const& charUuid, QObject* parent)
: QObject{parent} {
setCommServiceUuid(serviceUuid);
setCommCharacteristicUuid(charUuid);
}
void BLEComm::connectToDevice(const QBluetoothDeviceInfo& device) {
if (connected()) {
disconnectFromDevice();
}
m_controller = QLowEnergyController::createCentral(device, this);
QObject::connect(m_controller, &QLowEnergyController::disconnected, this,
&BLEComm::disconnectedFromDevice, Qt::QueuedConnection);
QObject::connect(m_controller, &QLowEnergyController::connected, this,
&BLEComm::handleConnection, Qt::QueuedConnection);
QObject::connect(m_controller, &QLowEnergyController::discoveryFinished, this,
&BLEComm::handleDiscovery, Qt::QueuedConnection);
QObject::connect(
m_controller,
static_cast<void (QLowEnergyController::*)(QLowEnergyController::Error)>(
&QLowEnergyController::error),
[&](QLowEnergyController::Error errorCode) {
emit connectionError(BLEComm::Error::ConnectonError,
QString("Connection error: %1 (code %2)")
.arg(m_controller->errorString(), errorCode));
disconnectFromDevice();
});
m_controller->connectToDevice();
}
void BLEComm::disconnectFromDevice() {
if (m_service != nullptr) {
m_service->deleteLater();
m_service = nullptr;
}
if (m_controller != nullptr) {
m_controller->disconnectFromDevice();
m_controller->deleteLater();
m_controller = nullptr;
}
}
void BLEComm::setCommServiceUuid(QBluetoothUuid const& uuid) {
if (m_serviceUuid != uuid) {
m_serviceUuid = uuid;
emit commServiceUuidChanged(m_serviceUuid);
}
}
void BLEComm::setCommCharacteristicUuid(QBluetoothUuid const& uuid) {
if (m_charUuid != uuid) {
m_charUuid = uuid;
emit commCharacteristicUuidChanged(m_charUuid);
}
}
void BLEComm::transmitData(const QByteArray& data) {
if (connected() && m_service != nullptr) {
m_service->writeCharacteristic(m_char, data);
}
}
auto BLEComm::connected() const -> bool {
return (m_controller != nullptr &&
(m_controller->state() == QLowEnergyController::ConnectedState ||
m_controller->state() == QLowEnergyController::DiscoveredState));
}
auto BLEComm::ready() const -> bool {
if (m_service != nullptr) {
return m_service->state() == QLowEnergyService::ServiceDiscovered;
}
return false;
}
auto BLEComm::connectedDeviceName() const -> QString {
if (connected()) {
return m_controller->remoteName();
}
return QString{};
}
auto BLEComm::connectedDeviceAddress() const -> QBluetoothAddress {
if (connected()) {
return m_controller->remoteAddress();
}
return QBluetoothAddress{};
}
auto BLEComm::commServiceUuid() const -> QBluetoothUuid {
return m_serviceUuid;
}
auto BLEComm::commCharacteristicUuid() const -> QBluetoothUuid {
return m_charUuid;
}
void BLEComm::handleConnection() {
emit connectedToDevice();
m_controller->discoverServices();
}
void BLEComm::handleDiscovery() {
auto found_services = m_controller->services();
auto service_ptr = std::find_if(found_services.begin(), found_services.end(),
[this](auto const& serviceUuid) {
return serviceUuid == commServiceUuid();
});
if (service_ptr == found_services.end()) {
emit connectionError(
BLEComm::Error::ServiceError,
QString("Cannot find service %1 on device %2!")
.arg(commServiceUuid().toString(), m_controller->remoteName()));
disconnectFromDevice();
} else {
m_service = m_controller->createServiceObject(*service_ptr, this);
QObject::connect(
m_service,
static_cast<void (QLowEnergyService::*)(
QLowEnergyService::ServiceError)>(&QLowEnergyService::error),
[&](QLowEnergyService::ServiceError errorCode) {
emit connectionError(
BLEComm::Error::ServiceError,
QString("An unknown service error happened (code %1)")
.arg(errorCode));
});
QObject::connect(m_service, &QLowEnergyService::characteristicChanged, this,
&BLEComm::handleData, Qt::QueuedConnection);
QObject::connect(
m_service, &QLowEnergyService::stateChanged,
[&](QLowEnergyService::ServiceState newState) {
if (newState == QLowEnergyService::ServiceDiscovered) {
m_char = m_service->characteristic(m_charUuid);
if (!m_char.isValid()) {
emit connectionError(
BLEComm::Error::CharacteristicError,
QString("Invalid characteristic %1 on device %2!")
.arg(m_charUuid.toString(), m_controller->remoteName()));
disconnectFromDevice();
return;
}
emit commsReady();
}
});
m_service->discoverDetails();
}
}
void BLEComm::handleData(const QLowEnergyCharacteristic& characteristic,
const QByteArray& data) {
if (characteristic == m_char) {
emit dataReceived(data);
}
}