diff options
author | Alex Blasche <alexander.blasche@qt.io> | 2016-12-22 12:38:35 +0100 |
---|---|---|
committer | Alex Blasche <alexander.blasche@qt.io> | 2017-01-23 07:41:27 +0000 |
commit | e149a33c2628682cdbb0aab2dced40bb84a44a00 (patch) | |
tree | 009ff3295c4d7f6142ea5e5ba64214d7c6dbb97f /src | |
parent | 072ca4833101f4130e4b4f1122f8e6b3e9ae64bd (diff) |
Setup characteristic permissions for Android peripheral
Change-Id: I32a3c4c711ac62ee89570534ae8c658a681bbdb8
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/bluetooth/qlowenergycontroller_android.cpp | 70 |
1 files changed, 66 insertions, 4 deletions
diff --git a/src/bluetooth/qlowenergycontroller_android.cpp b/src/bluetooth/qlowenergycontroller_android.cpp index 6cd1ced1..5c0bee7b 100644 --- a/src/bluetooth/qlowenergycontroller_android.cpp +++ b/src/bluetooth/qlowenergycontroller_android.cpp @@ -808,6 +808,70 @@ static QAndroidJniObject javaUuidfromQtUuid(const QBluetoothUuid& uuid) return javaUuid; } +/* + * Returns the Java char permissions based on the given characteristic data. + */ +static int setupCharPermissions(const QLowEnergyCharacteristicData &charData) +{ + int permission = 0; + if (charData.properties() & QLowEnergyCharacteristic::Read) { + if (int(charData.readConstraints()) == 0 // nothing is equivalent to simple read + || (charData.readConstraints() & QBluetooth::AttAuthorizationRequired)) { + permission |= QAndroidJniObject::getStaticField<jint>( + "android/bluetooth/BluetoothGattCharacteristic", + "PERMISSION_READ"); + } + + if (charData.readConstraints() & QBluetooth::AttAuthenticationRequired) { + permission |= QAndroidJniObject::getStaticField<jint>( + "android/bluetooth/BluetoothGattCharacteristic", + "PERMISSION_READ_ENCRYPTED"); + } + + if (charData.readConstraints() & QBluetooth::AttEncryptionRequired) { + permission |= QAndroidJniObject::getStaticField<jint>( + "android/bluetooth/BluetoothGattCharacteristic", + "PERMISSION_READ_ENCRYPTED_MITM"); + } + } + + if (charData.properties() & + (QLowEnergyCharacteristic::Write|QLowEnergyCharacteristic::WriteNoResponse) ) { + if (((int)charData.writeConstraints()) == 0 // no flag is equivalent ti simple write + || (charData.writeConstraints() & QBluetooth::AttAuthorizationRequired)) { + permission |= QAndroidJniObject::getStaticField<jint>( + "android/bluetooth/BluetoothGattCharacteristic", + "PERMISSION_WRITE"); + } + + if (charData.writeConstraints() & QBluetooth::AttAuthenticationRequired) { + permission |= QAndroidJniObject::getStaticField<jint>( + "android/bluetooth/BluetoothGattCharacteristic", + "PERMISSION_WRITE_ENCRYPTED"); + } + + if (charData.writeConstraints() & QBluetooth::AttEncryptionRequired) { + permission |= QAndroidJniObject::getStaticField<jint>( + "android/bluetooth/BluetoothGattCharacteristic", + "PERMISSION_WRITE_ENCRYPTED_MITM"); + } + } + + if (charData.properties() & QLowEnergyCharacteristic::WriteSigned) { + if (charData.writeConstraints() & QBluetooth::AttEncryptionRequired) { + permission |= QAndroidJniObject::getStaticField<jint>( + "android/bluetooth/BluetoothGattCharacteristic", + "PERMISSION_WRITE_SIGNED_MITM"); + } else { + permission |= QAndroidJniObject::getStaticField<jint>( + "android/bluetooth/BluetoothGattCharacteristic", + "PERMISSION_WRITE_SIGNED"); + } + } + + return permission; +} + void QLowEnergyControllerPrivate::addToGenericAttributeList(const QLowEnergyServiceData &serviceData, QLowEnergyHandle startHandle) { @@ -841,14 +905,12 @@ void QLowEnergyControllerPrivate::addToGenericAttributeList(const QLowEnergyServ // add characteristics const QList<QLowEnergyCharacteristicData> serviceCharsData = serviceData.characteristics(); for (const auto &charData: serviceCharsData) { - // TODO set all characteristic properties - int permissions = 0; - //TODO add chars and their descriptors QAndroidJniObject javaChar = QAndroidJniObject("android/bluetooth/BluetoothGattCharacteristic", "(Ljava/util/UUID;II)V", javaUuidfromQtUuid(charData.uuid()).object(), - int(charData.properties()), permissions); + int(charData.properties()), + setupCharPermissions(charData)); QAndroidJniEnvironment env; jbyteArray jb = env->NewByteArray(charData.value().size()); |