Skip to content

Commit a467ddb

Browse files
committed
1. Added a synchronized() call around the peripheral iteration when scanning to avoid java.util.ConcurrentModificationExceptions
1 parent 6ca1cb6 commit a467ddb

File tree

1 file changed

+38
-33
lines changed

1 file changed

+38
-33
lines changed

src/android/BLECentralPlugin.java

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ private void removeNotifyCallback(CallbackContext callbackContext, String macAdd
709709
// callbackContext.sendPluginResult(result);
710710
//
711711

712-
private void findLowEnergyDevices(CallbackContext callbackContext, UUID[] serviceUUIDs, int scanSeconds) {
712+
private void findLowEnergyDevices(CallbackContext callbackContext, UUID[] serviceUUIDs, int scanSeconds) {
713713

714714

715715
if(!PermissionHelper.hasPermission(this, ACCESS_COARSE_LOCATION)) {
@@ -721,48 +721,53 @@ private void findLowEnergyDevices(CallbackContext callbackContext, UUID[] servic
721721
return;
722722
}
723723

724-
// ignore if currently scanning, alternately could return an error
724+
725+
726+
// ignore if currently scanning, alternately could return an error
725727
if (bluetoothAdapter.isDiscovering()) {
726728
return;
727-
}
728729

729-
// TODO skip if currently scanning
730+
} else {
730731

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+
}
738742

739-
discoverCallback = callbackContext;
743+
discoverCallback = callbackContext;
740744

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+
}
746750

747-
if (scanSeconds > 0) {
751+
if (scanSeconds > 0) {
748752

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+
}
759763

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);
763767

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+
}
766771
}
767772

768773
private void listKnownDevices(CallbackContext callbackContext) {

0 commit comments

Comments
 (0)