ibeaconaltbeaconibeacon-androidandroid-ibeacon

Android Beacon Library - Keep logging periodicScanJobId and immediateScanJobId


I keep getting these logs even when I called the unbind method after ranging. Wonder what might cause the issue?

The code is pretty much the same as the Ranging Example Code on Android-beacon-libary-Samples The only difference is I'm using a ForegroundService instead of an Activity.

2020-05-20 14:06:40.319 31367-31367/? I/ScanJob: Using periodicScanJobId from manifest: 208352940
2020-05-20 14:06:52.567 31367-31367/? I/ScanJob: Using immediateScanJobId from manifest: 208352939
2020-05-20 14:06:52.570 31367-31367/? I/ScanJob: Using periodicScanJobId from manifest: 208352940
2020-05-20 14:07:05.136 31367-31367/? I/ScanJob: Using immediateScanJobId from manifest: 208352939
2020-05-20 14:07:05.139 31367-31367/? I/ScanJob: Using periodicScanJobId from manifest: 208352940
2020-05-20 14:07:17.260 31367-31367/? I/ScanJob: Using immediateScanJobId from manifest: 208352939
2020-05-20 14:07:17.261 31367-31367/? I/ScanJob: Using periodicScanJobId from manifest: 208352940
2020-05-20 14:07:29.559 31367-31367/? I/ScanJob: Using immediateScanJobId from manifest: 208352939
2020-05-20 14:07:29.561 31367-31367/? I/ScanJob: Using periodicScanJobId from manifest: 208352940
class BeaconForegroundService: Service(), BeaconConsumer {
    private lateinit var beaconManager: BeaconManager

    companion object {
        fun startBeaconService() {
            ContextCompat.startForegroundService(TestApp.appContext, Intent(TestApp.appContext, BeaconForegroundService::class.java))
        }

        fun stopBeaconService() {
            val signServiceIntent = Intent(TestApp.appContext, BeaconForegroundService::class.java)
            TestApp.appContext.stopService(signServiceIntent)
            val beaconManager = BeaconManager.getInstanceForApplication(TestApp.appContext)
            beaconManager.removeAllRangeNotifiers()
        }
    }

