startBackgroundScan

fun startBackgroundScan(scanSettings: ScanSettings, scanFilters: List<ScanFilter>, pendingIntent: PendingIntent)

Starts a background BLE scan that delivers results via a android.app.PendingIntent.

Unlike scanDevices, this method allows BLE scanning to continue even when your app is not in the foreground. Scan results are delivered to a android.content.BroadcastReceiver specified by the pendingIntent, enabling your app to react to nearby BLE devices without maintaining an active Activity or Service.

Setup Requirements

To use background scanning, you must:

  1. Create a BroadcastReceiver to handle incoming scan results:

     class BleScanReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
    val results = intent.getParcelableArrayListExtra<ScanResult>(
    BluetoothLeScanner.EXTRA_LIST_SCAN_RESULT
    )
    results?.forEach { result ->
    // Process discovered device
    }
    }
    }
  2. Register the receiver in your AndroidManifest.xml:

     <receiver android:name=".BleScanReceiver"
    android:exported="false" />
  3. Create a PendingIntent targeting your receiver:

     val intent = Intent(context, BleScanReceiver::class.java)
    val pendingIntent = PendingIntent.getBroadcast(
    context, REQUEST_CODE, intent,
    PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
    )

Battery Considerations

Background scanning can significantly impact battery life. Consider using:

Platform Limitations

  • On Android 8.0+, background scans without filters may be throttled or ignored

  • Some devices may limit the number of concurrent background scans

  • Results may be batched and delivered with delays to conserve battery

Parameters

scanSettings

Configuration for the scan, including scan mode and report delay. Use ScanSettings.SCAN_MODE_LOW_POWER for battery-efficient scanning.

scanFilters

List of ScanFilter objects to limit results. On Android 8.0+, providing at least one filter is strongly recommended for reliable background scanning.

pendingIntent

The android.app.PendingIntent that will be broadcast when scan results are available. Must target a registered BroadcastReceiver. Use the same PendingIntent instance with stopBackgroundScan to stop scanning.

See also