-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bluetooth.cpp
323 lines (267 loc) · 8.96 KB
/
Bluetooth.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include "Bluetooth.h"
#include <QDateTime>
Bluetooth::Bluetooth(QObject *parent) : QObject(parent)
{
this->logger = new Logger(this);
logger->setLogFile(this->LogFilePath);
logger->startLogging();
}
Bluetooth::~Bluetooth()
{
if(this->service){
delete this->service;
this->service = nullptr;
qDebug() << "del";
}
if(this->m_control){
this->m_control->disconnectFromDevice();
delete this->m_control;
this->m_control = nullptr;
qDebug() << "del";
}
this->logMessage("Destroying bluetooth object...");
this->logger->stopLogging();
}
void Bluetooth::refreshDeviceList()
{
this->discoveredDevices.clear();
QBluetoothDeviceDiscoveryAgent *discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
discoveryAgent->setLowEnergyDiscoveryTimeout(5000);
connect(discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)),
this, SLOT(deviceDiscovered(QBluetoothDeviceInfo)));
discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
}
QStringList Bluetooth::getDeviceList()
{
QStringList devices;
for(QBluetoothDeviceInfo device : discoveredDevices){
devices.append(device.name());
}
return devices;
}
QString Bluetooth::getDeviceName()
{
return device.name();
}
void Bluetooth::setDeviceByName(QString device)
{
for(int i = 0; i < discoveredDevices.size(); i++){
if(discoveredDevices[i].name() == device){
this->device = discoveredDevices.at(i); //Set class device info variable
}
}
this->logMessage(QString("Device set to %1").arg(this->device.name()));
}
QByteArray Bluetooth::readAll()
{
QByteArray data = this->dataBuffer;
this->dataBuffer.clear();
return data;
}
QString Bluetooth::getLine(QString terminator)
{
QString line;
qDebug() << dataBuffer;
QString buffer = QString::fromLocal8Bit(dataBuffer);
if(buffer.contains(terminator)){
line = buffer.split(terminator).at(0);
//Remove line from buffer
dataBuffer.remove(0, line.size());
}
qDebug() << line;
qDebug() << dataBuffer;
return line;
}
QStringList Bluetooth::getAllLines(QString terminator)
{
QStringList lines;
//Split data into lines
lines = QString::fromLocal8Bit(dataBuffer).split(terminator);
//Remove data from buffer
dataBuffer.clear();
return lines;
}
void Bluetooth::clearBuffer()
{
this->dataBuffer.clear();
}
void Bluetooth::write(QByteArray data)
{
QString logString = "Writing data.. '%1' (%2 bytes)";
logString = logString.arg(QString::fromUtf8(data)).arg(data.size());
this->logMessage(logString);
if(m_control){
//Service discovered.
QBluetoothUuid uuid = QBluetoothUuid(UART_TX_UUID);
QLowEnergyCharacteristic hrChar = service->characteristic(uuid);
if(hrChar.isValid()){
service->writeCharacteristic(hrChar, data);
}
else{
this->logMessage("Failed to write data");
}
}
}
void Bluetooth::write(const QString &data)
{
write(data.toLocal8Bit());
}
void Bluetooth::write(const char data[])
{
write(QByteArray::fromRawData(data, static_cast<int>(strlen(data))));
}
void Bluetooth::write(QStringList data)
{
for(QString str : data){
write(str.toLocal8Bit());
}
}
void Bluetooth::write(const char data)
{
write(QString("%1").arg(data));
}
void Bluetooth::connectToDevice()
{
this->logMessage(QString("Connecting to device %1...").arg(device.name()));
m_control = QLowEnergyController::createCentral(device, this);
connect(m_control, SIGNAL(error(QLowEnergyController::Error)),
this, SLOT(handleError(QLowEnergyController::Error)));
connect(m_control, SIGNAL(serviceDiscovered(QBluetoothUuid)),
this, SLOT(serviceDiscovered(QBluetoothUuid)));
connect(m_control, SIGNAL(discoveryFinished()),
this, SLOT(serviceScanDone()));
connect(m_control, SIGNAL(connected()), this, SLOT(handleDeviceConnection()));
connect(m_control, SIGNAL(disconnected()), this, SLOT(handleDeviceDisconnection()));
m_control->connectToDevice();
}
void Bluetooth::connectToDevice(QString device)
{
setDeviceByName(device);
connectToDevice();
}
void Bluetooth::disconnectFromDevice()
{
m_control->disconnectFromDevice();
delete service;
service = nullptr;
}
void Bluetooth::logMessage(QString message)
{
if(this->logger->isLogging()){
QString str;
QDateTime time = QDateTime::currentDateTime();
str += time.currentDateTime().toString(Qt::ISODateWithMs);
str += " - ";
str += message;
str += "\r\n"; //Only \n needed for most editors. Notepad needs \r\n
logger->log(str);
qDebug() << str;
}
}
void Bluetooth::deviceDiscovered(const QBluetoothDeviceInfo &device)
{
this->discoveredDevices.append(device);
emit deviceListAvailable();
}
//This function is called whenever a GATT service is discovered
void Bluetooth::serviceDiscovered(QBluetoothUuid uuid)
{
if(uuid == UARTuuid){
service = m_control->createServiceObject(uuid, this);
if(service){
qDebug() << "service started";
connect(service, SIGNAL(stateChanged(QLowEnergyService::ServiceState)), this, SLOT(handleServiceStateChange(QLowEnergyService::ServiceState)));
connect(service, SIGNAL(characteristicChanged(QLowEnergyCharacteristic, QByteArray)), this, SLOT(handleCharacteristicChange(QLowEnergyCharacteristic, QByteArray)));
connect(service, SIGNAL(descriptorWritten(QLowEnergyDescriptor, QByteArray)), this, SLOT(handleDescriptorWrite(QLowEnergyDescriptor, QByteArray)));
connect(service, SIGNAL(characteristicRead(QLowEnergyCharacteristic, QByteArray)), this, SLOT(handleCharacteristicChange(QLowEnergyCharacteristic, QByteArray)));
service->discoverDetails();
}
else{
qDebug() << "Failed to start UART service";
}
}
}
//This function is called whenever a GATT service scan is finished
void Bluetooth::serviceScanDone()
{
//Do nothing
}
void Bluetooth::handleDeviceConnection()
{
this->logMessage("Successfully connected");
this->logMessage("Discovering services...");
m_control->discoverServices();
emit deviceConnected();
}
void Bluetooth::handleDeviceDisconnection()
{
this->logMessage("Disconnected from device");
emit deviceDisconnected();
}
void Bluetooth::handleServiceStateChange(QLowEnergyService::ServiceState state)
{
switch(state){
case QLowEnergyService::DiscoveringServices:
this->logMessage("Service state changed to 'Discovering'");
//Discovering services, do nothing
break;
case QLowEnergyService::ServiceDiscovered:
//Scope needed for inner variables
{
this->logMessage("Service state changed to 'Discovered'");
//Service discovered.
QBluetoothUuid txUuid = QBluetoothUuid(UART_TX_UUID);
QLowEnergyCharacteristic txChar = service->characteristic(txUuid);
if(txChar.isValid()){
this->logMessage("UART TX service discovered");
service->writeCharacteristic(txChar, "Test data");
emit deviceTransmitReady();
}
else{
qDebug() << "TX Data not found.";
}
QBluetoothUuid rxUuid = QBluetoothUuid(UART_RX_UUID);
QLowEnergyCharacteristic rxChar = service->characteristic(rxUuid);
if(rxChar.isValid()){
this->logMessage("UART RX Service discovered");
}
else{
qDebug() << "RX Data not found.";
}
QLowEnergyDescriptor m_notificationDescTx = rxChar.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration);
if(m_notificationDescTx.isValid()){
service->writeDescriptor(m_notificationDescTx, QByteArray::fromHex("0100"));
this->logMessage("Notification desc");
}
break;
}
default:
break;
}
}
void Bluetooth::handleCharacteristicChange(QLowEnergyCharacteristic, QByteArray data)
{
this->logMessage(QString("Received data: '%1'").arg(QString::fromUtf8(data)));
this->dataBuffer.append(data);
emit dataAvailable();
}
void Bluetooth::handleDescriptorWrite(QLowEnergyDescriptor, QByteArray data)
{
//Do nothing
this->logMessage(QString("Descriptor written with data: %1").arg(QString::fromUtf8(data)));
}
void Bluetooth::handleError(QLowEnergyController::Error error)
{
qDebug() << "err";
QString errorText =
"Error received (Code: %1)"
" %2";
errorText = errorText.arg(error).arg(this->m_control->errorString());
this->logMessage(errorText);
}
void Bluetooth::handleServiceError(QLowEnergyService::ServiceError error)
{
QString errorText = "Error received (Code: %1)";
errorText = errorText.arg(error);
this->logMessage(errorText);
}