summaryrefslogtreecommitdiffstats
path: root/examples/bluetooth/lowenergyscanner/doc/src/lowenergyscanner.qdoc
blob: b4ad9fa26d12b548b45c72dc571810d9a6a38564 (plain)
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
/****************************************************************************
**
** Copyright (C) 2013 BlackBerry Limited all rights reserved
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Free Documentation License Usage
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file. Please review the following information to ensure
** the GNU Free Documentation License version 1.3 requirements
** will be met: http://www.gnu.org/copyleft/fdl.html.
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
    \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 and descriptors.

    \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
*/