In Swift, I start scanning peripherals with:
central.scanPeripherals(withServices: nil, options: [CBCentralManagerScanOptionAllowDuplicatesKey : self.allowDuplicateScan])
And it is allowing multiple discoveries of the same peripheral. So, the delegate didDiscover
function is called every time the peripheral is seen.
Now, I want to do exactly the same in Android (using blessed-android library). I'm calling central.scanForPeripheralsWithServices(services)
to discover new services. However, the onDiscoveredPeripheral
callback it's only called once per peripheral. So the question is if it is possible to have the same behavior than Swift?
Allowing multiple discoveries for the same peripheral is the default behaviour of blessed-android library so as the Android BLE as long as you don't change the scanning settings of BLE. This is so at least up to Android 13 as far as I know. However things might have been changed for Android 14.
If onDiscoveredPeripheral
is not getting called multiple times then you can manually change the Android BLE scanner settings to make it to do so by applying the following ScanSettings
to the startScan
method:
BluetoothCentralManager centralManager;
// Create a scan settings object and set callback type to CALLBACK_TYPE_ALL_MATCHES
ScanSettings scanSettings = new ScanSettings.Builder()
.setCallbackType(CALLBACK_TYPE_ALL_MATCHES)
.build();
// Finally pass your scan settings to the startScan method
centralManager.startScan(yourFilterList, scanSettings, yourScanCallback);
CALLBACK_TYPE_ALL_MATCHES
flag for the setCallbackType
method.CALLBACK_TYPE_FIRST_MATCH
flag to the setCallbackType
method.The callback types are available for the different API levels of Android SDK. So make sure your app is covering the required API checks.