I am writing C++ application for Bluetooth client, which should be able to discover, pair and connect with another Bluetooth device, having specific MAC address, using Bluetooth Classic (not BLE). The main question is, how to create connection? The discovering and pairing processes are well-described in the Qt Bluetooth Overview and work as expected:
// To switch bluetooth on, code inside MyClass constructor
QBluetoothLocalDevice localDevice;
// Check if Bluetooth is available on this device
if (localDevice.isValid())
{
// Turn Bluetooth on
localDevice.powerOn();
// Other code
}
void MyClass::startDeviceDiscovery()
{
// Create a discovery agent and connect to its signals
QBluetoothDeviceDiscoveryAgent *discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
connect(discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)),
this, SLOT(deviceDiscovered(QBluetoothDeviceInfo)));
// Start a discovery
discoveryAgent->start();
// Other code
}
// In MyClass local slot, read information about the found devices
void MyClass::deviceDiscovered(const QBluetoothDeviceInfo &device)
{
qDebug() << "Found new device:" << device.name() << '(' << device.address().toString() << ')';
}
I have also examined this answer, which contains complete implementation of code for switching on Bluetooth, discovering and pairing. However, after pairing is done, I can't find a way to implement connection with discovered and paired Bluetooth device. The Bluetooth Chat example from Qt documentation provides some kind of solution of this task, but several question arise from there. This example proposes to create the object
RemoteSelector remoteSelector(adapter);
where RemoteSelector
is the class, provided by Qt Mobility Components. Instance of this class will allow to create specific service
QBluetoothServiceInfo service = remoteSelector.service();
and then this service can be used to establish connection with remote device:
client->startClient(service);
The example also provides piece of code
m_discoveryAgent = new QBluetoothServiceDiscoveryAgent(localAdapter);
connect(m_discoveryAgent, &QBluetoothServiceDiscoveryAgent::serviceDiscovered,
this, &RemoteSelector::serviceDiscovered);
connect(m_discoveryAgent, &QBluetoothServiceDiscoveryAgent::finished,
this, &RemoteSelector::discoveryFinished);
connect(m_discoveryAgent, &QBluetoothServiceDiscoveryAgent::canceled,
this, &RemoteSelector::discoveryFinished);
Which is similar on code inside implementation of RemoteSelector
class, but not exactly the same.
So, several question arise from here:
Do I understand right, that to be able to implement bluetooth connection with paired device in Qt 6.5, it's necessary to install separate component, called Qt Mobility Components, and use
RemoteSelector
class from there?Why does Chat Bluetooth example provide part of code from implementation of
RemoteSelector
class constructor, partially different from this implementation, and is there any need to modify implementation ofRemoteSelector
based on the provided code?Is the connection way, described in Bluetooth Chat example, the best way to implement Bluetooth connection in Qt, in the sense of good coding practise, writing clean code, good code style and organization?
Generally, the described way of connection seems strange because while Qt Bluetooth provides straighforward way to switch on Bluetooth, to discover and pair devices, the connection process seems to be much more complex, it requires additional actions and additional component, Qt Mobility Components. Is there any explanation of this?
Note that I have no graphical interface in my program and don't use QT Creator, since the task is simply to write C++ application for Bluetooth connection, using Qt library.