androidandroid-intentbackgroundbluetooth-lowenergybeacon

Intent-based scan with Android Beacon Library


I'm trying to detect iBeacons with the Android Beacon Library while my Android application is in the killed state (doesn't appear in the task manager).

I've already managed to monitor and range iBeacon regions with foreground services and everything is working. Now, I want to try intent-based scans to detect beacons, but apparently I'm missing something to make that work. I tried to configure intent-based scans with the available reference Kotlin application, but I believe I didn't fully understand how intent-based scans work.

I report below the useful part of my code:

override fun onCreate() {
    super.onCreate()

    val beaconManager = BeaconManager.getInstanceForApplication(this)
    BeaconManager.setDebug(true)

    val parser = BeaconParser().
    setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24")
    parser.setHardwareAssistManufacturerCodes(arrayOf(0x004c).toIntArray())
    beaconManager.getBeaconParsers().add(
        parser)

    setupBeaconScanning()
}

fun setupBeaconScanning() {
    val beaconManager = BeaconManager.getInstanceForApplication(this)

    // Enable intent-based scans
    beaconManager.setIntentScanningStrategyEnabled(true)

    beaconManager.startMonitoring(region)

    val regionViewModel = BeaconManager.getInstanceForApplication(this).getRegionViewModel(region)
    regionViewModel.regionState.observeForever(centralMonitoringObserver)
}

val centralMonitoringObserver = Observer<Int> { state ->
    if (state == MonitorNotifier.OUTSIDE) {
        Log.d(TAG, "outside beacon region: " + region)
    }
    else {
        Log.d(TAG, "inside beacon region: " + region)
        sendNotification()
    }
}

With this configuration, the monitoring is not working (nor when the app is in foreground, neither when the app is in the background).


Solution

  • As of Android 14, Samsung devices will stop intent-backed Bluetooth scans if the user kills the app by swiping it off the screen.

    2024-04-14 20:05:58.646 4387-4990 BtGatt.GattService pid-4387 E [GSIM LOG]: gsimLogHandler, msg: MESSAGE_SCAN_STOP, appName: org.altbeacon.beaconreference, scannerId: 8, reportDelayMillis=0

    The scans will resume automatically when the library's "ScanJob" runs, which is scheduled to happen every 15 minutes +/- 5 minutes. After the sequence above, you can see this happens here about six minutes later:

    2024-04-14 20:11:07.541 28466-28466 BeaconManager           org.altbeacon.beaconreference        I  BeaconManager started up on pid 28466 named 'org.altbeacon.beaconreference' for application package 'org.altbeacon.beaconreference'.  isMainProcess=true
    2024-04-14 20:11:07.607 28466-28466 ScanJob                 org.altbeacon.beaconreference        I  Using immediateScanJobId from manifest: 208352939
    2024-04-14 20:11:08.178 28466-28496 ScanHelper              org.altbeacon.beaconreference        D  beacon detected : id1: 74278bda-b644-4520-8f0c-720eaf059935 id2: 256 id3: 15187
    

    This is actually a new run of the app, launched in the background when the 15 minute job started up.

    If you want to resume detections more quickly, it is only possible by the user interacting with the app. You may be able to send a notification to the user after the app is killed warning them they have disabled the app and asking them to re-enable it by tapping the notification (which re-launches the app and fixes the problem.)

    On a related point:

    As of Android 14, Samsung devices will not deliver Beacon scan results via BroadcastIntent once the screen is turned off after first app launch. Delivery will resume as soon as the screen is illuminated again.

    For apps using the Android Beacon Library and using the IntentScanStrategy, this means Samsung devices with Android 14 will not detect (briefly) between the first screen off and the next screen on operation after app launch. It is unclear whether this is a Samsung bug or some intentional behavior.

    See the related issue in the Android Beacon Library open source repo here for more info.