IMPORTANT: This forum is reserved for feature requests or reproducible bugs with the library itself. If you need help with using the library with your project, please open a new question on StackOverflow.com.
Application code:
class MainActivity : AppCompatActivity(), MonitorNotifier {
val TAG = "BeaconBackgroundScan"
private fun bringAppToFront(){
val intentToReopenApp = Intent(applicationContext, MainActivity::class.java)
intentToReopenApp.flags = Intent.FLAG_ACTIVITY_NEW_TASK /*+ Intent.FLAG_ACTIVITY_CLEAR_TOP + Intent.FLAG_ACTIVITY_REORDER_TO_FRONT*/
applicationContext.startActivity(intentToReopenApp)
}
override fun onStop() {
BeaconManager.setDebug(true)
val beaconManager = BeaconManager.getInstanceForApplication(this)
beaconManager.beaconParsers.clear()
beaconManager.beaconParsers.add(
BeaconParser().
setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"))
beaconManager.isRegionStatePersistenceEnabled = false
beaconManager.setBackgroundModeInternal(true)
beaconManager.setEnableScheduledScanJobs(false);
beaconManager.backgroundBetweenScanPeriod = 100;
beaconManager.backgroundScanPeriod = 1100;
Toast.makeText(this@MainActivity, "Scanning for beacon...", Toast.LENGTH_SHORT).show()
val region = Region("all-beacons-region", Identifier.parse("a32237e7-3ec0-c584-864b-b999f98203f7"), null, null)
beaconManager.startMonitoring(region)
beaconManager.addMonitorNotifier(this@MainActivity)
Log.d(TAG, "Scan started")
super.onStop()
}
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
Toast.makeText(this@MainActivity, "App started", Toast.LENGTH_SHORT).show();
if (ContextCompat.checkSelfPermission(this@MainActivity,
Manifest.permission.ACCESS_FINE_LOCATION) !==
PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this@MainActivity,
Manifest.permission.ACCESS_FINE_LOCATION)) {
ActivityCompat.requestPermissions(this@MainActivity,
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 1)
} else {
ActivityCompat.requestPermissions(this@MainActivity,
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 1)
}
}
if (ContextCompat.checkSelfPermission(this@MainActivity,
Manifest.permission.ACCESS_BACKGROUND_LOCATION) !==
PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this@MainActivity,
Manifest.permission.ACCESS_BACKGROUND_LOCATION)) {
ActivityCompat.requestPermissions(this@MainActivity,
arrayOf(Manifest.permission.ACCESS_BACKGROUND_LOCATION), 1)
} else {
ActivityCompat.requestPermissions(this@MainActivity,
arrayOf(Manifest.permission.ACCESS_BACKGROUND_LOCATION), 1)
}
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>,
grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
1 -> {
if (grantResults.isNotEmpty() && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
if ((ContextCompat.checkSelfPermission(this@MainActivity,
Manifest.permission.ACCESS_FINE_LOCATION) ===
PackageManager.PERMISSION_GRANTED) && (ContextCompat.checkSelfPermission(this@MainActivity,
Manifest.permission.ACCESS_BACKGROUND_LOCATION) ===
PackageManager.PERMISSION_GRANTED)) {
Toast.makeText(this, "Permissions Granted", Toast.LENGTH_SHORT).show()
}
} else {
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show()
}
return
}
}
}
override fun didEnterRegion(region: Region?) {
this.runOnUiThread {
Log.d(TAG, "did enter region.")
bringAppToFront()
Toast.makeText(this, "Beacon found", Toast.LENGTH_SHORT).show()
}
}
override fun didExitRegion(region: Region?) {
Log.d(TAG, "did exit region.")
}
override fun didDetermineStateForRegion(state: Int, region: Region?) {
if (state == MonitorNotifier.INSIDE) {
Log.d(TAG, "Beacon in range")
val beaconManager = BeaconManager.getInstanceForApplication(this)
if (region != null) {
beaconManager.stopMonitoring(region)
beaconManager.stopRangingBeacons(region)
}
}
else {
Log.d(TAG, "Beacon outside of range")
}
}
}
@JulianModlinski what you report is expected behavior. Android 8+ forbids background running for more than ~10 minutes. The alternative is to use a foreground service as described here.
Expected behavior
The iBeacon scan continues in the background
Actual behavior
The background scan stops suddenly Logcat:
Steps to reproduce this behavior
Mobile device model and OS version
Samsung Galaxy A10 Android version 11
Android Beacon Library version
2.19
IMPORTANT: This forum is reserved for feature requests or reproducible bugs with the library itself. If you need help with using the library with your project, please open a new question on StackOverflow.com.
Application code: