diff --git a/QBluetoothLeUart.pri b/QBluetoothLeUart.pri index 13a4be7..4e812a7 100644 --- a/QBluetoothLeUart.pri +++ b/QBluetoothLeUart.pri @@ -8,7 +8,6 @@ QBluetoothLeUart_QML { QT += core bluetooth -TEMPLATE = lib CONFIG += staticlib CONFIG += c++11 @@ -17,16 +16,12 @@ CONFIG += c++11 # In order to do so, uncomment the following line. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 +INCLUDEPATH += $$PWD/ + SOURCES += \ - qbluetoothleuart.cpp \ - qbluetoothleuartdevice.cpp + $$PWD/qbluetoothleuart.cpp \ + $$PWD/qbluetoothleuartdevice.cpp HEADERS += \ - qbluetoothleuart.h \ - qbluetoothleuartdevice.h - -# Default rules for deployment. -unix { - target.path = $$[QT_INSTALL_PLUGINS]/generic -} -!isEmpty(target.path): INSTALLS += target + $$PWD/qbluetoothleuart.h \ + $$PWD/qbluetoothleuartdevice.h diff --git a/qbluetoothleuart.cpp b/qbluetoothleuart.cpp index 728e45d..bac492c 100644 --- a/qbluetoothleuart.cpp +++ b/qbluetoothleuart.cpp @@ -8,6 +8,8 @@ QBluetoothLeUart::QBluetoothLeUart(QObject *parent) : QObject(parent) state = Idle; + this->setUUIDs("6e400001-b5a3-f393-e0a9-e50e24dcca9e", "6e400002-b5a3-f393-e0a9-e50e24dcca9e", "6e400003-b5a3-f393-e0a9-e50e24dcca9e"); + /* 1 Step: Bluetooth LE Device Discovery */ this->bluetoothDeviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(this); @@ -153,7 +155,7 @@ void QBluetoothLeUart::handleServiceDiscovered(const QBluetoothUuid &gatt){ qDebug() << "Found service with ID: " << gatt; - if(gatt==QBluetoothUuid(QUuid(UARTSERVICEUUID))){ + if(gatt==QBluetoothUuid(QUuid(this->uartServiceUUID))){ foundValidUARTService =true; qDebug() << "UART service found!"; } @@ -166,7 +168,7 @@ void QBluetoothLeUart::handleServiceScanDone(){ if(foundValidUARTService){ qDebug() << "Connecting to UART service..."; - bluetoothService = bluetoothController->createServiceObject(QBluetoothUuid(QUuid(UARTSERVICEUUID)),this); + bluetoothService = bluetoothController->createServiceObject(QBluetoothUuid(QUuid(this->uartServiceUUID)),this); } if(!bluetoothService){ @@ -194,7 +196,7 @@ void QBluetoothLeUart::handleServiceStateChange(QLowEnergyService::ServiceState { //looking for the TX characteristic - const QLowEnergyCharacteristic TxChar = bluetoothService->characteristic(QBluetoothUuid(QUuid(TXUUID))); + const QLowEnergyCharacteristic TxChar = bluetoothService->characteristic(QBluetoothUuid(QUuid(this->txUUID))); if (!TxChar.isValid()){ qDebug() << "Tx characteristic not found"; this->disconnectFromDevice(); @@ -202,7 +204,7 @@ void QBluetoothLeUart::handleServiceStateChange(QLowEnergyService::ServiceState } //looking for the RX characteristic - const QLowEnergyCharacteristic RxChar = bluetoothService->characteristic(QBluetoothUuid(QUuid(RXUUID))); + const QLowEnergyCharacteristic RxChar = bluetoothService->characteristic(QBluetoothUuid(QUuid(this->rxUUID))); if (!RxChar.isValid()) { qDebug() << "Rx characteristic not found"; this->disconnectFromDevice(); @@ -233,7 +235,7 @@ void QBluetoothLeUart::handleServiceStateChange(QLowEnergyService::ServiceState void QBluetoothLeUart::handleServiceCharacteristicChange(const QLowEnergyCharacteristic &c,const QByteArray &value) { // ignore any other characteristic change - if (c.uuid() != QBluetoothUuid(QUuid(TXUUID))) + if (c.uuid() != QBluetoothUuid(QUuid(this->txUUID))) return; emit dataReceived((QString) value); @@ -253,7 +255,7 @@ void QBluetoothLeUart::handleServiceDescriptorWritten(const QLowEnergyDescriptor void QBluetoothLeUart::sendData(QString s){ - const QLowEnergyCharacteristic RxChar = bluetoothService->characteristic(QBluetoothUuid(QUuid(RXUUID))); + const QLowEnergyCharacteristic RxChar = bluetoothService->characteristic(QBluetoothUuid(QUuid(this->rxUUID))); qDebug()<< s; QByteArray Data; @@ -276,3 +278,9 @@ void QBluetoothLeUart::setState(QBluetoothLeUart::BluetoothLeUartState newState) QBluetoothLeUart::BluetoothLeUartState QBluetoothLeUart::getState() const { return state; } + +void QBluetoothLeUart::setUUIDs(const char uartServiceUUID[36], const char rxUUID[36], const char txUUID[36]) { + this->uartServiceUUID = uartServiceUUID; + this->rxUUID = rxUUID; + this->txUUID = txUUID; +} diff --git a/qbluetoothleuart.h b/qbluetoothleuart.h index 94794e4..872b0c1 100644 --- a/qbluetoothleuart.h +++ b/qbluetoothleuart.h @@ -10,11 +10,7 @@ #include #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" +#include class QBluetoothLeUart : public QObject { @@ -46,7 +42,13 @@ public: 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;