/usr/share/qt5/doc/qtbluetooth/qtbluetooth-btchat-example.html is in qtconnectivity5-doc-html 5.7.1~20161021-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- btchat.qdoc -->
<title>Bluetooth Chat Example | Qt Bluetooth 5.7</title>
<link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
<script type="text/javascript">
window.onload = function(){document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");};
</script>
</head>
<body>
<div class="header" id="qtdocheader">
<div class="main">
<div class="main-rounded">
<div class="navigationbar">
<table><tr>
<td >Qt 5.7</td><td ><a href="qtbluetooth-index.html">Qt Bluetooth</a></td><td >Bluetooth Chat Example</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.7.1 Reference Documentation</td>
</tr></table>
</div>
</div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#running-the-example">Running the Example</a></li>
<li class="level1"><a href="#chat-server">Chat Server</a></li>
<li class="level1"><a href="#chat-client">Chat Client</a></li>
<li class="level1"><a href="#chat-dialog">Chat Dialog</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Bluetooth Chat Example</h1>
<span class="subtitle"></span>
<!-- $$$btchat-description -->
<div class="descr"> <a name="details"></a>
<p>The Bluetooth Chat example shows how to use the <a href="qtbluetooth-index.html">Qt Bluetooth</a> API to communicate with another application on a remote device using Bluetooth.</p>
<p class="centerAlign"><img src="images/btchat-example.png" alt="" /></p><p>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.</p>
<a name="running-the-example"></a>
<h2 id="running-the-example">Running the Example</h2>
<p>To run the example from Qt Creator, open the <b>Welcome</b> mode and select the example from <b>Examples</b>. For more information, visit Building and Running an Example.</p>
<a name="chat-server"></a>
<h2 id="chat-server">Chat Server</h2>
<p>The chat server is implemented by the ChatServer class. The ChatServer class is declared as:</p>
<pre class="cpp">
<span class="keyword">class</span> ChatServer : <span class="keyword">public</span> <span class="type">QObject</span>
{
Q_OBJECT
<span class="keyword">public</span>:
<span class="keyword">explicit</span> ChatServer(<span class="type">QObject</span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);
<span class="operator">~</span>ChatServer();
<span class="type">void</span> startServer(<span class="keyword">const</span> <span class="type">QBluetoothAddress</span> <span class="operator">&</span>localAdapter <span class="operator">=</span> <span class="type">QBluetoothAddress</span>());
<span class="type">void</span> stopServer();
<span class="keyword">public</span> <span class="keyword">slots</span>:
<span class="type">void</span> sendMessage(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>message);
<span class="keyword">signals</span>:
<span class="type">void</span> messageReceived(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>sender<span class="operator">,</span> <span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>message);
<span class="type">void</span> clientConnected(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>name);
<span class="type">void</span> clientDisconnected(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>name);
<span class="keyword">private</span> <span class="keyword">slots</span>:
<span class="type">void</span> clientConnected();
<span class="type">void</span> clientDisconnected();
<span class="type">void</span> readSocket();
<span class="keyword">private</span>:
<span class="type">QBluetoothServer</span> <span class="operator">*</span>rfcommServer;
<span class="type">QBluetoothServiceInfo</span> serviceInfo;
<span class="type">QList</span><span class="operator"><</span><span class="type">QBluetoothSocket</span> <span class="operator">*</span><span class="operator">></span> clientSockets;
};
</pre>
<p>The first thing the chat server needs to do is create an instance of QRfcommServer to listen for incoming Bluetooth connections. Our clientConnected() slot will be called whenever a new connection is created.</p>
<pre class="cpp">
rfcommServer <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QBluetoothServer</span>(<span class="type">QBluetoothServiceInfo</span><span class="operator">::</span>RfcommProtocol<span class="operator">,</span> <span class="keyword">this</span>);
connect(rfcommServer<span class="operator">,</span> SIGNAL(newConnection())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(clientConnected()));
bool result <span class="operator">=</span> rfcommServer<span class="operator">-</span><span class="operator">></span>listen(localAdapter);
<span class="keyword">if</span> (<span class="operator">!</span>result) {
<a href="../qtcore/qtglobal.html#qWarning">qWarning</a>() <span class="operator"><</span><span class="operator"><</span> <span class="string">"Cannot bind chat server to"</span> <span class="operator"><</span><span class="operator"><</span> localAdapter<span class="operator">.</span>toString();
<span class="keyword">return</span>;
}
</pre>
<p>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 <a href="qbluetoothserviceinfo.html">QBluetoothServiceInfo</a> class encapsulates a service record.</p>
<p>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.</p>
<p>The textural description of the service is stored in the ServiceName, ServiceDescription, and ServiceProvider attributes.</p>
<pre class="cpp">
serviceInfo<span class="operator">.</span>setAttribute(<span class="type">QBluetoothServiceInfo</span><span class="operator">::</span>ServiceName<span class="operator">,</span> tr(<span class="string">"Bt Chat Server"</span>));
serviceInfo<span class="operator">.</span>setAttribute(<span class="type">QBluetoothServiceInfo</span><span class="operator">::</span>ServiceDescription<span class="operator">,</span>
tr(<span class="string">"Example bluetooth chat server"</span>));
serviceInfo<span class="operator">.</span>setAttribute(<span class="type">QBluetoothServiceInfo</span><span class="operator">::</span>ServiceProvider<span class="operator">,</span> tr(<span class="string">"qt-project.org"</span>));
</pre>
<p>Bluetooth uses UUIDs as unique identifiers. The chat service uses a randomly generated UUID.</p>
<pre class="cpp">
<span class="keyword">static</span> <span class="keyword">const</span> QLatin1String serviceUuid(<span class="string">"e8e10f95-1a70-4b27-9ccf-02010264e9c8"</span>);
serviceInfo<span class="operator">.</span>setServiceUuid(<span class="type">QBluetoothUuid</span>(serviceUuid));
</pre>
<p>A Bluetooth service is only discoverable if it is in the PublicBrowseGroup.</p>
<pre class="cpp">
<span class="type">QBluetoothServiceInfo</span><span class="operator">::</span>Sequence publicBrowse;
publicBrowse <span class="operator"><</span><span class="operator"><</span> <span class="type">QVariant</span><span class="operator">::</span>fromValue(<span class="type">QBluetoothUuid</span>(<span class="type">QBluetoothUuid</span><span class="operator">::</span>PublicBrowseGroup));
serviceInfo<span class="operator">.</span>setAttribute(<span class="type">QBluetoothServiceInfo</span><span class="operator">::</span>BrowseGroupList<span class="operator">,</span>
publicBrowse);
</pre>
<p>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.</p>
<pre class="cpp">
<span class="type">QBluetoothServiceInfo</span><span class="operator">::</span>Sequence protocolDescriptorList;
<span class="type">QBluetoothServiceInfo</span><span class="operator">::</span>Sequence protocol;
protocol <span class="operator"><</span><span class="operator"><</span> <span class="type">QVariant</span><span class="operator">::</span>fromValue(<span class="type">QBluetoothUuid</span>(<span class="type">QBluetoothUuid</span><span class="operator">::</span>L2cap));
protocolDescriptorList<span class="operator">.</span>append(<span class="type">QVariant</span><span class="operator">::</span>fromValue(protocol));
protocol<span class="operator">.</span>clear();
protocol <span class="operator"><</span><span class="operator"><</span> <span class="type">QVariant</span><span class="operator">::</span>fromValue(<span class="type">QBluetoothUuid</span>(<span class="type">QBluetoothUuid</span><span class="operator">::</span>Rfcomm))
<span class="operator"><</span><span class="operator"><</span> <span class="type">QVariant</span><span class="operator">::</span>fromValue(<span class="type">quint8</span>(rfcommServer<span class="operator">-</span><span class="operator">></span>serverPort()));
protocolDescriptorList<span class="operator">.</span>append(<span class="type">QVariant</span><span class="operator">::</span>fromValue(protocol));
serviceInfo<span class="operator">.</span>setAttribute(<span class="type">QBluetoothServiceInfo</span><span class="operator">::</span>ProtocolDescriptorList<span class="operator">,</span>
protocolDescriptorList);
</pre>
<p>Finally, we register the service record with the system.</p>
<pre class="cpp">
serviceInfo<span class="operator">.</span>registerService(localAdapter);
</pre>
<p>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.</p>
<pre class="cpp">
<span class="type">void</span> ChatServer<span class="operator">::</span>clientConnected()
{
<span class="type">QBluetoothSocket</span> <span class="operator">*</span>socket <span class="operator">=</span> rfcommServer<span class="operator">-</span><span class="operator">></span>nextPendingConnection();
<span class="keyword">if</span> (<span class="operator">!</span>socket)
<span class="keyword">return</span>;
connect(socket<span class="operator">,</span> SIGNAL(readyRead())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(readSocket()));
connect(socket<span class="operator">,</span> SIGNAL(disconnected())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(clientDisconnected()));
clientSockets<span class="operator">.</span>append(socket);
<span class="keyword">emit</span> clientConnected(socket<span class="operator">-</span><span class="operator">></span>peerName());
}
</pre>
<p>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.</p>
<pre class="cpp">
<span class="type">void</span> ChatServer<span class="operator">::</span>readSocket()
{
<span class="type">QBluetoothSocket</span> <span class="operator">*</span>socket <span class="operator">=</span> qobject_cast<span class="operator"><</span><span class="type">QBluetoothSocket</span> <span class="operator">*</span><span class="operator">></span>(sender());
<span class="keyword">if</span> (<span class="operator">!</span>socket)
<span class="keyword">return</span>;
<span class="keyword">while</span> (socket<span class="operator">-</span><span class="operator">></span>canReadLine()) {
<span class="type">QByteArray</span> line <span class="operator">=</span> socket<span class="operator">-</span><span class="operator">></span>readLine()<span class="operator">.</span>trimmed();
<span class="keyword">emit</span> messageReceived(socket<span class="operator">-</span><span class="operator">></span>peerName()<span class="operator">,</span>
<span class="type">QString</span><span class="operator">::</span>fromUtf8(line<span class="operator">.</span>constData()<span class="operator">,</span> line<span class="operator">.</span>length()));
}
}
</pre>
<p>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.</p>
<pre class="cpp">
<span class="type">void</span> ChatServer<span class="operator">::</span>clientDisconnected()
{
<span class="type">QBluetoothSocket</span> <span class="operator">*</span>socket <span class="operator">=</span> qobject_cast<span class="operator"><</span><span class="type">QBluetoothSocket</span> <span class="operator">*</span><span class="operator">></span>(sender());
<span class="keyword">if</span> (<span class="operator">!</span>socket)
<span class="keyword">return</span>;
<span class="keyword">emit</span> clientDisconnected(socket<span class="operator">-</span><span class="operator">></span>peerName());
clientSockets<span class="operator">.</span>removeOne(socket);
socket<span class="operator">-</span><span class="operator">></span>deleteLater();
}
</pre>
<p>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.</p>
<pre class="cpp">
<span class="type">void</span> ChatServer<span class="operator">::</span>sendMessage(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>message)
{
<span class="type">QByteArray</span> text <span class="operator">=</span> message<span class="operator">.</span>toUtf8() <span class="operator">+</span> <span class="char">'\n'</span>;
foreach (<span class="type">QBluetoothSocket</span> <span class="operator">*</span>socket<span class="operator">,</span> clientSockets)
socket<span class="operator">-</span><span class="operator">></span>write(text);
}
</pre>
<p>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.</p>
<pre class="cpp">
<span class="type">void</span> ChatServer<span class="operator">::</span>stopServer()
{
<span class="comment">// Unregister service</span>
serviceInfo<span class="operator">.</span>unregisterService();
<span class="comment">// Close sockets</span>
<a href="../qtcore/qtalgorithms.html#qDeleteAll">qDeleteAll</a>(clientSockets);
<span class="comment">// Close server</span>
<span class="keyword">delete</span> rfcommServer;
rfcommServer <span class="operator">=</span> <span class="number">0</span>;
}
</pre>
<a name="chat-client"></a>
<h2 id="chat-client">Chat Client</h2>
<p>The chat client is implemented by the ChatClient class. The ChatClient class is declared as:</p>
<pre class="cpp">
<span class="keyword">class</span> ChatClient : <span class="keyword">public</span> <span class="type">QObject</span>
{
Q_OBJECT
<span class="keyword">public</span>:
<span class="keyword">explicit</span> ChatClient(<span class="type">QObject</span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);
<span class="operator">~</span>ChatClient();
<span class="type">void</span> startClient(<span class="keyword">const</span> <span class="type">QBluetoothServiceInfo</span> <span class="operator">&</span>remoteService);
<span class="type">void</span> stopClient();
<span class="keyword">public</span> <span class="keyword">slots</span>:
<span class="type">void</span> sendMessage(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>message);
<span class="keyword">signals</span>:
<span class="type">void</span> messageReceived(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>sender<span class="operator">,</span> <span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>message);
<span class="type">void</span> connected(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>name);
<span class="type">void</span> disconnected();
<span class="keyword">private</span> <span class="keyword">slots</span>:
<span class="type">void</span> readSocket();
<span class="type">void</span> connected();
<span class="keyword">private</span>:
<span class="type">QBluetoothSocket</span> <span class="operator">*</span>socket;
};
</pre>
<p>The client creates a new <a href="qbluetoothsocket.html">QBluetoothSocket</a> and connects to the remote service described by the <i>remoteService</i> parameter. Slots are connected to the sockets readyRead(), connected() and disconnected() signals.</p>
<pre class="cpp">
<span class="type">void</span> ChatClient<span class="operator">::</span>startClient(<span class="keyword">const</span> <span class="type">QBluetoothServiceInfo</span> <span class="operator">&</span>remoteService)
{
<span class="keyword">if</span> (socket)
<span class="keyword">return</span>;
<span class="comment">// Connect to service</span>
socket <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QBluetoothSocket</span>(<span class="type">QBluetoothServiceInfo</span><span class="operator">::</span>RfcommProtocol);
<a href="../qtcore/qtglobal.html#qDebug">qDebug</a>() <span class="operator"><</span><span class="operator"><</span> <span class="string">"Create socket"</span>;
socket<span class="operator">-</span><span class="operator">></span>connectToService(remoteService);
<a href="../qtcore/qtglobal.html#qDebug">qDebug</a>() <span class="operator"><</span><span class="operator"><</span> <span class="string">"ConnectToService done"</span>;
connect(socket<span class="operator">,</span> SIGNAL(readyRead())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(readSocket()));
connect(socket<span class="operator">,</span> SIGNAL(connected())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(connected()));
connect(socket<span class="operator">,</span> SIGNAL(disconnected())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SIGNAL(disconnected()));
}
</pre>
<p>On successful socket connection we emit a signal to notify others.</p>
<pre class="cpp">
<span class="type">void</span> ChatClient<span class="operator">::</span>connected()
{
<span class="keyword">emit</span> connected(socket<span class="operator">-</span><span class="operator">></span>peerName());
}
</pre>
<p>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.</p>
<pre class="cpp">
<span class="type">void</span> ChatClient<span class="operator">::</span>readSocket()
{
<span class="keyword">if</span> (<span class="operator">!</span>socket)
<span class="keyword">return</span>;
<span class="keyword">while</span> (socket<span class="operator">-</span><span class="operator">></span>canReadLine()) {
<span class="type">QByteArray</span> line <span class="operator">=</span> socket<span class="operator">-</span><span class="operator">></span>readLine();
<span class="keyword">emit</span> messageReceived(socket<span class="operator">-</span><span class="operator">></span>peerName()<span class="operator">,</span>
<span class="type">QString</span><span class="operator">::</span>fromUtf8(line<span class="operator">.</span>constData()<span class="operator">,</span> line<span class="operator">.</span>length()));
}
}
</pre>
<p>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.</p>
<pre class="cpp">
<span class="type">void</span> ChatClient<span class="operator">::</span>sendMessage(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>message)
{
<span class="type">QByteArray</span> text <span class="operator">=</span> message<span class="operator">.</span>toUtf8() <span class="operator">+</span> <span class="char">'\n'</span>;
socket<span class="operator">-</span><span class="operator">></span>write(text);
}
</pre>
<p>To disconnect from the remote chat service, the <a href="qbluetoothsocket.html">QBluetoothSocket</a> instance is deleted.</p>
<pre class="cpp">
<span class="type">void</span> ChatClient<span class="operator">::</span>stopClient()
{
<span class="keyword">delete</span> socket;
socket <span class="operator">=</span> <span class="number">0</span>;
}
</pre>
<a name="chat-dialog"></a>
<h2 id="chat-dialog">Chat Dialog</h2>
<p>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:</p>
<pre class="cpp">
<span class="keyword">class</span> Chat : <span class="keyword">public</span> <span class="type">QDialog</span>
{
Q_OBJECT
<span class="keyword">public</span>:
Chat(<span class="type">QWidget</span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);
<span class="operator">~</span>Chat();
<span class="keyword">signals</span>:
<span class="type">void</span> sendMessage(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>message);
<span class="keyword">private</span> <span class="keyword">slots</span>:
<span class="type">void</span> connectClicked();
<span class="type">void</span> sendClicked();
<span class="type">void</span> showMessage(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>sender<span class="operator">,</span> <span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>message);
<span class="type">void</span> clientConnected(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>name);
<span class="type">void</span> clientDisconnected(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>name);
<span class="type">void</span> clientDisconnected();
<span class="type">void</span> connected(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>name);
<span class="type">void</span> newAdapterSelected();
<span class="keyword">private</span>:
<span class="type">int</span> adapterFromUserSelection() <span class="keyword">const</span>;
<span class="type">int</span> currentAdapterIndex;
Ui_Chat <span class="operator">*</span>ui;
ChatServer <span class="operator">*</span>server;
<span class="type">QList</span><span class="operator"><</span>ChatClient <span class="operator">*</span><span class="operator">></span> clients;
<span class="type">QList</span><span class="operator"><</span><span class="type">QBluetoothHostInfo</span><span class="operator">></span> localAdapters;
<span class="type">QString</span> localName;
};
</pre>
<p>First we construct the user interface</p>
<pre class="cpp">
ui<span class="operator">-</span><span class="operator">></span>setupUi(<span class="keyword">this</span>);
connect(ui<span class="operator">-</span><span class="operator">></span>quitButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(accept()));
connect(ui<span class="operator">-</span><span class="operator">></span>connectButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(connectClicked()));
connect(ui<span class="operator">-</span><span class="operator">></span>sendButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(sendClicked()));
</pre>
<p>We create an instance of the ChatServer and respond to its clientConnected(), clientDiconnected(), and messageReceived() signals.</p>
<pre class="cpp">
server <span class="operator">=</span> <span class="keyword">new</span> ChatServer(<span class="keyword">this</span>);
connect(server<span class="operator">,</span> SIGNAL(clientConnected(<span class="type">QString</span>))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(clientConnected(<span class="type">QString</span>)));
connect(server<span class="operator">,</span> SIGNAL(clientDisconnected(<span class="type">QString</span>))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(clientDisconnected(<span class="type">QString</span>)));
connect(server<span class="operator">,</span> SIGNAL(messageReceived(<span class="type">QString</span><span class="operator">,</span><span class="type">QString</span>))<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> SLOT(showMessage(<span class="type">QString</span><span class="operator">,</span><span class="type">QString</span>)));
connect(<span class="keyword">this</span><span class="operator">,</span> SIGNAL(sendMessage(<span class="type">QString</span>))<span class="operator">,</span> server<span class="operator">,</span> SLOT(sendMessage(<span class="type">QString</span>)));
server<span class="operator">-</span><span class="operator">></span>startServer();
</pre>
<p>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.</p>
<pre class="cpp">
<span class="type">void</span> Chat<span class="operator">::</span>clientConnected(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>name)
{
ui<span class="operator">-</span><span class="operator">></span>chat<span class="operator">-</span><span class="operator">></span>insertPlainText(<span class="type">QString</span><span class="operator">::</span>fromLatin1(<span class="string">"%1 has joined chat.\n"</span>)<span class="operator">.</span>arg(name));
}
<span class="type">void</span> Chat<span class="operator">::</span>clientDisconnected(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>name)
{
ui<span class="operator">-</span><span class="operator">></span>chat<span class="operator">-</span><span class="operator">></span>insertPlainText(<span class="type">QString</span><span class="operator">::</span>fromLatin1(<span class="string">"%1 has left.\n"</span>)<span class="operator">.</span>arg(name));
}
</pre>
<p>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.</p>
<pre class="cpp">
<span class="type">void</span> Chat<span class="operator">::</span>showMessage(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>sender<span class="operator">,</span> <span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>message)
{
ui<span class="operator">-</span><span class="operator">></span>chat<span class="operator">-</span><span class="operator">></span>insertPlainText(<span class="type">QString</span><span class="operator">::</span>fromLatin1(<span class="string">"%1: %2\n"</span>)<span class="operator">.</span>arg(sender<span class="operator">,</span> message));
ui<span class="operator">-</span><span class="operator">></span>chat<span class="operator">-</span><span class="operator">></span>ensureCursorVisible();
}
</pre>
<p>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.</p>
<pre class="cpp">
<span class="type">void</span> Chat<span class="operator">::</span>connectClicked()
{
ui<span class="operator">-</span><span class="operator">></span>connectButton<span class="operator">-</span><span class="operator">></span>setEnabled(<span class="keyword">false</span>);
<span class="comment">// scan for services</span>
<span class="keyword">const</span> <span class="type">QBluetoothAddress</span> adapter <span class="operator">=</span> localAdapters<span class="operator">.</span>isEmpty() <span class="operator">?</span>
<span class="type">QBluetoothAddress</span>() :
localAdapters<span class="operator">.</span>at(currentAdapterIndex)<span class="operator">.</span>address();
RemoteSelector remoteSelector(adapter);
remoteSelector<span class="operator">.</span>startDiscovery(<span class="type">QBluetoothUuid</span>(serviceUuid));
<span class="keyword">if</span> (remoteSelector<span class="operator">.</span>exec() <span class="operator">=</span><span class="operator">=</span> <span class="type">QDialog</span><span class="operator">::</span>Accepted) {
<span class="type">QBluetoothServiceInfo</span> service <span class="operator">=</span> remoteSelector<span class="operator">.</span>service();
<a href="../qtcore/qtglobal.html#qDebug">qDebug</a>() <span class="operator"><</span><span class="operator"><</span> <span class="string">"Connecting to service 2"</span> <span class="operator"><</span><span class="operator"><</span> service<span class="operator">.</span>serviceName()
<span class="operator"><</span><span class="operator"><</span> <span class="string">"on"</span> <span class="operator"><</span><span class="operator"><</span> service<span class="operator">.</span>device()<span class="operator">.</span>name();
<span class="comment">// Create client</span>
<a href="../qtcore/qtglobal.html#qDebug">qDebug</a>() <span class="operator"><</span><span class="operator"><</span> <span class="string">"Going to create client"</span>;
ChatClient <span class="operator">*</span>client <span class="operator">=</span> <span class="keyword">new</span> ChatClient(<span class="keyword">this</span>);
<a href="../qtcore/qtglobal.html#qDebug">qDebug</a>() <span class="operator"><</span><span class="operator"><</span> <span class="string">"Connecting..."</span>;
connect(client<span class="operator">,</span> SIGNAL(messageReceived(<span class="type">QString</span><span class="operator">,</span><span class="type">QString</span>))<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> SLOT(showMessage(<span class="type">QString</span><span class="operator">,</span><span class="type">QString</span>)));
connect(client<span class="operator">,</span> SIGNAL(disconnected())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(clientDisconnected()));
connect(client<span class="operator">,</span> SIGNAL(connected(<span class="type">QString</span>))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(connected(<span class="type">QString</span>)));
connect(<span class="keyword">this</span><span class="operator">,</span> SIGNAL(sendMessage(<span class="type">QString</span>))<span class="operator">,</span> client<span class="operator">,</span> SLOT(sendMessage(<span class="type">QString</span>)));
<a href="../qtcore/qtglobal.html#qDebug">qDebug</a>() <span class="operator"><</span><span class="operator"><</span> <span class="string">"Start client"</span>;
client<span class="operator">-</span><span class="operator">></span>startClient(service);
clients<span class="operator">.</span>append(client);
}
ui<span class="operator">-</span><span class="operator">></span>connectButton<span class="operator">-</span><span class="operator">></span>setEnabled(<span class="keyword">true</span>);
}
</pre>
<p>In reponse to the connected() signals from ChatClient, we display the a "Joined chat with X." message in the chat session.</p>
<pre class="cpp">
<span class="type">void</span> Chat<span class="operator">::</span>connected(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>name)
{
ui<span class="operator">-</span><span class="operator">></span>chat<span class="operator">-</span><span class="operator">></span>insertPlainText(<span class="type">QString</span><span class="operator">::</span>fromLatin1(<span class="string">"Joined chat with %1.\n"</span>)<span class="operator">.</span>arg(name));
}
<span class="type">void</span> Chat<span class="operator">::</span>newAdapterSelected()
{
<span class="keyword">const</span> <span class="type">int</span> newAdapterIndex <span class="operator">=</span> adapterFromUserSelection();
<span class="keyword">if</span> (currentAdapterIndex <span class="operator">!</span><span class="operator">=</span> newAdapterIndex) {
server<span class="operator">-</span><span class="operator">></span>stopServer();
currentAdapterIndex <span class="operator">=</span> newAdapterIndex;
<span class="keyword">const</span> <span class="type">QBluetoothHostInfo</span> info <span class="operator">=</span> localAdapters<span class="operator">.</span>at(currentAdapterIndex);
<span class="type">QBluetoothLocalDevice</span> adapter(info<span class="operator">.</span>address());
adapter<span class="operator">.</span>setHostMode(<span class="type">QBluetoothLocalDevice</span><span class="operator">::</span>HostDiscoverable);
server<span class="operator">-</span><span class="operator">></span>startServer(info<span class="operator">.</span>address());
localName <span class="operator">=</span> info<span class="operator">.</span>name();
}
}
<span class="type">int</span> Chat<span class="operator">::</span>adapterFromUserSelection() <span class="keyword">const</span>
{
<span class="type">int</span> result <span class="operator">=</span> <span class="number">0</span>;
<span class="type">QBluetoothAddress</span> newAdapter <span class="operator">=</span> localAdapters<span class="operator">.</span>at(<span class="number">0</span>)<span class="operator">.</span>address();
<span class="keyword">if</span> (ui<span class="operator">-</span><span class="operator">></span>secondAdapter<span class="operator">-</span><span class="operator">></span>isChecked()) {
newAdapter <span class="operator">=</span> localAdapters<span class="operator">.</span>at(<span class="number">1</span>)<span class="operator">.</span>address();
result <span class="operator">=</span> <span class="number">1</span>;
}
<span class="keyword">return</span> result;
}
</pre>
<p>Messages are sent to all remote devices via the ChatServer and ChatClient instances by emitting the sendMessage() signal.</p>
<pre class="cpp">
<span class="type">void</span> Chat<span class="operator">::</span>sendClicked()
{
ui<span class="operator">-</span><span class="operator">></span>sendButton<span class="operator">-</span><span class="operator">></span>setEnabled(<span class="keyword">false</span>);
ui<span class="operator">-</span><span class="operator">></span>sendText<span class="operator">-</span><span class="operator">></span>setEnabled(<span class="keyword">false</span>);
showMessage(localName<span class="operator">,</span> ui<span class="operator">-</span><span class="operator">></span>sendText<span class="operator">-</span><span class="operator">></span>text());
<span class="keyword">emit</span> sendMessage(ui<span class="operator">-</span><span class="operator">></span>sendText<span class="operator">-</span><span class="operator">></span>text());
ui<span class="operator">-</span><span class="operator">></span>sendText<span class="operator">-</span><span class="operator">></span>clear();
ui<span class="operator">-</span><span class="operator">></span>sendText<span class="operator">-</span><span class="operator">></span>setEnabled(<span class="keyword">true</span>);
ui<span class="operator">-</span><span class="operator">></span>sendButton<span class="operator">-</span><span class="operator">></span>setEnabled(<span class="keyword">true</span>);
}
</pre>
<p>We need to clean up ChatClient instances when the remote device forces a disconnect.</p>
<pre class="cpp">
<span class="type">void</span> Chat<span class="operator">::</span>clientDisconnected()
{
ChatClient <span class="operator">*</span>client <span class="operator">=</span> qobject_cast<span class="operator"><</span>ChatClient <span class="operator">*</span><span class="operator">></span>(sender());
<span class="keyword">if</span> (client) {
clients<span class="operator">.</span>removeOne(client);
client<span class="operator">-</span><span class="operator">></span>deleteLater();
}
}
</pre>
<p>Files:</p>
<ul>
<li><a href="qtbluetooth-btchat-chat-cpp.html">btchat/chat.cpp</a></li>
<li><a href="qtbluetooth-btchat-chat-h.html">btchat/chat.h</a></li>
<li><a href="qtbluetooth-btchat-chat-ui.html">btchat/chat.ui</a></li>
<li><a href="qtbluetooth-btchat-chatclient-cpp.html">btchat/chatclient.cpp</a></li>
<li><a href="qtbluetooth-btchat-chatclient-h.html">btchat/chatclient.h</a></li>
<li><a href="qtbluetooth-btchat-chatserver-cpp.html">btchat/chatserver.cpp</a></li>
<li><a href="qtbluetooth-btchat-chatserver-h.html">btchat/chatserver.h</a></li>
<li><a href="qtbluetooth-btchat-remoteselector-cpp.html">btchat/remoteselector.cpp</a></li>
<li><a href="qtbluetooth-btchat-remoteselector-h.html">btchat/remoteselector.h</a></li>
<li><a href="qtbluetooth-btchat-remoteselector-ui.html">btchat/remoteselector.ui</a></li>
<li><a href="qtbluetooth-btchat-main-cpp.html">btchat/main.cpp</a></li>
<li><a href="qtbluetooth-btchat-btchat-pro.html">btchat/btchat.pro</a></li>
</ul>
</div>
<!-- @@@btchat -->
</div>
</div>
</div>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2016 The Qt Company Ltd.
Documentation contributions included herein are the copyrights of
their respective owners.<br> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. </p>
</div>
</body>
</html>
|