From f87fb99f0f6ecf6cd66f9da120306bb6f6561053 Mon Sep 17 00:00:00 2001 From: Florian Mhun Date: Mon, 11 Jul 2016 15:27:45 +0200 Subject: [PATCH 1/2] Fix issue #294 - call setCharacteristicNotification when subscribe to notification from Android 4.3. --- src/android/BluetoothLePlugin.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/android/BluetoothLePlugin.java b/src/android/BluetoothLePlugin.java index f1e838cd..34fe67b0 100644 --- a/src/android/BluetoothLePlugin.java +++ b/src/android/BluetoothLePlugin.java @@ -1730,6 +1730,10 @@ private boolean subscribeAction(JSONArray args, CallbackContext callbackContext) return false; } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + bluetoothGatt.setCharacteristicNotification(characteristic, true); + } + BluetoothGattDescriptor descriptor = characteristic.getDescriptor(clientConfigurationDescriptorUuid); if (isNotDescriptor(descriptor, device, callbackContext)) { @@ -1823,6 +1827,10 @@ private boolean unsubscribeAction(JSONArray args, CallbackContext callbackContex return false; } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + bluetoothGatt.setCharacteristicNotification(characteristic, false); + } + BluetoothGattDescriptor descriptor = characteristic.getDescriptor(clientConfigurationDescriptorUuid); if (isNotDescriptor(descriptor, device, callbackContext)) { From 1b9bb243e055dee69273887f4177aec1f09862f1 Mon Sep 17 00:00:00 2001 From: Florian Mhun Date: Tue, 12 Jul 2016 19:34:13 +0200 Subject: [PATCH 2/2] Handle setCharacteristicNotification error, remove sdk target check. --- src/android/BluetoothLePlugin.java | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/android/BluetoothLePlugin.java b/src/android/BluetoothLePlugin.java index 34fe67b0..c585e331 100644 --- a/src/android/BluetoothLePlugin.java +++ b/src/android/BluetoothLePlugin.java @@ -1730,10 +1730,6 @@ private boolean subscribeAction(JSONArray args, CallbackContext callbackContext) return false; } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { - bluetoothGatt.setCharacteristicNotification(characteristic, true); - } - BluetoothGattDescriptor descriptor = characteristic.getDescriptor(clientConfigurationDescriptorUuid); if (isNotDescriptor(descriptor, device, callbackContext)) { @@ -1772,6 +1768,15 @@ private boolean subscribeAction(JSONArray args, CallbackContext callbackContext) return false; } + result = bluetoothGatt.setCharacteristicNotification(characteristic, true); + + if (!result) { + addProperty(returnObj, keyError, errorSubscription); + addProperty(returnObj, keyMessage, logSubscribeFail); + callbackContext.error(returnObj); + return false; + } + AddCallback(characteristicUuid, connection, operationSubscribe, callbackContext); //Write the descriptor value @@ -1827,10 +1832,6 @@ private boolean unsubscribeAction(JSONArray args, CallbackContext callbackContex return false; } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { - bluetoothGatt.setCharacteristicNotification(characteristic, false); - } - BluetoothGattDescriptor descriptor = characteristic.getDescriptor(clientConfigurationDescriptorUuid); if (isNotDescriptor(descriptor, device, callbackContext)) { @@ -1864,6 +1865,15 @@ private boolean unsubscribeAction(JSONArray args, CallbackContext callbackContex return false; } + result = bluetoothGatt.setCharacteristicNotification(characteristic, false); + + if (!result) { + addProperty(returnObj, keyError, errorSubscription); + addProperty(returnObj, keyMessage, logSubscribeFail); + callbackContext.error(returnObj); + return false; + } + AddCallback(characteristicUuid, connection, operationUnsubscribe, callbackContext); //Write the actual descriptor value