start Background Scan
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:
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
}
}
}Content copied to clipboardRegister the receiver in your AndroidManifest.xml:
<receiver android:name=".BleScanReceiver"
android:exported="false" />Content copied to clipboardCreate 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
)Content copied to clipboard
Battery Considerations
Background scanning can significantly impact battery life. Consider using:
ScanSettings.SCAN_MODE_LOW_POWER or ScanSettings.SCAN_MODE_OPPORTUNISTIC
Specific ScanFilter entries to reduce wake-ups
Appropriate scan intervals based on your use case
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
Configuration for the scan, including scan mode and report delay. Use ScanSettings.SCAN_MODE_LOW_POWER for battery-efficient scanning.
List of ScanFilter objects to limit results. On Android 8.0+, providing at least one filter is strongly recommended for reliable background scanning.
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.