From 049951ebed550535f7666fa849405ee835fc7e81 Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Fri, 9 Oct 2020 00:03:51 +0200 Subject: [PATCH] added some basic controls --- qbluetoothleuart.cpp | 32 +++++++++++++++++++++++++++++--- qbluetoothleuart.h | 4 +++- qbluetoothleuartdevice.h | 4 ++-- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/qbluetoothleuart.cpp b/qbluetoothleuart.cpp index bac492c..ebd2940 100644 --- a/qbluetoothleuart.cpp +++ b/qbluetoothleuart.cpp @@ -27,14 +27,13 @@ QBluetoothLeUart::~QBluetoothLeUart(){ void QBluetoothLeUart::init() { #ifdef QBluetoothLeUart_QML - qmlRegisterUncreatableType("de.itsblue.bluetoothleuart", 1, 0, "BluetoothDeviceInfo", "BluetoothDeviceInfo cannot be created"); - qmlRegisterType("de.itsblue.bluetoothleuart", 1, 0, "BluetoothLeUART"); + qmlRegisterUncreatableType("de.itsblue.bluetoothleuart", 1, 0, "QBluetoothDeviceInfo", "BluetoothDeviceInfo cannot be created"); + qmlRegisterType("de.itsblue.bluetoothleuart", 1, 0, "QBluetoothLeUART"); qRegisterMetaType("QBluetoothLeUart::BluetoothLeUartState"); qRegisterMetaType("QBluetoothLeUart::BluetoothScanError"); #endif } - void QBluetoothLeUart::startScanningForDevices(){ foreach(QBluetoothLeUartDevice* oldDevice, this->availableDevices) @@ -79,6 +78,25 @@ QList QBluetoothLeUart::getAvailableDevices() { return this->availableDevices; } +QVariantList QBluetoothLeUart::getAvailableDevicesQML() { + QVariantList result; + + for(int i=0; i < this->availableDevices.length(); i++) { + if(this->availableDevices[i]->getName().isEmpty()) + continue; + + QVariantMap device; + + device.insert("id", i); + device.insert("name", this->availableDevices[i]->getName()); + device.insert("address", this->availableDevices[i]->getAddress()); + + result.append(device); + } + + return result; +} + void QBluetoothLeUart::handleDeviceScanError(QBluetoothDeviceDiscoveryAgent::Error error) { if (error == QBluetoothDeviceDiscoveryAgent::PoweredOffError) @@ -90,6 +108,14 @@ void QBluetoothLeUart::handleDeviceScanError(QBluetoothDeviceDiscoveryAgent::Err } + +void QBluetoothLeUart::connectToDevice(int deviceId) { + if(deviceId < 0 || deviceId >= this->availableDevices.length()) + return; + + this->connectToDevice(this->availableDevices[deviceId]); +} + void QBluetoothLeUart::connectToDevice(QBluetoothLeUartDevice *device){ m_qvMeasurements.clear(); diff --git a/qbluetoothleuart.h b/qbluetoothleuart.h index 872b0c1..3191b60 100644 --- a/qbluetoothleuart.h +++ b/qbluetoothleuart.h @@ -15,7 +15,7 @@ class QBluetoothLeUart : public QObject { Q_OBJECT - Q_PROPERTY(QList avaliableDevices READ getAvailableDevices NOTIFY avaliableDevicesChanged) + Q_PROPERTY(QVariantList avaliableDevices READ getAvailableDevicesQML NOTIFY avaliableDevicesChanged) Q_PROPERTY(BluetoothLeUartState state READ getState NOTIFY stateChanged) public: @@ -70,7 +70,9 @@ public slots: Q_INVOKABLE void startScanningForDevices(); Q_INVOKABLE void stopScanningForDevices(); Q_INVOKABLE QList getAvailableDevices(); + Q_INVOKABLE QVariantList getAvailableDevicesQML(); Q_INVOKABLE void connectToDevice(QBluetoothLeUartDevice *device); + Q_INVOKABLE void connectToDevice(int deviceId); Q_INVOKABLE void sendData(QString s); Q_INVOKABLE void disconnectFromDevice(); diff --git a/qbluetoothleuartdevice.h b/qbluetoothleuartdevice.h index f7bc161..533ed71 100644 --- a/qbluetoothleuartdevice.h +++ b/qbluetoothleuartdevice.h @@ -18,8 +18,8 @@ public: friend class QBluetoothLeUart; - QString getName(); - QString getAddress(); + Q_INVOKABLE QString getName(); + Q_INVOKABLE QString getAddress(); protected: QBluetoothDeviceInfo getDevice();