From 55397831874d28833552a6d316726239f1f49d62 Mon Sep 17 00:00:00 2001 From: James Swan <122404367+swan-amazon@users.noreply.github.com> Date: Fri, 3 Mar 2023 06:47:28 -0800 Subject: [PATCH] [Android] Fallback to BLE Notification when Indication unsupported (#25444) To support devices based on older revisions of the SDK that did not include the GATT Server Characteristic property change (from Notification to Indication), the GATT Characteristic properties are queried to determine if Indication and/or Notification is supported. When available, Indication is preferred. --- .../java/chip/platform/AndroidBleManager.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/platform/android/java/chip/platform/AndroidBleManager.java b/src/platform/android/java/chip/platform/AndroidBleManager.java index 20b51ee008683e..3bc484985d231d 100644 --- a/src/platform/android/java/chip/platform/AndroidBleManager.java +++ b/src/platform/android/java/chip/platform/AndroidBleManager.java @@ -157,6 +157,9 @@ public void onDescriptorWrite( if (desc.getValue() == BluetoothGattDescriptor.ENABLE_INDICATION_VALUE) { mPlatform.handleSubscribeComplete( connId, svcIdBytes, charIdBytes, status == BluetoothGatt.GATT_SUCCESS); + } else if (desc.getValue() == BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE) { + mPlatform.handleSubscribeComplete( + connId, svcIdBytes, charIdBytes, status == BluetoothGatt.GATT_SUCCESS); } else if (desc.getValue() == BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE) { mPlatform.handleUnsubscribeComplete( connId, svcIdBytes, charIdBytes, status == BluetoothGatt.GATT_SUCCESS); @@ -283,10 +286,18 @@ public boolean onSubscribeCharacteristic(int connId, byte[] svcId, byte[] charId BluetoothGattDescriptor descriptor = subscribeChar.getDescriptor(UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG)); - descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); - if (!bluetoothGatt.writeDescriptor(descriptor)) { - Log.e(TAG, "writeDescriptor failed"); - return false; + if ((subscribeChar.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) { + descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); + if (!bluetoothGatt.writeDescriptor(descriptor)) { + Log.e(TAG, "writeDescriptor failed"); + return false; + } + } else if ((subscribeChar.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) { + descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); + if (!bluetoothGatt.writeDescriptor(descriptor)) { + Log.e(TAG, "writeDescriptor failed"); + return false; + } } return true; }