/usr/share/qt5/doc/qtbluetooth/qtbluetooth-lowenergyscanner-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 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- lowenergyscanner.qdoc -->
<title>Bluetooth Low Energy Scanner 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 Low Energy Scanner 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="#scanning-for-devices">Scanning for Devices</a></li>
<li class="level1"><a href="#connecting-to-services">Connecting to Services</a></li>
<li class="level1"><a href="#reading-service-data">Reading Service Data</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Bluetooth Low Energy Scanner Example</h1>
<span class="subtitle"></span>
<!-- $$$lowenergyscanner-description -->
<div class="descr"> <a name="details"></a>
<p>The Bluetooth Low Energy Scanner Example shows how to develop Bluetooth Low Energy applications using the Qt Bluetooth API. The application covers scanning for Low Energy devices, scanning their services and reading the service characteristics and descriptors.</p>
<p class="centerAlign"><img src="images/lowenergyscanner-services.png" alt="" /></p><p>The example introduces the following Qt classes:</p>
<ul>
<li><a href="qlowenergycontroller.html">QLowEnergyController</a></li>
<li><a href="qlowenergyservice.html">QLowEnergyService</a></li>
<li><a href="qlowenergycharacteristic.html">QLowEnergyCharacteristic</a></li>
<li><a href="qlowenergydescriptor.html">QLowEnergyDescriptor</a></li>
</ul>
<p>The example can be used with any arbitrary Bluetooth Low Energy peripheral device. It creates a snapshot of all services, characteristics and descriptors and presents them to the user. Therefore the application provides an easy way of browsing the content offered by a peripheral device.</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="scanning-for-devices"></a>
<h2 id="scanning-for-devices">Scanning for Devices</h2>
<p>The first step is to find all peripheral devices. The devices can be found using the <a href="qbluetoothdevicediscoveryagent.html">QBluetoothDeviceDiscoveryAgent</a> class. The discovery process is started using <a href="qbluetoothdevicediscoveryagent.html#start">start()</a>. Each new device is advertised via the <a href="qbluetoothdevicediscoveryagent.html#deviceDiscovered">deviceDiscovered()</a> signal:</p>
<pre class="cpp">
discoveryAgent <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QBluetoothDeviceDiscoveryAgent</span>();
connect(discoveryAgent<span class="operator">,</span> SIGNAL(deviceDiscovered(<span class="keyword">const</span> <span class="type">QBluetoothDeviceInfo</span><span class="operator">&</span>))<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> SLOT(addDevice(<span class="keyword">const</span> <span class="type">QBluetoothDeviceInfo</span><span class="operator">&</span>)));
connect(discoveryAgent<span class="operator">,</span> SIGNAL(error(<span class="type">QBluetoothDeviceDiscoveryAgent</span><span class="operator">::</span>Error))<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> SLOT(deviceScanError(<span class="type">QBluetoothDeviceDiscoveryAgent</span><span class="operator">::</span>Error)));
connect(discoveryAgent<span class="operator">,</span> SIGNAL(finished())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(deviceScanFinished()));
discoveryAgent<span class="operator">-</span><span class="operator">></span>start();
</pre>
<p>The below <code>addDevice()</code> slot is triggered as a reaction to the discovery of a new device. It filters all found devices which have the <a href="qbluetoothdeviceinfo.html#CoreConfiguration-enum">QBluetoothDeviceInfo::LowEnergyCoreConfiguration</a> flag and adds them to a list which is shown to the user.</p>
<pre class="cpp">
<span class="type">void</span> Device<span class="operator">::</span>addDevice(<span class="keyword">const</span> <span class="type">QBluetoothDeviceInfo</span> <span class="operator">&</span>info)
{
<span class="keyword">if</span> (info<span class="operator">.</span>coreConfigurations() <span class="operator">&</span> <span class="type">QBluetoothDeviceInfo</span><span class="operator">::</span>LowEnergyCoreConfiguration) {
DeviceInfo <span class="operator">*</span>d <span class="operator">=</span> <span class="keyword">new</span> DeviceInfo(info);
devices<span class="operator">.</span>append(d);
setUpdate(<span class="string">"Last device added: "</span> <span class="operator">+</span> d<span class="operator">-</span><span class="operator">></span>getName());
}
}
</pre>
<p>The list of devices may look like in the image below.</p>
<p><b>Note: </b>It is a prerequisite that the remote devices actively advertise their presence.</p><p class="centerAlign"><img src="images/lowenergyscanner-devices.png" alt="" /></p><a name="connecting-to-services"></a>
<h2 id="connecting-to-services">Connecting to Services</h2>
<p>After the user has selected a device from the list the application connects to the device and scans all services. The <a href="qlowenergycontroller.html">QLowEnergyController</a> class is used to connect to the device. The <a href="qlowenergycontroller.html#connectToDevice">QLowEnergyController::connectToDevice</a>() function triggers the connection process which lasts until the <a href="qlowenergycontroller.html#connected">QLowEnergyController::connected</a>() signal is received or an error has occurred:</p>
<pre class="cpp">
<span class="keyword">if</span> (<span class="operator">!</span>controller) {
<span class="comment">// Connecting signals and slots for connecting to LE services.</span>
controller <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QLowEnergyController</span>(currentDevice<span class="operator">.</span>getDevice());
connect(controller<span class="operator">,</span> SIGNAL(connected())<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> SLOT(deviceConnected()));
connect(controller<span class="operator">,</span> SIGNAL(error(<span class="type">QLowEnergyController</span><span class="operator">::</span>Error))<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> SLOT(errorReceived(<span class="type">QLowEnergyController</span><span class="operator">::</span>Error)));
connect(controller<span class="operator">,</span> SIGNAL(disconnected())<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> SLOT(deviceDisconnected()));
connect(controller<span class="operator">,</span> SIGNAL(serviceDiscovered(<span class="type">QBluetoothUuid</span>))<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> SLOT(addLowEnergyService(<span class="type">QBluetoothUuid</span>)));
connect(controller<span class="operator">,</span> SIGNAL(discoveryFinished())<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> SLOT(serviceScanDone()));
}
<span class="keyword">if</span> (isRandomAddress())
controller<span class="operator">-</span><span class="operator">></span>setRemoteAddressType(<span class="type">QLowEnergyController</span><span class="operator">::</span>RandomAddress);
<span class="keyword">else</span>
controller<span class="operator">-</span><span class="operator">></span>setRemoteAddressType(<span class="type">QLowEnergyController</span><span class="operator">::</span>PublicAddress);
controller<span class="operator">-</span><span class="operator">></span>connectToDevice();
</pre>
<p>The slot triggered by the <a href="qlowenergycontroller.html#connected">connected()</a> signal immediately calls <a href="qlowenergycontroller.html#discoverServices">QLowEnergyController::discoverServices</a>() to start the service discovery on the connected peripheral device.</p>
<pre class="cpp">
controller<span class="operator">-</span><span class="operator">></span>discoverServices();
</pre>
<p>The resulting list is presented to the user.The image below displays the results when the SensorTag device is selected. The view lists the names of the services, whether they are primary or secondary services and the UUID which determines the service type.</p>
<p class="centerAlign"><img src="images/lowenergyscanner-services.png" alt="" /></p><p>As soon as the service is chosen the related <a href="qlowenergyservice.html">QLowEnergyService</a> instance is created to permit interaction with it:</p>
<pre class="cpp">
<span class="type">QLowEnergyService</span> <span class="operator">*</span>service <span class="operator">=</span> controller<span class="operator">-</span><span class="operator">></span>createServiceObject(serviceUuid);
<span class="keyword">if</span> (<span class="operator">!</span>service) {
<a href="../qtcore/qtglobal.html#qWarning">qWarning</a>() <span class="operator"><</span><span class="operator"><</span> <span class="string">"Cannot create service for uuid"</span>;
<span class="keyword">return</span>;
}
</pre>
<p>The service object provides the required signals and functions to discover the service details, read and write characteristics and descriptors, as well as receive data change notifications. Change notifications can be triggered as a result of writing a value or due to an on-device update potentially triggered by the internal logic. During the initial detail search the service's <a href="qlowenergyservice.html#state">state()</a> transitions from <a href="qlowenergyservice.html#ServiceState-enum">DiscoveryRequired</a> to <a href="qlowenergyservice.html#ServiceState-enum">DiscoveringServices</a> and eventually ends with <a href="qlowenergyservice.html#ServiceState-enum">ServiceDiscovered</a>:</p>
<pre class="cpp">
connect(service<span class="operator">,</span> SIGNAL(stateChanged(<span class="type">QLowEnergyService</span><span class="operator">::</span>ServiceState))<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> SLOT(serviceDetailsDiscovered(<span class="type">QLowEnergyService</span><span class="operator">::</span>ServiceState)));
service<span class="operator">-</span><span class="operator">></span>discoverDetails();
setUpdate(<span class="string">"Back\n(Discovering details...)"</span>);
</pre>
<a name="reading-service-data"></a>
<h2 id="reading-service-data">Reading Service Data</h2>
<p>Upon selection of a service the service details are shown. Each characteristic is listed together with its name, UUID, value, handle and properties.</p>
<p class="centerAlign"><img src="images/lowenergyscanner-chars.png" alt="" /></p><p>It is possible to retrieve the service's characteristics via <a href="qlowenergyservice.html#characteristics">QLowEnergyService::characteristics</a>() and in turn, each descriptor can be obtained via <a href="qlowenergycharacteristic.html#descriptors">QLowEnergyCharacteristic::descriptors</a>().</p>
<pre class="cpp">
<span class="keyword">const</span> <span class="type">QList</span><span class="operator"><</span><span class="type">QLowEnergyCharacteristic</span><span class="operator">></span> chars <span class="operator">=</span> service<span class="operator">-</span><span class="operator">></span>characteristics();
foreach (<span class="keyword">const</span> <span class="type">QLowEnergyCharacteristic</span> <span class="operator">&</span>ch<span class="operator">,</span> chars) {
CharacteristicInfo <span class="operator">*</span>cInfo <span class="operator">=</span> <span class="keyword">new</span> CharacteristicInfo(ch);
m_characteristics<span class="operator">.</span>append(cInfo);
}
</pre>
<p>Although the example application does not display descriptors it uses descriptors to get the name of an individual characteristic if its name cannot be discerned based on its UUID. The second way to obtain the name is the existence of a descriptor of the type <a href="qbluetoothuuid.html#DescriptorType-enum">QBluetoothUuid::CharacteristicUserDescription</a>. The code below demonstrates how this may be achieved:</p>
<pre class="cpp">
<span class="type">QString</span> name <span class="operator">=</span> m_characteristic<span class="operator">.</span>name();
<span class="keyword">if</span> (<span class="operator">!</span>name<span class="operator">.</span>isEmpty())
<span class="keyword">return</span> name;
<span class="comment">// find descriptor with CharacteristicUserDescription</span>
foreach (<span class="keyword">const</span> <span class="type">QLowEnergyDescriptor</span> <span class="operator">&</span>descriptor<span class="operator">,</span> m_characteristic<span class="operator">.</span>descriptors()) {
<span class="keyword">if</span> (descriptor<span class="operator">.</span>type() <span class="operator">=</span><span class="operator">=</span> <span class="type">QBluetoothUuid</span><span class="operator">::</span>CharacteristicUserDescription) {
name <span class="operator">=</span> descriptor<span class="operator">.</span>value();
<span class="keyword">break</span>;
}
}
</pre>
<p>Files:</p>
<ul>
<li><a href="qtbluetooth-lowenergyscanner-characteristicinfo-cpp.html">lowenergyscanner/characteristicinfo.cpp</a></li>
<li><a href="qtbluetooth-lowenergyscanner-characteristicinfo-h.html">lowenergyscanner/characteristicinfo.h</a></li>
<li><a href="qtbluetooth-lowenergyscanner-device-cpp.html">lowenergyscanner/device.cpp</a></li>
<li><a href="qtbluetooth-lowenergyscanner-device-h.html">lowenergyscanner/device.h</a></li>
<li><a href="qtbluetooth-lowenergyscanner-deviceinfo-cpp.html">lowenergyscanner/deviceinfo.cpp</a></li>
<li><a href="qtbluetooth-lowenergyscanner-deviceinfo-h.html">lowenergyscanner/deviceinfo.h</a></li>
<li><a href="qtbluetooth-lowenergyscanner-serviceinfo-cpp.html">lowenergyscanner/serviceinfo.cpp</a></li>
<li><a href="qtbluetooth-lowenergyscanner-serviceinfo-h.html">lowenergyscanner/serviceinfo.h</a></li>
<li><a href="qtbluetooth-lowenergyscanner-assets-characteristics-qml.html">lowenergyscanner/assets/Characteristics.qml</a></li>
<li><a href="qtbluetooth-lowenergyscanner-assets-dialog-qml.html">lowenergyscanner/assets/Dialog.qml</a></li>
<li><a href="qtbluetooth-lowenergyscanner-assets-header-qml.html">lowenergyscanner/assets/Header.qml</a></li>
<li><a href="qtbluetooth-lowenergyscanner-assets-label-qml.html">lowenergyscanner/assets/Label.qml</a></li>
<li><a href="qtbluetooth-lowenergyscanner-assets-menu-qml.html">lowenergyscanner/assets/Menu.qml</a></li>
<li><a href="qtbluetooth-lowenergyscanner-assets-services-qml.html">lowenergyscanner/assets/Services.qml</a></li>
<li><a href="qtbluetooth-lowenergyscanner-assets-main-qml.html">lowenergyscanner/assets/main.qml</a></li>
<li><a href="qtbluetooth-lowenergyscanner-main-cpp.html">lowenergyscanner/main.cpp</a></li>
<li><a href="qtbluetooth-lowenergyscanner-lowenergyscanner-pro.html">lowenergyscanner/lowenergyscanner.pro</a></li>
<li><a href="qtbluetooth-lowenergyscanner-resources-qrc.html">lowenergyscanner/resources.qrc</a></li>
</ul>
</div>
<!-- @@@lowenergyscanner -->
</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>
|