summaryrefslogtreecommitdiffstats
path: root/examples/bluetooth/lowenergyscanner
diff options
context:
space:
mode:
authorAlex Blasche <alexander.blasche@digia.com>2014-08-20 09:21:49 +0200
committerAlex Blasche <alexander.blasche@digia.com>2014-08-25 14:03:25 +0200
commit0a8327d422228489a62aa8fed8bdaa9d1291c055 (patch)
tree325d64d985793adc1f93d0a8f7619c7a9102d11d /examples/bluetooth/lowenergyscanner
parent7a3a3f7664e22dbe2e5bc8f016d95ee1f9558a2e (diff)
Update and extend the lowenergyscanner example docs
Change-Id: I0be30191cbceb963c7e1bfd7e55a1f2f38772d5f Reviewed-by: Leena Miettinen <riitta-leena.miettinen@digia.com>
Diffstat (limited to 'examples/bluetooth/lowenergyscanner')
-rw-r--r--examples/bluetooth/lowenergyscanner/characteristicinfo.cpp2
-rw-r--r--examples/bluetooth/lowenergyscanner/device.cpp17
-rw-r--r--examples/bluetooth/lowenergyscanner/doc/images/lowenergyscanner-chars.pngbin0 -> 51040 bytes
-rw-r--r--examples/bluetooth/lowenergyscanner/doc/images/lowenergyscanner-devices.pngbin0 -> 40304 bytes
-rw-r--r--examples/bluetooth/lowenergyscanner/doc/images/lowenergyscanner-example.pngbin31581 -> 0 bytes
-rw-r--r--examples/bluetooth/lowenergyscanner/doc/images/lowenergyscanner-example1.pngbin44107 -> 0 bytes
-rw-r--r--examples/bluetooth/lowenergyscanner/doc/images/lowenergyscanner-services.pngbin0 -> 58825 bytes
-rw-r--r--examples/bluetooth/lowenergyscanner/doc/src/lowenergyscanner.qdoc105
8 files changed, 121 insertions, 3 deletions
diff --git a/examples/bluetooth/lowenergyscanner/characteristicinfo.cpp b/examples/bluetooth/lowenergyscanner/characteristicinfo.cpp
index d7ae8438..f5290459 100644
--- a/examples/bluetooth/lowenergyscanner/characteristicinfo.cpp
+++ b/examples/bluetooth/lowenergyscanner/characteristicinfo.cpp
@@ -60,6 +60,7 @@ void CharacteristicInfo::setCharacteristic(const QLowEnergyCharacteristic &chara
QString CharacteristicInfo::getName() const
{
+ //! [les-get-descriptors]
QString name = m_characteristic.name();
if (!name.isEmpty())
return name;
@@ -71,6 +72,7 @@ QString CharacteristicInfo::getName() const
break;
}
}
+ //! [les-get-descriptors]
if (name.isEmpty())
name = "Unknown";
diff --git a/examples/bluetooth/lowenergyscanner/device.cpp b/examples/bluetooth/lowenergyscanner/device.cpp
index 3d82800f..9ffb4d56 100644
--- a/examples/bluetooth/lowenergyscanner/device.cpp
+++ b/examples/bluetooth/lowenergyscanner/device.cpp
@@ -52,12 +52,14 @@
Device::Device():
connected(false), controller(0), m_deviceScanState(false)
{
+ //! [les-devicediscovery-1]
discoveryAgent = new QBluetoothDeviceDiscoveryAgent();
connect(discoveryAgent, SIGNAL(deviceDiscovered(const QBluetoothDeviceInfo&)),
this, SLOT(addDevice(const QBluetoothDeviceInfo&)));
connect(discoveryAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)),
this, SLOT(deviceScanError(QBluetoothDeviceDiscoveryAgent::Error)));
connect(discoveryAgent, SIGNAL(finished()), this, SLOT(deviceScanFinished()));
+ //! [les-devicediscovery-1]
setUpdate("Search");
}
@@ -81,11 +83,14 @@ void Device::startDeviceDiscovery()
emit devicesUpdated();
setUpdate("Scanning for devices ...");
+ //! [les-devicediscovery-2]
discoveryAgent->start();
+ //! [les-devicediscovery-2]
m_deviceScanState = true;
Q_EMIT stateChanged();
}
+//! [les-devicediscovery-3]
void Device::addDevice(const QBluetoothDeviceInfo &info)
{
if (info.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) {
@@ -94,6 +99,7 @@ void Device::addDevice(const QBluetoothDeviceInfo &info)
setUpdate("Last device added: " + d->getName());
}
}
+//! [les-devicediscovery-3]
void Device::deviceScanFinished()
{
@@ -154,6 +160,7 @@ void Device::scanServices(const QString &address)
controller = 0;
}
+ //! [les-controller-1]
if (!controller) {
// Connecting signals and slots for connecting to LE services.
controller = new QLowEnergyController(currentDevice.getDevice().address());
@@ -170,20 +177,24 @@ void Device::scanServices(const QString &address)
}
controller->connectToDevice();
+ //! [les-controller-1]
}
void Device::addLowEnergyService(const QBluetoothUuid &serviceUuid)
{
+ //! [les-service-1]
QLowEnergyService *service = controller->createServiceObject(serviceUuid);
if (!service) {
qWarning() << "Cannot create service for uuid";
return;
}
+ //! [les-service-1]
ServiceInfo *serv = new ServiceInfo(service);
m_services.append(serv);
emit servicesUpdated();
}
+//! [les-service-1]
void Device::serviceScanDone()
{
@@ -209,9 +220,11 @@ void Device::connectToService(const QString &uuid)
emit characteristicsUpdated();
if (service->state() == QLowEnergyService::DiscoveryRequired) {
+ //! [les-service-3]
connect(service, SIGNAL(stateChanged(QLowEnergyService::ServiceState)),
this, SLOT(serviceDetailsDiscovered(QLowEnergyService::ServiceState)));
service->discoverDetails();
+ //! [les-service-3]
return;
}
@@ -229,7 +242,9 @@ void Device::deviceConnected()
{
setUpdate("Discovering services!");
connected = true;
+ //! [les-service-2]
controller->discoverServices();
+ //! [les-service-2]
}
void Device::errorReceived(QLowEnergyController::Error /*error*/)
@@ -265,11 +280,13 @@ void Device::serviceDetailsDiscovered(QLowEnergyService::ServiceState newState)
if (!service)
return;
+ //! [les-chars]
const QList<QLowEnergyCharacteristic> chars = service->characteristics();
foreach (const QLowEnergyCharacteristic &ch, chars) {
CharacteristicInfo *cInfo = new CharacteristicInfo(ch);
m_characteristics.append(cInfo);
}
+ //! [les-chars]
emit characteristicsUpdated();
}
diff --git a/examples/bluetooth/lowenergyscanner/doc/images/lowenergyscanner-chars.png b/examples/bluetooth/lowenergyscanner/doc/images/lowenergyscanner-chars.png
new file mode 100644
index 00000000..ff6d9e3e
--- /dev/null
+++ b/examples/bluetooth/lowenergyscanner/doc/images/lowenergyscanner-chars.png
Binary files differ
diff --git a/examples/bluetooth/lowenergyscanner/doc/images/lowenergyscanner-devices.png b/examples/bluetooth/lowenergyscanner/doc/images/lowenergyscanner-devices.png
new file mode 100644
index 00000000..49a1c462
--- /dev/null
+++ b/examples/bluetooth/lowenergyscanner/doc/images/lowenergyscanner-devices.png
Binary files differ
diff --git a/examples/bluetooth/lowenergyscanner/doc/images/lowenergyscanner-example.png b/examples/bluetooth/lowenergyscanner/doc/images/lowenergyscanner-example.png
deleted file mode 100644
index bdc3a22e..00000000
--- a/examples/bluetooth/lowenergyscanner/doc/images/lowenergyscanner-example.png
+++ /dev/null
Binary files differ
diff --git a/examples/bluetooth/lowenergyscanner/doc/images/lowenergyscanner-example1.png b/examples/bluetooth/lowenergyscanner/doc/images/lowenergyscanner-example1.png
deleted file mode 100644
index 4854abf7..00000000
--- a/examples/bluetooth/lowenergyscanner/doc/images/lowenergyscanner-example1.png
+++ /dev/null
Binary files differ
diff --git a/examples/bluetooth/lowenergyscanner/doc/images/lowenergyscanner-services.png b/examples/bluetooth/lowenergyscanner/doc/images/lowenergyscanner-services.png
new file mode 100644
index 00000000..5420c257
--- /dev/null
+++ b/examples/bluetooth/lowenergyscanner/doc/images/lowenergyscanner-services.png
Binary files differ
diff --git a/examples/bluetooth/lowenergyscanner/doc/src/lowenergyscanner.qdoc b/examples/bluetooth/lowenergyscanner/doc/src/lowenergyscanner.qdoc
index fe7e007f..0dc38efa 100644
--- a/examples/bluetooth/lowenergyscanner/doc/src/lowenergyscanner.qdoc
+++ b/examples/bluetooth/lowenergyscanner/doc/src/lowenergyscanner.qdoc
@@ -1,6 +1,7 @@
/****************************************************************************
**
** Copyright (C) 2013 BlackBerry Limited all rights reserved
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the documentation of the Qt Toolkit.
@@ -28,13 +29,111 @@
/*!
\example lowenergyscanner
\title Bluetooth Low Energy Scanner Example
+ \brief An application designed to browse the content of Bluetooth Low
+ Energy peripheral devices. The example demonstrates the use of all Qt Bluetooth
+ Low Energy classes.
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.
+ the service characteristics and descriptors.
- \image lowenergyscanner-example.png
- \image lowenergyscanner-example1.png
+ \image lowenergyscanner-services.png
+ The example introduces the following Qt classes:
+
+ \list
+ \li \l QLowEnergyController
+ \li \l QLowEnergyService
+ \li \l QLowEnergyCharacteristic
+ \li \l QLowEnergyDescriptor
+ \endlist
+
+ 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.
+
+ \include examples-run.qdocinc
+
+ \section1 Scanning for Devices
+
+ The first step is to find all peripheral devices. The devices can be found using
+ the \l QBluetoothDeviceDiscoveryAgent class. The discovery process is started using
+ \l {QBluetoothDeviceDiscoveryAgent::start()}{start()}. Each new device is advertised via
+ the \l {QBluetoothDeviceDiscoveryAgent::deviceDiscovered()}{deviceDiscovered()} signal:
+
+ \snippet lowenergyscanner/device.cpp les-devicediscovery-1
+ \snippet lowenergyscanner/device.cpp les-devicediscovery-2
+
+ The below \c addDevice() slot is triggered as a reaction to the discovery of a new
+ device. It filters all found devices which
+ have the \l QBluetoothDeviceInfo::LowEnergyCoreConfiguration flag and adds them to a
+ list which is shown to the user.
+
+ \snippet lowenergyscanner/device.cpp les-devicediscovery-3
+
+ The list of devices may look like in the image below. \note It is a prerequisite
+ that the remote devices actively advertise their presence.
+
+ \image lowenergyscanner-devices.png
+
+ \section1 Connecting to Services
+
+ After the user has selected a device from the list the application connects to the
+ device and scans all services. The \l QLowEnergyController class is used to connect
+ to the device. The \l {QLowEnergyController::connectToDevice()} function triggers the
+ connection process which lasts until the \l {QLowEnergyController::connected()} signal
+ is received or an error has occurred:
+
+ \snippet lowenergyscanner/device.cpp les-controller-1
+
+ The slot triggered by the \l {QLowEnergyController::connected()}{connected()}
+ signal immediately calls \l {QLowEnergyController::discoverServices()} to start the service
+ discovery on the connected peripheral device.
+
+ \snippet lowenergyscanner/device.cpp les-service-2
+
+ 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.
+
+ \image lowenergyscanner-services.png
+
+ As soon as the service is chosen the related \l QLowEnergyService instance is created to
+ permit interaction with it:
+
+ \snippet lowenergyscanner/device.cpp les-service-1
+
+ 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 \l {QLowEnergyService::state()}{state()} transitions
+ from \l {QLowEnergyService::DiscoveryRequired}{DiscoveryRequired} to
+ \l {QLowEnergyService::DiscoveringServices}{DiscoveringServices} and eventually ends with
+ \l {QLowEnergyService::ServiceDiscovered}{ServiceDiscovered}:
+
+ \snippet lowenergyscanner/device.cpp les-service-3
+
+ \section1 Reading Service Data
+
+ Upon selection of a service the service details are shown. Each characteristic
+ is listed together with its name, UUID, value, handle and properties.
+
+ \image lowenergyscanner-chars.png
+
+ It is possible to retrieve the service's characteristics via
+ \l QLowEnergyService::characteristics() and in turn, each descriptor can be obtained
+ via \l QLowEnergyCharacteristic::descriptors().
+
+ \snippet lowenergyscanner/device.cpp les-chars
+
+ 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
+ \l {QBluetoothUuid::CharacteristicUserDescription}. The code below demonstrates how this
+ may be achieved:
+
+ \snippet lowenergyscanner/characteristicinfo.cpp les-get-descriptors
*/