Android Beacon库-继续记录周期扫描作业ID和即时扫描作业ID

不操千曲而后晓声,观千剑而后识器。这篇文章主要讲述Android Beacon库-继续记录周期扫描作业ID和即时扫描作业ID相关的知识,希望能为你提供帮助。
【Android Beacon库-继续记录周期扫描作业ID和即时扫描作业ID】即使在测距之后调用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: BeaconManagercompanion 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。

    推荐阅读