bluetooth-lowenergyandroid-bluetooth

Discover the same peripheral multiple times in blessed-android


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?


Solution

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

    Remarks

    NOTE THAT!

    The callback types are available for the different API levels of Android SDK. So make sure your app is covering the required API checks.