Skip to content

Commit

Permalink
Misc
Browse files Browse the repository at this point in the history
Add support for changing device type
Add support for detecting device type
Add support for loading device from json file
Fix a crach in BLE communication
Show more information to users
  • Loading branch information
wh201906 committed Jun 18, 2023
1 parent f78a6de commit b4f008f
Show file tree
Hide file tree
Showing 20 changed files with 255 additions and 141 deletions.
19 changes: 19 additions & 0 deletions Qt/comms/comm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <QDebug>
#include <QTimer>
#include <QBluetoothLocalDevice>

Comm::Comm(QObject *parent)
: QObject{parent}
Expand Down Expand Up @@ -93,3 +94,21 @@ QByteArray Comm::checkValidity(QByteArray data)
}
return removeCheckSum(data);
}

QBluetoothAddress Comm::getLocalAddress()
{
QBluetoothAddress localAddress;

auto BTAdapterList = QBluetoothLocalDevice::allDevices();
for(auto it = BTAdapterList.cbegin(); it != BTAdapterList.cend(); ++it)
{
qDebug() << "dev:" << it->name() << it->address();
QBluetoothLocalDevice dev(it->address());
if(dev.isValid() && dev.hostMode() != QBluetoothLocalDevice::HostPoweredOff)
{
localAddress = it->address();
break; // find the first valid one
}
}
return localAddress;
}
3 changes: 3 additions & 0 deletions Qt/comms/comm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define COMM_H

#include <QObject>
#include <QBluetoothAddress>

class Comm : public QObject
{
Expand All @@ -14,6 +15,7 @@ class Comm : public QObject
static QByteArray addChecksum(QByteArray data);
static QByteArray removeCheckSum(QByteArray data);
static QByteArray checkValidity(QByteArray data);
static QBluetoothAddress getLocalAddress();
public slots:
bool sendCommand(const QByteArray& cmd, bool isRaw = false);
bool sendCommand(const char* hexCmd, bool isRaw = false);
Expand All @@ -25,6 +27,7 @@ protected slots:
void newData(const QByteArray& data);
void stateChanged(bool connected);
void showMessage(const QString& msg);
void deviceFeature(const QString& feature, bool isBLE = true);
};

