【问题标题】:Android Beacon Library - Keep logging periodicScanJobId and immediateScanJobIdAndroid Beacon Library - 保持记录periodicScanJobId 和immediateScanJobId
【发布时间】:2020-05-20 02:15:43
【问题描述】:

即使在测距后调用 unbind 方法时,我也会不断收到这些日志。想知道可能导致问题的原因是什么?

代码与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

【问题讨论】:

  • 你能发布实际的代码示例吗?
  • 这些日志表明作业调度程序正在使用中。但是它被设置为使用前台服务,似乎不起作用。我真的需要看代码才能说更多。 “几乎相同”在编程世界中意味着“完全不同”!
  • @davidgyoung 我刚刚做了一些更新并在上面添加了一些示例代码
  • 嗨@Jheron 我刚刚添加了一些有问题的示例代码。谢谢你的评论
  • 删除 beaconManager.stopRangingBeaconsInRegion(region) 似乎对我有用。

标签: ibeacon altbeacon ibeacon-android android-ibeacon


【解决方案1】:

该库已经有一种简单的内置方法来设置前台服务扫描,可以解决您遇到的许多问题。详情请见here

如果出于某种原因您不想使用上述内容,您当然可以推出自己的前台服务,但这会使管理生命周期变得更加复杂。这是很难解决的问题。

一般来说,要彻底关闭库的扫描,您需要做几件事:

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

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

最后,如果您要麻烦推出自己的前台服务,那么您可能需要考虑是否希望库使用其使用 Android 作业调度程序执行扫描的默认行为,这仅限于每约 15 分钟一次。简单地让图书馆自己的扫描服务运行以允许更频繁的扫描可能是合适的。您可以使用 beaconManager.setEnableScheduledScanJobs(false) 进行配置(仅在调用 bind() 之前)

【讨论】:

  • 非常感谢您的回答。我使用自己的前台服务的原因是我不需要一直监控信标。当用户走进 GeoFence 时,我的应用程序将启动前台服务来查找信标,这就是我使用 Ranging 而不是 Monitoring 的原因。接收到信标后,我将关闭信标测距并终止前台服务,这样就完成了。
  • 我确实在找到信标后拨打了stopRangingBeaconsInRegionunbind 停止了前台服务。我将在bind 方法之前添加setEnableScheduledScanJobs。我只想要这个前台服务中的测距方法,不需要一直监控后台的信标。所以我认为内置的前台服务可能不适合我。谢谢
  • 经过大量测试。我认为beaconManager.setEnableScheduledScanJobs(false) 完成了这项工作。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多