This commit is contained in:
Dorian Zedler 2020-10-08 22:03:31 +02:00
parent 8cc71db6e2
commit 49d6653504
Signed by: dorian
GPG key ID: D3B255CB8BC7CD37
3 changed files with 27 additions and 22 deletions

View file

@ -8,7 +8,6 @@ QBluetoothLeUart_QML {
QT += core bluetooth QT += core bluetooth
TEMPLATE = lib
CONFIG += staticlib CONFIG += staticlib
CONFIG += c++11 CONFIG += c++11
@ -17,16 +16,12 @@ CONFIG += c++11
# In order to do so, uncomment the following line. # 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 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
INCLUDEPATH += $$PWD/
SOURCES += \ SOURCES += \
qbluetoothleuart.cpp \ $$PWD/qbluetoothleuart.cpp \
qbluetoothleuartdevice.cpp $$PWD/qbluetoothleuartdevice.cpp
HEADERS += \ HEADERS += \
qbluetoothleuart.h \ $$PWD/qbluetoothleuart.h \
qbluetoothleuartdevice.h $$PWD/qbluetoothleuartdevice.h
# Default rules for deployment.
unix {
target.path = $$[QT_INSTALL_PLUGINS]/generic
}
!isEmpty(target.path): INSTALLS += target

View file

@ -8,6 +8,8 @@ QBluetoothLeUart::QBluetoothLeUart(QObject *parent) : QObject(parent)
state = Idle; 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 */ /* 1 Step: Bluetooth LE Device Discovery */
this->bluetoothDeviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(this); this->bluetoothDeviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
@ -153,7 +155,7 @@ void QBluetoothLeUart::handleServiceDiscovered(const QBluetoothUuid &gatt){
qDebug() << "Found service with ID: " << gatt; qDebug() << "Found service with ID: " << gatt;
if(gatt==QBluetoothUuid(QUuid(UARTSERVICEUUID))){ if(gatt==QBluetoothUuid(QUuid(this->uartServiceUUID))){
foundValidUARTService =true; foundValidUARTService =true;
qDebug() << "UART service found!"; qDebug() << "UART service found!";
} }
@ -166,7 +168,7 @@ void QBluetoothLeUart::handleServiceScanDone(){
if(foundValidUARTService){ if(foundValidUARTService){
qDebug() << "Connecting to UART service..."; qDebug() << "Connecting to UART service...";
bluetoothService = bluetoothController->createServiceObject(QBluetoothUuid(QUuid(UARTSERVICEUUID)),this); bluetoothService = bluetoothController->createServiceObject(QBluetoothUuid(QUuid(this->uartServiceUUID)),this);
} }
if(!bluetoothService){ if(!bluetoothService){
@ -194,7 +196,7 @@ void QBluetoothLeUart::handleServiceStateChange(QLowEnergyService::ServiceState
{ {
//looking for the TX characteristic //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()){ if (!TxChar.isValid()){
qDebug() << "Tx characteristic not found"; qDebug() << "Tx characteristic not found";
this->disconnectFromDevice(); this->disconnectFromDevice();
@ -202,7 +204,7 @@ void QBluetoothLeUart::handleServiceStateChange(QLowEnergyService::ServiceState
} }
//looking for the RX characteristic //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()) { if (!RxChar.isValid()) {
qDebug() << "Rx characteristic not found"; qDebug() << "Rx characteristic not found";
this->disconnectFromDevice(); this->disconnectFromDevice();
@ -233,7 +235,7 @@ void QBluetoothLeUart::handleServiceStateChange(QLowEnergyService::ServiceState
void QBluetoothLeUart::handleServiceCharacteristicChange(const QLowEnergyCharacteristic &c,const QByteArray &value) void QBluetoothLeUart::handleServiceCharacteristicChange(const QLowEnergyCharacteristic &c,const QByteArray &value)
{ {
// ignore any other characteristic change // ignore any other characteristic change
if (c.uuid() != QBluetoothUuid(QUuid(TXUUID))) if (c.uuid() != QBluetoothUuid(QUuid(this->txUUID)))
return; return;
emit dataReceived((QString) value); emit dataReceived((QString) value);
@ -253,7 +255,7 @@ void QBluetoothLeUart::handleServiceDescriptorWritten(const QLowEnergyDescriptor
void QBluetoothLeUart::sendData(QString s){ void QBluetoothLeUart::sendData(QString s){
const QLowEnergyCharacteristic RxChar = bluetoothService->characteristic(QBluetoothUuid(QUuid(RXUUID))); const QLowEnergyCharacteristic RxChar = bluetoothService->characteristic(QBluetoothUuid(QUuid(this->rxUUID)));
qDebug()<< s; qDebug()<< s;
QByteArray Data; QByteArray Data;
@ -276,3 +278,9 @@ void QBluetoothLeUart::setState(QBluetoothLeUart::BluetoothLeUartState newState)
QBluetoothLeUart::BluetoothLeUartState QBluetoothLeUart::getState() const { QBluetoothLeUart::BluetoothLeUartState QBluetoothLeUart::getState() const {
return state; 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;
}

View file

@ -10,11 +10,7 @@
#include <QQmlApplicationEngine> #include <QQmlApplicationEngine>
#endif #endif
#include "qbluetoothleuartdevice.h" #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 class QBluetoothLeUart : public QObject
{ {
@ -46,7 +42,13 @@ public:
static void init(); static void init();
void setUUIDs(const char uartServiceUUID[36], const char rxUUID[36], const char txUUID[36]);
private: private:
QString uartServiceUUID;
QString rxUUID;
QString txUUID;
QBluetoothLeUartDevice *currentBluetoothDevice; QBluetoothLeUartDevice *currentBluetoothDevice;
QBluetoothDeviceDiscoveryAgent *bluetoothDeviceDiscoveryAgent; QBluetoothDeviceDiscoveryAgent *bluetoothDeviceDiscoveryAgent;
//QList<QObject*> m_qlDevices; //QList<QObject*> m_qlDevices;