1/***************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the QtBluetooth module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include <QtBluetooth/qlowenergyadvertisingdata.h>
52#include <QtBluetooth/qlowenergyadvertisingparameters.h>
53#include <QtBluetooth/qlowenergycharacteristic.h>
54#include <QtBluetooth/qlowenergycharacteristicdata.h>
55#include <QtBluetooth/qlowenergydescriptordata.h>
56#include <QtBluetooth/qlowenergycontroller.h>
57#include <QtBluetooth/qlowenergyservice.h>
58#include <QtBluetooth/qlowenergyservicedata.h>
59#include <QtCore/qbytearray.h>
60#include <QtCore/qcoreapplication.h>
61#include <QtCore/qlist.h>
62#include <QtCore/qloggingcategory.h>
63#include <QtCore/qscopedpointer.h>
64#include <QtCore/qtimer.h>
65
66int main(int argc, char *argv[])
67{
68 //QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true"));
69 QCoreApplication app(argc, argv);
70
71 //! [Advertising Data]
72 QLowEnergyAdvertisingData advertisingData;
73 advertisingData.setDiscoverability(QLowEnergyAdvertisingData::DiscoverabilityGeneral);
74 advertisingData.setIncludePowerLevel(true);
75 advertisingData.setLocalName("HeartRateServer");
76 advertisingData.setServices(QList<QBluetoothUuid>() << QBluetoothUuid::HeartRate);
77 //! [Advertising Data]
78
79 //! [Service Data]
80 QLowEnergyCharacteristicData charData;
81 charData.setUuid(QBluetoothUuid::HeartRateMeasurement);
82 charData.setValue(QByteArray(2, 0));
83 charData.setProperties(QLowEnergyCharacteristic::Notify);
84 const QLowEnergyDescriptorData clientConfig(QBluetoothUuid::ClientCharacteristicConfiguration,
85 QByteArray(2, 0));
86 charData.addDescriptor(clientConfig);
87
88 QLowEnergyServiceData serviceData;
89 serviceData.setType(QLowEnergyServiceData::ServiceTypePrimary);
90 serviceData.setUuid(QBluetoothUuid::HeartRate);
91 serviceData.addCharacteristic(charData);
92 //! [Service Data]
93
94 //! [Start Advertising]
95 const QScopedPointer<QLowEnergyController> leController(QLowEnergyController::createPeripheral());
96 QScopedPointer<QLowEnergyService> service(leController->addService(serviceData));
97 leController->startAdvertising(QLowEnergyAdvertisingParameters(), advertisingData,
98 advertisingData);
99 //! [Start Advertising]
100
101 //! [Provide Heartbeat]
102 QTimer heartbeatTimer;
103 quint8 currentHeartRate = 60;
104 enum ValueChange { ValueUp, ValueDown } valueChange = ValueUp;
105 const auto heartbeatProvider = [&service, &currentHeartRate, &valueChange]() {
106 QByteArray value;
107 value.append(char(0)); // Flags that specify the format of the value.
108 value.append(char(currentHeartRate)); // Actual value.
109 QLowEnergyCharacteristic characteristic
110 = service->characteristic(QBluetoothUuid::HeartRateMeasurement);
111 Q_ASSERT(characteristic.isValid());
112 service->writeCharacteristic(characteristic, value); // Potentially causes notification.
113 if (currentHeartRate == 60)
114 valueChange = ValueUp;
115 else if (currentHeartRate == 100)
116 valueChange = ValueDown;
117 if (valueChange == ValueUp)
118 ++currentHeartRate;
119 else
120 --currentHeartRate;
121 };
122 QObject::connect(&heartbeatTimer, &QTimer::timeout, heartbeatProvider);
123 heartbeatTimer.start(1000);
124 //! [Provide Heartbeat]
125
126 auto reconnect = [&leController, advertisingData, &service, serviceData]()
127 {
128 service.reset(leController->addService(serviceData));
129 if (!service.isNull())
130 leController->startAdvertising(QLowEnergyAdvertisingParameters(),
131 advertisingData, advertisingData);
132 };
133 QObject::connect(leController.data(), &QLowEnergyController::disconnected, reconnect);
134
135 return app.exec();
136}
137