Skip to content

Commit

Permalink
Revert "Merge pull request #696 from VinardoZzZ2000/feat/notify-queue"
Browse files Browse the repository at this point in the history
This reverts commit 2e33e86, reversing
changes made to d9517d8.
  • Loading branch information
randdusing committed Jul 30, 2021
1 parent 000f6fb commit 62807f1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 93 deletions.
13 changes: 3 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ##
Expand Down Expand Up @@ -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 ###
Expand Down Expand Up @@ -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
Expand Down
88 changes: 5 additions & 83 deletions src/android/BluetoothLePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ public class BluetoothLePlugin extends CordovaPlugin {
//Quick Writes
private LinkedList<byte[]> queueQuick = new LinkedList<byte[]>();

//Peripheral queue
private LinkedList<Operation> peripheralQueue = new LinkedList<Operation>();

//Object keys
private final String keyStatus = "status";
private final String keyError = "error";
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);

Expand All @@ -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));
Expand All @@ -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");
Expand All @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -3269,38 +3253,6 @@ private void queueRemove(HashMap<Object, Object> 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<Object, Object> EnsureCallback(UUID characteristicUuid, HashMap<Object, Object> connection) {
HashMap<Object, Object> characteristicCallbacks = (HashMap<Object, Object>) connection.get(characteristicUuid);

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 62807f1

Please sign in to comment.