Background scan times out on S10 (maybe other Samsungs)



  • I followed the discussion on scanning failing without native scanfilters when the app is in background, and because I was seeing the same problem, I followed the advice.

    Unfortunately the scanning continues not to work. I believe from the log output that the scanning is actually happening - it's not being blocked. But it returns nothing - the onEvent callback is never called. When the app is in foreground, it works perfectly. I've been through the testing loop multiple times and this is completely reproducible.

    App in foreground:
    2020-04-09 12:40:06.738 +0200 CEST I BluetoothLeScanner Logcat:0 logcat Start Scan with callback
    2020-04-09 12:40:06.738 +0200 CEST I BluetoothLeScanner Logcat:0 logcat onScannerRegistered() - status=0 scannerId=13 mScannerId=0
    2020-04-09 12:40:06.738 +0200 CEST I BluetoothAdapter Logcat:0 logcat STATE_ON
    2020-04-09 12:40:06.742 +0200 CEST I BluetoothTemperatureAndClockService Logcat:0 logcat current = [ON, SCANNING, BLE_SCAN_READY]
    2020-04-09 12:40:06.742 +0200 CEST I BluetoothTemperatureAndClockService Logcat:0 logcat exited = [STARTING_SCAN]
    2020-04-09 12:40:06.742 +0200 CEST I BluetoothTemperatureAndClockService Logcat:0 logcat State change event: StateEvent
    2020-04-09 12:40:06.742 +0200 CEST I BluetoothTemperatureAndClockService Logcat:0 logcat entered = [SCANNING]
    2020-04-09 12:40:07.175 +0200 CEST I P_ScanManager [Native] Logcat:0 logcat MAIN(20251) onScanResult() [50:F1:4A:C7:A8:D6] - Discovered new device via POST-LOLLIPOP scan.
    2020-04-09 12:40:07.182 +0200 CEST I BleManager Logcat:0 logcat UPDATE(21857) onDiscovered_wrapItUp() [50:F1:4A:C7:A8:D6] - Re-discovered BleDevice iWEECARE Temp Pal
    2020-04-09 12:40:07.204 +0200 CEST I BluetoothTemperatureAndClockService Logcat:0 logcat Discovered something: iweecare_temp_pal
    2020-04-09 12:40:07.204 +0200 CEST I BluetoothTemperatureAndClockService Logcat:0 logcat Read scan successful. Latest reading is ThermometerReading{deviceType='TempPal', temperatureInCentidegressCelcius=2546, batteryMillivolts=3957, timestampLinuxUTC=1586428807198}
    2020-04-09 12:40:07.21 +0200 CEST I PA_Task Logcat:0 logcat UPDATE(21857) setState() - Scan(SUCCEEDED 52768592 ) - 5759

    App in background:

    2020-04-09 12:35:06.92 +0200 CEST I BluetoothLeScanner Logcat:0 logcat Start Scan with callback
    2020-04-09 12:35:06.92 +0200 CEST I BluetoothAdapter Logcat:0 logcat STATE_ON
    2020-04-09 12:35:06.921 +0200 CEST I BluetoothLeScanner Logcat:0 logcat onScannerRegistered() - status=0 scannerId=13 mScannerId=0
    2020-04-09 12:35:06.962 +0200 CEST I BluetoothTemperatureAndClockService Logcat:0 logcat State change event: StateEvent
    2020-04-09 12:35:06.962 +0200 CEST I BluetoothTemperatureAndClockService Logcat:0 logcat entered = [SCANNING]
    2020-04-09 12:35:06.962 +0200 CEST I BluetoothTemperatureAndClockService Logcat:0 logcat exited = [STARTING_SCAN]
    2020-04-09 12:35:06.963 +0200 CEST I BluetoothTemperatureAndClockService Logcat:0 logcat current = [ON, SCANNING, BLE_SCAN_READY]
    2020-04-09 12:35:46.916 +0200 CEST I PA_Task Logcat:0 logcat UPDATE(20726) setState() - Scan(TIMED_OUT 83843218 ) - 4346
    2020-04-09 12:35:46.919 +0200 CEST I BluetoothAdapter Logcat:0 logcat STATE_ON
    2020-04-09 12:35:46.926 +0200 CEST I BluetoothLeScanner Logcat:0 logcat Stop Scan with callback

    Note that the scanning code is being run in a foreground service.

    Anyone suffering from this issue or similar?



  • It turns out that the answer was unexpectedly simple and related to Android 10:

    https://stackoverflow.com/questions/58428763/android-10-not-working-with-ble-bluetooth-scanning

    Thanks to the folks at SweetBlue for helping me out on this one.



  • @blawlor Funny enough, I'm seeing this behavior despite using a foreground service to scan. So in theory I shouldn't need the background location permission. Have you seen something like that too?