Android 信标库 - 继续记录 periodicScanJobId 和 immediateScanJobId

Android Beacon Library - Keep logging periodicScanJobId and immediateScanJobId

即使在测距后调用解除绑定方法,我仍会收到这些日志。想知道是什么导致了这个问题?

该代码与 Android-beacon-libary-Samples 上的测距示例代码几乎相同,唯一的区别是我使用的是 ForegroundService 而不是 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)
    }
}

我在上面添加了有问题的代码。我认为当我添加这行代码时会发生这种情况,

beaconManager.stopRangingBeaconsInRegion(region)

我想我不应该在找到信标时停止测距?我应该停止前台服务吗?


删除 stopRangingBeaconsInRegion 方法后,出现这些日志:

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

图书馆已经有一个简单的内置方法来设置前台服务扫描,可以解决您遇到的许多问题。有关详细信息,请参阅 here

如果出于某种原因您不想使用上述服务,您当然可以推出自己的前台服务,但这会使管理生命周期变得更加复杂。这是一件很难做对的事情。

一般来说,要从扫描中完全关闭库,您需要做几件事:

  1. 致电beaconManager.stopRangingBeaconsInRegion()
  2. 致电beaconManager.unbind(beaconConsumerInstance)

请注意,对 unbind 的调用是异步的。您不想快速调用 bind() / unbind()。

最后,如果您不厌其烦地推出自己的前台服务,那么您可能需要考虑是否希望库使用其默认行为,即使用 Android Job Scheduler 来执行扫描,限制为每约 15 分钟一次。简单地让图书馆自己的扫描服务 运行 允许更频繁的扫描可能是合适的。您可以使用 beaconManager.setEnableScheduledScanJobs(false)

配置(仅在调用 bind() 之前)