@@ -89,9 +89,6 @@ public class BluetoothLePlugin extends CordovaPlugin {
8989 //Quick Writes
9090 private LinkedList <byte []> queueQuick = new LinkedList <byte []>();
9191
92- //Queueing
93- private LinkedList <Operation > queue = new LinkedList <Operation >();
94-
9592 //Object keys
9693 private final String keyStatus = "status" ;
9794 private final String keyError = "error" ;
@@ -130,6 +127,7 @@ public class BluetoothLePlugin extends CordovaPlugin {
130127 private final String keyConnectionPriority = "connectionPriority" ;
131128 private final String keyMtu = "mtu" ;
132129 private final String keyPin = "pin" ;
130+ private final String keyQueue = "queue" ;
133131
134132 //Write Types
135133 private final String writeTypeNoResponse = "noResponse" ;
@@ -353,30 +351,24 @@ public boolean execute(String action, final JSONArray args, final CallbackContex
353351 discoverAction (args , callbackContext );
354352 } else if ("read" .equals (action )) {
355353 Operation operation = new Operation ("read" , args , callbackContext );
356- queue .add (operation );
357- queueStart ();
354+ queueAdd (operation );
358355 } else if ("subscribe" .equals (action )) {
359356 Operation operation = new Operation ("subscribe" , args , callbackContext );
360- queue .add (operation );
361- queueStart ();
357+ queueAdd (operation );
362358 } else if ("unsubscribe" .equals (action )) {
363359 Operation operation = new Operation ("unsubscribe" , args , callbackContext );
364- queue .add (operation );
365- queueStart ();
360+ queueAdd (operation );
366361 } else if ("write" .equals (action )) {
367362 Operation operation = new Operation ("write" , args , callbackContext );
368- queue .add (operation );
369- queueStart ();
363+ queueAdd (operation );
370364 } else if ("writeQ" .equals (action )) {
371365 writeQAction (args , callbackContext );
372366 } else if ("readDescriptor" .equals (action )) {
373367 Operation operation = new Operation ("readDescriptor" , args , callbackContext );
374- queue .add (operation );
375- queueStart ();
368+ queueAdd (operation );
376369 } else if ("writeDescriptor" .equals (action )) {
377370 Operation operation = new Operation ("writeDescriptor" , args , callbackContext );
378- queue .add (operation );
379- queueStart ();
371+ queueAdd (operation );
380372 } else if ("rssi" .equals (action )) {
381373 rssiAction (args , callbackContext );
382374 } else if ("isInitialized" .equals (action )) {
@@ -747,10 +739,14 @@ private void startAdvertisingAction(JSONArray args, CallbackContext callbackCont
747739 dataBuilder .addManufacturerData (manufacturerId , manufacturerSpecificData );
748740 }
749741
750- //dataBuilder.addServiceData();
751742 UUID uuid = getUUID (obj .optString ("service" , null ));
752743 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+ }
754750 }
755751
756752 dataBuilder .setIncludeDeviceName (obj .optBoolean ("includeDeviceName" , true ));
@@ -1497,6 +1493,10 @@ private void connectAction(JSONArray args, CallbackContext callbackContext) {
14971493 }
14981494
14991495 connection .put (keyPeripheral , bluetoothGatt );
1496+
1497+ LinkedList <Operation > queue = new LinkedList <Operation >();
1498+
1499+ connection .put (keyQueue , queue );
15001500 }
15011501
15021502 private void reconnectAction (JSONArray args , CallbackContext callbackContext ) {
@@ -1637,12 +1637,6 @@ private void closeAction(JSONArray args, CallbackContext callbackContext) {
16371637 connections .remove (device .getAddress ());
16381638
16391639 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- }
16461640 }
16471641
16481642 private void discoverAction (JSONArray args , CallbackContext callbackContext ) {
@@ -1727,9 +1721,6 @@ private boolean readAction(Operation operation) {
17271721 }
17281722
17291723 JSONObject obj = getArgsObject (args );
1730- if (isNotArgsObject (obj , callbackContext )) {
1731- return false ;
1732- }
17331724
17341725 String address = getAddress (obj );
17351726 if (isNotAddress (address , callbackContext )) {
@@ -1783,8 +1774,6 @@ private boolean readAction(Operation operation) {
17831774 return false ;
17841775 }
17851776
1786- operation .device = device ;
1787-
17881777 return true ;
17891778 }
17901779
@@ -1797,9 +1786,6 @@ private boolean subscribeAction(Operation operation) {
17971786 }
17981787
17991788 JSONObject obj = getArgsObject (args );
1800- if (isNotArgsObject (obj , callbackContext )) {
1801- return false ;
1802- }
18031789
18041790 String address = getAddress (obj );
18051791 if (isNotAddress (address , callbackContext )) {
@@ -1891,8 +1877,6 @@ private boolean subscribeAction(Operation operation) {
18911877 return false ;
18921878 }
18931879
1894- operation .device = device ;
1895-
18961880 return true ;
18971881 }
18981882
@@ -1905,9 +1889,6 @@ private boolean unsubscribeAction(Operation operation) {
19051889 }
19061890
19071891 JSONObject obj = getArgsObject (args );
1908- if (isNotArgsObject (obj , callbackContext )) {
1909- return false ;
1910- }
19111892
19121893 String address = getAddress (obj );
19131894 if (isNotAddress (address , callbackContext )) {
@@ -1984,8 +1965,6 @@ private boolean unsubscribeAction(Operation operation) {
19841965 return false ;
19851966 }
19861967
1987- operation .device = device ;
1988-
19891968 return true ;
19901969 }
19911970
@@ -1998,9 +1977,6 @@ private boolean writeAction(Operation operation) {
19981977 }
19991978
20001979 JSONObject obj = getArgsObject (args );
2001- if (isNotArgsObject (obj , callbackContext )) {
2002- return false ;
2003- }
20041980
20051981 String address = getAddress (obj );
20061982 if (isNotAddress (address , callbackContext )) {
@@ -2073,8 +2049,6 @@ private boolean writeAction(Operation operation) {
20732049 return false ;
20742050 }
20752051
2076- operation .device = device ;
2077-
20782052 return true ;
20792053 }
20802054
@@ -2229,9 +2203,6 @@ private boolean readDescriptorAction(Operation operation) {
22292203 }
22302204
22312205 JSONObject obj = getArgsObject (args );
2232- if (isNotArgsObject (obj , callbackContext )) {
2233- return false ;
2234- }
22352206
22362207 String address = getAddress (obj );
22372208 if (isNotAddress (address , callbackContext )) {
@@ -2292,8 +2263,6 @@ private boolean readDescriptorAction(Operation operation) {
22922263 return false ;
22932264 }
22942265
2295- operation .device = device ;
2296-
22972266 return true ;
22982267 }
22992268
@@ -2306,9 +2275,6 @@ private boolean writeDescriptorAction(Operation operation) {
23062275 }
23072276
23082277 JSONObject obj = getArgsObject (args );
2309- if (isNotArgsObject (obj , callbackContext )) {
2310- return false ;
2311- }
23122278
23132279 String address = getAddress (obj );
23142280 if (isNotAddress (address , callbackContext )) {
@@ -2394,8 +2360,6 @@ private boolean writeDescriptorAction(Operation operation) {
23942360 return false ;
23952361 }
23962362
2397- operation .device = device ;
2398-
23992363 return true ;
24002364 }
24012365
@@ -3202,18 +3166,43 @@ private BluetoothGattDescriptor getDescriptor(JSONObject obj, BluetoothGattChara
32023166 }
32033167
32043168 //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 );
32063194 //Attempt to start the queue whenever a new operation is added
32073195 if (queue .size () > 1 ) {
32083196 //There was already something in the queue so wait for queueNext to be called
32093197 return ;
32103198 }
32113199
32123200 //Added to queue and immediately ready for processing
3213- queueNext ();
3201+ queueNext (connection );
32143202 }
32153203
3216- private void queueNext () {
3204+ private void queueNext (HashMap <Object , Object > connection ) {
3205+ LinkedList <Operation > queue = (LinkedList <Operation >) connection .get (keyQueue );
32173206 //Start to process the next command
32183207 Operation operation = queue .peek ();
32193208 //If the operation was unsuccessful, remove immediately and start next
@@ -3232,11 +3221,12 @@ private void queueNext() {
32323221 result = unsubscribeAction (operation );
32333222 }
32343223 if (!result ) {
3235- queueRemove ();
3224+ queueRemove (connection );
32363225 }
32373226 }
32383227
3239- private void queueRemove () {
3228+ private void queueRemove (HashMap <Object , Object > connection ) {
3229+ LinkedList <Operation > queue = (LinkedList <Operation >) connection .get (keyQueue );
32403230 //Ensure the queue has something in it, this should never be empty
32413231 if (queue .size () == 0 ) {
32423232 return ;
@@ -3250,7 +3240,7 @@ private void queueRemove() {
32503240 }
32513241
32523242 //Start the next item
3253- queueNext ();
3243+ queueNext (connection );
32543244 }
32553245
32563246 private HashMap <Object , Object > EnsureCallback (UUID characteristicUuid , HashMap <Object , Object > connection ) {
@@ -4013,19 +4003,20 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState
40134003 BluetoothDevice device = gatt .getDevice ();
40144004 String address = device .getAddress ();
40154005
4006+ HashMap <Object , Object > connection = connections .get (address );
4007+ if (connection == null ) {
4008+ return ;
4009+ }
4010+
40164011 //Check for queued operations in progress on this device
40174012 if (newState == BluetoothProfile .STATE_DISCONNECTED ) {
4013+ LinkedList <Operation > queue = (LinkedList <Operation >) connection .get (keyQueue );
40184014 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 );
40214017 }
40224018 }
40234019
4024- HashMap <Object , Object > connection = connections .get (address );
4025- if (connection == null ) {
4026- return ;
4027- }
4028-
40294020 CallbackContext callbackContext = (CallbackContext ) connection .get (operationConnect );
40304021
40314022 JSONObject returnObj = new JSONObject ();
@@ -4144,8 +4135,6 @@ public void onServicesDiscovered(BluetoothGatt gatt, int status) {
41444135
41454136 @ Override
41464137 public void onCharacteristicRead (BluetoothGatt gatt , BluetoothGattCharacteristic characteristic , int status ) {
4147- queueRemove ();
4148-
41494138 //Get the connected device
41504139 BluetoothDevice device = gatt .getDevice ();
41514140 String address = device .getAddress ();
@@ -4155,6 +4144,8 @@ public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic
41554144 return ;
41564145 }
41574146
4147+ queueRemove (connection );
4148+
41584149 UUID characteristicUuid = characteristic .getUuid ();
41594150
41604151 CallbackContext callbackContext = GetCallback (characteristicUuid , connection , operationRead );
@@ -4219,8 +4210,6 @@ public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteris
42194210
42204211 @ Override
42214212 public void onCharacteristicWrite (BluetoothGatt gatt , BluetoothGattCharacteristic characteristic , int status ) {
4222- queueRemove ();
4223-
42244213 //Get the connected device
42254214 BluetoothDevice device = gatt .getDevice ();
42264215 String address = device .getAddress ();
@@ -4230,6 +4219,8 @@ public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristi
42304219 return ;
42314220 }
42324221
4222+ queueRemove (connection );
4223+
42334224 UUID characteristicUuid = characteristic .getUuid ();
42344225
42354226 CallbackContext callbackContext = GetCallback (characteristicUuid , connection , operationWrite );
@@ -4288,8 +4279,6 @@ public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristi
42884279
42894280 @ Override
42904281 public void onDescriptorRead (BluetoothGatt gatt , BluetoothGattDescriptor descriptor , int status ) {
4291- queueRemove ();
4292-
42934282 //Get the connected device
42944283 BluetoothDevice device = gatt .getDevice ();
42954284 String address = device .getAddress ();
@@ -4299,6 +4288,8 @@ public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descrip
42994288 return ;
43004289 }
43014290
4291+ queueRemove (connection );
4292+
43024293 BluetoothGattCharacteristic characteristic = descriptor .getCharacteristic ();
43034294 UUID characteristicUuid = characteristic .getUuid ();
43044295 UUID descriptorUuid = descriptor .getUuid ();
@@ -4332,8 +4323,6 @@ public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descrip
43324323
43334324 @ Override
43344325 public void onDescriptorWrite (BluetoothGatt gatt , BluetoothGattDescriptor descriptor , int status ) {
4335- queueRemove ();
4336-
43374326 //Get the connected device
43384327 BluetoothDevice device = gatt .getDevice ();
43394328 String address = device .getAddress ();
@@ -4343,6 +4332,8 @@ public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descri
43434332 return ;
43444333 }
43454334
4335+ queueRemove (connection );
4336+
43464337 BluetoothGattCharacteristic characteristic = descriptor .getCharacteristic ();
43474338 UUID characteristicUuid = characteristic .getUuid ();
43484339 UUID descriptorUuid = descriptor .getUuid ();
0 commit comments