2020-10-08 20:01:21 +02:00
|
|
|
#ifndef BLUETOOTHLEUART_H
|
|
|
|
#define BLUETOOTHLEUART_H
|
|
|
|
|
|
|
|
#include <QBluetoothDeviceDiscoveryAgent>
|
|
|
|
#include <QBluetoothDeviceInfo>
|
|
|
|
#include <QLowEnergyController>
|
|
|
|
#include <QLowEnergyService>
|
|
|
|
|
|
|
|
#ifdef QBluetoothLeUart_QML
|
|
|
|
#include <QQmlApplicationEngine>
|
|
|
|
#endif
|
|
|
|
|
2020-10-08 22:03:31 +02:00
|
|
|
#include <qbluetoothleuartdevice.h>
|
2020-10-08 20:01:21 +02:00
|
|
|
|
|
|
|
class QBluetoothLeUart : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
Q_PROPERTY(QList<QBluetoothLeUartDevice*> avaliableDevices READ getAvailableDevices NOTIFY avaliableDevicesChanged)
|
|
|
|
Q_PROPERTY(BluetoothLeUartState state READ getState NOTIFY stateChanged)
|
|
|
|
|
|
|
|
public:
|
|
|
|
enum BluetoothLeUartState {
|
|
|
|
Idle = 0,
|
|
|
|
Scanning,
|
|
|
|
ScanFinished,
|
|
|
|
Connecting,
|
|
|
|
Connected,
|
|
|
|
ServiceFound,
|
|
|
|
AcquireData
|
|
|
|
};
|
|
|
|
Q_ENUM(BluetoothLeUartState)
|
|
|
|
|
|
|
|
enum BluetoothScanError {
|
|
|
|
UnknownError,
|
|
|
|
AdapterTurnedOffError,
|
|
|
|
InputOutputError
|
|
|
|
};
|
|
|
|
Q_ENUM(BluetoothScanError);
|
|
|
|
|
|
|
|
QBluetoothLeUart(QObject *parent = nullptr);
|
|
|
|
~QBluetoothLeUart();
|
|
|
|
|
|
|
|
static void init();
|
|
|
|
|
2020-10-08 22:03:31 +02:00
|
|
|
void setUUIDs(const char uartServiceUUID[36], const char rxUUID[36], const char txUUID[36]);
|
|
|
|
|
2020-10-08 20:01:21 +02:00
|
|
|
private:
|
2020-10-08 22:03:31 +02:00
|
|
|
QString uartServiceUUID;
|
|
|
|
QString rxUUID;
|
|
|
|
QString txUUID;
|
|
|
|
|
2020-10-08 20:01:21 +02:00
|
|
|
QBluetoothLeUartDevice *currentBluetoothDevice;
|
|
|
|
QBluetoothDeviceDiscoveryAgent *bluetoothDeviceDiscoveryAgent;
|
|
|
|
//QList<QObject*> m_qlDevices;
|
|
|
|
QList<QBluetoothLeUartDevice*> availableDevices;
|
|
|
|
//QList<QString> m_qlFoundDevices;
|
|
|
|
QVector<quint16> m_qvMeasurements;
|
|
|
|
QLowEnergyController *bluetoothController;
|
|
|
|
QLowEnergyService *bluetoothService;
|
|
|
|
QLowEnergyDescriptor bluetoothTransmissionDescriptor;
|
|
|
|
bool foundValidUARTService;
|
|
|
|
|
|
|
|
QBluetoothLeUart::BluetoothLeUartState state;
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
|
|
|
|
/* Slots for user */
|
|
|
|
Q_INVOKABLE QBluetoothLeUart::BluetoothLeUartState getState() const;
|
|
|
|
|
|
|
|
Q_INVOKABLE void startScanningForDevices();
|
|
|
|
Q_INVOKABLE void stopScanningForDevices();
|
|
|
|
Q_INVOKABLE QList<QBluetoothLeUartDevice*> getAvailableDevices();
|
|
|
|
Q_INVOKABLE void connectToDevice(QBluetoothLeUartDevice *device);
|
|
|
|
Q_INVOKABLE void sendData(QString s);
|
|
|
|
Q_INVOKABLE void disconnectFromDevice();
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
void setState(QBluetoothLeUart::BluetoothLeUartState newState);
|
|
|
|
|
|
|
|
/* Slots for QBluetothDeviceDiscoveryAgent */
|
|
|
|
void handleDeviceDiscovered(const QBluetoothDeviceInfo&);
|
|
|
|
void handleScanFinished();
|
|
|
|
void handleDeviceScanError(QBluetoothDeviceDiscoveryAgent::Error);
|
|
|
|
|
|
|
|
/* Slots for QLowEnergyController */
|
|
|
|
void handleServiceDiscovered(const QBluetoothUuid &);
|
|
|
|
void handleServiceScanDone();
|
|
|
|
void handleControllerError(QLowEnergyController::Error);
|
|
|
|
void handleDeviceConnected();
|
|
|
|
void handleDeviceDisconnected();
|
|
|
|
|
|
|
|
/* Slotes for QLowEnergyService */
|
|
|
|
void handleServiceStateChange(QLowEnergyService::ServiceState s);
|
|
|
|
void handleServiceCharacteristicChange(const QLowEnergyCharacteristic &c, const QByteArray &value);
|
|
|
|
void handleServiceDescriptorWritten(const QLowEnergyDescriptor &d, const QByteArray &value);
|
|
|
|
|
|
|
|
signals:
|
|
|
|
/* Signals for user */
|
|
|
|
void stateChanged(QBluetoothLeUart::BluetoothLeUartState newState);
|
|
|
|
void avaliableDevicesChanged(QList<QBluetoothLeUartDevice*> avaliableDevices);
|
|
|
|
void foundNewDevice(QBluetoothLeUartDevice* device);
|
|
|
|
void scanningErrorOccured(QBluetoothLeUart::BluetoothScanError error);
|
|
|
|
void dataReceived(QString s);
|
|
|
|
void connectedToDevice();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // BLUETOOTHLEUART_H
|