Bluetooth Low Energy Heart Rate Server Example

An example demonstrating how to set up and advertise a GATT service. The example demonstrates the use of the Qt Bluetooth Low Energy classes related to peripheral (slave) functionality.

The Bluetooth Low Energy Heart Rate Server is a command-line application that shows how to develop a Bluetooth GATT server using the Qt Bluetooth API. The application covers setting up a service, advertising it and notifying clients about changes to characteristic values.

The example makes use of the following Qt classes:

The example implements a server application, which means it has no graphical user interface. To visualize what it is doing, you can use the Heart Rate Game example, which is basically the client-side counterpart to this application.

Note: On Linux, advertising requires privileged access, so you need to run the example as root, for instance via sudo.

Setting up Advertising Data and Parameters

Two classes are used to configure the advertising process: QLowEnergyAdvertisingData to specify which information is to be broadcast, and QLowEnergyAdvertisingParameters for specific aspects such as setting the advertising interval or controlling which devices are allowed to connect. In our example, we simply use the default parameters.

The information contained in the QLowEnergyAdvertisingData will be visible to other devices that are currently scanning. They can use it to decide whether they want to establish a connection or not. In our example, we include the type of service we offer, a name that adequately describes our device to humans, and the transmit power level of the device. The latter is often useful to potential clients, because they can tell how far away our device is by comparing the received signal strength to the advertised one.

Note: Space for the advertising data is very limited (only 31 bytes in total), so variable-length data such as the device name should be kept as short as possible.

QLowEnergyAdvertisingData advertisingData;
advertisingData.setDiscoverability(QLowEnergyAdvertisingData::DiscoverabilityGeneral);
advertisingData.setIncludePowerLevel(true);
advertisingData.setLocalName("HeartRateServer");
advertisingData.setServices(QList<QBluetoothUuid>() << QBluetoothUuid::HeartRate);

Setting up Service Data

Next we configure the kind of service we want to offer. We use the Heart Rate service as defined in the Bluetooth specification in its minimal form, that is, consisting only of the Heart Rate Measurement characteristic. This characteristic must support the Notify property (and no others), and it needs to have a Client Characteristic Configuration descriptor, which enables clients to register to get notified about changes to characteristic values. We set the initial heart rate value to zero, as it cannot be read anyway (the only way the client can get at the value is via notifications).

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);

Advertising and Listening for Incoming Connections

Now that all the data has been set up, we can start advertising. First we create a QLowEnergyController object in the peripheral role and use it to create a (dynamic) QLowEnergyService object from our (static) QLowEnergyServiceData. Then we call QLowEnergyController::startAdvertising(). Note that we hand in our QLowEnergyAdvertisingData twice: The first argument acts as the actual advertising data, the second one as the scan response data. They could transport different information, but here we don't have a need for that. We also pass a default-constructed instance of QLowEnergyAdvertisingParameters, because the default advertising parameters are fine for us. If a client is interested in the advertised service, it can now establish a connection to our device. When that happens, the device stops advertising and the QLowEnergyController::connected() signal is emitted.

Note: When a client disconnects, advertising does not resume automatically. If you want that to happen, you need to connect to the QLowEnergyController::disconnected() signal and call QLowEnergyController::startAdvertising() in the respective slot.

const QScopedPointer<QLowEnergyController> leController(QLowEnergyController::createPeripheral());
QScopedPointer<QLowEnergyService> service(leController->addService(serviceData));
leController->startAdvertising(QLowEnergyAdvertisingParameters(), advertisingData,
                               advertisingData);

Providing the Heartrate

So far, so good. But how does a client actually get at the heart rate? This happens by regularly updating the value of the respective characteristic in the QLowEnergyService object that we received from the QLowEnergyController in the code snippet above. The source of the heart rate would normally be some kind of sensor, but in our example, we just make up values that we let oscillate between 60 and 100. The most important part in the following code snippet is the call to QLowEnergyService::writeCharacteristic. If a client is currently connected and has enabled notifications by writing to the aforementioned Client Characteristic Configuration, it will get notified about the new value.

QTimer heartbeatTimer;
quint8 currentHeartRate = 60;
enum ValueChange { ValueUp, ValueDown } valueChange = ValueUp;
const auto heartbeatProvider = [&service, &currentHeartRate, &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);

Example project @ code.qt.io

© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.