/usr/share/qt5/doc/qtbluetooth/qtbluetooth-overview.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 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- bluetooth-overview.qdoc -->
<title>Qt Bluetooth Overview | 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 >Qt Bluetooth Overview</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="#retrieving-local-device-information">Retrieving Local Device Information</a></li>
<li class="level1"><a href="#scanning-for-bluetooth-devices">Scanning for Bluetooth Devices</a></li>
<li class="level1"><a href="#pushing-files-to-remote-devices">Pushing Files to Remote Devices</a></li>
<li class="level1"><a href="#exchanging-data-between-devices">Exchanging Data Between Devices</a></li>
<li class="level1"><a href="#bluetooth-low-energy">Bluetooth Low Energy</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Qt Bluetooth Overview</h1>
<span class="subtitle"></span>
<!-- $$$qtbluetooth-overview.html-description -->
<div class="descr"> <a name="details"></a>
<p>With the Qt Bluetooth API typical use cases are:</p>
<ul>
<li>Retrieve information about the local Bluetooth device.</li>
<li>Scan for other Bluetooth devices in range and retrieve information about them.</li>
<li>Push files to remote devices using the OBEX Object Push Profile (OPP)</li>
<li>Connect to remote devices through a RFCOMM channel using the Serial Port Profile (SPP).</li>
<li>Create a RFCOMM server that allows incoming connections using SPP.</li>
<li>Retrieve specification about Bluetooth Low Energy device.</li>
<li>Connect to Bluetooth Low Energy device.</li>
<li>Receive advertisement from Bluetooth Low Energy device.</li>
</ul>
<p>Note that the Object Push Profile is not supported on Android.</p>
<p>The following sections describe how to use the Qt Bluetooth C++ API classes for the above use cases.</p>
<a name="retrieving-local-device-information"></a>
<h2 id="retrieving-local-device-information">Retrieving Local Device Information</h2>
<p>The Qt Bluetooth API has three main purposes. The first one is to obtain local and remote device information. The first steps in retrieving device information are to check if Bluetooth is available on the device and read the local device address and name. <a href="qbluetoothlocaldevice.html">QBluetoothLocalDevice</a> is the class that provides all of this information. Additionally you can use it to turn Bluetooth on/off, set the visibility of the device and determine the current connections.</p>
<pre class="cpp">
<span class="type">QBluetoothLocalDevice</span> localDevice;
<span class="type">QString</span> localDeviceName;
<span class="comment">// Check if Bluetooth is available on this device</span>
<span class="keyword">if</span> (localDevice<span class="operator">.</span>isValid()) {
<span class="comment">// Turn Bluetooth on</span>
localDevice<span class="operator">.</span>powerOn();
<span class="comment">// Read local device name</span>
localDeviceName <span class="operator">=</span> localDevice<span class="operator">.</span>name();
<span class="comment">// Make it visible to others</span>
localDevice<span class="operator">.</span>setHostMode(<span class="type">QBluetoothLocalDevice</span><span class="operator">::</span>HostDiscoverable);
<span class="comment">// Get connected devices</span>
<span class="type">QList</span><span class="operator"><</span><span class="type">QBluetoothAddress</span><span class="operator">></span> remotes;
remotes <span class="operator">=</span> localDevice<span class="operator">.</span>connectedDevices();
}
</pre>
<a name="scanning-for-bluetooth-devices"></a>
<h2 id="scanning-for-bluetooth-devices">Scanning for Bluetooth Devices</h2>
<p>Similar to the <a href="qbluetoothlocaldevice.html">QBluetoothLocalDevice</a>, the API offers <a href="qbluetoothdeviceinfo.html">QBluetoothDeviceInfo</a> which provides similar information for remote devices. Although you can just create <a href="qbluetoothdeviceinfo.html">QBluetoothDeviceInfo</a> objects on your own and fill them with data, the easier way is to use the <a href="qbluetoothdevicediscoveryagent.html">QBluetoothDeviceDiscoveryAgent</a> to start an automated search for visible Bluetooth devices within the connectable range.</p>
<pre class="cpp">
<span class="type">void</span> MyClass<span class="operator">::</span>startDeviceDiscovery()
{
<span class="comment">// Create a discovery agent and connect to its signals</span>
<span class="type">QBluetoothDeviceDiscoveryAgent</span> <span class="operator">*</span>discoveryAgent <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QBluetoothDeviceDiscoveryAgent</span>(<span class="keyword">this</span>);
connect(discoveryAgent<span class="operator">,</span> SIGNAL(deviceDiscovered(<span class="type">QBluetoothDeviceInfo</span>))<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> SLOT(deviceDiscovered(<span class="type">QBluetoothDeviceInfo</span>)));
<span class="comment">// Start a discovery</span>
discoveryAgent<span class="operator">-</span><span class="operator">></span>start();
<span class="comment">//...</span>
}
<span class="comment">// In your local slot, read information about the found devices</span>
<span class="type">void</span> MyClass<span class="operator">::</span>deviceDiscovered(<span class="keyword">const</span> <span class="type">QBluetoothDeviceInfo</span> <span class="operator">&</span>device)
{
<a href="../qtcore/qtglobal.html#qDebug">qDebug</a>() <span class="operator"><</span><span class="operator"><</span> <span class="string">"Found new device:"</span> <span class="operator"><</span><span class="operator"><</span> device<span class="operator">.</span>name() <span class="operator"><</span><span class="operator"><</span> <span class="char">'('</span> <span class="operator"><</span><span class="operator"><</span> device<span class="operator">.</span>address()<span class="operator">.</span>toString() <span class="operator"><</span><span class="operator"><</span> <span class="char">')'</span>;
}
</pre>
<a name="pushing-files-to-remote-devices"></a>
<h2 id="pushing-files-to-remote-devices">Pushing Files to Remote Devices</h2>
<p>Once the desired device was found, there are two main use cases provided by Qt Bluetooth. The simpler one is to send files via the Obex Object Push Profile (OPP). As the name describes, this profile can push files from one device to another. Currently it is not possible to pull files or browse the remote file system. The profile does not require the two devices to be paired before exchanging data. To push files to remote devices, create a <a href="qbluetoothtransferrequest.html">QBluetoothTransferRequest</a> and ask the <a href="qbluetoothtransfermanager.html">QBluetoothTransferManager</a> to push the file contained in the request by calling its <a href="qbluetoothtransfermanager.html#put">put()</a> function.</p>
<pre class="cpp">
<span class="comment">// Create a transfer manager</span>
<span class="type">QBluetoothTransferManager</span> <span class="operator">*</span>transferManager <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QBluetoothTransferManager</span>(<span class="keyword">this</span>);
<span class="comment">// Create the transfer request and file to be sent</span>
<span class="type">QBluetoothAddress</span> remoteAddress(<span class="string">"00:11:22:33:44:55:66"</span>);
<span class="type">QBluetoothTransferRequest</span> request(remoteAddress);
<span class="type">QFile</span> <span class="operator">*</span>file <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QFile</span>(<span class="string">"testfile.txt"</span>);
<span class="comment">// Ask the transfer manager to send it</span>
<span class="type">QBluetoothTransferReply</span> <span class="operator">*</span>reply <span class="operator">=</span> transferManager<span class="operator">-</span><span class="operator">></span>put(request<span class="operator">,</span> file);
<span class="keyword">if</span> (reply<span class="operator">-</span><span class="operator">></span>error() <span class="operator">=</span><span class="operator">=</span> <span class="type">QBluetoothTransferReply</span><span class="operator">::</span>NoError) {
<span class="comment">// Connect to the reply's signals to be informed about the status and do cleanups when done</span>
<span class="type">QObject</span><span class="operator">::</span>connect(reply<span class="operator">,</span> SIGNAL(finished(<span class="type">QBluetoothTransferReply</span><span class="operator">*</span>))<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> SLOT(transferFinished(<span class="type">QBluetoothTransferReply</span><span class="operator">*</span>)));
<span class="type">QObject</span><span class="operator">::</span>connect(reply<span class="operator">,</span> SIGNAL(error(<span class="type">QBluetoothTransferReply</span><span class="operator">::</span>TransferError))<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> SLOT(error(<span class="type">QBluetoothTransferReply</span><span class="operator">::</span>TransferError)));
} <span class="keyword">else</span> {
<a href="../qtcore/qtglobal.html#qWarning">qWarning</a>() <span class="operator"><</span><span class="operator"><</span> <span class="string">"Cannot push testfile.txt:"</span> <span class="operator"><</span><span class="operator"><</span> reply<span class="operator">-</span><span class="operator">></span>errorString();
}
</pre>
<a name="exchanging-data-between-devices"></a>
<h2 id="exchanging-data-between-devices">Exchanging Data Between Devices</h2>
<p>The more flexible approach for communication between two Bluetooth enabled devices, is to create a virtual serial port connection and freely exchange data over that connection. This can be done by the Serial Port Profile (SPP). The Serial Port Profile emulates a serial connection over the Bluetooth transport protocol RFCOMM.</p>
<p>To be able to receive incoming SPP connections, you need to listen to incoming connections using <a href="qbluetoothserver.html">QBluetoothServer</a>.</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>Connect to this server from another device playing the client role by using a <a href="qbluetoothsocket.html">QBluetoothSocket</a>:</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>Using such a connection allows to exchange any form of data in both directions. It is perfectly suited for gaming or for syncing the state between two instances of an application on two devices. For more detailed descriptions on how to configure the server and client, please refer to the detailed description sections in the <a href="qbluetoothserver.html">QBluetoothServer</a> and <a href="qbluetoothsocket.html">QBluetoothSocket</a> classes. A good example to start with SPP is the <a href="qtbluetooth-btchat-example.html">Bluetooth Chat</a> example.</p>
<a name="bluetooth-low-energy"></a>
<h2 id="bluetooth-low-energy">Bluetooth Low Energy</h2>
<p>Bluetooth Low Energy, also known as Bluetooth Smart, is a new technology enabling devices with low energy consumption to communicate with each other. More details about this technology and the related Qt APIs can be found in the <a href="qtbluetooth-le-overview.html">Bluetooth Low Energy Overview</a>.</p>
</div>
<!-- @@@qtbluetooth-overview.html -->
</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>
|