From 1cd6884e7f84bf5fc2222422120aeb201e33a551 Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Fri, 9 Oct 2020 00:03:50 +0200 Subject: [PATCH] added some basic controls --- OmobiDisplayApp/OmobiDisplayApp.pro | 2 +- OmobiDisplayApp/main.cpp | 8 ++--- OmobiDisplayApp/main.qml | 44 +++++++++++++++++++++++++ OmobiDisplayApp/omobidisplaybackend.cpp | 44 +++++-------------------- OmobiDisplayApp/omobidisplaybackend.h | 8 ++++- 5 files changed, 64 insertions(+), 42 deletions(-) diff --git a/OmobiDisplayApp/OmobiDisplayApp.pro b/OmobiDisplayApp/OmobiDisplayApp.pro index 05717a1..c21201f 100644 --- a/OmobiDisplayApp/OmobiDisplayApp.pro +++ b/OmobiDisplayApp/OmobiDisplayApp.pro @@ -1,5 +1,5 @@ QT += quick bluetooth -CONFIG += c++11 console +CONFIG += c++11 TARGET = OmobiDisplayApp diff --git a/OmobiDisplayApp/main.cpp b/OmobiDisplayApp/main.cpp index dfe87bb..857793b 100644 --- a/OmobiDisplayApp/main.cpp +++ b/OmobiDisplayApp/main.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "omobidisplaybackend.h" @@ -9,10 +10,9 @@ int main(int argc, char *argv[]) QGuiApplication app(argc, argv); - OmobiDisplayBackend b; - b.startScanning(); + qmlRegisterType("de.itsblue.omobidisplayapp", 1, 0, "OmobiDisplayBackend"); + QBluetoothLeUart::init(); - /* QQmlApplicationEngine engine; const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, @@ -21,6 +21,6 @@ int main(int argc, char *argv[]) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url); -*/ + return app.exec(); } diff --git a/OmobiDisplayApp/main.qml b/OmobiDisplayApp/main.qml index 564b258..377b76f 100644 --- a/OmobiDisplayApp/main.qml +++ b/OmobiDisplayApp/main.qml @@ -1,10 +1,54 @@ import QtQuick 2.12 import QtQuick.Controls 2.0 import QtQuick.Window 2.12 +import de.itsblue.omobidisplayapp 1.0 +import de.itsblue.bluetoothleuart 1.0 ApplicationWindow { width: 640 height: 480 visible: true title: qsTr("Hello World") + + Page { + id: app + + anchors.fill: parent + + OmobiDisplayBackend { + id: backend + } + + Button { + id: connectButton + text: "Scan for devices" + + visible: backend.bleController.state === QBluetoothLeUART.Idle + + onClicked: { + backend.bleController.startScanningForDevices() + } + } + + ListView { + id: list + anchors.top: connectButton.bottom + anchors.margins: 10 + + height: parent.height + + model: backend.bleController.avaliableDevices.length + + delegate: Button { + property var thisDevice: backend.bleController.avaliableDevices[index] + + text: thisDevice["name"] + + onClicked: { + backend.bleController.connectToDevice(thisDevice["id"]) + } + + } + } + } } diff --git a/OmobiDisplayApp/omobidisplaybackend.cpp b/OmobiDisplayApp/omobidisplaybackend.cpp index 11d8006..3b3ec13 100644 --- a/OmobiDisplayApp/omobidisplaybackend.cpp +++ b/OmobiDisplayApp/omobidisplaybackend.cpp @@ -8,7 +8,7 @@ OmobiDisplayBackend::OmobiDisplayBackend(QObject *parent) : QObject(parent) connect(this->ble, &QBluetoothLeUart::stateChanged, this, &OmobiDisplayBackend::handleBluetoothStateChange); connect(this->ble, &QBluetoothLeUart::foundNewDevice, this, &OmobiDisplayBackend::handleFoundNewDevice); connect(this->ble, &QBluetoothLeUart::dataReceived, this, &OmobiDisplayBackend::DataHandler); - + connect(this->ble, &QBluetoothLeUart::connectedToDevice, this, &OmobiDisplayBackend::handleBluetoothDeviceConected); } @@ -21,36 +21,6 @@ void OmobiDisplayBackend::handleBluetoothStateChange(QBluetoothLeUart::Bluetooth qDebug() << state; switch(state){ - - case QBluetoothLeUart::Scanning: - { - qDebug() << "Now scanning"; - break; - } - - - case QBluetoothLeUart::ScanFinished: - { - qDebug() << "Scan finished"; - - break; - } - - case QBluetoothLeUart::Connecting: - { - qDebug() << "Now connecting"; - break; - } - case QBluetoothLeUart::Connected: - { - qDebug() << "Connected."; - break; - } - case QBluetoothLeUart::ServiceFound: - { - qDebug() << "Service found!"; - break; - } case QBluetoothLeUart::AcquireData: { qDebug() << "Now acquiring data!"; @@ -68,13 +38,12 @@ void OmobiDisplayBackend::handleBluetoothStateChange(QBluetoothLeUart::Bluetooth } } +void OmobiDisplayBackend::handleBluetoothDeviceConected() { + this->ble->sendData("Hallihallo ich bin Julia Mueller"); +} + void OmobiDisplayBackend::handleFoundNewDevice(QBluetoothLeUartDevice* device) { qDebug() << "Found a device: name: " << device->getName() << " address: " << device->getAddress(); - - if(device->getName() == "ESP32 Chat Test") { - this->ble->stopScanningForDevices(); - this->ble->connectToDevice(device); - } } void OmobiDisplayBackend::DataHandler(const QString &s){ @@ -83,3 +52,6 @@ void OmobiDisplayBackend::DataHandler(const QString &s){ } +QBluetoothLeUart* OmobiDisplayBackend::getBleController() { + return this->ble; +} diff --git a/OmobiDisplayApp/omobidisplaybackend.h b/OmobiDisplayApp/omobidisplaybackend.h index 98c111a..9aa02f7 100644 --- a/OmobiDisplayApp/omobidisplaybackend.h +++ b/OmobiDisplayApp/omobidisplaybackend.h @@ -7,20 +7,26 @@ class OmobiDisplayBackend : public QObject { Q_OBJECT + Q_PROPERTY(QBluetoothLeUart* bleController READ getBleController NOTIFY bleControllerChanged) public: explicit OmobiDisplayBackend(QObject *parent = nullptr); - void startScanning(); + Q_INVOKABLE void startScanning(); private: QBluetoothLeUart *ble; +public slots: + QBluetoothLeUart* getBleController(); + private slots: void handleBluetoothStateChange(QBluetoothLeUart::BluetoothLeUartState state); void handleFoundNewDevice(QBluetoothLeUartDevice* device); void DataHandler(const QString &s); + void handleBluetoothDeviceConected(); signals: + void bleControllerChanged(); };