summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Blasche <alexander.blasche@qt.io>2017-01-20 14:35:27 +0100
committerAlex Blasche <alexander.blasche@qt.io>2017-02-06 15:04:23 +0000
commit7bde4b5dbd0381cf2f79158074625e9dea52b67e (patch)
tree7a205e5c5cf93f126a47ccc8fe5decc3533386cb
parent319fb3088ab5dd0ce347bcc76bce7bc197bbf488 (diff)
Provide support for QLEService::writeDescriptor() on Android Peripheral
This permits the server itself to change descriptors at runtime. Change-Id: I5d60cfb1838ba83737cd748671520c7072201bc4 Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io> Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
-rw-r--r--src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLEServer.java38
-rw-r--r--src/bluetooth/qlowenergycontroller_android.cpp27
2 files changed, 60 insertions, 5 deletions
diff --git a/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLEServer.java b/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLEServer.java
index 7930d0fd..00217904 100644
--- a/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLEServer.java
+++ b/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLEServer.java
@@ -497,6 +497,44 @@ public class QtBluetoothLEServer {
}
/*
+ Updates the local database value for the given \a descUuid to \a newValue.
+
+ This function is called from the Qt thread.
+ */
+ public boolean writeDescriptor(BluetoothGattService service, UUID charUuid, UUID descUuid,
+ byte[] newValue)
+ {
+ BluetoothGattDescriptor foundDesc = null;
+ BluetoothGattCharacteristic foundChar = null;
+ final List<BluetoothGattCharacteristic> charList = service.getCharacteristics();
+ for (BluetoothGattCharacteristic iter: charList) {
+ if (!iter.getUuid().equals(charUuid))
+ continue;
+
+ if (foundChar == null) {
+ foundChar = iter;
+ } else {
+ Log.w(TAG, "Found second char with same UUID. Wrong char may have been selected.");
+ break;
+ }
+ }
+
+ if (foundChar != null)
+ foundDesc = foundChar.getDescriptor(descUuid);
+
+ if (foundChar == null || foundDesc == null) {
+ Log.w(TAG, "writeDescriptor: update for unknown char or desc failed (" + foundChar + ")");
+ return false;
+ }
+
+ // we even write CLIENT_CHARACTERISTIC_CONFIGURATION_UUID this way as we choose
+ // to interpret the server's call as a change of the default value.
+ foundDesc.setValue(newValue);
+
+ return true;
+ }
+
+ /*
* Call back handler for Advertisement requests.
*/
private AdvertiseCallback mAdvertiseListener = new AdvertiseCallback()
diff --git a/src/bluetooth/qlowenergycontroller_android.cpp b/src/bluetooth/qlowenergycontroller_android.cpp
index b5485480..a1decd96 100644
--- a/src/bluetooth/qlowenergycontroller_android.cpp
+++ b/src/bluetooth/qlowenergycontroller_android.cpp
@@ -290,7 +290,7 @@ void QLowEnergyControllerPrivate::writeCharacteristic(
void QLowEnergyControllerPrivate::writeDescriptor(
const QSharedPointer<QLowEnergyServicePrivate> service,
- const QLowEnergyHandle /*charHandle*/,
+ const QLowEnergyHandle charHandle,
const QLowEnergyHandle descHandle,
const QByteArray &newValue)
{
@@ -304,10 +304,27 @@ void QLowEnergyControllerPrivate::writeDescriptor(
bool result = false;
if (hub) {
- qCDebug(QT_BT_ANDROID) << "Write descriptor with handle " << descHandle
- << newValue.toHex() << "(service:" << service->uuid << ")";
- result = hub->javaObject().callMethod<jboolean>("writeDescriptor", "(I[B)Z",
- descHandle, payload);
+ if (role == QLowEnergyController::CentralRole) {
+ qCDebug(QT_BT_ANDROID) << "Write descriptor with handle " << descHandle
+ << newValue.toHex() << "(service:" << service->uuid << ")";
+ result = hub->javaObject().callMethod<jboolean>("writeDescriptor", "(I[B)Z",
+ descHandle, payload);
+ } else {
+ const auto &characteristic = characteristicForHandle(charHandle);
+ const auto &descriptor = descriptorForHandle(descHandle);
+ if (characteristic.isValid() && descriptor.isValid()) {
+ qCDebug(QT_BT_ANDROID) << "Write descriptor" << descriptor.uuid()
+ << "(service:" << service->uuid
+ << "char: " << characteristic.uuid() << ")";
+ const QAndroidJniObject charUuid = javaUuidfromQtUuid(characteristic.uuid());
+ const QAndroidJniObject descUuid = javaUuidfromQtUuid(descriptor.uuid());
+ result = hub->javaObject().callMethod<jboolean>(
+ "writeDescriptor",
+ "(Landroid/bluetooth/BluetoothGattService;Ljava/util/UUID;Ljava/util/UUID;[B)Z",
+ service->androidService.object(), charUuid.object(),
+ descUuid.object(), payload);
+ }
+ }
}
if (env->ExceptionOccurred()) {