forked from Gawhary/Qt-BLE-Tester
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbleinterface.h
More file actions
130 lines (108 loc) · 3.73 KB
/
Copy pathbleinterface.h
File metadata and controls
130 lines (108 loc) · 3.73 KB
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
#ifndef BLEINTERFACE_H
#define BLEINTERFACE_H
#include <QObject>
#include <QBluetoothDeviceDiscoveryAgent>
#include <QBluetoothDeviceInfo>
#include <QLowEnergyController>
#include <QLowEnergyService>
#include <QTimer>
#include "lib-qt-qml-tricks/src/qqmlhelpers.h"
#define READ_INTERVAL_MS 3000
#define CHUNK_SIZE 20
class DeviceInfo: public QObject
{
Q_OBJECT
Q_PROPERTY(QString deviceName READ getName NOTIFY deviceChanged)
Q_PROPERTY(QString deviceAddress READ getAddress NOTIFY deviceChanged)
public:
DeviceInfo(const QBluetoothDeviceInfo &device);
void setDevice(const QBluetoothDeviceInfo &device);
QString getName() const { return m_device.name(); }
QString getAddress() const;
QBluetoothDeviceInfo getDevice() const;
signals:
void deviceChanged();
private:
QBluetoothDeviceInfo m_device;
};
class BLEInterface : public QObject
{
Q_OBJECT
Q_PROPERTY(bool connected READ isConnected NOTIFY connectedChanged)
Q_PROPERTY(int currentService READ currentService WRITE setCurrentService NOTIFY currentServiceChanged)
QML_WRITABLE_PROPERTY(int, currentDevice)
QML_READONLY_PROPERTY(QStringList, devicesNames)
QML_READONLY_PROPERTY(QStringList, services)
public:
explicit BLEInterface(QObject *parent = 0);
~BLEInterface();
void connectCurrentDevice();
void disconnectDevice();
Q_INVOKABLE void scanDevices();
void write(const QByteArray& data);
bool isConnected() const
{
return m_connected;
}
int currentService() const
{
return m_currentService;
}
public slots:
void setCurrentService(int currentService)
{
if (m_currentService == currentService)
return;
update_currentService(currentService);
m_currentService = currentService;
emit currentServiceChanged(currentService);
}
signals:
void statusInfoChanged(QString info, bool isGood);
void dataReceived(const QByteArray &data);
void connectedChanged(bool connected);
void currentServiceChanged(int currentService);
private slots:
//QBluetothDeviceDiscoveryAgent
void addDevice(const QBluetoothDeviceInfo&);
void onScanFinished();
void onDeviceScanError(QBluetoothDeviceDiscoveryAgent::Error);
//QLowEnergyController
void onServiceDiscovered(const QBluetoothUuid &);
void onServiceScanDone();
void onControllerError(QLowEnergyController::Error);
void onDeviceConnected();
void onDeviceDisconnected();
//QLowEnergyService
void onServiceStateChanged(QLowEnergyService::ServiceState s);
void onCharacteristicChanged(const QLowEnergyCharacteristic &c,
const QByteArray &value);
void serviceError(QLowEnergyService::ServiceError e);
void read();
void onCharacteristicRead(const QLowEnergyCharacteristic &c, const QByteArray &value);
void onCharacteristicWrite(const QLowEnergyCharacteristic &c, const QByteArray &value);
void update_currentService(int currentSerice);
private:
inline void waitForWrite();
void update_connected(bool connected){
if(connected != m_connected){
m_connected = connected;
emit connectedChanged(connected);
}
}
QBluetoothDeviceDiscoveryAgent *m_deviceDiscoveryAgent;
QLowEnergyDescriptor m_notificationDesc;
QLowEnergyController *m_control;
QList<QBluetoothUuid> m_servicesUuid;
QLowEnergyService *m_service;
QLowEnergyCharacteristic m_readCharacteristic;
QLowEnergyCharacteristic m_writeCharacteristic;
QList<DeviceInfo*> m_devices;
// bool m_foundService;
QTimer *m_readTimer;
bool m_connected;
void searchCharacteristic();
int m_currentService;
QLowEnergyService::WriteMode m_writeMode;
};
#endif // BLEINTERFACE_H