QBluetoothLeUart/qbluetoothleuart.h

107 lines
3.4 KiB
C++

#ifndef BLUETOOTHLEUART_H
#define BLUETOOTHLEUART_H
#include <QBluetoothDeviceDiscoveryAgent>
#include <QBluetoothDeviceInfo>
#include <QLowEnergyController>
#include <QLowEnergyService>
#ifdef QBluetoothLeUart_QML
#include <QQmlApplicationEngine>
#endif
#include "qbluetoothleuartdevice.h"
#define UARTSERVICEUUID "6e400001-b5a3-f393-e0a9-e50e24dcca9e"
#define RXUUID "6e400002-b5a3-f393-e0a9-e50e24dcca9e"
#define TXUUID "6e400003-b5a3-f393-e0a9-e50e24dcca9e"
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();
private:
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