From 1bd23bf4e0dcbc076c7df59aa64e047e40c38d62 Mon Sep 17 00:00:00 2001 From: wh201906 Date: Fri, 9 Jun 2023 13:56:53 +0800 Subject: [PATCH] Specify service UUID in BT Client mode Some device doesn't use the SerialPort service UUID(0x1101) --- src/connection.cpp | 3 +-- src/connection.h | 4 +++- src/devicetab.cpp | 27 +++++++++++++++++++++++++++ src/devicetab.h | 1 + src/ui/devicetab.ui | 30 ++++++++++++++++++++++++++++-- 5 files changed, 60 insertions(+), 5 deletions(-) diff --git a/src/connection.cpp b/src/connection.cpp index e6b10e6..4750433 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -250,8 +250,7 @@ void Connection::open() else if(m_type == BT_Client) { changeState(Connecting); - // TODO: add user defined UUID support - m_BTSocket->connectToService(m_currBTArgument.deviceAddress, QBluetoothUuid::SerialPort); + m_BTSocket->connectToService(m_currBTArgument.deviceAddress, m_currBTArgument.RxServiceUUID); } else if(m_type == BT_Server) { diff --git a/src/connection.h b/src/connection.h index a956a67..7ccd9d7 100644 --- a/src/connection.h +++ b/src/connection.h @@ -58,8 +58,10 @@ class Connection : public QObject // for Bluetooth classic server QString serverServiceName; QBluetoothAddress localAdapterAddress; + // for BLE(RxServiceUUID) and Bluetooth classic(serviceUUID) + QBluetoothUuid RxServiceUUID; // for BLE only - QBluetoothUuid RxServiceUUID, RxCharacteristicUUID; + QBluetoothUuid RxCharacteristicUUID; QBluetoothUuid TxServiceUUID, TxCharacteristicUUID; }; diff --git a/src/devicetab.cpp b/src/devicetab.cpp index 6a1f06f..0374025 100644 --- a/src/devicetab.cpp +++ b/src/devicetab.cpp @@ -17,6 +17,7 @@ const QMap DeviceTab::m_historyPrefix = { {QLatin1String("SP"), QLatin1String("SerialTest_History_SerialPort")}, + {QLatin1String("BTClient"), QLatin1String("SerialTest_History_BT_Client")}, {QLatin1String("BTServer"), QLatin1String("SerialTest_History_BT_Server")}, {QLatin1String("BLEC"), QLatin1String("SerialTest_History_BLE_Central")}, {QLatin1String("TCPServer"), QLatin1String("SerialTest_History_TCP_Server")}, @@ -130,8 +131,14 @@ void DeviceTab::initSettings() loadSPPreference(m_SPArgHistory.last()); // TCP client preference(last connected) is loaded in on_typeBox_currentIndexChanged() + // because TCP client/TCP server/UDP share the same widgets // non-arrays + settings->beginGroup(m_historyPrefix["BTClient"]); + ui->BTClient_serviceUUIDBox->setChecked(settings->value("UserSpecifiedServiceUUID", false).toBool()); + ui->BTClient_serviceUUIDEdit->setText(settings->value("ServiceUUID").toString()); + settings->endGroup(); + on_BTClient_serviceUUIDBox_clicked(); settings->beginGroup(m_historyPrefix["BTServer"]); ui->BTServer_serviceNameEdit->setText(settings->value("LastServiceName", "SerialTest_BT").toString()); settings->endGroup(); @@ -617,8 +624,20 @@ void DeviceTab::on_openButton_clicked() Connection::BTArgument arg; arg.localAdapterAddress = QBluetoothAddress(ui->BTClient_adapterBox->currentData().toString()); arg.deviceAddress = QBluetoothAddress(ui->BTClient_targetAddrBox->currentText()); + if(ui->BTClient_serviceUUIDBox->isChecked() && !ui->BTClient_serviceUUIDEdit->text().isEmpty()) + arg.RxServiceUUID = String2UUID(ui->BTClient_serviceUUIDEdit->text()); + else + arg.RxServiceUUID = QBluetoothUuid::SerialPort; m_connection->setArgument(arg); m_connection->open(); + + settings->beginGroup(m_historyPrefix["BTClient"]); + if(arg.RxServiceUUID != QBluetoothUuid::SerialPort) + { + settings->setValue("UserSpecifiedServiceUUID", ui->BTClient_serviceUUIDBox->isChecked()); + settings->setValue("ServiceUUID", arg.RxServiceUUID); + } + settings->endGroup(); } else if(currType == Connection::BT_Server) { @@ -1640,3 +1659,11 @@ DeviceTab::SP_ID::operator bool() const { return (m_vid != 0 || m_pid != 0 || !m_serialNumber.isEmpty()); } + +void DeviceTab::on_BTClient_serviceUUIDBox_clicked() +{ + bool userSpecifiedUUID = ui->BTClient_serviceUUIDBox->isChecked(); + ui->BTClient_serviceUUIDEdit->setVisible(userSpecifiedUUID); + ui->BTClient_tipLabel->setVisible(!userSpecifiedUUID); +} + diff --git a/src/devicetab.h b/src/devicetab.h index 7426766..4e01b83 100644 --- a/src/devicetab.h +++ b/src/devicetab.h @@ -134,6 +134,7 @@ private slots: void on_BTServer_deviceList_cellChanged(int row, int column); void on_Net_addrPortList_cellChanged(int row, int column); void on_BLEC_ServiceUUIDBox_currentTextChanged(const QString &arg1); + void on_BTClient_serviceUUIDBox_clicked(); }; #endif // DEVICETAB_H diff --git a/src/ui/devicetab.ui b/src/ui/devicetab.ui index 808e654..023f76a 100644 --- a/src/ui/devicetab.ui +++ b/src/ui/devicetab.ui @@ -827,6 +827,32 @@ margin-bottom:3em; + + + + Service UUID + + + + + + + + + + + 0 + 0 + + + + If Service UUID is not specified, the SerialPort service UUID(0x1101) will be used. + + + true + + + @@ -926,7 +952,7 @@ margin-bottom:3em; 0 0 267 - 334 + 335 @@ -1086,7 +1112,7 @@ margin-bottom:3em; 0 0 159 - 351 + 352