Skip to content

Commit 62807f1

Browse files
committed
Revert "Merge pull request #696 from VinardoZzZ2000/feat/notify-queue"
This reverts commit 2e33e86, reversing changes made to d9517d8.
1 parent 000f6fb commit 62807f1

File tree

2 files changed

+8
-93
lines changed

2 files changed

+8
-93
lines changed

readme.md

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,7 @@ Discovery works differently between Android and iOS. In Android, a single functi
181181

182182

183183
## Queuing (Android) ##
184-
On Android, the plugin automatically queues the following operations:
185-
* read
186-
* write
187-
* subscribe
188-
* unsubscribe
189-
* readDescriptor
190-
* writeDescriptor
191-
* notify
184+
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```
192185

193186

194187
## UUIDs ##
@@ -1830,7 +1823,7 @@ Initialization works slightly different between Android and iOS. On iOS, you don
18301823

18311824

18321825
### Notifications ###
1833-
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.
1826+
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.
18341827

18351828

18361829
### Descriptors ###
@@ -1866,7 +1859,7 @@ bluetoothle.initializePeripheral(success, error, params);
18661859
* status => subscribed = Subscription started request, use notify() to send new data
18671860
* status => unsubscribed = Subscription ended request, stop sending data
18681861
* status => notificationReady = Resume sending subscription updates (iOS)
1869-
* status => notificationSent = Notification has been sent (Android) [DEPRECATED: Use notify() callback instead]
1862+
* status => notificationSent = Notification has been sent (Android)
18701863
* status => connected = A device has connected
18711864
* status => disconnected = A device has disconnected
18721865
* status => mtuChanged = MTU has changed for device

src/android/BluetoothLePlugin.java

Lines changed: 5 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,6 @@ public class BluetoothLePlugin extends CordovaPlugin {
8989
//Quick Writes
9090
private LinkedList<byte[]> queueQuick = new LinkedList<byte[]>();
9191

92-
//Peripheral queue
93-
private LinkedList<Operation> peripheralQueue = new LinkedList<Operation>();
94-
9592
//Object keys
9693
private final String keyStatus = "status";
9794
private final String keyError = "error";
@@ -130,7 +127,6 @@ public class BluetoothLePlugin extends CordovaPlugin {
130127
private final String keyConnectionPriority = "connectionPriority";
131128
private final String keyMtu = "mtu";
132129
private final String keyPin = "pin";
133-
private final String keySent = "sent";
134130
private final String keyQueue = "queue";
135131

136132
//Write Types
@@ -159,7 +155,6 @@ public class BluetoothLePlugin extends CordovaPlugin {
159155
private final String statusRssi = "rssi";
160156
private final String statusConnectionPriorityRequested = "connectionPriorityRequested";
161157
private final String statusMtu = "mtu";
162-
private final String statusNotified = "notified";
163158

164159
//Properties
165160
private final String propertyBroadcast = "broadcast";
@@ -217,7 +212,6 @@ public class BluetoothLePlugin extends CordovaPlugin {
217212
private final String errorRequestConnectionPriority = "requestConnectPriority";
218213
private final String errorMtu = "mtu";
219214
private final String errorRetrievePeripheralsByAddress = "retrievePeripheralsByAddress";
220-
private final String errorNotify = "notify";
221215

222216
//Error Messages
223217
//Initialization
@@ -420,9 +414,7 @@ public boolean execute(String action, final JSONArray args, final CallbackContex
420414
} else if ("respond".equals(action)) {
421415
respondAction(args, callbackContext);
422416
} else if ("notify".equals(action)) {
423-
Operation operation = new Operation("notify", args, callbackContext);
424-
peripheralQueue.add(operation);
425-
peripheralQueueStart();
417+
notifyAction(args, callbackContext);
426418
} else if ("setPin".equals(action)) {
427419
setPinAction(args, callbackContext);
428420
} else if ("retrievePeripheralsByAddress".equals(action)) {
@@ -830,18 +822,15 @@ private void respondAction(JSONArray args, CallbackContext callbackContext) {
830822
}
831823
}
832824

833-
private boolean notifyAction(Operation operation) {
834-
JSONArray args = operation.args;
835-
CallbackContext callbackContext = operation.callbackContext;
836-
825+
private void notifyAction(JSONArray args, CallbackContext callbackContext) {
837826
JSONObject obj = getArgsObject(args);
838827
if (isNotArgsObject(obj, callbackContext)) {
839-
return false;
828+
return;
840829
}
841830

842831
String address = getAddress(obj);
843832
if (isNotAddress(address, callbackContext)) {
844-
return false;
833+
return;
845834
}
846835
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address);
847836

@@ -852,7 +841,6 @@ private boolean notifyAction(Operation operation) {
852841
addProperty(returnObj, "error", "service");
853842
addProperty(returnObj, "message", "Service not found");
854843
callbackContext.error(returnObj);
855-
return false;
856844
}
857845

858846
UUID characteristicUuid = getUUID(obj.optString("characteristic", null));
@@ -862,7 +850,6 @@ private boolean notifyAction(Operation operation) {
862850
addProperty(returnObj, "error", "characteristic");
863851
addProperty(returnObj, "message", "Characteristic not found");
864852
callbackContext.error(returnObj);
865-
return false;
866853
}
867854

868855
byte[] value = getPropertyBytes(obj, "value");
@@ -872,7 +859,6 @@ private boolean notifyAction(Operation operation) {
872859
addProperty(returnObj, "error", "respond");
873860
addProperty(returnObj, "message", "Failed to set value");
874861
callbackContext.error(returnObj);
875-
return false;
876862
}
877863

878864
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(clientConfigurationDescriptorUuid);
@@ -883,16 +869,14 @@ private boolean notifyAction(Operation operation) {
883869
isIndicate = true;
884870
}
885871

872+
//Wait for onNotificationSent event
886873
boolean result = gattServer.notifyCharacteristicChanged(device, characteristic, isIndicate);
887874
if (!result) {
888875
JSONObject returnObj = new JSONObject();
889876
addProperty(returnObj, "error", "notify");
890877
addProperty(returnObj, "message", "Failed to notify");
891878
callbackContext.error(returnObj);
892-
return false;
893879
}
894-
895-
return true;
896880
}
897881

898882
public void hasPermissionAction(CallbackContext callbackContext) {
@@ -3269,38 +3253,6 @@ private void queueRemove(HashMap<Object, Object> connection) {
32693253
queueNext(connection);
32703254
}
32713255

3272-
private void peripheralQueueStart() {
3273-
if (peripheralQueue.size() > 1) {
3274-
return;
3275-
}
3276-
3277-
peripheralQueueNext();
3278-
}
3279-
3280-
private void peripheralQueueNext() {
3281-
Operation operation = peripheralQueue.peek();
3282-
3283-
boolean result = notifyAction(operation);
3284-
3285-
if (!result) {
3286-
peripheralQueueRemove();
3287-
}
3288-
}
3289-
3290-
private void peripheralQueueRemove() {
3291-
if (peripheralQueue.size() == 0) {
3292-
return;
3293-
}
3294-
3295-
peripheralQueue.poll();
3296-
3297-
if (peripheralQueue.size() == 0) {
3298-
return;
3299-
}
3300-
3301-
peripheralQueueNext();
3302-
}
3303-
33043256
private HashMap<Object, Object> EnsureCallback(UUID characteristicUuid, HashMap<Object, Object> connection) {
33053257
HashMap<Object, Object> characteristicCallbacks = (HashMap<Object, Object>) connection.get(characteristicUuid);
33063258

@@ -4711,36 +4663,6 @@ public void onMtuChanged(BluetoothDevice device, int mtu) {
47114663
}
47124664

47134665
public void onNotificationSent(BluetoothDevice device, int status) {
4714-
Operation operation = peripheralQueue.peek();
4715-
4716-
JSONArray args = operation.args;
4717-
CallbackContext callbackContext = operation.callbackContext;
4718-
4719-
peripheralQueueRemove();
4720-
4721-
//If no callback, just return
4722-
if (callbackContext == null) {
4723-
return;
4724-
}
4725-
4726-
JSONObject obj = getArgsObject(args);
4727-
4728-
JSONObject returnObj = new JSONObject();
4729-
4730-
addDevice(returnObj, device);
4731-
4732-
//If successfully notified, return value
4733-
if (status == BluetoothGatt.GATT_SUCCESS) {
4734-
addProperty(returnObj, keyStatus, statusNotified);
4735-
addProperty(returnObj, keySent, true);
4736-
callbackContext.success(returnObj);
4737-
} else {
4738-
//Else it failed
4739-
addProperty(returnObj, keyError, errorNotify);
4740-
callbackContext.error(returnObj);
4741-
}
4742-
4743-
//TODO: Remove code below (kept for backward compatibility)
47444666
if (initPeripheralCallback == null) {
47454667
return;
47464668
}

0 commit comments

Comments
 (0)