Search code examples
androidbluetooth-lowenergybeaconaltbeacon

Bluetooth scan methods - PendingIntent or ScanCallback?


my application scans BLE devices in background. I don't understand what determines the choice between these two methods: advantage / disadvantage. (I tested both and it works)

public int startScan (List<ScanFilter> filters, 
            ScanSettings settings, 
            PendingIntent callbackIntent)

public void startScan (List<ScanFilter> filters, 
                ScanSettings settings, 
                ScanCallback callback)`

thank you for help


Solution

  • Android 8+ allows you to deliver Bluetooth scan results with two different messaging transports: Callback and Intent.

    Callback

    This makes a call to a method you define with the scan results.

    Pros:

    • Simpler setup in code
    • Lighter weight (less CPU usage) due to no need to serialize scan result data structures. This is especially important if you expect a heavy volume of detections.
    • Works on older Android versions, not just 8+

    Cons:

    • Background scans are generally limited to 10 minutes at a time unless you start a Foreground Service to keep your app running in the background.
    • Cannot re-launch your app in the background on detection

    Intent

    This delivers the scan results to a BroadcastReceiver using Android’s Intent messaging structure

    Pros:

    • Delivers results in the background indefinitely without a Foreground Service
    • Can re-launch your app on detection

    Cons:

    • Higher power usage, especially with heavy scan results
    • More complex code setup
    • Does not work before Android 8
    • Scans to detect pattern match loss will be missed if Android Doze mode has kicked in
    • Processing scan results at regular intervals (if needed) still requires timers and perhaps a Foreground Service, negating many of the Pros