Bluetooth Low Energy Heart Rate Server Example

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.

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.

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.

 
Sélectionnez
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).

 
Sélectionnez
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 QL