AltBeacon / android-beacon-library

Allows Android apps to interact with BLE beacons
Apache License 2.0
2.83k stars 834 forks source link

Android 7.0 - Background scanning stops when the app has been killed from recent tasks #1056

Open perihanmirkelam opened 2 years ago

perihanmirkelam commented 2 years ago

Expected behavior

The app should continue to scan beacons when the app kills by the user except for "force stop".

Actual behavior

Background scanning stops when the app has been killed from recent tasks on Android 7.0 devices. (Scanning continues on Android 11 devices)

Steps to reproduce this behavior

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    val builder = Notification.Builder(appContext, "beacon-ref-notification-id")
    builder.setSmallIcon(R.mipmap.ic_launcher_myapp)
    builder.setContentTitle(appContext.resources.getString(R.string.reservation_notification_description))
    val intent = Intent(appContext, MainActivity::class.java)
    val pendingIntent = PendingIntent.getActivity(
        appContext,
        0,
        intent,
        PendingIntent.FLAG_UPDATE_CURRENT + PendingIntent.FLAG_IMMUTABLE
    )
    builder.setContentIntent(pendingIntent)
    val channel = NotificationChannel(
        "beacon-ref-notification-id",
        "Myapp Beacon Scanner Notification", NotificationManager.IMPORTANCE_NONE
    )
    channel.description = "Myapp Notification Channel Description"
    val notificationManager = appContext.getSystemService(
        Context.NOTIFICATION_SERVICE
    ) as NotificationManager
    notificationManager.createNotificationChannel(channel)
    builder.setChannelId(channel.id)
    BeaconManager.getInstanceForApplication(appContext)
        .enableForegroundServiceScanning(builder.build(), 456)
} else {
    val builder = NotificationCompat.Builder(appContext)
    builder.setSmallIcon(R.mipmap.ic_launcher_myapp)
    builder.setContentTitle(appContext.resources.getString(R.string.reservation_notification_description))
    val notification = builder.build()
    notification.flags = notification.flags or Notification.FLAG_FOREGROUND_SERVICE
    notification.flags = notification.flags or Notification.FLAG_ONGOING_EVENT

    val intent = Intent(appContext, MainActivity::class.java)
    val pendingIntent = PendingIntent.getActivity(
        appContext,
        0,
        intent,
        PendingIntent.FLAG_UPDATE_CURRENT + PendingIntent.FLAG_IMMUTABLE
    )
    builder.setContentIntent(pendingIntent)
    BeaconManager.getInstanceForApplication(appContext)
        .enableForegroundServiceScanning(builder.build(), 456)
}

Mobile device model and OS version

Lenovo VIBE K6 Note Android 7.0

Android Beacon Library version

2.19-beta5