Skip to content

Commit

Permalink
Specify service UUID in BT Client mode
Browse files Browse the repository at this point in the history
Some device doesn't use the SerialPort service UUID(0x1101)
  • Loading branch information
wh201906 committed Jun 9, 2023
1 parent 5f304cf commit 1bd23bf
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
3 changes: 1 addition & 2 deletions src/connection.cpp
Expand Up @@ -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)
{
Expand Down
4 changes: 3 additions & 1 deletion src/connection.h
Expand Up @@ -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;
};

Expand Down
27 changes: 27 additions & 0 deletions src/devicetab.cpp
Expand Up @@ -17,6 +17,7 @@
const QMap<QString, QString> 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")},
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}

1 change: 1 addition & 0 deletions src/devicetab.h
Expand Up @@ -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
30 changes: 28 additions & 2 deletions src/ui/devicetab.ui
Expand Up @@ -827,6 +827,32 @@ margin-bottom:3em;
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="BTClient_serviceUUIDBox">
<property name="text">
<string>Service UUID</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="BTClient_serviceUUIDEdit"/>
</item>
<item>
<widget class="QLabel" name="BTClient_tipLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Ignored" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>If Service UUID is not specified, the SerialPort service UUID(0x1101) will be used.</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
Expand Down Expand Up @@ -926,7 +952,7 @@ margin-bottom:3em;
<x>0</x>
<y>0</y>
<width>267</width>
<height>334</height>
<height>335</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_15">
Expand Down Expand Up @@ -1086,7 +1112,7 @@ margin-bottom:3em;
<x>0</x>
<y>0</y>
<width>159</width>
<height>351</height>
<height>352</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_16">
Expand Down

0 comments on commit 1bd23bf

Please sign in to comment.