-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bluetooth.h
112 lines (94 loc) · 3.15 KB
/
Bluetooth.h
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
#ifndef BLUETOOTH_H
#define BLUETOOTH_H
/*
* Name: BLE UART handler class
* Author: Todd Morehouse
* Date: July 19th, 2019
*
* This class abstracts Qt's BLE library for simple BLE UART Communication
*
* Simply specify the name of the device you wish to connect to then use the read and write
* functions to communicate with the device.
*
* This class was designed to be used with the Adafruit BLE UART friend.
*
* Revision History
*
* * Date: July 19th, 2019 - Todd Morehouse
* Class was created.
*
*
*/
//Project includes
#include "Logger.h"
//Qt includes
#include <QObject>
#include <QString>
//Qt bluetooth includes
#include <QBluetoothDeviceDiscoveryAgent>
#include <QBluetoothDeviceInfo>
#include <QBluetoothSocket>
#include <QLowEnergyController>
#include <QLowEnergyService>
#include <QLowEnergyCharacteristic>
#include <QLowEnergyDescriptor>
#include <QBluetoothUuid>
class Bluetooth : public QObject
{
Q_OBJECT
public:
explicit Bluetooth(QObject *parent = nullptr);
~Bluetooth();
//Connection functions
void refreshDeviceList();
QStringList getDeviceList();
QString getDeviceName();
void setDeviceByName(QString device);
//Reading functions
QByteArray readAll();
QString getLine(QString terminator);
QStringList getAllLines(QString terminator);
void clearBuffer();
//Writing functions
void write(QByteArray data);
void write(const QString &data);
void write(const char data[]);
void write(QStringList data);
void write(const char data);
signals:
void dataAvailable();
void deviceConnected();
void deviceDisconnected();
void deviceListAvailable();
void deviceTransmitReady();
public slots:
void connectToDevice();
void connectToDevice(QString device);
void disconnectFromDevice();
private:
QList<QBluetoothDeviceInfo> discoveredDevices;
QBluetoothDeviceInfo device;
QByteArray dataBuffer;
const QString UART_UUID = "{6e400001-b5a3-f393-e0a9-e50e24dcca9e}"; //UART GATT UUID
const QString UART_TX_UUID = "{6e400002-b5a3-f393-e0a9-e50e24dcca9e}"; //UART TX Characteristic UUID
const QString UART_RX_UUID = "{6e400003-b5a3-f393-e0a9-e50e24dcca9e}"; //UART RX Characteristic UUID
QLowEnergyController *m_control = nullptr;
QLowEnergyService *service = nullptr;
QBluetoothUuid UARTuuid = QBluetoothUuid(UART_UUID);
QLowEnergyDescriptor m_notificationDesc;
Logger *logger = nullptr;
QString LogFilePath = "BluetoothDebug.txt";
void logMessage(QString message);
private slots:
void deviceDiscovered(const QBluetoothDeviceInfo &device);
void serviceDiscovered(QBluetoothUuid uuid);
void serviceScanDone();
void handleDeviceConnection();
void handleDeviceDisconnection();
void handleServiceStateChange(QLowEnergyService::ServiceState state);
void handleCharacteristicChange(QLowEnergyCharacteristic characteristic, QByteArray data);
void handleDescriptorWrite(QLowEnergyDescriptor descriptor, QByteArray data);
void handleError(QLowEnergyController::Error error);
void handleServiceError(QLowEnergyService::ServiceError error);
};
#endif // BLUETOOTH_H