@@ -709,7 +709,7 @@ private void removeNotifyCallback(CallbackContext callbackContext, String macAdd
709
709
// callbackContext.sendPluginResult(result);
710
710
//
711
711
712
- private void findLowEnergyDevices (CallbackContext callbackContext , UUID [] serviceUUIDs , int scanSeconds ) {
712
+ private void findLowEnergyDevices (CallbackContext callbackContext , UUID [] serviceUUIDs , int scanSeconds ) {
713
713
714
714
715
715
if (!PermissionHelper .hasPermission (this , ACCESS_COARSE_LOCATION )) {
@@ -721,48 +721,53 @@ private void findLowEnergyDevices(CallbackContext callbackContext, UUID[] servic
721
721
return ;
722
722
}
723
723
724
- // ignore if currently scanning, alternately could return an error
724
+
725
+
726
+ // ignore if currently scanning, alternately could return an error
725
727
if (bluetoothAdapter .isDiscovering ()) {
726
728
return ;
727
- }
728
729
729
- // TODO skip if currently scanning
730
+ } else {
730
731
731
- // clear non-connected cached peripherals
732
- for (Iterator <Map .Entry <String , Peripheral >> iterator = peripherals .entrySet ().iterator (); iterator .hasNext (); ) {
733
- Map .Entry <String , Peripheral > entry = iterator .next ();
734
- if (!entry .getValue ().isConnected ()) {
735
- iterator .remove ();
736
- }
737
- }
732
+ // 11/4/19 - NVF Added this to allow multiple threads to call this without causing a java.util.ConcurrentModificationException:
733
+ synchronized (peripherals ) {
734
+ // clear non-connected cached peripherals
735
+ for (Iterator <Map .Entry <String , Peripheral >> iterator = peripherals .entrySet ().iterator (); iterator .hasNext (); ) {
736
+ Map .Entry <String , Peripheral > entry = iterator .next ();
737
+ if (!entry .getValue ().isConnected ()) {
738
+ iterator .remove ();
739
+ }
740
+ }
741
+ }
738
742
739
- discoverCallback = callbackContext ;
743
+ discoverCallback = callbackContext ;
740
744
741
- if (serviceUUIDs != null && serviceUUIDs .length > 0 && !partialMatch ) {
742
- bluetoothAdapter .startLeScan (serviceUUIDs , this ); // Find a specific device - assumes it conforms to bluetooth specs and adverstises with standardized uuids
743
- } else {
744
- bluetoothAdapter .startLeScan (this ); // Look for all devices
745
- }
745
+ if (serviceUUIDs != null && serviceUUIDs .length > 0 && !partialMatch ) {
746
+ bluetoothAdapter .startLeScan (serviceUUIDs , this ); // Find a specific device - assumes it conforms to bluetooth specs and adverstises with standardized uuids
747
+ } else {
748
+ bluetoothAdapter .startLeScan (this ); // Look for all devices
749
+ }
746
750
747
- if (scanSeconds > 0 ) {
751
+ if (scanSeconds > 0 ) {
748
752
749
- Handler handler = new Handler (Looper .getMainLooper ()); // NVF Added the Looper.getMainLooper() call to allow us to embed this thread in another
750
- handler .postDelayed (new Runnable () {
751
- @ Override
752
- public void run () {
753
- LOG .d (TAG , "Stopping Scan" );
754
- BLECentralPlugin .this .bluetoothAdapter .stopLeScan (BLECentralPlugin .this );
755
- //setupCallbackBasedOnService("ba11f08c5f140b0d1080");
756
- }
757
- }, scanSeconds * 1000 );
758
- }
753
+ Handler handler = new Handler (Looper .getMainLooper ()); // NVF Added the Looper.getMainLooper() call to allow us to embed this thread in another
754
+ handler .postDelayed (new Runnable () {
755
+ @ Override
756
+ public void run () {
757
+ LOG .d (TAG , "Stopping Scan" );
758
+ BLECentralPlugin .this .bluetoothAdapter .stopLeScan (BLECentralPlugin .this );
759
+ //setupCallbackBasedOnService("ba11f08c5f140b0d1080");
760
+ }
761
+ }, scanSeconds * 1000 );
762
+ }
759
763
760
- // Default to call the failure callback unless we find something
761
- PluginResult result = new PluginResult (PluginResult .Status .NO_RESULT );
762
- result .setKeepCallback (true );
764
+ // Default to call the failure callback unless we find something
765
+ PluginResult result = new PluginResult (PluginResult .Status .NO_RESULT );
766
+ result .setKeepCallback (true );
763
767
764
- if (callbackContext !=null ) // 5/6/17 - NVF Added this to avoid cores - not sure if it will cause other issues instead of figuring out why...
765
- callbackContext .sendPluginResult (result );
768
+ if (callbackContext != null ) // 5/6/17 - NVF Added this to avoid cores - not sure if it will cause other issues instead of figuring out why...
769
+ callbackContext .sendPluginResult (result );
770
+ }
766
771
}
767
772
768
773
private void listKnownDevices (CallbackContext callbackContext ) {
0 commit comments