Bluetooth QML Ping Pong example▲
The Bluetooth QML Ping Pong example presents the socket communication between two Bluetooth devices. The basic concept is the ping pong game where two players communicate via sockets.
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.
At the beginning, the user selects the role. One device acts as a server and the second one as a client. After selecting the role, adjustments to the screen size are done (two devices might have different screen sizes). The server side starts a service named "PingPong server".
m_serverInfo =
new
QBluetoothServer(QBluetoothServiceInfo::
RfcommProtocol, this
);
connect(m_serverInfo, &
amp;QBluetoothServer::
newConnection,
this
, &
amp;PingPong::
clientConnected);
connect(m_serverInfo, QOverload&
lt;QBluetoothServer::
Error&
gt;::
of(&
amp;QBluetoothServer::
error),
this
, &
amp;PingPong::
serverError);
const
QBluetoothUuid uuid(serviceUuid);
m_serverInfo-&
gt;listen(uuid, QStringLiteral("PingPong server"
));
On the client side, the full service discovery on the nearby Bluetooth devices is done.
discoveryAgent =
new
QBluetoothServiceDiscoveryAgent(QBluetoothAddress());
connect(discoveryAgent, &
amp;QBluetoothServiceDiscoveryAgent::
serviceDiscovered,
this
, &
amp;PingPong::
addService);
connect(discoveryAgent, &
amp;QBluetoothServiceDiscoveryAgent::
finished,
this
, &
amp;PingPong::
done);
connect(discoveryAgent, QOverload&
lt;QBluetoothServiceDiscoveryAgent::
Error&
gt;::
of(&
amp;QBluetoothServiceDiscoveryAgent::
error),
this
, &
amp;PingPong::
serviceScanError);
#ifdef Q_OS_ANDROID
//see QTBUG-61392
if
(QtAndroid::
androidSdkVersion() &
gt;=
23
)
discoveryAgent-&
gt;setUuidFilter(QBluetoothUuid(androidUuid));
else
discoveryAgent-&
gt;setUuidFilter(QBluetoothUuid(serviceUuid));
#else
discoveryAgent-&
gt;setUuidFilter(QBluetoothUuid(serviceUuid));
#endif
discoveryAgent-&
gt;start(QBluetoothServiceDiscoveryAgent::
FullDiscovery);
When the ping pong service is discovered, the client connects to the server using the socket.
socket =
new
QBluetoothSocket(QBluetoothServiceInfo::
RfcommProtocol);
socket-&
gt;connectToService(service);
connect(socket, &
amp;QBluetoothSocket::
readyRead, this
, &
amp;PingPong::
readSocket);
connect(socket, &
amp;QBluetoothSocket::
connected, this
, &
amp;PingPong::
serverConnected);
connect(socket, &
amp;QBluetoothSocket::
disconnected, this
, &
amp;PingPong::
serverDisconnected);
On the server side, the connected signal is emitted initiating that the client is connected. The necessary signals and slots on the server side are connected.
if
(!
m_serverInfo-&
gt;hasPendingConnections()) {
setMessage("FAIL: expected pending server connection"
);
return
;
}
socket =
m_serverInfo-&
gt;nextPendingConnection();
if
(!
socket)
return
;
socket-&
gt;setParent(this
);
connect(socket, &
amp;QBluetoothSocket::
readyRead,
this
, &
amp;PingPong::
readSocket);
connect(socket,