@@ -89,9 +89,6 @@ public class BluetoothLePlugin extends CordovaPlugin {
89
89
//Quick Writes
90
90
private LinkedList <byte []> queueQuick = new LinkedList <byte []>();
91
91
92
- //Queueing
93
- private LinkedList <Operation > queue = new LinkedList <Operation >();
94
-
95
92
//Object keys
96
93
private final String keyStatus = "status" ;
97
94
private final String keyError = "error" ;
@@ -130,6 +127,7 @@ public class BluetoothLePlugin extends CordovaPlugin {
130
127
private final String keyConnectionPriority = "connectionPriority" ;
131
128
private final String keyMtu = "mtu" ;
132
129
private final String keyPin = "pin" ;
130
+ private final String keyQueue = "queue" ;
133
131
134
132
//Write Types
135
133
private final String writeTypeNoResponse = "noResponse" ;
@@ -353,30 +351,24 @@ public boolean execute(String action, final JSONArray args, final CallbackContex
353
351
discoverAction (args , callbackContext );
354
352
} else if ("read" .equals (action )) {
355
353
Operation operation = new Operation ("read" , args , callbackContext );
356
- queue .add (operation );
357
- queueStart ();
354
+ queueAdd (operation );
358
355
} else if ("subscribe" .equals (action )) {
359
356
Operation operation = new Operation ("subscribe" , args , callbackContext );
360
- queue .add (operation );
361
- queueStart ();
357
+ queueAdd (operation );
362
358
} else if ("unsubscribe" .equals (action )) {
363
359
Operation operation = new Operation ("unsubscribe" , args , callbackContext );
364
- queue .add (operation );
365
- queueStart ();
360
+ queueAdd (operation );
366
361
} else if ("write" .equals (action )) {
367
362
Operation operation = new Operation ("write" , args , callbackContext );
368
- queue .add (operation );
369
- queueStart ();
363
+ queueAdd (operation );
370
364
} else if ("writeQ" .equals (action )) {
371
365
writeQAction (args , callbackContext );
372
366
} else if ("readDescriptor" .equals (action )) {
373
367
Operation operation = new Operation ("readDescriptor" , args , callbackContext );
374
- queue .add (operation );
375
- queueStart ();
368
+ queueAdd (operation );
376
369
} else if ("writeDescriptor" .equals (action )) {
377
370
Operation operation = new Operation ("writeDescriptor" , args , callbackContext );
378
- queue .add (operation );
379
- queueStart ();
371
+ queueAdd (operation );
380
372
} else if ("rssi" .equals (action )) {
381
373
rssiAction (args , callbackContext );
382
374
} else if ("isInitialized" .equals (action )) {
@@ -747,10 +739,14 @@ private void startAdvertisingAction(JSONArray args, CallbackContext callbackCont
747
739
dataBuilder .addManufacturerData (manufacturerId , manufacturerSpecificData );
748
740
}
749
741
750
- //dataBuilder.addServiceData();
751
742
UUID uuid = getUUID (obj .optString ("service" , null ));
752
743
if (uuid != null ) {
753
- dataBuilder .addServiceUuid (new ParcelUuid (uuid ));
744
+ byte [] serviceData = getPropertyBytes (obj , "serviceData" );
745
+ if (serviceData != null ) {
746
+ dataBuilder .addServiceData (new ParcelUuid (uuid ), serviceData );
747
+ } else {
748
+ dataBuilder .addServiceUuid (new ParcelUuid (uuid ));
749
+ }
754
750
}
755
751
756
752
dataBuilder .setIncludeDeviceName (obj .optBoolean ("includeDeviceName" , true ));
@@ -1497,6 +1493,10 @@ private void connectAction(JSONArray args, CallbackContext callbackContext) {
1497
1493
}
1498
1494
1499
1495
connection .put (keyPeripheral , bluetoothGatt );
1496
+
1497
+ LinkedList <Operation > queue = new LinkedList <Operation >();
1498
+
1499
+ connection .put (keyQueue , queue );
1500
1500
}
1501
1501
1502
1502
private void reconnectAction (JSONArray args , CallbackContext callbackContext ) {
@@ -1637,12 +1637,6 @@ private void closeAction(JSONArray args, CallbackContext callbackContext) {
1637
1637
connections .remove (device .getAddress ());
1638
1638
1639
1639
callbackContext .success (returnObj );
1640
-
1641
- //Check for queued operations in progress on this device
1642
- Operation operation = queue .peek ();
1643
- if (operation != null && operation .device != null && operation .device .getAddress ().equals (address )) {
1644
- queueRemove ();
1645
- }
1646
1640
}
1647
1641
1648
1642
private void discoverAction (JSONArray args , CallbackContext callbackContext ) {
@@ -1727,9 +1721,6 @@ private boolean readAction(Operation operation) {
1727
1721
}
1728
1722
1729
1723
JSONObject obj = getArgsObject (args );
1730
- if (isNotArgsObject (obj , callbackContext )) {
1731
- return false ;
1732
- }
1733
1724
1734
1725
String address = getAddress (obj );
1735
1726
if (isNotAddress (address , callbackContext )) {
@@ -1783,8 +1774,6 @@ private boolean readAction(Operation operation) {
1783
1774
return false ;
1784
1775
}
1785
1776
1786
- operation .device = device ;
1787
-
1788
1777
return true ;
1789
1778
}
1790
1779
@@ -1797,9 +1786,6 @@ private boolean subscribeAction(Operation operation) {
1797
1786
}
1798
1787
1799
1788
JSONObject obj = getArgsObject (args );
1800
- if (isNotArgsObject (obj , callbackContext )) {
1801
- return false ;
1802
- }
1803
1789
1804
1790
String address = getAddress (obj );
1805
1791
if (isNotAddress (address , callbackContext )) {
@@ -1891,8 +1877,6 @@ private boolean subscribeAction(Operation operation) {
1891
1877
return false ;
1892
1878
}
1893
1879
1894
- operation .device = device ;
1895
-
1896
1880
return true ;
1897
1881
}
1898
1882
@@ -1905,9 +1889,6 @@ private boolean unsubscribeAction(Operation operation) {
1905
1889
}
1906
1890
1907
1891
JSONObject obj = getArgsObject (args );
1908
- if (isNotArgsObject (obj , callbackContext )) {
1909
- return false ;
1910
- }
1911
1892
1912
1893
String address = getAddress (obj );
1913
1894
if (isNotAddress (address , callbackContext )) {
@@ -1984,8 +1965,6 @@ private boolean unsubscribeAction(Operation operation) {
1984
1965
return false ;
1985
1966
}
1986
1967
1987
- operation .device = device ;
1988
-
1989
1968
return true ;
1990
1969
}
1991
1970
@@ -1998,9 +1977,6 @@ private boolean writeAction(Operation operation) {
1998
1977
}
1999
1978
2000
1979
JSONObject obj = getArgsObject (args );
2001
- if (isNotArgsObject (obj , callbackContext )) {
2002
- return false ;
2003
- }
2004
1980
2005
1981
String address = getAddress (obj );
2006
1982
if (isNotAddress (address , callbackContext )) {
@@ -2073,8 +2049,6 @@ private boolean writeAction(Operation operation) {
2073
2049
return false ;
2074
2050
}
2075
2051
2076
- operation .device = device ;
2077
-
2078
2052
return true ;
2079
2053
}
2080
2054
@@ -2229,9 +2203,6 @@ private boolean readDescriptorAction(Operation operation) {
2229
2203
}
2230
2204
2231
2205
JSONObject obj = getArgsObject (args );
2232
- if (isNotArgsObject (obj , callbackContext )) {
2233
- return false ;
2234
- }
2235
2206
2236
2207
String address = getAddress (obj );
2237
2208
if (isNotAddress (address , callbackContext )) {
@@ -2292,8 +2263,6 @@ private boolean readDescriptorAction(Operation operation) {
2292
2263
return false ;
2293
2264
}
2294
2265
2295
- operation .device = device ;
2296
-
2297
2266
return true ;
2298
2267
}
2299
2268
@@ -2306,9 +2275,6 @@ private boolean writeDescriptorAction(Operation operation) {
2306
2275
}
2307
2276
2308
2277
JSONObject obj = getArgsObject (args );
2309
- if (isNotArgsObject (obj , callbackContext )) {
2310
- return false ;
2311
- }
2312
2278
2313
2279
String address = getAddress (obj );
2314
2280
if (isNotAddress (address , callbackContext )) {
@@ -2394,8 +2360,6 @@ private boolean writeDescriptorAction(Operation operation) {
2394
2360
return false ;
2395
2361
}
2396
2362
2397
- operation .device = device ;
2398
-
2399
2363
return true ;
2400
2364
}
2401
2365
@@ -3202,18 +3166,43 @@ private BluetoothGattDescriptor getDescriptor(JSONObject obj, BluetoothGattChara
3202
3166
}
3203
3167
3204
3168
//Helpers for Callbacks
3205
- private void queueStart () {
3169
+ private void queueAdd (Operation operation ) {
3170
+ JSONArray args = operation .args ;
3171
+ CallbackContext callbackContext = operation .callbackContext ;
3172
+
3173
+ JSONObject obj = getArgsObject (args );
3174
+ if (isNotArgsObject (obj , callbackContext )) {
3175
+ return ;
3176
+ }
3177
+
3178
+ String address = getAddress (obj );
3179
+ if (isNotAddress (address , callbackContext )) {
3180
+ return ;
3181
+ }
3182
+
3183
+ HashMap <Object , Object > connection = connections .get (address );
3184
+
3185
+ if (connection != null ) {
3186
+ LinkedList <Operation > queue = (LinkedList <Operation >) connection .get (keyQueue );
3187
+ queue .add (operation );
3188
+ queueStart (connection );
3189
+ }
3190
+ }
3191
+
3192
+ private void queueStart (HashMap <Object , Object > connection ) {
3193
+ LinkedList <Operation > queue = (LinkedList <Operation >) connection .get (keyQueue );
3206
3194
//Attempt to start the queue whenever a new operation is added
3207
3195
if (queue .size () > 1 ) {
3208
3196
//There was already something in the queue so wait for queueNext to be called
3209
3197
return ;
3210
3198
}
3211
3199
3212
3200
//Added to queue and immediately ready for processing
3213
- queueNext ();
3201
+ queueNext (connection );
3214
3202
}
3215
3203
3216
- private void queueNext () {
3204
+ private void queueNext (HashMap <Object , Object > connection ) {
3205
+ LinkedList <Operation > queue = (LinkedList <Operation >) connection .get (keyQueue );
3217
3206
//Start to process the next command
3218
3207
Operation operation = queue .peek ();
3219
3208
//If the operation was unsuccessful, remove immediately and start next
@@ -3232,11 +3221,12 @@ private void queueNext() {
3232
3221
result = unsubscribeAction (operation );
3233
3222
}
3234
3223
if (!result ) {
3235
- queueRemove ();
3224
+ queueRemove (connection );
3236
3225
}
3237
3226
}
3238
3227
3239
- private void queueRemove () {
3228
+ private void queueRemove (HashMap <Object , Object > connection ) {
3229
+ LinkedList <Operation > queue = (LinkedList <Operation >) connection .get (keyQueue );
3240
3230
//Ensure the queue has something in it, this should never be empty
3241
3231
if (queue .size () == 0 ) {
3242
3232
return ;
@@ -3250,7 +3240,7 @@ private void queueRemove() {
3250
3240
}
3251
3241
3252
3242
//Start the next item
3253
- queueNext ();
3243
+ queueNext (connection );
3254
3244
}
3255
3245
3256
3246
private HashMap <Object , Object > EnsureCallback (UUID characteristicUuid , HashMap <Object , Object > connection ) {
@@ -4013,19 +4003,20 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState
4013
4003
BluetoothDevice device = gatt .getDevice ();
4014
4004
String address = device .getAddress ();
4015
4005
4006
+ HashMap <Object , Object > connection = connections .get (address );
4007
+ if (connection == null ) {
4008
+ return ;
4009
+ }
4010
+
4016
4011
//Check for queued operations in progress on this device
4017
4012
if (newState == BluetoothProfile .STATE_DISCONNECTED ) {
4013
+ LinkedList <Operation > queue = (LinkedList <Operation >) connection .get (keyQueue );
4018
4014
Operation operation = queue .peek ();
4019
- if (operation != null && operation . device != null && operation . device . getAddress (). equals ( address ) ) {
4020
- queueRemove ();
4015
+ if (operation != null ) {
4016
+ queueRemove (connection );
4021
4017
}
4022
4018
}
4023
4019
4024
- HashMap <Object , Object > connection = connections .get (address );
4025
- if (connection == null ) {
4026
- return ;
4027
- }
4028
-
4029
4020
CallbackContext callbackContext = (CallbackContext ) connection .get (operationConnect );
4030
4021
4031
4022
JSONObject returnObj = new JSONObject ();
@@ -4144,8 +4135,6 @@ public void onServicesDiscovered(BluetoothGatt gatt, int status) {
4144
4135
4145
4136
@ Override
4146
4137
public void onCharacteristicRead (BluetoothGatt gatt , BluetoothGattCharacteristic characteristic , int status ) {
4147
- queueRemove ();
4148
-
4149
4138
//Get the connected device
4150
4139
BluetoothDevice device = gatt .getDevice ();
4151
4140
String address = device .getAddress ();
@@ -4155,6 +4144,8 @@ public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic
4155
4144
return ;
4156
4145
}
4157
4146
4147
+ queueRemove (connection );
4148
+
4158
4149
UUID characteristicUuid = characteristic .getUuid ();
4159
4150
4160
4151
CallbackContext callbackContext = GetCallback (characteristicUuid , connection , operationRead );
@@ -4219,8 +4210,6 @@ public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteris
4219
4210
4220
4211
@ Override
4221
4212
public void onCharacteristicWrite (BluetoothGatt gatt , BluetoothGattCharacteristic characteristic , int status ) {
4222
- queueRemove ();
4223
-
4224
4213
//Get the connected device
4225
4214
BluetoothDevice device = gatt .getDevice ();
4226
4215
String address = device .getAddress ();
@@ -4230,6 +4219,8 @@ public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristi
4230
4219
return ;
4231
4220
}
4232
4221
4222
+ queueRemove (connection );
4223
+
4233
4224
UUID characteristicUuid = characteristic .getUuid ();
4234
4225
4235
4226
CallbackContext callbackContext = GetCallback (characteristicUuid , connection , operationWrite );
@@ -4288,8 +4279,6 @@ public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristi
4288
4279
4289
4280
@ Override
4290
4281
public void onDescriptorRead (BluetoothGatt gatt , BluetoothGattDescriptor descriptor , int status ) {
4291
- queueRemove ();
4292
-
4293
4282
//Get the connected device
4294
4283
BluetoothDevice device = gatt .getDevice ();
4295
4284
String address = device .getAddress ();
@@ -4299,6 +4288,8 @@ public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descrip
4299
4288
return ;
4300
4289
}
4301
4290
4291
+ queueRemove (connection );
4292
+
4302
4293
BluetoothGattCharacteristic characteristic = descriptor .getCharacteristic ();
4303
4294
UUID characteristicUuid = characteristic .getUuid ();
4304
4295
UUID descriptorUuid = descriptor .getUuid ();
@@ -4332,8 +4323,6 @@ public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descrip
4332
4323
4333
4324
@ Override
4334
4325
public void onDescriptorWrite (BluetoothGatt gatt , BluetoothGattDescriptor descriptor , int status ) {
4335
- queueRemove ();
4336
-
4337
4326
//Get the connected device
4338
4327
BluetoothDevice device = gatt .getDevice ();
4339
4328
String address = device .getAddress ();
@@ -4343,6 +4332,8 @@ public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descri
4343
4332
return ;
4344
4333
}
4345
4334
4335
+ queueRemove (connection );
4336
+
4346
4337
BluetoothGattCharacteristic characteristic = descriptor .getCharacteristic ();
4347
4338
UUID characteristicUuid = characteristic .getUuid ();
4348
4339
UUID descriptorUuid = descriptor .getUuid ();
0 commit comments