Use heartrate-game example for snippets in BTLE overview
heartlistener example will be removed. Change-Id: Iaa5c5f629cf351b97c18f61b4fab2cbbd8d0744c Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io> Reviewed-by: Oliver Wolff <oliver.wolff@qt.io> Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
This commit is contained in:
parent
2274719da5
commit
e83df9ffb9
|
@ -46,6 +46,7 @@ DeviceFinder::DeviceFinder(DeviceHandler *handler, QObject *parent):
|
|||
BluetoothBaseClass(parent),
|
||||
m_deviceHandler(handler)
|
||||
{
|
||||
//! [devicediscovery-1]
|
||||
m_deviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
|
||||
m_deviceDiscoveryAgent->setLowEnergyDiscoveryTimeout(5000);
|
||||
|
||||
|
@ -55,6 +56,7 @@ DeviceFinder::DeviceFinder(DeviceHandler *handler, QObject *parent):
|
|||
|
||||
connect(m_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &DeviceFinder::scanFinished);
|
||||
connect(m_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::canceled, this, &DeviceFinder::scanFinished);
|
||||
//! [devicediscovery-1]
|
||||
|
||||
|
||||
#ifdef SIMULATOR
|
||||
|
@ -82,21 +84,28 @@ void DeviceFinder::startSearch()
|
|||
#ifdef SIMULATOR
|
||||
m_demoTimer.start();
|
||||
#else
|
||||
//! [devicediscovery-2]
|
||||
m_deviceDiscoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
|
||||
//! [devicediscovery-2]
|
||||
#endif
|
||||
emit scanningChanged();
|
||||
setInfo(tr("Scanning for devices..."));
|
||||
}
|
||||
|
||||
//! [devicediscovery-3]
|
||||
void DeviceFinder::addDevice(const QBluetoothDeviceInfo &device)
|
||||
{
|
||||
// If device is LowEnergy-device, add it to the list
|
||||
if (device.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) {
|
||||
m_devices.append(new DeviceInfo(device));
|
||||
setInfo(tr("Low Energy device found. Scanning more..."));
|
||||
//! [devicediscovery-3]
|
||||
emit devicesChanged();
|
||||
//! [devicediscovery-4]
|
||||
}
|
||||
//...
|
||||
}
|
||||
//! [devicediscovery-4]
|
||||
|
||||
void DeviceFinder::scanError(QBluetoothDeviceDiscoveryAgent::Error error)
|
||||
{
|
||||
|
|
|
@ -103,8 +103,11 @@ void DeviceHandler::setDevice(DeviceInfo *device)
|
|||
if (m_currentDevice) {
|
||||
|
||||
// Make connections
|
||||
//! [Connect-Signals-1]
|
||||
m_control = new QLowEnergyController(m_currentDevice->getDevice(), this);
|
||||
//! [Connect-Signals-1]
|
||||
m_control->setRemoteAddressType(m_addressType);
|
||||
//! [Connect-Signals-2]
|
||||
connect(m_control, &QLowEnergyController::serviceDiscovered,
|
||||
this, &DeviceHandler::serviceDiscovered);
|
||||
connect(m_control, &QLowEnergyController::discoveryFinished,
|
||||
|
@ -125,6 +128,7 @@ void DeviceHandler::setDevice(DeviceInfo *device)
|
|||
|
||||
// Connect
|
||||
m_control->connectToDevice();
|
||||
//! [Connect-Signals-2]
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -149,6 +153,7 @@ void DeviceHandler::stopMeasurement()
|
|||
emit measuringChanged();
|
||||
}
|
||||
|
||||
//! [Filter HeartRate service 1]
|
||||
void DeviceHandler::serviceDiscovered(const QBluetoothUuid &gatt)
|
||||
{
|
||||
if (gatt == QBluetoothUuid(QBluetoothUuid::HeartRate)) {
|
||||
|
@ -156,6 +161,7 @@ void DeviceHandler::serviceDiscovered(const QBluetoothUuid &gatt)
|
|||
m_foundHeartRateService = true;
|
||||
}
|
||||
}
|
||||
//! [Filter HeartRate service 1]
|
||||
|
||||
void DeviceHandler::serviceScanDone()
|
||||
{
|
||||
|
@ -167,6 +173,7 @@ void DeviceHandler::serviceScanDone()
|
|||
m_service = 0;
|
||||
}
|
||||
|
||||
//! [Filter HeartRate service 2]
|
||||
// If heartRateService found, create new service
|
||||
if (m_foundHeartRateService)
|
||||
m_service = m_control->createServiceObject(QBluetoothUuid(QBluetoothUuid::HeartRate), this);
|
||||
|
@ -179,9 +186,11 @@ void DeviceHandler::serviceScanDone()
|
|||
} else {
|
||||
setError("Heart Rate Service not found.");
|
||||
}
|
||||
//! [Filter HeartRate service 2]
|
||||
}
|
||||
|
||||
// Service functions
|
||||
//! [Find HRM characteristic]
|
||||
void DeviceHandler::serviceStateChanged(QLowEnergyService::ServiceState s)
|
||||
{
|
||||
switch (s) {
|
||||
|
@ -211,7 +220,9 @@ void DeviceHandler::serviceStateChanged(QLowEnergyService::ServiceState s)
|
|||
|
||||
emit aliveChanged();
|
||||
}
|
||||
//! [Find HRM characteristic]
|
||||
|
||||
//! [Reading value]
|
||||
void DeviceHandler::updateHeartRateValue(const QLowEnergyCharacteristic &c, const QByteArray &value)
|
||||
{
|
||||
// ignore any other characteristic change -> shouldn't really happen though
|
||||
|
@ -230,6 +241,7 @@ void DeviceHandler::updateHeartRateValue(const QLowEnergyCharacteristic &c, cons
|
|||
|
||||
addMeasurement(hrvalue);
|
||||
}
|
||||
//! [Reading value]
|
||||
|
||||
#ifdef SIMULATOR
|
||||
void DeviceHandler::updateDemoHR()
|
||||
|
|
|
@ -165,7 +165,7 @@ Low Energy devices.
|
|||
their services, as well as reading and writing data stored on the device.
|
||||
On the server side, it allows to set up services, advertise them, and get notified when the
|
||||
client writes characteristics.
|
||||
The example code below is taken from the \l {heartlistener}{Heart Listener} and
|
||||
The example code below is taken from the \l {heartrate-game}{Heart Rate Game} and
|
||||
\l {heartrate-server}{Heart Rate Server} examples.
|
||||
|
||||
\section2 Establishing a Connection
|
||||
|
@ -176,15 +176,15 @@ Low Energy devices.
|
|||
\l QBluetoothDeviceDiscoveryAgent class. We connect to its \l {QBluetoothDeviceDiscoveryAgent::deviceDiscovered()}
|
||||
signal and start the search with \l {QBluetoothDeviceDiscoveryAgent::start()}{start()}:
|
||||
|
||||
\snippet heartlistener/heartrate.cpp devicediscovery-1
|
||||
\snippet heartlistener/heartrate.cpp devicediscovery-2
|
||||
\snippet heartrate-game/devicefinder.cpp devicediscovery-1
|
||||
\snippet heartrate-game/devicefinder.cpp devicediscovery-2
|
||||
|
||||
Since we are only interested in Low Energy devices we filter the device type within the
|
||||
receiving slot. The device type can be ascertained using the \l QBluetoothDeviceInfo::coreConfigurations()
|
||||
flag:
|
||||
|
||||
\snippet heartlistener/heartrate.cpp devicediscovery-3
|
||||
\snippet heartlistener/heartrate.cpp devicediscovery-4
|
||||
\snippet heartrate-game/devicefinder.cpp devicediscovery-3
|
||||
\snippet heartrate-game/devicefinder.cpp devicediscovery-4
|
||||
|
||||
Once the address of the peripheral device is known we use the \l QLowEnergyController class.
|
||||
This class is the entry point for all Bluetooth Low Energy development. The constructor of the class
|
||||
|
@ -192,20 +192,20 @@ Low Energy devices.
|
|||
directly connect to the device using
|
||||
\l {QLowEnergyController::connectToDevice()}{connectToDevice()}:
|
||||
|
||||
\snippet heartlistener/heartrate.cpp Connect signals
|
||||
\snippet heartrate-game/devicehandler.cpp Connect-Signals-1
|
||||
\snippet heartrate-game/devicehandler.cpp Connect-Signals-2
|
||||
|
||||
\section2 Service Search
|
||||
|
||||
As soon as the connection is established we initiate the service discovery:
|
||||
|
||||
\snippet heartlistener/heartrate.cpp Connecting to service
|
||||
The above code snippet how the application initiates the service discovery once the connection has
|
||||
been established.
|
||||
|
||||
The \c serviceDiscovered() slot below is triggered as a result of the
|
||||
\l {QLowEnergyController::serviceDiscovered()} signal and provides an intermittent progress report.
|
||||
Since we are talking about the heart listener app which monitors HeartRate devices in the vicinity
|
||||
we ignore any service that is not of type \l QBluetoothUuid::HeartRate.
|
||||
|
||||
\snippet heartlistener/heartrate.cpp Filter HeartRate service 1
|
||||
\snippet heartrate-game/devicehandler.cpp Filter HeartRate service 1
|
||||
|
||||
Eventually the \l {QLowEnergyController::discoveryFinished()} signal is emitted to indicate
|
||||
the successful completion of the service discovery. Provided a HeartRate service was found,
|
||||
|
@ -213,14 +213,14 @@ Low Energy devices.
|
|||
provides the required signals for update notifications and the discovery of service details
|
||||
is triggered using \l QLowEnergyService::discoverDetails():
|
||||
|
||||
\snippet heartlistener/heartrate.cpp Filter HeartRate service 2
|
||||
\snippet heartrate-game/devicehandler.cpp Filter HeartRate service 2
|
||||
|
||||
During the detail search the service's \l {QLowEnergyService::state()}{state()} transitions
|
||||
from \l {QLowEnergyService::DiscoveryRequired}{DiscoveryRequired} to
|
||||
\l {QLowEnergyService::DiscoveringServices}{DiscoveringServices} and eventually ends with
|
||||
\l {QLowEnergyService::ServiceDiscovered}{ServiceDiscovered}:
|
||||
|
||||
\snippet heartlistener/heartrate.cpp Find HRM characteristic
|
||||
\snippet heartrate-game/devicehandler.cpp Find HRM characteristic
|
||||
|
||||
\section2 Interaction with the Peripheral Device
|
||||
|
||||
|
@ -235,8 +235,7 @@ Low Energy devices.
|
|||
|
||||
Finally, we process the value of the HeartRate characteristic, as per Bluetooth Low Energy standard:
|
||||
|
||||
\snippet heartlistener/heartrate.cpp Reading value 1
|
||||
\snippet heartlistener/heartrate.cpp Reading value 2
|
||||
\snippet heartrate-game/devicehandler.cpp Reading value
|
||||
|
||||
In general a characteristic value is a series of bytes. The precise interpretation of
|
||||
those bytes depends on the characteristic type and value structure.
|
||||
|
|
Loading…
Reference in New Issue