added current device property

This commit is contained in:
Dorian Zedler 2020-10-13 01:56:54 +02:00
parent 0b053eaa88
commit dad7187909
Signed by: dorian
GPG key ID: D3B255CB8BC7CD37
2 changed files with 21 additions and 4 deletions

View file

@ -84,6 +84,9 @@ QBluetoothLeUartDeviceModel* QBluetoothLeUart::getAvailableDevicesModel() {
return this->availableDevicesModel; return this->availableDevicesModel;
} }
QBluetoothLeUartDevice* QBluetoothLeUart::getCurrentDevice() {
return this->currentBluetoothDevice;
}
bool QBluetoothLeUart::connectToDevice(int deviceId) { bool QBluetoothLeUart::connectToDevice(int deviceId) {
if(deviceId < 0 || deviceId >= this->availableDevices.length()) if(deviceId < 0 || deviceId >= this->availableDevices.length())
@ -101,6 +104,7 @@ bool QBluetoothLeUart::connectToDevice(QBluetoothLeUartDevice *device){
this->stopScanningForDevices(); this->stopScanningForDevices();
this->currentBluetoothDevice = device; this->currentBluetoothDevice = device;
emit this->currentDeviceChanged();
if (bluetoothController) { if (bluetoothController) {
bluetoothController->disconnectFromDevice(); bluetoothController->disconnectFromDevice();
@ -141,12 +145,17 @@ bool QBluetoothLeUart::disconnectFromDevice() {
this->bluetoothService = nullptr; this->bluetoothService = nullptr;
} }
this->currentBluetoothDevice->deleteLater();
this->currentBluetoothDevice = nullptr;
emit this->currentDeviceChanged();
this->setState(Idle); this->setState(Idle);
return true; return true;
} }
bool QBluetoothLeUart::sendData(QString data){ bool QBluetoothLeUart::sendData(QString data, bool asynchronous){
Q_UNUSED(asynchronous)
if(this->state != Connected) if(this->state != Connected)
return false; return false;
@ -155,7 +164,7 @@ bool QBluetoothLeUart::sendData(QString data){
QByteArray Data; QByteArray Data;
Data.append(data); Data.append(data);
bluetoothService->writeCharacteristic(RxChar, Data,QLowEnergyService::WriteWithoutResponse); bluetoothService->writeCharacteristic(RxChar, Data, QLowEnergyService::WriteWithoutResponse);
return true; return true;
} }
@ -185,7 +194,7 @@ void QBluetoothLeUart::handleScanFinished()
{ {
if (this->availableDevices.size() == 0) if (this->availableDevices.size() == 0)
{ {
qWarning() << "No Low Energy devices found" << endl; qWarning() << "No Low Energy devices found";
} }
emit this->scanFinished(this->availableDevices); emit this->scanFinished(this->availableDevices);

View file

@ -118,6 +118,7 @@ class QBluetoothLeUart : public QObject
Q_OBJECT Q_OBJECT
Q_PROPERTY(QVariantList availableDevices READ getAvailableDevicesDetailList NOTIFY avaliableDevicesChanged) Q_PROPERTY(QVariantList availableDevices READ getAvailableDevicesDetailList NOTIFY avaliableDevicesChanged)
Q_PROPERTY(QBluetoothLeUartDeviceModel* availableDevicesModel READ getAvailableDevicesModel NOTIFY avaliableDevicesModelChanged) Q_PROPERTY(QBluetoothLeUartDeviceModel* availableDevicesModel READ getAvailableDevicesModel NOTIFY avaliableDevicesModelChanged)
Q_PROPERTY(QBluetoothLeUartDevice* currentDevice READ getCurrentDevice NOTIFY currentDeviceChanged)
Q_PROPERTY(BluetoothLeUartState state READ getState NOTIFY stateChanged) Q_PROPERTY(BluetoothLeUartState state READ getState NOTIFY stateChanged)
public: public:
@ -245,6 +246,12 @@ public slots:
*/ */
Q_INVOKABLE QBluetoothLeUartDeviceModel* getAvailableDevicesModel(); Q_INVOKABLE QBluetoothLeUartDeviceModel* getAvailableDevicesModel();
/*!
* \brief Function to get the currently connected device
* \return The currently connected device or nullptr if no device is connected
*/
Q_INVOKABLE QBluetoothLeUartDevice* getCurrentDevice();
/*! /*!
* \brief Function connect to a device using its internal id * \brief Function connect to a device using its internal id
* *
@ -293,7 +300,7 @@ public slots:
* \param data The data to send * \param data The data to send
* \return false if there was not device connected, true otherwise * \return false if there was not device connected, true otherwise
*/ */
Q_INVOKABLE bool sendData(QString data); Q_INVOKABLE bool sendData(QString data, bool asynchronous = true);
/*! /*!
* \brief Function to get the current state of QBluetoothLeUart * \brief Function to get the current state of QBluetoothLeUart
@ -328,6 +335,7 @@ signals:
void foundNewDevice(QBluetoothLeUartDevice* device); void foundNewDevice(QBluetoothLeUartDevice* device);
void avaliableDevicesChanged(QList<QBluetoothLeUartDevice*> avaliableDevices); void avaliableDevicesChanged(QList<QBluetoothLeUartDevice*> avaliableDevices);
void avaliableDevicesModelChanged(); void avaliableDevicesModelChanged();
void currentDeviceChanged();
void scanFinished(QList<QBluetoothLeUartDevice*> availableDevices); void scanFinished(QList<QBluetoothLeUartDevice*> availableDevices);
void scanningErrorOccured(QBluetoothLeUart::BluetoothScanError error); void scanningErrorOccured(QBluetoothLeUart::BluetoothScanError error);