Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qlowenergyservice.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2016 Javier S. Pedro <maemo@javispedro.com>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#include <QtCore/QCoreApplication>
6#include <QtCore/QPointer>
7#include <QtBluetooth/QLowEnergyService>
8
9#include <algorithm>
10
13
14#ifdef Q_OS_DARWIN
16#endif // Q_OS_DARWIN
17
19
24
25
370QLowEnergyService::QLowEnergyService(QSharedPointer<QLowEnergyServicePrivate> p,
371 QObject *parent)
372 : QObject(parent),
373 d_ptr(p)
374{
375 qRegisterMetaType<QLowEnergyService::ServiceState>();
376 qRegisterMetaType<QLowEnergyService::ServiceError>();
377 qRegisterMetaType<QLowEnergyService::ServiceType>();
378 qRegisterMetaType<QLowEnergyService::WriteMode>();
379
394}
395
402
420QList<QBluetoothUuid> QLowEnergyService::includedServices() const
421{
422 return d_ptr->includedServices;
423}
424
453
466QLowEnergyService::ServiceTypes QLowEnergyService::type() const
467{
468 return d_ptr->type;
469}
470
481{
483 for ( ; charIt != d_ptr->characteristicList.constEnd(); ++charIt) {
484 const QLowEnergyHandle charHandle = charIt.key();
485 const QLowEnergyServicePrivate::CharData &charDetails = charIt.value();
486
487 if (charDetails.uuid == uuid)
488 return QLowEnergyCharacteristic(d_ptr, charHandle);
489 }
490
492}
493
503QList<QLowEnergyCharacteristic> QLowEnergyService::characteristics() const
504{
505 QList<QLowEnergyCharacteristic> results;
506 QList<QLowEnergyHandle> handles = d_ptr->characteristicList.keys();
507 std::sort(handles.begin(), handles.end());
508
509 for (const QLowEnergyHandle &handle : std::as_const(handles)) {
512 }
513 return results;
514}
515
516
521{
522 return d_ptr->uuid;
523}
524
525
533{
534 bool ok = false;
535 quint16 clsId = d_ptr->uuid.toUInt16(&ok);
536 if (ok) {
538 = static_cast<QBluetoothUuid::ServiceClassUuid>(clsId);
540 if (!name.isEmpty())
541 return name;
542 }
543 return qApp ?
544 qApp->translate("QBluetoothServiceDiscoveryAgent", "Unknown Service") :
545 QStringLiteral("Unknown Service");
546}
547
577{
579
580 if (!d->controller || d->state == QLowEnergyService::InvalidService) {
582 return;
583 }
584
586 return;
587
589
590 d->controller->discoverServiceDetails(d->uuid, mode);
591}
592
600
609{
610 if (characteristic.d_ptr.isNull() || !characteristic.data)
611 return false;
612
613 if (d_ptr == characteristic.d_ptr
614 && d_ptr->characteristicList.contains(characteristic.attributeHandle())) {
615 return true;
616 }
617
618 return false;
619}
620
621
645 const QLowEnergyCharacteristic &characteristic)
646{
648
649 if (d->controller == nullptr || state() != RemoteServiceDiscovered || !contains(characteristic)) {
651 return;
652 }
653
654 d->controller->readCharacteristic(characteristic.d_ptr,
655 characteristic.attributeHandle());
656}
657
714 const QLowEnergyCharacteristic &characteristic,
716{
717 //TODO check behavior when writing to WriteSigned characteristic
719
720 if (d->controller == nullptr
721 || (d->controller->role == QLowEnergyController::CentralRole
725 return;
726 }
727
728 // don't write if properties don't permit it
729 d->controller->writeCharacteristic(characteristic.d_ptr,
730 characteristic.attributeHandle(),
731 newValue,
732 mode);
733}
734
739{
740 if (descriptor.d_ptr.isNull() || !descriptor.data)
741 return false;
742
743 const QLowEnergyHandle charHandle = descriptor.characteristicHandle();
744 if (!charHandle)
745 return false;
746
747 if (d_ptr == descriptor.d_ptr
748 && d_ptr->characteristicList.contains(charHandle)
749 && d_ptr->characteristicList[charHandle].descriptorList.contains(descriptor.handle()))
750 {
751 return true;
752 }
753
754 return false;
755}
756
775 const QLowEnergyDescriptor &descriptor)
776{
778
779 if (d->controller == nullptr || state() != RemoteServiceDiscovered || !contains(descriptor)) {
781 return;
782 }
783
784 d->controller->readDescriptor(descriptor.d_ptr,
785 descriptor.characteristicHandle(),
786 descriptor.handle());
787}
788
817 const QByteArray &newValue)
818{
820
821 if (d->controller == nullptr
822 || (d->controller->role == QLowEnergyController::CentralRole
824 || !contains(descriptor)) {
826 return;
827 }
828#ifdef Q_OS_DARWIN
830 // We have to identify a special case - ClientCharacteristicConfiguration
831 // since with CoreBluetooth:
832 //
833 // "You cannot use this method to write the value of a client configuration descriptor
834 // (represented by the CBUUIDClientCharacteristicConfigurationString constant),
835 // which describes how notification or indications are configured for a
836 // characteristic’s value with respect to a client. If you want to manage
837 // notifications or indications for a characteristic’s value, you must
838 // use the setNotifyValue:forCharacteristic: method instead."
839 auto controller = static_cast<QLowEnergyControllerPrivateDarwin *>(d->controller.data());
840 return controller->setNotifyValue(descriptor.d_ptr, descriptor.characteristicHandle(), newValue);
841 }
842#endif // Q_OS_DARWIN
843
844 d->controller->writeDescriptor(descriptor.d_ptr,
845 descriptor.characteristicHandle(),
846 descriptor.handle(),
847 newValue);
848}
849
851
852#include "moc_qlowenergyservice.cpp"
\inmodule QtBluetooth
quint16 toUInt16(bool *ok=nullptr) const
Returns the 16 bit representation of this UUID.
ServiceClassUuid
This enum is a convienience type for Bluetooth service class and profile UUIDs.
static QString serviceClassToString(ServiceClassUuid uuid)
Returns a human-readable and translated name for the given service class represented by uuid.
\inmodule QtCore
Definition qbytearray.h:57
void append(parameter_type t)
Definition qlist.h:458
\inmodule QtBluetooth
QBluetoothUuid uuid() const
Returns the UUID of this descriptor if \l isValid() returns true; otherwise a \l {QUuid::isNull()}{nu...
void characteristicWritten(const QLowEnergyCharacteristic &characteristic, const QByteArray &newValue)
QList< QBluetoothUuid > includedServices
void characteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &newValue)
void characteristicRead(const QLowEnergyCharacteristic &info, const QByteArray &value)
void descriptorWritten(const QLowEnergyDescriptor &descriptor, const QByteArray &newValue)
void descriptorRead(const QLowEnergyDescriptor &info, const QByteArray &value)
QLowEnergyService::ServiceError lastError
QLowEnergyService::ServiceTypes type
void stateChanged(QLowEnergyService::ServiceState newState)
void errorOccurred(QLowEnergyService::ServiceError error)
QLowEnergyService::ServiceState state
QHash< QLowEnergyHandle, CharData > characteristicList
\inmodule QtBluetooth
void characteristicChanged(const QLowEnergyCharacteristic &info, const QByteArray &value)
If the associated controller object is in the \l {QLowEnergyController::CentralRole}{central} role,...
void descriptorRead(const QLowEnergyDescriptor &info, const QByteArray &value)
This signal is emitted when the read request for descriptor successfully returned its value.
void readDescriptor(const QLowEnergyDescriptor &descriptor)
Reads the value of descriptor.
void stateChanged(QLowEnergyService::ServiceState newState)
This signal is emitted when the service's state changes.
void readCharacteristic(const QLowEnergyCharacteristic &characteristic)
Reads the value of characteristic.
void errorOccurred(QLowEnergyService::ServiceError error)
This signal is emitted when an error occurrs.
QLowEnergyCharacteristic characteristic(const QBluetoothUuid &uuid) const
Returns the matching characteristic for uuid; otherwise an invalid characteristic.
DiscoveryMode
This enum lists service discovery modes.
ServiceError error() const
Returns the last occurred error or \l NoError.
void writeDescriptor(const QLowEnergyDescriptor &descriptor, const QByteArray &newValue)
Writes newValue as value for descriptor.
QString serviceName() const
Returns the name of the service; otherwise an empty string.
QLowEnergyService::ServiceTypes type() const
Returns the type of the service.
void characteristicRead(const QLowEnergyCharacteristic &info, const QByteArray &value)
This signal is emitted when the read request for characteristic successfully returned its value.
void characteristicWritten(const QLowEnergyCharacteristic &info, const QByteArray &value)
This signal is emitted when the value of characteristic is successfully changed to newValue.
QBluetoothUuid serviceUuid() const
Returns the UUID of the service; otherwise a null UUID.
ServiceError
This enum describes all possible error conditions during the service's existence.
QList< QBluetoothUuid > includedServices() const
Returns the UUIDs of all services which are included by the current service.
ServiceState
This enum describes the \l state() of the service object.
WriteMode
This enum describes the mode to be used when writing a characteristic value.
void descriptorWritten(const QLowEnergyDescriptor &info, const QByteArray &value)
This signal is emitted when the value of descriptor is successfully changed to newValue.
bool contains(const QLowEnergyCharacteristic &characteristic) const
Returns true if characteristic belongs to this service; otherwise false.
~QLowEnergyService()
Destroys the \l QLowEnergyService instance.
void writeCharacteristic(const QLowEnergyCharacteristic &characteristic, const QByteArray &newValue, WriteMode mode=WriteWithResponse)
Writes newValue as value for the characteristic.
QLowEnergyService::ServiceState state() const
Returns the current state of the service.
void discoverDetails(DiscoveryMode mode=FullDiscovery)
Initiates discovery of the service's included services, characteristics, and their associated descrip...
ServiceType
This enum describes the type of the service.
QList< QLowEnergyCharacteristic > characteristics() const
Returns all characteristics associated with this QLowEnergyService instance.
\inmodule QtCore
Definition qobject.h:103
bool isNull() const noexcept
Returns true if this object refers to \nullptr.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
else opt state
[0]
Combined button and popup list for selecting options.
quint16 QLowEnergyHandle
Definition qbluetooth.h:42
#define qApp
#define QT_IMPL_METATYPE_EXTERN_TAGGED(TYPE, TAG)
Definition qmetatype.h:1384
GLuint64 GLenum void * handle
GLenum mode
GLuint name
GLfloat GLfloat p
[1]
#define QStringLiteral(str)
unsigned short quint16
Definition qtypes.h:48
connect(quitButton, &QPushButton::clicked, &app, &QCoreApplication::quit, Qt::QueuedConnection)