Bluetooth Chat Example▲
The Bluetooth Chat example shows how to use the Qt Bluetooth API to communicate with another application on a remote device using Bluetooth.
The Bluetooth Chat example implements a simple chat program between multiple parties. The application always acts as both a server and a client eliminating the need to determine who should connect to whom.
Running the Example▲
To run the example from Qt Creator, open the Welcome mode and select the example from Examples. For more information, visit Building and Running an Example.
Chat Server▲
The chat server is implemented by the ChatServer class. The ChatServer class is declared as:
class
ChatServer : public
QObject
{
Q_OBJECT
public
:
explicit
ChatServer(QObject *
parent =
nullptr
);
~
ChatServer();
void
startServer(const
QBluetoothAddress &
amp;localAdapter =
QBluetoothAddress());
void
stopServer();
public
slots:
void
sendMessage(const
QString &
amp;message);
signals
:
void
messageReceived(const
QString &
amp;sender, const
QString &
amp;message);
void
clientConnected(const
QString &
amp;name);
void
clientDisconnected(const
QString &
amp;name);
private
slots:
void
clientConnected();
void
clientDisconnected();
void
readSocket();
private
:
QBluetoothServer *
rfcommServer =
nullptr
;
QBluetoothServiceInfo serviceInfo;
QList&
lt;QBluetoothSocket *&
gt; clientSockets;
QMap&
lt;QBluetoothSocket *
, QString&
gt; clientNames;
}
;
The first thing the chat server needs to do is create an instance of QBluetoothServer to listen for incoming Bluetooth connections. Our clientConnected() slot will be called whenever a new connection is created.
rfcommServer =
new
QBluetoothServer(QBluetoothServiceInfo::
RfcommProtocol, this
);
connect(rfcommServer, &
amp;QBluetoothServer::
newConnection,
this
, QOverload&
lt;&
gt;::
of(&
amp;ChatServer::
clientConnected));
bool
result =
rfcommServer-&
gt;listen(localAdapter);
if
(!
result) {
qWarning() &
lt;&
lt; "Cannot bind chat server to"
&
lt;&
lt; localAdapter.toString();
return
;
}
The chat server is only useful if others know that it is there. To enable other devices to discover it, a record describing the service needs to be published in the systems SDP (Service Discovery Protocol) database. The QBluetoothServiceInfo class encapsulates a service record.
We will publish a service record that contains some textural descriptions of the services, a UUID that uniquely identifies the service, the discoverability attribute, and connection parameters.
The textural description of the service is stored in the ServiceName, ServiceDescription, and ServiceProvider attributes.
serviceInfo.setAttribute(QBluetoothServiceInfo::
ServiceName, tr("Bt Chat Server"
));
serviceInfo.setAttribute(QBluetoothServiceInfo::
ServiceDescription,
tr("Example bluetooth chat server"
));
serviceInfo.setAttribute(QBluetoothServiceInfo::
ServiceProvider, tr("qt-project.org"
));
Bluetooth uses UUIDs as unique identifiers. The chat service uses a randomly generated UUID.
static
const
QLatin1String serviceUuid("e8e10f95-1a70-4b27-9ccf-02010264e9c8"
);
serviceInfo.setServiceUuid(QBluetoothUuid(serviceUuid));
A Bluetooth service is only discoverable if it is in the PublicBrowseGroup.
QBluetoothServiceInfo::
Sequence publicBrowse;
publicBrowse &
lt;&
lt; QVariant::
fromValue(QBluetoothUuid(QBluetoothUuid::ServiceClassUuid::
PublicBrowseGroup));
serviceInfo.setAttribute(QBluetoothServiceInfo::
BrowseGroupList,
publicBrowse);
The ProtocolDescriptorList attribute is used to publish the connection parameters that the remote device requires to connect to our service. Here we specify that the Rfcomm protocol is used and set the port number to the port that our rfcommServer instance is listening to.
QBluetoothServiceInfo::
Sequence protocolDescriptorList;
QBluetoothServiceInfo::
Sequence protocol;
protocol &
lt;&
lt; QVariant::
fromValue(QBluetoothUuid(QBluetoothUuid::ProtocolUuid::
L2cap));
protocolDescriptorList.append(QVariant::
fromValue(protocol));
protocol.clear();
protocol &
lt;&
lt; QVariant::
fromValue(QBluetoothUuid(QBluetoothUuid::ProtocolUuid::
Rfcomm))
&
lt;&
lt; QVariant::
fromValue(quint8(rfcommServer-&
gt;serverPort()));
protocolDescriptorList.append(QVariant::
fromValue(protocol));
serviceInfo.setAttribute(QBluetoothServiceInfo::
ProtocolDescriptorList,
protocolDescriptorList);
Finally, we register the service record with the system.
serviceInfo.registerService(localAdapter);
As mentioned earlier, incoming connections are handled in the clientConnected() slot where pending connections are connected to the readyRead() and disconnected() signals. The signals notify others that a new client has connected.
void
ChatServer::
clientConnected()
{
QBluetoothSocket *
socket =
rfcommServer-&
gt;nextPendingConnection();
if
(!
socket)
return
;
connect(socket, &
amp;QBluetoothSocket::
readyRead, this
, &
amp;ChatServer::
readSocket);
connect(socket, &
amp;QBluetoothSocket::
disconnected, this
, QOverload&
lt;&
gt;::
of(&
amp;ChatServer::
clientDisconnected));
clientSockets.append(socket);
clientNames[socket] =
socket-&
gt;peerName();
emit clientConnected(socket-&
gt;peerName());
}
The readSocket() slot is called whenever data is ready to be read from a client socket. The slot reads individual lines from the socket, converts them from UTF-8, and emits the messageReceived() signal.
void
ChatServer::
readSocket()
{
QBluetoothSocket *
socket =
qobject_cast&
lt;QBluetoothSocket *&
gt;(sender());
if
(!
socket)
return
;
while
(socket-&
gt;canReadLine()) {
QByteArray line =
socket-&
gt;readLine().trimmed();
emit messageReceived(clientNames[socket],
QString::
fromUtf8(line.constData(), line.length()));
}
}
The clientDisconnected() slot is called whenever a client disconnects from the service. The slot emits a signal to notify others that a client has disconnected, and deletes the socket.
void
ChatServer::
clientDisconnected()
{
QBluetoothSocket *
socket =
qobject_cast&
lt;QBluetoothSocket *&
gt;(sender());
if
(!
socket)
return
;
emit clientDisconnected(clientNames[socket]);
clientSockets.removeOne(socket);
clientNames.remove(socket);
socket-&
gt;deleteLater();
}
The sendMessage() slot is used to send a message to all connected clients. The message is converted into UTF-8 and appended with a newline before being sent to all clients.
void
ChatServer::
sendMessage(const
QString &
amp;message)
{
QByteArray text =
message.toUtf8() +
'
\n
'
;
for
(QBluetoothSocket *
socket : std::
as_const(clientSockets))
socket-&
gt;write(text);
}
When the chat server is stopped the service record is removed from the system SDP database, all connected client sockets are deleted, and the QRfcommServer instance is deleted.
void
ChatServer::
stopServer()
{
// Unregister service
serviceInfo.unregisterService();
// Close sockets
qDeleteAll(clientSockets);
clientNames.clear();
// Close server
delete
rfcommServer;
rfcommServer =
nullptr
;
}
Chat Client▲
The chat client is implemented by the ChatClient class. The ChatClient class is declared as:
class
ChatClient : public
QObject
{
Q_OBJECT
public
:
explicit
ChatClient(QObject *
parent =
nullptr
);
~
ChatClient();
void
startClient(const
QBluetoothServiceInfo &
amp;remoteService);
void
stopClient();
public
slots:
void
sendMessage(const
QString &
amp;message);
signals
:
void
messageReceived(const
QString &
amp;sender, const
QString &
amp;message);
void
connected(const
QString &
amp;name);
void
disconnected();
void
socketErrorOccurred(const
QString &
amp;errorString);
private
slots:
void
readSocket();
void
connected();
void
onSocketErrorOccurred(QBluetoothSocket::
SocketError);
private
:
QBluetoothSocket *
socket =
nullptr
;
}
;
The client creates a new QBluetoothSocket and connects to the remote service described by the remoteService parameter. Slots are connected to the sockets readyRead(), connected() and disconnected() signals.
void
ChatClient::
startClient(const
QBluetoothServiceInfo &
amp;remoteService)
{
if
(socket)
return
;
// Connect to service
socket =
new
QBluetoothSocket(QBluetoothServiceInfo::
RfcommProtocol);
qDebug() &
lt;&
lt; "Create socket"
;
socket-&
gt;connectToService(remoteService);
qDebug() &
lt;&
lt; "ConnectToService done"
;
connect(socket, &
amp;QBluetoothSocket::
readyRead, this
, &
amp;ChatClient::
readSocket);
connect(socket, &
amp;QBluetoothSocket::
connected, this
, QOverload&
lt;&
gt;::
of(&
amp;ChatClient::
connected));
connect(socket, &
amp;QBluetoothSocket::
disconnected, this
, &
amp;ChatClient::
disconnected);
connect(socket, &
amp;QBluetoothSocket::
errorOccurred, this
, &
amp;ChatClient::
onSocketErrorOccurred);
}
On successful socket connection we emit a signal to notify others.
void
ChatClient::
connected()
{
emit connected(socket-&
gt;peerName());
}
Similarly to the chat server, the readSocket() slot is called when data is available from the socket. Lines are read individually and converted from UTF-8. The messageReceived() signal is emitted.
void
ChatClient::
readSocket()
{
if
(!
socket)
return
;
while
(socket-&
gt;canReadLine()) {
QByteArray line =
socket-&
gt;readLine().trimmed();
emit messageReceived(socket-&
gt;peerName(),
QString::
fromUtf8(line.constData(), line.length()));
}
}
The sendMessage() slot is used to send a message to the remote device. The message is converted to UTF-8 and a newline is appended.
void
ChatClient::
sendMessage(const
QString &
amp;message)
{
QByteArray text =
message.toUtf8() +
'
\n
'
;
socket-&
gt;write(text);
}
To disconnect from the remote chat service, the QBluetoothSocket instance is deleted.
void
ChatClient::
stopClient()
{
delete
socket;
socket =
nullptr
;
}
Chat Dialog▲
The main window of this example is the chat dialog, implemented in the Chat class. This class displays a chat session between a single ChatServer and zero or more ChatClients. The Chat class is declared as:
class
Chat : public
QDialog
{
Q_OBJECT
public
:
explicit
Chat(QWidget *
parent =
nullptr
);
~
Chat();
signals
:
void
sendMessage(const
QString &
amp;message);
private
slots:
void
connectClicked();
void
sendClicked();
void
showMessage(const
QString &
amp;sender, const
QString &
amp;message);
void
clientConnected(const
QString &
amp;name);
void
clientDisconnected(const
QString &
amp;name);
void
clientDisconnected();
void
connected(const
QString &
amp;name);
void
reactOnSocketError(const
QString &
amp;error);
void
newAdapterSelected();
private
:
int
adapterFromUserSelection() const
;
int
currentAdapterIndex =
0
;
Ui_Chat *
ui;
ChatServer *
server;
QList&
lt;ChatClient *&
gt; clients;
QList&
lt;QBluetoothHostInfo&
gt; localAdapters;
QString localName;
}
;
First we construct the user interface
ui-&
gt;setupUi(this
);
connect(ui-&
gt;quitButton, &
amp;QPushButton::
clicked, this
, &
amp;Chat::
accept);
connect(ui-&
gt;connectButton, &
amp;QPushButton::
clicked, this
, &
amp;Chat::
connectClicked);
connect(ui-&
gt;sendButton, &
amp;QPushButton::
clicked, this
, &
amp;Chat::
sendClicked);
We create an instance of the ChatServer and respond to its clientConnected(), clientDiconnected(), and messageReceived() signals.
server =
new
ChatServer(this
);
connect(server, QOverload&
lt;const
QString &
amp;&
gt;::
of(&
amp;ChatServer::
clientConnected),
this
, &
amp;Chat::
clientConnected);
connect(server, QOverload&
lt;const
QString &
amp;&
gt;::
of(&
amp;ChatServer::
clientDisconnected),
this
, QOverload&
lt;const
QString &
amp;&
gt;::
of(&
amp;Chat::
clientDisconnected));
connect(server, &
amp;ChatServer::
messageReceived,
this
, &
amp;Chat::
showMessage);
connect(this
, &
amp;Chat::
sendMessage, server, &
amp;ChatServer::
sendMessage);
server-&
gt;startServer();
In response to the clientConnected() and clientDisconnected() signals of the ChatServer, we display the typical "X has joined chat." and "Y has left." messages in the chat session.
void
Chat::
clientConnected(const
QString &
amp;name)
{
ui-&
gt;chat-&
gt;insertPlainText(QString::
fromLatin1("%1 has joined chat.
\n
"
).arg(name));
}
void
Chat::
clientDisconnected(const
QString &
amp;name)
{
ui-&
gt;chat-&
gt;insertPlainText(QString::
fromLatin1("%1 has left.
\n
"
).arg(name));
}
Incoming messages from clients connected to the ChatServer are handled in the showMessage() slot. The message text tagged with the remote device name is displayed in the chat session.
void
Chat::
showMessage(const
QString &
amp;sender, const
QString &
amp;message)
{
ui-&
gt;chat-&
gt;insertPlainText(QString::
fromLatin1("%1: %2
\n
"
).arg(sender, message));
ui-&
gt;chat-&
gt;ensureCursorVisible();
}
In response to the connect button being clicked, the application starts service discovery and presents a list of discovered chat services on remote devices. A ChatClient for the service is selected by the user.
void
Chat::
connectClicked()
{
ui-&
gt;connectButton-&
gt;setEnabled(false
);
// scan for services
const
QBluetoothAddress adapter =
localAdapters.isEmpty() ?
QBluetoothAddress() :
localAdapters.at(currentAdapterIndex).address();
RemoteSelector remoteSelector(adapter);
#ifdef Q_OS_ANDROID
// QTBUG-61392
Q_UNUSED(serviceUuid);
remoteSelector.startDiscovery(QBluetoothUuid(reverseUuid));
#else
remoteSelector.startDiscovery(QBluetoothUuid(serviceUuid));
#endif
if
(remoteSelector.exec() ==
QDialog::
Accepted) {
QBluetoothServiceInfo service =
remoteSelector.service();
qDebug() &
lt;&
lt; "Connecting to service 2"
&
lt;&
lt; service.serviceName()
&
lt;&
lt; "on"
&
lt;&
lt; service.device().name();
// Create client
qDebug() &
lt;&
lt; "Going to create client"
;
ChatClient *
client =
new
ChatClient(this
);
qDebug() &
lt;&
lt; "Connecting..."
;
connect(client, &
amp;ChatClient::
messageReceived,
this
, &
amp;Chat::
showMessage);
connect(client, &
amp;ChatClient::
disconnected,
this
, QOverload&
lt;&
gt;::
of(&
amp;Chat::
clientDisconnected));
connect(client, QOverload&
lt;const
QString &
amp;&
gt;::
of(&
amp;ChatClient::
connected),
this
, &
amp;Chat::
connected);
connect(client, &
amp;ChatClient::
socketErrorOccurred,
this
, &
amp;Chat::
reactOnSocketError);
connect(this
, &
amp;Chat::
sendMessage, client, &
amp;ChatClient::
sendMessage);
qDebug() &
lt;&
lt; "Start client"
;
client-&
gt;startClient(service);
clients.append(client);
}
ui-&
gt;connectButton-&
gt;setEnabled(true
);
}
In reponse to the connected() signals from ChatClient, we display the a "Joined chat with X." message in the chat session.
void
Chat::
connected(const
QString &
amp;name)
{
ui-&
gt;chat-&
gt;insertPlainText(QString::
fromLatin1("Joined chat with %1.
\n
"
).arg(name));
}
Messages are sent to all remote devices via the ChatServer and ChatClient instances by emitting the sendMessage() signal.
void
Chat::
sendClicked()
{
ui-&
gt;sendButton-&
gt;setEnabled(false
);
ui-&
gt;sendText-&
gt;setEnabled(false
);
showMessage(localName, ui-&
gt;sendText-&
gt;text());
emit sendMessage(ui-&
gt;sendText-&
gt;text());
ui-&
gt;sendText-&
gt;clear();
ui-&
gt;sendText-&
gt;setEnabled(true
);
ui-&
gt;sendButton-&
gt;setEnabled(true
);
ui-&
gt;sendText-&
gt;setFocus();
}
We need to clean up ChatClient instances when the remote device forces a disconnect.
void
Chat::
clientDisconnected()
{
ChatClient *
client =
qobject_cast&
lt;ChatClient *&
gt;(sender());
if
(client) {
clients.removeOne(client);
client-&
gt;deleteLater();
}
}