Skip to content

Commit

Permalink
BLE: Add support for WriteNoResponse operation
Browse files Browse the repository at this point in the history
Fixes #46
  • Loading branch information
wh201906 committed Apr 6, 2024
1 parent c88b47e commit 0e5c82e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
50 changes: 38 additions & 12 deletions src/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -781,9 +781,13 @@ qint64 Connection::write(const char *data, qint64 len)
else if(m_type == BLE_Central)
{
if(m_BLERxTxMode == BLE_2S2C)
m_BLETxService->writeCharacteristic(m_BLETxCharacteristic, QByteArray::fromRawData(data, len));
{
m_BLETxService->writeCharacteristic(m_BLETxCharacteristic, QByteArray::fromRawData(data, len), m_BLETxWriteMode);
}
else
m_BLERxTxService->writeCharacteristic(m_BLETxCharacteristic, QByteArray::fromRawData(data, len));
{
m_BLERxTxService->writeCharacteristic(m_BLETxCharacteristic, QByteArray::fromRawData(data, len), m_BLETxWriteMode);
}
return len; // no feedback
}
else if(m_type == TCP_Client)
Expand Down Expand Up @@ -1250,11 +1254,18 @@ void Connection::BLEC_onServiceDetailDiscovered(QLowEnergyService::ServiceState
{
for(auto it = chars.cbegin(); it != chars.cend(); ++it)
{
if(!m_BLERxCharacteristicValid && it->uuid() == m_currBTArgument.RxCharacteristicUUID && it->properties().testFlag(QLowEnergyCharacteristic::Notify) && it->properties().testFlag(QLowEnergyCharacteristic::Write))
if(!m_BLERxCharacteristicValid && it->uuid() == m_currBTArgument.RxCharacteristicUUID && it->properties().testFlag(QLowEnergyCharacteristic::Notify))
{
m_BLERxCharacteristicValid = true;
m_BLETxCharacteristicValid = true;
deleteService = false;
const bool hasWriteProperty = it->properties().testFlag(QLowEnergyCharacteristic::Write);
const bool hasWriteNoResponseProperty = it->properties().testFlag(QLowEnergyCharacteristic::WriteNoResponse);
if(hasWriteProperty || hasWriteNoResponseProperty)
{
// Use WRITE if both properties are supported
m_BLETxWriteMode = hasWriteProperty ? QLowEnergyService::WriteWithResponse : QLowEnergyService::WriteWithoutResponse;
m_BLERxCharacteristicValid = true;
m_BLETxCharacteristicValid = true;
deleteService = false;
}
}
}
}
Expand All @@ -1267,10 +1278,17 @@ void Connection::BLEC_onServiceDetailDiscovered(QLowEnergyService::ServiceState
m_BLERxCharacteristicValid = true;
deleteService = false;
}
if(!m_BLETxCharacteristicValid && it->uuid() == m_currBTArgument.TxCharacteristicUUID && it->properties().testFlag(QLowEnergyCharacteristic::Write))
if(!m_BLETxCharacteristicValid && it->uuid() == m_currBTArgument.TxCharacteristicUUID)
{
m_BLETxCharacteristicValid = true;
deleteService = false;
const bool hasWriteProperty = it->properties().testFlag(QLowEnergyCharacteristic::Write);
const bool hasWriteNoResponseProperty = it->properties().testFlag(QLowEnergyCharacteristic::WriteNoResponse);
if(hasWriteProperty || hasWriteNoResponseProperty)
{
// Use WRITE if both properties are supported
m_BLETxWriteMode = hasWriteProperty ? QLowEnergyService::WriteWithResponse : QLowEnergyService::WriteWithoutResponse;
m_BLETxCharacteristicValid = true;
deleteService = false;
}
}
}
}
Expand All @@ -1289,10 +1307,17 @@ void Connection::BLEC_onServiceDetailDiscovered(QLowEnergyService::ServiceState
{
for(auto it = chars.cbegin(); it != chars.cend(); ++it)
{
if(!m_BLETxCharacteristicValid && it->uuid() == m_currBTArgument.TxCharacteristicUUID && it->properties().testFlag(QLowEnergyCharacteristic::Write))
if(!m_BLETxCharacteristicValid && it->uuid() == m_currBTArgument.TxCharacteristicUUID)
{
m_BLETxCharacteristicValid = true;
deleteService = false;
const bool hasWriteProperty = it->properties().testFlag(QLowEnergyCharacteristic::Write);
const bool hasWriteNoResponseProperty = it->properties().testFlag(QLowEnergyCharacteristic::WriteNoResponse);
if(hasWriteProperty || hasWriteNoResponseProperty)
{
// Use WRITE if both properties are supported
m_BLETxWriteMode = hasWriteProperty ? QLowEnergyService::WriteWithResponse : QLowEnergyService::WriteWithoutResponse;
m_BLETxCharacteristicValid = true;
deleteService = false;
}
}
}
}
Expand Down Expand Up @@ -1334,6 +1359,7 @@ void Connection::BLEC_onServiceDetailDiscovered(QLowEnergyService::ServiceState
// all root services and included services are handled
if(m_BLEDiscoveredServices.isEmpty() && m_BLEController->state() == QLowEnergyController::DiscoveredState)
{
qDebug() << "Cannot find Tx/Rx Characteristic matching the requirement.";
close();
}
}
Expand Down
1 change: 1 addition & 0 deletions src/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ public slots:
bool m_BLERxCharacteristicValid = false;
bool m_BLETxCharacteristicValid = false;
QLowEnergyCharacteristic m_BLETxCharacteristic;
QLowEnergyService::WriteMode m_BLETxWriteMode;
QTcpServer* m_TCPServer = nullptr;
QTcpSocket* m_TCPSocket = nullptr;
QUdpSocket* m_UDPSocket = nullptr;
Expand Down

0 comments on commit 0e5c82e

Please sign in to comment.