From cfdd43d118fbc54a3157eb0c9f7c3ab2bf8cc822 Mon Sep 17 00:00:00 2001 From: Alex Blasche Date: Thu, 9 Apr 2015 17:05:27 +0200 Subject: Android: Implement QLEService:readCharacateristic & readDescriptor In general we extend the concept of the write queue. Not only do we queue up write requests but also read requests. The handling of read errors is still missing. Right now the QLEService::error() signal is not emitted when a readCharacteristic() and readDescriptor() call fails. Change-Id: I4b4f086c351c4a29d6e48e8ee9079e9f33f36539 Reviewed-by: Timur Pocheptsov Reviewed-by: Alex Blasche --- src/bluetooth/qlowenergycontroller_android.cpp | 63 ++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 4 deletions(-) (limited to 'src/bluetooth/qlowenergycontroller_android.cpp') diff --git a/src/bluetooth/qlowenergycontroller_android.cpp b/src/bluetooth/qlowenergycontroller_android.cpp index 2794fe17..c1fa0813 100644 --- a/src/bluetooth/qlowenergycontroller_android.cpp +++ b/src/bluetooth/qlowenergycontroller_android.cpp @@ -236,18 +236,57 @@ void QLowEnergyControllerPrivate::writeDescriptor( } void QLowEnergyControllerPrivate::readCharacteristic( - const QSharedPointer /*service*/, - const QLowEnergyHandle /*charHandle*/) + const QSharedPointer service, + const QLowEnergyHandle charHandle) { + Q_ASSERT(!service.isNull()); + + if (!service->characteristicList.contains(charHandle)) + return; + + QAndroidJniEnvironment env; + bool result = false; + if (hub) { + qCDebug(QT_BT_ANDROID) << "Read characteristic with handle" + << charHandle << service->uuid; + result = hub->javaObject().callMethod("readCharacteristic", + "(I)Z", charHandle); + } + if (env->ExceptionOccurred()) { + env->ExceptionDescribe(); + env->ExceptionClear(); + result = false; + } + + if (!result) + service->setError(QLowEnergyService::CharacteristicWriteError); } void QLowEnergyControllerPrivate::readDescriptor( - const QSharedPointer /*service*/, + const QSharedPointer service, const QLowEnergyHandle /*charHandle*/, - const QLowEnergyHandle /*descriptorHandle*/) + const QLowEnergyHandle descriptorHandle) { + Q_ASSERT(!service.isNull()); + + QAndroidJniEnvironment env; + bool result = false; + if (hub) { + qCDebug(QT_BT_ANDROID) << "Read descriptor with handle" + << descriptorHandle << service->uuid; + result = hub->javaObject().callMethod("readDescriptor", + "(I)Z", descriptorHandle); + } + + if (env->ExceptionOccurred()) { + env->ExceptionDescribe(); + env->ExceptionClear(); + result = false; + } + if (!result) + service->setError(QLowEnergyService::DescriptorWriteError); } void QLowEnergyControllerPrivate::connectionUpdated( @@ -390,6 +429,15 @@ void QLowEnergyControllerPrivate::characteristicRead( charDetails.value = data; //value handle always one larger than characteristics value handle charDetails.valueHandle = charHandle + 1; + + if (service->state == QLowEnergyService::ServiceDiscovered) { + QLowEnergyCharacteristic characteristic = characteristicForHandle(charHandle); + if (!characteristic.isValid()) { + qCWarning(QT_BT_ANDROID) << "characteristicRead: Cannot find characteristic"; + return; + } + emit service->characteristicRead(characteristic, data); + } } void QLowEnergyControllerPrivate::descriptorRead( @@ -421,6 +469,13 @@ void QLowEnergyControllerPrivate::descriptorRead( if (!entryUpdated) { qCWarning(QT_BT_ANDROID) << "Cannot find/update descriptor" << descUuid << charUuid << serviceUuid; + } else if (service->state == QLowEnergyService::ServiceDiscovered){ + QLowEnergyDescriptor descriptor = descriptorForHandle(descHandle); + if (!descriptor.isValid()) { + qCWarning(QT_BT_ANDROID) << "descriptorRead: Cannot find descriptor"; + return; + } + emit service->descriptorRead(descriptor, data); } } -- cgit v1.2.3