| 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 | #ifndef Q_OS_ANDROID |
| 61 | #include <QtCore/qcoreapplication.h> |
| 62 | #else |
| 63 | #include <QtGui/qguiapplication.h> |
| 64 | #endif |
| 65 | #include <QtCore/qlist.h> |
| 66 | #include <QtCore/qloggingcategory.h> |
| 67 | #include <QtCore/qscopedpointer.h> |
| 68 | #include <QtCore/qtimer.h> |
| 69 | |
| 70 | int main(int argc, char *argv[]) |
| 71 | { |
| 72 | //QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true")); |
| 73 | #ifndef Q_OS_ANDROID |
| 74 | QCoreApplication app(argc, argv); |
| 75 | #else |
| 76 | QGuiApplication app(argc, argv); |
| 77 | #endif |
| 78 | |
| 79 | //! [Advertising Data] |
| 80 | QLowEnergyAdvertisingData advertisingData; |
| 81 | advertisingData.setDiscoverability(QLowEnergyAdvertisingData::DiscoverabilityGeneral); |
| 82 | advertisingData.setIncludePowerLevel(true); |
| 83 | advertisingData.setLocalName("HeartRateServer" ); |
| 84 | advertisingData.setServices(QList<QBluetoothUuid>() << QBluetoothUuid::HeartRate); |
| 85 | //! [Advertising Data] |
| 86 | |
| 87 | //! [Service Data] |
| 88 | QLowEnergyCharacteristicData charData; |
| 89 | charData.setUuid(QBluetoothUuid::HeartRateMeasurement); |
| 90 | charData.setValue(QByteArray(2, 0)); |
| 91 | charData.setProperties(QLowEnergyCharacteristic::Notify); |
| 92 | const QLowEnergyDescriptorData clientConfig(QBluetoothUuid::ClientCharacteristicConfiguration, |
| 93 | QByteArray(2, 0)); |
| 94 | charData.addDescriptor(descriptor: clientConfig); |
| 95 | |
| 96 | QLowEnergyServiceData serviceData; |
| 97 | serviceData.setType(QLowEnergyServiceData::ServiceTypePrimary); |
| 98 | serviceData.setUuid(QBluetoothUuid::HeartRate); |
| 99 | serviceData.addCharacteristic(characteristic: charData); |
| 100 | //! [Service Data] |
| 101 | |
| 102 | //! [Start Advertising] |
| 103 | const QScopedPointer<QLowEnergyController> leController(QLowEnergyController::createPeripheral()); |
| 104 | QScopedPointer<QLowEnergyService> service(leController->addService(service: serviceData)); |
| 105 | leController->startAdvertising(parameters: QLowEnergyAdvertisingParameters(), advertisingData, |
| 106 | scanResponseData: advertisingData); |
| 107 | //! [Start Advertising] |
| 108 | |
| 109 | //! [Provide Heartbeat] |
| 110 | QTimer heartbeatTimer; |
| 111 | quint8 currentHeartRate = 60; |
| 112 | enum ValueChange { ValueUp, ValueDown } valueChange = ValueUp; |
| 113 | const auto heartbeatProvider = [&service, ¤tHeartRate, &valueChange]() { |
| 114 | QByteArray value; |
| 115 | value.append(c: char(0)); // Flags that specify the format of the value. |
| 116 | value.append(c: char(currentHeartRate)); // Actual value. |
| 117 | QLowEnergyCharacteristic characteristic |
| 118 | = service->characteristic(uuid: QBluetoothUuid::HeartRateMeasurement); |
| 119 | Q_ASSERT(characteristic.isValid()); |
| 120 | service->writeCharacteristic(characteristic, newValue: value); // Potentially causes notification. |
| 121 | if (currentHeartRate == 60) |
| 122 | valueChange = ValueUp; |
| 123 | else if (currentHeartRate == 100) |
| 124 | valueChange = ValueDown; |
| 125 | if (valueChange == ValueUp) |
| 126 | ++currentHeartRate; |
| 127 | else |
| 128 | --currentHeartRate; |
| 129 | }; |
| 130 | QObject::connect(sender: &heartbeatTimer, signal: &QTimer::timeout, slot: heartbeatProvider); |
| 131 | heartbeatTimer.start(msec: 1000); |
| 132 | //! [Provide Heartbeat] |
| 133 | |
| 134 | auto reconnect = [&leController, advertisingData, &service, serviceData]() |
| 135 | { |
| 136 | service.reset(other: leController->addService(service: serviceData)); |
| 137 | if (!service.isNull()) |
| 138 | leController->startAdvertising(parameters: QLowEnergyAdvertisingParameters(), |
| 139 | advertisingData, scanResponseData: advertisingData); |
| 140 | }; |
| 141 | QObject::connect(sender: leController.data(), signal: &QLowEnergyController::disconnected, slot: reconnect); |
| 142 | |
| 143 | return app.exec(); |
| 144 | } |
| 145 | |