    private fun setupForegroundNotificationService(title: String, contentText: String): NotificationCompat.Builder {
        //... a notification
        return builder
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        val notification = setupForegroundNotificationService("Beacon", "Testing").build()
        startForeground(111989, notification)

        beaconManager = BeaconManager.getInstanceForApplication(TestApp.appContext)
        beaconManager.beaconParsers.add(BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"))
        beaconManager.bind(this)

        return START_NOT_STICKY
    }

    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    override fun onUnbind(intent: Intent?): Boolean {
        beaconManager.unbind(this)
        return super.onUnbind(intent)
    }

    override fun onDestroy() {
        beaconManager.unbind(this)
        super.onDestroy()
    }

    private var countT = 0
    private val region = Region("com.example.myDeviceRegion", Identifier.fromUuid(UUID.fromString("39ED98FF-2900-441A-802F-9C398FC199D2")), Identifier.fromInt(100), Identifier.fromInt(1))

    override fun onBeaconServiceConnect() {
        beaconManager.removeAllRangeNotifiers()
        beaconManager.addRangeNotifier { beacons, region ->
            if (beacons.isNotEmpty()) {
                val beacon = beacons.iterator().next()
                Log.i("MrFu", "The first beacon I see is about "+ beacon.distance +" meters away. ${beacon}....$countT")
                if (countT > 10) {
                    // Reason why the logs appear.
                    beaconManager.stopRangingBeaconsInRegion(region)
                    stopBeaconService()
                    countT = 0
                }
                countT += 1
            }
            Log.i("MrFu", "beacons = ${beacons.size}  region = ${region.uniqueId} ")
        }
        beaconManager.startRangingBeaconsInRegion(region)
    }
}

I've added the code above that has the issue. I think it happens when I added this line of code,

beaconManager.stopRangingBeaconsInRegion(region)

I assume I shouldn't stop ranging when I found the beacon? I should just stop the foreground service?


After removing the stopRangingBeaconsInRegion method, these logs are appearing:

2020-05-20 16:53:15.029 12066-12066/com.whosonlocation.wolmobile2 I/ScanJob: Scan job runtime expired: org.altbeacon.beacon.service.ScanJob@89aa544
2020-05-20 16:53:15.044 12066-12066/com.whosonlocation.wolmobile2 D/BluetoothAdapter: isLeEnabled(): ON
2020-05-20 16:53:15.062 12066-12387/com.whosonlocation.wolmobile2 D/BluetoothAdapter: isLeEnabled(): ON
2020-05-20 16:53:15.062 12066-12387/com.whosonlocation.wolmobile2 D/BluetoothLeScanner: could not find callback wrapper
2020-05-20 16:53:15.071 12066-12387/com.whosonlocation.wolmobile2 D/BluetoothAdapter: isLeEnabled(): ON
2020-05-20 16:53:15.071 12066-12387/com.whosonlocation.wolmobile2 D/BluetoothLeScanner: could not find callback wrapper
2020-05-20 16:53:15.165 12066-12066/com.whosonlocation.wolmobile2 I/ScanJob: Using immediateScanJobId from manifest: 208352939
2020-05-20 16:53:15.176 12066-12066/com.whosonlocation.wolmobile2 I/ScanJob: Using periodicScanJobId from manifest: 208352940
2020-05-20 16:53:15.177 12066-12066/com.whosonlocation.wolmobile2 W/JobInfo: Requested interval +5m0s0ms for job 208352940 is too small; raising to +15m0s0ms
2020-05-20 16:53:15.177 12066-12066/com.whosonlocation.wolmobile2 W/JobInfo: Requested flex 0 for job 208352940 is too small; raising to +5m0s0ms
2020-05-20 16:53:15.268 12066-12475/com.whosonlocation.wolmobile2 I/CycledLeScanner: Using Android O scanner
2020-05-20 16:53:15.271 12066-12475/com.whosonlocation.wolmobile2 I/ScanJob: Using immediateScanJobId from manifest: 208352939
2020-05-20 16:53:15.271 12066-12475/com.whosonlocation.wolmobile2 I/ScanJob: Running immediate scan job: instance is org.altbeacon.beacon.service.ScanJob@614cce7
2020-05-20 16:53:15.273 12066-12475/com.whosonlocation.wolmobile2 I/ScanJob: scanJob version 2.17 is starting up on the main process
2020-05-20 16:53:15.276 12066-12475/com.whosonlocation.wolmobile2 W/ModelSpecificDistanceCalculator: Cannot find match for this device.  Using default
2020-05-20 16:53:15.276 12066-12475/com.whosonlocation.wolmobile2 W/ModelSpecificDistanceCalculator: Cannot find match for this device.  Using default
2020-05-20 16:53:15.280 12066-12475/com.whosonlocation.wolmobile2 D/BluetoothAdapter: isLeEnabled(): ON
2020-05-20 16:53:15.286 12066-12475/com.whosonlocation.wolmobile2 I/ScanJob: Scan job running for 300000 millis
2020-05-20 16:53:15.287 12066-12476/com.whosonlocation.wolmobile2 D/BluetoothAdapter: isLeEnabled(): ON
2020-05-20 16:53:15.292 12066-12375/com.whosonlocation.wolmobile2 D/BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=10 mScannerId=0
2020-05-20 16:53:22.066 12066-12476/com.whosonlocation.wolmobile2 D/BluetoothAdapter: isLeEnabled(): ON
2020-05-20 16:53:22.085 12066-12476/com.whosonlocation.wolmobile2 D/BluetoothAdapter: isLeEnabled(): ON
2020-05-20 16:53:22.100 12066-12375/com.whosonlocation.wolmobile2 D/BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=9 mScannerId=0
2020-05-20 16:53:28.879 12066-12476/com.whosonlocation.wolmobile2 D/BluetoothAdapter: isLeEnabled(): ON
2020-05-20 16:53:28.907 12066-12476/com.whosonlocation.wolmobile2 D/BluetoothAdapter: isLeEnabled(): ON
2020-05-20 16:53:28.926 12066-12375/com.whosonlocation.wolmobile2 D/BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=9 mScannerId=0
2020-05-20 16:53:35.733 12066-12476/com.whosonlocation.wolmobile2 D/BluetoothAdapter: isLeEnabled(): ON
2020-05-20 16:53:35.745 12066-12476/com.whosonlocation.wolmobile2 D/BluetoothAdapter: isLeEnabled(): ON
2020-05-20 16:53:35.752 12066-12500/com.whosonlocation.wolmobile2 D/BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=9 mScannerId=0
2020-05-20 16:53:42.535 12066-12476/com.whosonlocation.wolmobile2 D/BluetoothAdapter: isLeEnabled(): ON
2020-05-20 16:53:42.553 12066-12476/com.whosonlocation.wolmobile2 D/BluetoothAdapter: isLeEnabled(): ON
2020-05-20 16:53:42.561 12066-12500/com.whosonlocation.wolmobile2 D/BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=9 mScannerId=0
2020-05-20 16:53:49.382 12066-12476/com.whosonlocation.wolmobile2 D/BluetoothAdapter: isLeEnabled(): ON
2020-05-20 16:53:49.391 12066-12476/com.whosonlocation.wolmobile2 D/BluetoothAdapter: isLeEnabled(): ON
2020-05-20 16:53:49.402 12066-12375/com.whosonlocation.wolmobile2 D/BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=9 mScannerId=0
2020-05-20 16:53:56.223 12066-12476/com.whosonlocation.wolmobile2 D/BluetoothAdapter: isLeEnabled(): ON
2020-05-20 16:53:56.246 12066-12476/com.whosonlocation.wolmobile2 D/BluetoothAdapter: isLeEnabled(): ON
2020-05-20 16:53:56.253 12066-12500/com.whosonlocation.wolmobile2 D/BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=9 mScannerId=0
2020-05-20 16:54:03.029 12066-12476/com.whosonlocation.wolmobile2 D/BluetoothAdapter: isLeEnabled(): ON
2020-05-20 16:54:03.041 12066-12476/com.whosonlocation.wolmobile2 D/BluetoothAdapter: isLeEnabled(): ON
2020-05-20 16:54:03.045 12066-12150/com.whosonlocation.wolmobile2 D/BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=9 mScannerId=0
2020-05-20 16:54:09.906 12066-12476/com.whosonlocation.wolmobile2 D/BluetoothAdapter: isLeEnabled(): ON
2020-05-20 16:54:09.916 12066-12476/com.whosonlocation.wolmobile2 D/BluetoothAdapter: isLeEnabled(): ON
2020-05-20 16:54:09.921 12066-12150/com.whosonlocation.wolmobile2 D/BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=9 mScannerId=0

Solution

  • The library already has an easy built-in way to set up foreground service scanning that takes care of many of the issues you are seeing. See here for details.

    If for some reason you don't want to use the above, you can certainly roll your own foreground service, but it will make managing the lifecycle more complicated. This is tricky stuff to get right.

    In general, to cleanly shut down the library from scanning you need to do a few things:

    1. Call beaconManager.stopRangingBeaconsInRegion()
    2. Call beaconManager.unbind(beaconConsumerInstance)

    Note that the call to unbind is asynchronous. You do not want to be calling bind() / unbind() rapidly.

    Finally, if you are going to the trouble to roll your own foreground service, then you might want to consider whether you want the library to be using its default behavior of using the Android Job Scheduler to perform scans, which is limited to once every ~15 minutes. It may be appropriate to simply let the library's own scanning service run to allow more frequent scans. You can configure that (before calling bind() only) with beaconManager.setEnableScheduledScanJobs(false)