From 62807f1b0dbbfe44fee3a0ce5e4f65080669701d Mon Sep 17 00:00:00 2001 From: Rand Dusing Date: Thu, 29 Jul 2021 21:30:15 -0500 Subject: [PATCH] Revert "Merge pull request #696 from VinardoZzZ2000/feat/notify-queue" This reverts commit 2e33e861749dd01883bb43e2fca4be9d9779e29b, reversing changes made to d9517d8590cd687a4f8720b0e934a813fc44ad9e. --- readme.md | 13 +---- src/android/BluetoothLePlugin.java | 88 ++---------------------------- 2 files changed, 8 insertions(+), 93 deletions(-) diff --git a/readme.md b/readme.md index b200146..708ebf5 100644 --- a/readme.md +++ b/readme.md @@ -181,14 +181,7 @@ Discovery works differently between Android and iOS. In Android, a single functi ## Queuing (Android) ## -On Android, the plugin automatically queues the following operations: -* read -* write -* subscribe -* unsubscribe -* readDescriptor -* writeDescriptor -* notify +Read, write, subscribe, unsubscribe, readDescriptor and writeDescriptor queueing has been added to the master branch and will be part of the 4.1.0 release. If you'd like to try it out, install the plugin directly from GitHub using: ```cordova plugin https://github.com/randdusing/cordova-plugin-bluetoothle``` ## UUIDs ## @@ -1830,7 +1823,7 @@ Initialization works slightly different between Android and iOS. On iOS, you don ### Notifications ### -Notifications work slightly differently between Android and iOS. Queueing for notifications is currently only implemented on Android. On Android, the notify() success callback will always include ```sent: true```. On iOS, if the sent property is set to false, you should wait until receiving the ```peripheralManagerIsReadyToUpdateSubscribers``` event to resend the notification. In future versions, I hope to standardize the functionality between platforms. +Notifications work slightly differently between Android and iOS. On Android, you should wait for the ```notificationSent``` event before calling notify() again. On iOS, you need to check the notify() callback for the sent property. If the sent property is set to false, you should wait until receiving the ```peripheralManagerIsReadyToUpdateSubscribers``` event to resend the notification. In future versions, I hope to standardize the functionality between platforms. ### Descriptors ### @@ -1866,7 +1859,7 @@ bluetoothle.initializePeripheral(success, error, params); * status => subscribed = Subscription started request, use notify() to send new data * status => unsubscribed = Subscription ended request, stop sending data * status => notificationReady = Resume sending subscription updates (iOS) -* status => notificationSent = Notification has been sent (Android) [DEPRECATED: Use notify() callback instead] +* status => notificationSent = Notification has been sent (Android) * status => connected = A device has connected * status => disconnected = A device has disconnected * status => mtuChanged = MTU has changed for device diff --git a/src/android/BluetoothLePlugin.java b/src/android/BluetoothLePlugin.java index 72ab03c..0d8048c 100644 --- a/src/android/BluetoothLePlugin.java +++ b/src/android/BluetoothLePlugin.java @@ -89,9 +89,6 @@ public class BluetoothLePlugin extends CordovaPlugin { //Quick Writes private LinkedList queueQuick = new LinkedList(); - //Peripheral queue - private LinkedList peripheralQueue = new LinkedList(); - //Object keys private final String keyStatus = "status"; private final String keyError = "error"; @@ -130,7 +127,6 @@ public class BluetoothLePlugin extends CordovaPlugin { private final String keyConnectionPriority = "connectionPriority"; private final String keyMtu = "mtu"; private final String keyPin = "pin"; - private final String keySent = "sent"; private final String keyQueue = "queue"; //Write Types @@ -159,7 +155,6 @@ public class BluetoothLePlugin extends CordovaPlugin { private final String statusRssi = "rssi"; private final String statusConnectionPriorityRequested = "connectionPriorityRequested"; private final String statusMtu = "mtu"; - private final String statusNotified = "notified"; //Properties private final String propertyBroadcast = "broadcast"; @@ -217,7 +212,6 @@ public class BluetoothLePlugin extends CordovaPlugin { private final String errorRequestConnectionPriority = "requestConnectPriority"; private final String errorMtu = "mtu"; private final String errorRetrievePeripheralsByAddress = "retrievePeripheralsByAddress"; - private final String errorNotify = "notify"; //Error Messages //Initialization @@ -420,9 +414,7 @@ public boolean execute(String action, final JSONArray args, final CallbackContex } else if ("respond".equals(action)) { respondAction(args, callbackContext); } else if ("notify".equals(action)) { - Operation operation = new Operation("notify", args, callbackContext); - peripheralQueue.add(operation); - peripheralQueueStart(); + notifyAction(args, callbackContext); } else if ("setPin".equals(action)) { setPinAction(args, callbackContext); } else if ("retrievePeripheralsByAddress".equals(action)) { @@ -830,18 +822,15 @@ private void respondAction(JSONArray args, CallbackContext callbackContext) { } } - private boolean notifyAction(Operation operation) { - JSONArray args = operation.args; - CallbackContext callbackContext = operation.callbackContext; - + private void notifyAction(JSONArray args, CallbackContext callbackContext) { JSONObject obj = getArgsObject(args); if (isNotArgsObject(obj, callbackContext)) { - return false; + return; } String address = getAddress(obj); if (isNotAddress(address, callbackContext)) { - return false; + return; } BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address); @@ -852,7 +841,6 @@ private boolean notifyAction(Operation operation) { addProperty(returnObj, "error", "service"); addProperty(returnObj, "message", "Service not found"); callbackContext.error(returnObj); - return false; } UUID characteristicUuid = getUUID(obj.optString("characteristic", null)); @@ -862,7 +850,6 @@ private boolean notifyAction(Operation operation) { addProperty(returnObj, "error", "characteristic"); addProperty(returnObj, "message", "Characteristic not found"); callbackContext.error(returnObj); - return false; } byte[] value = getPropertyBytes(obj, "value"); @@ -872,7 +859,6 @@ private boolean notifyAction(Operation operation) { addProperty(returnObj, "error", "respond"); addProperty(returnObj, "message", "Failed to set value"); callbackContext.error(returnObj); - return false; } BluetoothGattDescriptor descriptor = characteristic.getDescriptor(clientConfigurationDescriptorUuid); @@ -883,16 +869,14 @@ private boolean notifyAction(Operation operation) { isIndicate = true; } + //Wait for onNotificationSent event boolean result = gattServer.notifyCharacteristicChanged(device, characteristic, isIndicate); if (!result) { JSONObject returnObj = new JSONObject(); addProperty(returnObj, "error", "notify"); addProperty(returnObj, "message", "Failed to notify"); callbackContext.error(returnObj); - return false; } - - return true; } public void hasPermissionAction(CallbackContext callbackContext) { @@ -3269,38 +3253,6 @@ private void queueRemove(HashMap connection) { queueNext(connection); } - private void peripheralQueueStart() { - if (peripheralQueue.size() > 1) { - return; - } - - peripheralQueueNext(); - } - - private void peripheralQueueNext() { - Operation operation = peripheralQueue.peek(); - - boolean result = notifyAction(operation); - - if (!result) { - peripheralQueueRemove(); - } - } - - private void peripheralQueueRemove() { - if (peripheralQueue.size() == 0) { - return; - } - - peripheralQueue.poll(); - - if (peripheralQueue.size() == 0) { - return; - } - - peripheralQueueNext(); - } - private HashMap EnsureCallback(UUID characteristicUuid, HashMap connection) { HashMap characteristicCallbacks = (HashMap) connection.get(characteristicUuid); @@ -4711,36 +4663,6 @@ public void onMtuChanged(BluetoothDevice device, int mtu) { } public void onNotificationSent(BluetoothDevice device, int status) { - Operation operation = peripheralQueue.peek(); - - JSONArray args = operation.args; - CallbackContext callbackContext = operation.callbackContext; - - peripheralQueueRemove(); - - //If no callback, just return - if (callbackContext == null) { - return; - } - - JSONObject obj = getArgsObject(args); - - JSONObject returnObj = new JSONObject(); - - addDevice(returnObj, device); - - //If successfully notified, return value - if (status == BluetoothGatt.GATT_SUCCESS) { - addProperty(returnObj, keyStatus, statusNotified); - addProperty(returnObj, keySent, true); - callbackContext.success(returnObj); - } else { - //Else it failed - addProperty(returnObj, keyError, errorNotify); - callbackContext.error(returnObj); - } - - //TODO: Remove code below (kept for backward compatibility) if (initPeripheralCallback == null) { return; }