Home > Enterprise >  Background scan for iBeacon stops after kill the application
Background scan for iBeacon stops after kill the application

Time:06-16

I'm developing an app to scan for iBeacons using android beacon library. I have problem scanning in the background after I kill the application. The moment I kill the application the In logcat I receive the following information:

2022-06-16 11:56:04.176 4020-4020/? I/ActivityManager: Killing 18682:com.example.beaconbackgroundscan/u0a360 (adj 1001): remove task
2022-06-16 11:56:04.177 4020-6040/? W/NotificationService: Toast already killed. pkg=com.example.beaconbackgroundscan token=android.os.BinderProxy@91a7c6e
2022-06-16 11:56:04.218 4382-4399/? D/LeAppInfo: removeLeacReportedServerApp, appName: com.example.beaconbackgroundscan
2022-06-16 11:56:04.222 4382-18751/? D/LeAppInfo: removeLeacReportedServerApp, appName: com.example.beaconbackgroundscan
2022-06-16 11:56:04.226 4382-4871/? E/BtGatt.GattService: [GSIM LOG]: gsimLogHandler, msg: MESSAGE_SCAN_STOP, appName: com.example.beaconbackgroundscan, scannerId: 7, reportDelayMillis=0

My code to initialize the scan is as following:

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.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()
    }

How can I ensure that the scan will continue in the background?

CodePudding user response:

I think WorkManager is a good solution. That's the documentation. I'm sure there are a lot tutorials out there too.

CodePudding user response:

I posted the same question as an issue on the GitHub of the plugin. The creator's answer is that this behavior is normal. It is caused by the operating system. His suggestion is to use foreground service in this case.

  • Related