#ifndef BLUETOOTHLEUART_H #define BLUETOOTHLEUART_H #include #include #include #include #ifdef QBluetoothLeUart_QML #include #endif #include class QBluetoothLeUart : public QObject { Q_OBJECT Q_PROPERTY(QList 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(); void setUUIDs(const char uartServiceUUID[36], const char rxUUID[36], const char txUUID[36]); private: QString uartServiceUUID; QString rxUUID; QString txUUID; QBluetoothLeUartDevice *currentBluetoothDevice; QBluetoothDeviceDiscoveryAgent *bluetoothDeviceDiscoveryAgent; //QList m_qlDevices; QList availableDevices; //QList m_qlFoundDevices; QVector 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 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 avaliableDevices); void foundNewDevice(QBluetoothLeUartDevice* device); void scanningErrorOccured(QBluetoothLeUart::BluetoothScanError error); void dataReceived(QString s); void connectedToDevice(); }; #endif // BLUETOOTHLEUART_H