/usr/share/qt5/doc/qtbluetooth/qtbluetooth-pingpong-example.html is in qtconnectivity5-doc-html 5.9.5-0ubuntu1.
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 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- pingpong.qdoc -->
<title>Bluetooth QML Ping Pong example | Qt Bluetooth 5.9</title>
<link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
<script type="text/javascript">
document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
// loading style sheet breaks anchors that were jumped to before
// so force jumping to anchor again
setTimeout(function() {
var anchor = location.hash;
// need to jump to different anchor first (e.g. none)
location.hash = "#";
setTimeout(function() {
location.hash = anchor;
}, 0);
}, 0);
</script>
</head>
<body>
<div class="header" id="qtdocheader">
<div class="main">
<div class="main-rounded">
<div class="navigationbar">
<table><tr>
<td >Qt 5.9</td><td ><a href="qtbluetooth-index.html">Qt Bluetooth</a></td><td >Bluetooth QML Ping Pong example</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.9.5 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>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Bluetooth QML Ping Pong example</h1>
<span class="subtitle"></span>
<!-- $$$pingpong-description -->
<div class="descr"> <a name="details"></a>
<p>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.</p>
<p class="centerAlign"><img src="images/intro.png" alt="" /></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>
<p>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".</p>
<pre class="cpp">
m_serverInfo <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qbluetoothserver.html">QBluetoothServer</a></span>(<span class="type"><a href="qbluetoothserviceinfo.html">QBluetoothServiceInfo</a></span><span class="operator">::</span>RfcommProtocol<span class="operator">,</span> <span class="keyword">this</span>);
connect(m_serverInfo<span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="qbluetoothserver.html">QBluetoothServer</a></span><span class="operator">::</span>newConnection<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>PingPong<span class="operator">::</span>clientConnected);
connect(m_serverInfo<span class="operator">,</span> <span class="type">QOverload</span><span class="operator"><</span><span class="type"><a href="qbluetoothserver.html">QBluetoothServer</a></span><span class="operator">::</span>Error<span class="operator">></span><span class="operator">::</span>of(<span class="operator">&</span><span class="type"><a href="qbluetoothserver.html">QBluetoothServer</a></span><span class="operator">::</span>error)<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>PingPong<span class="operator">::</span>serverError);
<span class="keyword">const</span> <span class="type"><a href="qbluetoothuuid.html">QBluetoothUuid</a></span> uuid(serviceUuid);
m_serverInfo<span class="operator">-</span><span class="operator">></span>listen(uuid<span class="operator">,</span> <span class="type"><a href="../qtcore/qstring.html#QStringLiteral">QStringLiteral</a></span>(<span class="string">"PingPong server"</span>));
</pre>
<p>On the client side, the full service discovery on the nearby Bluetooth devices is done.</p>
<pre class="cpp">
discoveryAgent <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qbluetoothservicediscoveryagent.html">QBluetoothServiceDiscoveryAgent</a></span>(<span class="type"><a href="qbluetoothaddress.html">QBluetoothAddress</a></span>());
connect(discoveryAgent<span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="qbluetoothservicediscoveryagent.html">QBluetoothServiceDiscoveryAgent</a></span><span class="operator">::</span>serviceDiscovered<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>PingPong<span class="operator">::</span>addService);
connect(discoveryAgent<span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="qbluetoothservicediscoveryagent.html">QBluetoothServiceDiscoveryAgent</a></span><span class="operator">::</span>finished<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>PingPong<span class="operator">::</span>done);
connect(discoveryAgent<span class="operator">,</span> <span class="type">QOverload</span><span class="operator"><</span><span class="type"><a href="qbluetoothservicediscoveryagent.html">QBluetoothServiceDiscoveryAgent</a></span><span class="operator">::</span>Error<span class="operator">></span><span class="operator">::</span>of(<span class="operator">&</span><span class="type"><a href="qbluetoothservicediscoveryagent.html">QBluetoothServiceDiscoveryAgent</a></span><span class="operator">::</span>error)<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>PingPong<span class="operator">::</span>serviceScanError);
<span class="preprocessor">#ifdef Q_OS_ANDROID //see QTBUG-61392</span>
<span class="keyword">if</span> (<span class="type">QtAndroid</span><span class="operator">::</span>androidSdkVersion() <span class="operator">></span><span class="operator">=</span> <span class="number">23</span>)
discoveryAgent<span class="operator">-</span><span class="operator">></span>setUuidFilter(<span class="type"><a href="qbluetoothuuid.html">QBluetoothUuid</a></span>(androidUuid));
<span class="keyword">else</span>
discoveryAgent<span class="operator">-</span><span class="operator">></span>setUuidFilter(<span class="type"><a href="qbluetoothuuid.html">QBluetoothUuid</a></span>(serviceUuid));
<span class="preprocessor">#else</span>
discoveryAgent<span class="operator">-</span><span class="operator">></span>setUuidFilter(<span class="type"><a href="qbluetoothuuid.html">QBluetoothUuid</a></span>(serviceUuid));
<span class="preprocessor">#endif</span>
discoveryAgent<span class="operator">-</span><span class="operator">></span>start(<span class="type"><a href="qbluetoothservicediscoveryagent.html">QBluetoothServiceDiscoveryAgent</a></span><span class="operator">::</span>FullDiscovery);
</pre>
<p>When the ping pong service is discovered, the client connects to the server using the socket.</p>
<pre class="cpp">
socket <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qbluetoothsocket.html">QBluetoothSocket</a></span>(<span class="type"><a href="qbluetoothserviceinfo.html">QBluetoothServiceInfo</a></span><span class="operator">::</span>RfcommProtocol);
socket<span class="operator">-</span><span class="operator">></span>connectToService(service);
connect(socket<span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="qbluetoothsocket.html">QBluetoothSocket</a></span><span class="operator">::</span>readyRead<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>PingPong<span class="operator">::</span>readSocket);
connect(socket<span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="qbluetoothsocket.html">QBluetoothSocket</a></span><span class="operator">::</span>connected<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>PingPong<span class="operator">::</span>serverConnected);
connect(socket<span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="qbluetoothsocket.html">QBluetoothSocket</a></span><span class="operator">::</span>disconnected<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>PingPong<span class="operator">::</span>serverDisconnected);
</pre>
<p>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.</p>
<pre class="cpp">
<span class="keyword">if</span> (<span class="operator">!</span>m_serverInfo<span class="operator">-</span><span class="operator">></span>hasPendingConnections()) {
setMessage(<span class="string">"FAIL: expected pending server connection"</span>);
<span class="keyword">return</span>;
}
socket <span class="operator">=</span> m_serverInfo<span class="operator">-</span><span class="operator">></span>nextPendingConnection();
<span class="keyword">if</span> (<span class="operator">!</span>socket)
<span class="keyword">return</span>;
socket<span class="operator">-</span><span class="operator">></span>setParent(<span class="keyword">this</span>);
connect(socket<span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="qbluetoothsocket.html">QBluetoothSocket</a></span><span class="operator">::</span>readyRead<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>PingPong<span class="operator">::</span>readSocket);
connect(socket<span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="qbluetoothsocket.html">QBluetoothSocket</a></span><span class="operator">::</span>disconnected<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>PingPong<span class="operator">::</span>clientDisconnected);
connect(socket<span class="operator">,</span> <span class="type">QOverload</span><span class="operator"><</span><span class="type"><a href="qbluetoothsocket.html">QBluetoothSocket</a></span><span class="operator">::</span>SocketError<span class="operator">></span><span class="operator">::</span>of(<span class="operator">&</span><span class="type"><a href="qbluetoothsocket.html">QBluetoothSocket</a></span><span class="operator">::</span>error)<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>PingPong<span class="operator">::</span>socketError);
</pre>
<p>The game starts after the devices are connected and the screen is adjusted.</p>
<pre class="cpp">
<span class="keyword">if</span> (m_role <span class="operator">=</span><span class="operator">=</span> <span class="number">1</span>)
updateDirection();
m_timer<span class="operator">-</span><span class="operator">></span>start(<span class="number">50</span>);
</pre>
<p>The server updates the ball direction and coordinates. The coordinates of pedals are sent to each other every 50ms.</p>
<pre class="cpp">
<span class="keyword">if</span> (m_role <span class="operator">=</span><span class="operator">=</span> <span class="number">1</span>) {
checkBoundaries();
m_ballPreviousX <span class="operator">=</span> m_ballX;
m_ballPreviousY <span class="operator">=</span> m_ballY;
m_ballY <span class="operator">=</span> m_direction<span class="operator">*</span>(m_ballX<span class="operator">+</span>interval) <span class="operator">-</span> m_direction<span class="operator">*</span>m_ballX <span class="operator">+</span> m_ballY;
m_ballX <span class="operator">=</span> m_ballX <span class="operator">+</span> interval;
size<span class="operator">.</span>setNum(m_ballX);
size<span class="operator">.</span>append(<span class="char">' '</span>);
<span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span> size1;
size1<span class="operator">.</span>setNum(m_ballY);
size<span class="operator">.</span>append(size1);
size<span class="operator">.</span>append(<span class="char">' '</span>);
size1<span class="operator">.</span>setNum(m_leftBlockY);
size<span class="operator">.</span>append(size1);
size<span class="operator">.</span>append(<span class="string">" \n"</span>);
socket<span class="operator">-</span><span class="operator">></span>write(size<span class="operator">.</span>constData());
<span class="keyword">emit</span> ballChanged();
}
<span class="keyword">else</span> <span class="keyword">if</span> (m_role <span class="operator">=</span><span class="operator">=</span> <span class="number">2</span>) {
size<span class="operator">.</span>setNum(m_rightBlockY);
size<span class="operator">.</span>append(<span class="string">" \n"</span>);
socket<span class="operator">-</span><span class="operator">></span>write(size<span class="operator">.</span>constData());
}
</pre>
<p>The coordinates are updated and exchanged via sockets. As presented, the server sends its pedal's y coordinate and the ball coordinates whereas, the client sends only its pedal y coordinate.</p>
<pre class="cpp">
<span class="keyword">if</span> (((m_ballX <span class="operator">+</span> ballWidth) <span class="operator">></span> (m_boardWidth <span class="operator">-</span> blockSize)) <span class="operator">&</span><span class="operator">&</span> ((m_ballY <span class="operator">+</span> ballWidth) <span class="operator"><</span> (m_rightBlockY <span class="operator">+</span> blockHeight))
<span class="operator">&</span><span class="operator">&</span> (m_ballY <span class="operator">></span> m_rightBlockY)) {
m_targetY <span class="operator">=</span> <span class="number">2</span> <span class="operator">*</span> m_ballY <span class="operator">-</span> m_ballPreviousY;
m_targetX <span class="operator">=</span> m_ballPreviousX;
interval <span class="operator">=</span> <span class="operator">-</span><span class="number">5</span>;
updateDirection();
}
<span class="keyword">else</span> <span class="keyword">if</span> ((m_ballX <span class="operator"><</span> blockSize) <span class="operator">&</span><span class="operator">&</span> ((m_ballY <span class="operator">+</span> ballWidth) <span class="operator"><</span> (m_leftBlockY <span class="operator">+</span> blockHeight))
<span class="operator">&</span><span class="operator">&</span> (m_ballY <span class="operator">></span> m_leftBlockY)) {
m_targetY <span class="operator">=</span> <span class="number">2</span> <span class="operator">*</span> m_ballY <span class="operator">-</span> m_ballPreviousY;
m_targetX <span class="operator">=</span> m_ballPreviousX;
interval <span class="operator">=</span> <span class="number">5</span>;
updateDirection();
}
<span class="keyword">else</span> <span class="keyword">if</span> (m_ballY <span class="operator"><</span> <span class="number">0</span> <span class="operator">|</span><span class="operator">|</span> (m_ballY <span class="operator">+</span> ballWidth <span class="operator">></span> m_boardHeight)) {
m_targetY <span class="operator">=</span> m_ballPreviousY;
m_targetX <span class="operator">=</span> m_ballX <span class="operator">+</span> interval;
updateDirection();
}
</pre>
<p>In the code above, it was shown how the server checks whether the ball has reached the boundaries of the board. In the case of the goal, the server updates the results via its socket.</p>
<p class="centerAlign"><img src="images/intro1.png" alt="" /></p><p>Files:</p>
<ul>
<li><a href="qtbluetooth-pingpong-pingpong-cpp.html">pingpong/pingpong.cpp</a></li>
<li><a href="qtbluetooth-pingpong-pingpong-h.html">pingpong/pingpong.h</a></li>
<li><a href="qtbluetooth-pingpong-assets-board-qml.html">pingpong/assets/Board.qml</a></li>
<li><a href="qtbluetooth-pingpong-assets-dialog-qml.html">pingpong/assets/Dialog.qml</a></li>
<li><a href="qtbluetooth-pingpong-assets-menu-qml.html">pingpong/assets/Menu.qml</a></li>
<li><a href="qtbluetooth-pingpong-assets-main-qml.html">pingpong/assets/main.qml</a></li>
<li><a href="qtbluetooth-pingpong-main-cpp.html">pingpong/main.cpp</a></li>
<li><a href="qtbluetooth-pingpong-pingpong-pro.html">pingpong/pingpong.pro</a></li>
<li><a href="qtbluetooth-pingpong-resource-qrc.html">pingpong/resource.qrc</a></li>
</ul>
</div>
<!-- @@@pingpong -->
</div>
</div>
</div>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2017 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>
|