#endif // COMM_H
55 changes: 26 additions & 29 deletions Qt/comms/commble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,11 @@ void CommBLE::open(const QString &address)
{
if(m_Controller != nullptr)
m_Controller->deleteLater();
QBluetoothAddress localAddress;

auto BTAdapterList = QBluetoothLocalDevice::allDevices();
for(auto it = BTAdapterList.cbegin(); it != BTAdapterList.cend(); ++it)
{
qDebug() << "dev:" << it->name() << it->address();
QBluetoothLocalDevice dev(it->address());
if(dev.isValid() && dev.hostMode() != QBluetoothLocalDevice::HostPoweredOff)
{
localAddress = it->address();
break; // find the first valid one
}
}
QBluetoothAddress localAddress = getLocalAddress();
if(localAddress.isNull())
return; // invalid

#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
m_BLEController = new QLowEnergyController(QBluetoothAddress(address), localAddress);
#else
Expand All @@ -43,7 +33,7 @@ void CommBLE::close()
{
if(m_RxTxService != nullptr)
{
QLowEnergyDescriptor desc = m_RxTxService->characteristic(m_RxUuid).descriptor(QBluetoothUuid::ClientCharacteristicConfiguration);
QLowEnergyDescriptor desc = m_RxTxService->characteristic(m_RxUUID).descriptor(QBluetoothUuid::ClientCharacteristicConfiguration);
m_RxTxService->writeDescriptor(desc, QByteArray::fromHex("0000"));
m_RxTxService->deleteLater();
m_RxTxService = nullptr;
Expand Down Expand Up @@ -85,44 +75,45 @@ void CommBLE::onServiceDetailDiscovered(QLowEnergyService::ServiceState newState
deleteService = true;
const QList<QLowEnergyCharacteristic> chars = service->characteristics();
// delete unused service
bool isRxUuidValid = false;
bool isTxUuidValid = false;
QBluetoothUuid TxUuid;
bool isRxUUIDValid = false;
bool isTxUUIDValid = false;
QBluetoothUuid TxUUID;

for(auto it = chars.cbegin(); it != chars.cend(); ++it)
{
auto uuid = it->uuid();
if(!isRxUuidValid && it->properties().testFlag(QLowEnergyCharacteristic::Notify)
&& (specialRxUuidList.contains(uuid) || (uuid.toString().contains("2-1a48-11e9-ab14-d663bd873d93", Qt::CaseInsensitive) && !specialTxUuidList.contains(uuid))))
if(!isRxUUIDValid && it->properties().testFlag(QLowEnergyCharacteristic::Notify)
&& (specialRxUUIDList.contains(uuid) || (uuid.toString().contains("2-1a48-11e9-ab14-d663bd873d93", Qt::CaseInsensitive) && !specialTxUUIDList.contains(uuid))))
{
isRxUuidValid = true;
m_RxUuid = uuid;
isRxUUIDValid = true;
m_RxUUID = uuid;
deleteService = false;
}
if(!isTxUuidValid && it->properties().testFlag(QLowEnergyCharacteristic::Write)
&& (specialTxUuidList.contains(uuid) || (uuid.toString().contains("3-1a48-11e9-ab14-d663bd873d93", Qt::CaseInsensitive) && !specialRxUuidList.contains(uuid))))
if(!isTxUUIDValid && it->properties().testFlag(QLowEnergyCharacteristic::Write)
&& (specialTxUUIDList.contains(uuid) || (uuid.toString().contains("3-1a48-11e9-ab14-d663bd873d93", Qt::CaseInsensitive) && !specialRxUUIDList.contains(uuid))))
{
isTxUuidValid = true;
TxUuid = uuid;
isTxUUIDValid = true;
TxUUID = uuid;
deleteService = false;
}
}

if(!deleteService)
{
if(isRxUuidValid && isTxUuidValid)
if(isRxUUIDValid && isTxUUIDValid)
{
connect(m_RxTxService, &QLowEnergyService::stateChanged, this, &CommBLE::onServiceStateChanged);
// Rx
connect(m_RxTxService, QOverload<QLowEnergyService::ServiceError>::of(&QLowEnergyService::error), this, &CommBLE::onErrorOccurred);
connect(m_RxTxService, &QLowEnergyService::characteristicChanged, this, &CommBLE::onDataArrived);
connect(m_RxTxService, &QLowEnergyService::characteristicRead, this, &CommBLE::onDataArrived); // not necessary
QLowEnergyDescriptor desc = m_RxTxService->characteristic(m_RxUuid).descriptor(QBluetoothUuid::ClientCharacteristicConfiguration);
QLowEnergyDescriptor desc = m_RxTxService->characteristic(m_RxUUID).descriptor(QBluetoothUuid::ClientCharacteristicConfiguration);
m_RxTxService->writeDescriptor(desc, QByteArray::fromHex("0100")); // Enable notify
// Tx
m_TxCharacteristic = m_RxTxService->characteristic(TxUuid);
m_TxCharacteristic = m_RxTxService->characteristic(TxUUID);
emit stateChanged(true);
emit showMessage(tr("Device Connected"));
emit deviceFeature(m_RxTxService->serviceUuid().toString());
}
}
else
Expand All @@ -131,6 +122,7 @@ void CommBLE::onServiceDetailDiscovered(QLowEnergyService::ServiceState newState
}
}
}

void CommBLE::onErrorOccurred()
{
if(sender() == m_Controller)
Expand All @@ -157,8 +149,13 @@ void CommBLE::onDataArrived(const QLowEnergyCharacteristic &characteristic, cons

qint64 CommBLE::write(const QByteArray &data)
{
m_RxTxService->writeCharacteristic(m_TxCharacteristic, data);
return data.length(); // no feedback
if(m_RxTxService != nullptr)
{
m_RxTxService->writeCharacteristic(m_TxCharacteristic, data);
return data.length(); // no feedback
}
else
return -1;
}

void CommBLE::onServiceStateChanged(QLowEnergyService::ServiceState newState)
Expand Down
7 changes: 3 additions & 4 deletions Qt/comms/commble.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class CommBLE : public Comm
explicit CommBLE(QObject *parent = nullptr);
void open(const QString& address) override;
void close() override;
public slots:
protected:
qint64 write(const QByteArray &data) override;
private slots:
Expand All @@ -24,14 +23,14 @@ private slots:
QLowEnergyController* m_Controller = nullptr;
QList<QBluetoothUuid> m_DiscoveredServices;
QLowEnergyService* m_RxTxService = nullptr;
QBluetoothUuid m_RxUuid;
QBluetoothUuid m_RxUUID;
QLowEnergyCharacteristic m_TxCharacteristic;
const QList<QBluetoothUuid> specialRxUuidList =
const QList<QBluetoothUuid> specialRxUUIDList =
{
QBluetoothUuid(QLatin1String("00001000-0000-1000-8992-00805f9b34fb")),
QBluetoothUuid(QLatin1String("48090001-1a48-11e9-ab14-d663bd873d93")),
};
const QList<QBluetoothUuid> specialTxUuidList =
const QList<QBluetoothUuid> specialTxUUIDList =
{
QBluetoothUuid(QLatin1String("00001000-0000-1000-8993-00805f9b34fb")),
QBluetoothUuid(QLatin1String("48090002-1a48-11e9-ab14-d663bd873d93")),
Expand Down
2 changes: 0 additions & 2 deletions Qt/comms/winbthelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class WinBTHelper : public QObject
explicit WinBTHelper(QObject *parent = nullptr);
public slots:
void start();
private:

signals:
void deviceDiscovered(QBluetoothDeviceInfo info);
void finished();
Expand Down
11 changes: 7 additions & 4 deletions Qt/deviceform.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "deviceform.h"
#include "ui_deviceform.h"
#include "comms/comm.h"

#include <QDebug>
#include <QBluetoothUuid>
#include <QBluetoothLocalDevice>
#ifdef Q_OS_ANDROID
#include <QtAndroid>
#include <QAndroidJniEnvironment>
Expand Down Expand Up @@ -48,6 +50,11 @@ DeviceForm::~DeviceForm()

void DeviceForm::onSearchButtonClicked()
{
if(Comm::getLocalAddress().isNull())
{
emit showMessage(tr("Bluetooth is not available"));
return;
}
if(sender() == ui->searchRFCOMMButton)
m_isCurrDiscoveryMethodBLE = false;
else if(sender() == ui->searchBLEButton)
Expand Down Expand Up @@ -104,7 +111,6 @@ void DeviceForm::onDeviceDiscovered(const QBluetoothDeviceInfo &info)
deviceTable->setItem(i, 2, typeItem);
m_shownDevices.append(address);


qDebug() << name
<< address
<< info.isValid()
Expand Down Expand Up @@ -148,13 +154,11 @@ void DeviceForm::on_connectButton_clicked()
emit connectTo(ui->deviceAddressEdit->text(), isBLE);
}


void DeviceForm::on_disconnectButton_clicked()
{
emit disconnectDevice();
}


void DeviceForm::on_searchStopButton_clicked()
{
m_discoveryAgent->stop();
Expand Down Expand Up @@ -223,4 +227,3 @@ void DeviceForm::getBondedTarget(bool isBLE)
}
}
#endif

1 change: 1 addition & 0 deletions Qt/deviceform.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ private slots:
void connectTo(const QString& address, bool isBLE);
void disconnectDevice();
void startDiscovery();
void showMessage(const QString& msg);
};

#endif // DEVICEFORM_H
Loading

0 comments on commit b4f008f

Please sign in to comment.