2016-01-15 14:30:53 +00:00
|
|
|
/***************************************************************************
|
|
|
|
**
|
2017-09-28 13:52:24 +00:00
|
|
|
** Copyright (C) 2017 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2016-01-15 14:30:53 +00:00
|
|
|
**
|
|
|
|
** This file is part of the examples of the QtBluetooth module of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:BSD$
|
2017-09-28 13:52:24 +00:00
|
|
|
** 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 https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
**
|
|
|
|
** BSD License Usage
|
|
|
|
** Alternatively, you may use this file under the terms of the BSD license
|
|
|
|
** as follows:
|
2016-01-15 14:30:53 +00:00
|
|
|
**
|
|
|
|
** "Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions are
|
|
|
|
** met:
|
|
|
|
** * Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** * Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in
|
|
|
|
** the documentation and/or other materials provided with the
|
|
|
|
** distribution.
|
|
|
|
** * Neither the name of The Qt Company Ltd nor the names of its
|
|
|
|
** contributors may be used to endorse or promote products derived
|
|
|
|
** from this software without specific prior written permission.
|
|
|
|
**
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <QtBluetooth/qlowenergyadvertisingdata.h>
|
|
|
|
#include <QtBluetooth/qlowenergyadvertisingparameters.h>
|
|
|
|
#include <QtBluetooth/qlowenergycharacteristic.h>
|
|
|
|
#include <QtBluetooth/qlowenergycharacteristicdata.h>
|
|
|
|
#include <QtBluetooth/qlowenergydescriptordata.h>
|
|
|
|
#include <QtBluetooth/qlowenergycontroller.h>
|
|
|
|
#include <QtBluetooth/qlowenergyservice.h>
|
|
|
|
#include <QtBluetooth/qlowenergyservicedata.h>
|
|
|
|
#include <QtCore/qbytearray.h>
|
2020-02-11 11:48:38 +00:00
|
|
|
#ifndef Q_OS_ANDROID
|
2016-01-15 14:30:53 +00:00
|
|
|
#include <QtCore/qcoreapplication.h>
|
2020-02-11 11:48:38 +00:00
|
|
|
#else
|
|
|
|
#include <QtGui/qguiapplication.h>
|
|
|
|
#endif
|
2016-01-15 14:30:53 +00:00
|
|
|
#include <QtCore/qlist.h>
|
2017-04-06 11:29:15 +00:00
|
|
|
#include <QtCore/qloggingcategory.h>
|
2016-01-15 14:30:53 +00:00
|
|
|
#include <QtCore/qscopedpointer.h>
|
|
|
|
#include <QtCore/qtimer.h>
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2017-04-06 11:29:15 +00:00
|
|
|
//QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true"));
|
2020-02-11 11:48:38 +00:00
|
|
|
#ifndef Q_OS_ANDROID
|
2016-01-15 14:30:53 +00:00
|
|
|
QCoreApplication app(argc, argv);
|
2020-02-11 11:48:38 +00:00
|
|
|
#else
|
|
|
|
QGuiApplication app(argc, argv);
|
|
|
|
#endif
|
2016-01-15 14:30:53 +00:00
|
|
|
|
|
|
|
//! [Advertising Data]
|
|
|
|
QLowEnergyAdvertisingData advertisingData;
|
|
|
|
advertisingData.setDiscoverability(QLowEnergyAdvertisingData::DiscoverabilityGeneral);
|
|
|
|
advertisingData.setIncludePowerLevel(true);
|
|
|
|
advertisingData.setLocalName("HeartRateServer");
|
|
|
|
advertisingData.setServices(QList<QBluetoothUuid>() << QBluetoothUuid::HeartRate);
|
|
|
|
//! [Advertising Data]
|
|
|
|
|
|
|
|
//! [Service Data]
|
|
|
|
QLowEnergyCharacteristicData charData;
|
|
|
|
charData.setUuid(QBluetoothUuid::HeartRateMeasurement);
|
|
|
|
charData.setValue(QByteArray(2, 0));
|
|
|
|
charData.setProperties(QLowEnergyCharacteristic::Notify);
|
|
|
|
const QLowEnergyDescriptorData clientConfig(QBluetoothUuid::ClientCharacteristicConfiguration,
|
|
|
|
QByteArray(2, 0));
|
|
|
|
charData.addDescriptor(clientConfig);
|
|
|
|
|
|
|
|
QLowEnergyServiceData serviceData;
|
|
|
|
serviceData.setType(QLowEnergyServiceData::ServiceTypePrimary);
|
|
|
|
serviceData.setUuid(QBluetoothUuid::HeartRate);
|
|
|
|
serviceData.addCharacteristic(charData);
|
|
|
|
//! [Service Data]
|
|
|
|
|
|
|
|
//! [Start Advertising]
|
|
|
|
const QScopedPointer<QLowEnergyController> leController(QLowEnergyController::createPeripheral());
|
2018-08-30 08:46:58 +00:00
|
|
|
QScopedPointer<QLowEnergyService> service(leController->addService(serviceData));
|
2016-01-15 14:30:53 +00:00
|
|
|
leController->startAdvertising(QLowEnergyAdvertisingParameters(), advertisingData,
|
|
|
|
advertisingData);
|
|
|
|
//! [Start Advertising]
|
|
|
|
|
|
|
|
//! [Provide Heartbeat]
|
|
|
|
QTimer heartbeatTimer;
|
|
|
|
quint8 currentHeartRate = 60;
|
|
|
|
enum ValueChange { ValueUp, ValueDown } valueChange = ValueUp;
|
|
|
|
const auto heartbeatProvider = [&service, ¤tHeartRate, &valueChange]() {
|
|
|
|
QByteArray value;
|
|
|
|
value.append(char(0)); // Flags that specify the format of the value.
|
|
|
|
value.append(char(currentHeartRate)); // Actual value.
|
|
|
|
QLowEnergyCharacteristic characteristic
|
|
|
|
= service->characteristic(QBluetoothUuid::HeartRateMeasurement);
|
|
|
|
Q_ASSERT(characteristic.isValid());
|
|
|
|
service->writeCharacteristic(characteristic, value); // Potentially causes notification.
|
|
|
|
if (currentHeartRate == 60)
|
|
|
|
valueChange = ValueUp;
|
|
|
|
else if (currentHeartRate == 100)
|
|
|
|
valueChange = ValueDown;
|
|
|
|
if (valueChange == ValueUp)
|
|
|
|
++currentHeartRate;
|
|
|
|
else
|
|
|
|
--currentHeartRate;
|
|
|
|
};
|
|
|
|
QObject::connect(&heartbeatTimer, &QTimer::timeout, heartbeatProvider);
|
|
|
|
heartbeatTimer.start(1000);
|
|
|
|
//! [Provide Heartbeat]
|
|
|
|
|
2018-08-30 08:46:58 +00:00
|
|
|
auto reconnect = [&leController, advertisingData, &service, serviceData]()
|
|
|
|
{
|
|
|
|
service.reset(leController->addService(serviceData));
|
|
|
|
if (!service.isNull())
|
|
|
|
leController->startAdvertising(QLowEnergyAdvertisingParameters(),
|
|
|
|
advertisingData, advertisingData);
|
2016-01-15 14:30:53 +00:00
|
|
|
};
|
|
|
|
QObject::connect(leController.data(), &QLowEnergyController::disconnected, reconnect);
|
|
|
|
|
|
|
|
return app.exec();
|
|
|
|
}
|