我正在我的一个android项目中使用altbeacon库。在该项目中,beaconsumer已经在其中一项活动中实施。当我尝试在代码中实现信标检测时,信标没有检测到。
但是,当我从以前编写的代码中删除beaconsumer代码时,我的代码就会工作并检测beacon。
这是信标未检测到的日志。为什么有多个消费者,这是问题所在吗?如果是,如何删除多个消费者。
I/System.out: ---------onBeaconServiceConnect
D/BeaconParser: Parsing beacon layout: m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25
D/BeaconParser: Parsing beacon layout: x,s:0-1=feaa,m:2-2=20,d:3-3,d:4-5,d:6-7,d:8-11,d:12-15
D/BeaconParser: Parsing beacon layout: s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19
D/BeaconParser: Parsing beacon layout: s:0-1=feaa,m:2-2=10,p:3-3:-41,i:4-20v
D/BeaconParser: Parsing beacon layout: m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24
D/BeaconLocalBroadcastProcessor: Register calls: global=6 instance=6
D/BeaconLocalBroadcastProcessor: Register calls: global=7 instance=7
D/BeaconManager: consumer count is now: 2
D/BeaconManager: This consumer is not bound. Binding now: MainActivity@11b9235
D/BeaconManager: Not starting beacon scanning service. Using scheduled jobs
D/BeaconLocalBroadcastProcessor: Register calls: global=8 instance=8
D/BeaconManager: consumer count is now: 3
D/BeaconManager: Unbinding
D/BeaconManager: Not unbinding from scanning service as we are using scan jobs.
D/BeaconManager: Before unbind, consumer count is 3
D/BeaconManager: After unbind, consumer count is 2
这是我正在使用的类
class BeaconsDataSource(private val context: Context) : BeaconConsumer, RangeNotifier,MonitorNotifier {
private val beaconManager: BeaconManager =
BeaconManager.getInstanceForApplication(context)
private val region = Region("myRegion", null, null, null)
private lateinit var data: Step
private var listener: ((Step) -> Unit)? = null
override fun getApplicationContext(): Context = context
override fun unbindService(p0: ServiceConnection?) {
applicationContext.unbindService(p0!!)
}
override fun bindService(p0: Intent?, p1: ServiceConnection?, p2: Int): Boolean {
return applicationContext.bindService(p0, p1!!, p2)
}
override fun onBeaconServiceConnect() {
BeaconManager.setDebug(true)
beaconManager.beaconParsers
.add(BeaconParser().setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"))
beaconManager.beaconParsers
.add(BeaconParser().setBeaconLayout("x,s:0-1=feaa,m:2-2=20,d:3-3,d:4-5,d:6-7,d:8-11,d:12-15"))
beaconManager.beaconParsers
.add(BeaconParser().setBeaconLayout("s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19"))
beaconManager.beaconParsers.add(BeaconParser().setBeaconLayout("s:0-1=feaa,m:2-2=10,p:3-3:-41,i:4-20v"))
beaconManager.beaconParsers
.add(BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"))
beaconManager.removeAllMonitorNotifiers()
beaconManager.removeAllRangeNotifiers()
beaconManager.addMonitorNotifier(this)
beaconManager.addRangeNotifier(this)
try {
beaconManager.startMonitoringBeaconsInRegion(region)
beaconManager.startRangingBeaconsInRegion(region)
} catch (e: RemoteException) {
e.printStackTrace()
}
}
override fun didRangeBeaconsInRegion(p0: MutableCollection<Beacon>, p1: Region?) {
data = Step.ENTER_NUMBER
notifyListeners()
}
private fun notifyListeners() {
listener?.invoke(data)
}
fun requestBeaconsUpdate(listener: (Step) -> Unit) {
this.listener = listener
beaconManager.bind(this)
}
fun stopBeaconsUpdate() {
beaconManager.unbind(this)
}
override fun didDetermineStateForRegion(p0: Int, p1: Region?) {
val msg = "---------did determine state for region $p0"
Log.d("TAG", msg)
}
override fun didEnterRegion(p0: Region?) {
val msg = "-----------did enter region"
val zone = p0.toString()
Log.d("TAG", "----------Enter in region")
val text = "Enter in $zone"
Log.d("TAG", msg)
}
override fun didExitRegion(p0: Region?) {
val msg = "------------did exit region"
Log.d("TAG", msg)
}
}
这是我从viewmodel调用的livedata类
class BeaconsLiveData() : LiveData<Step>() {
private val beaconDataSource = BeaconsDataSource(App.getAppContext())
private val listener = { data: Step ->
value = data
}
override fun onActive() {
super.onActive()
beaconDataSource.requestBeaconsUpdate(listener)
}
override fun onInactive() {
super.onInactive()
beaconDataSource.stopBeaconsUpdate()
}
}
1条答案
按热度按时间ffx8fchx1#
很难说是什么导致了多重绑定操作,但这很可能是导致检测问题的原因。我怀疑这与livedata的生命周期有关。不幸的是,调试这段代码要比查看代码片段更复杂(对我来说)。
一般来说,您应该设计您的系统,使其能够调用
beaconManager.bind(...)
以及.unbind(...)
很少。这样做经常会导致问题,因为它会启动和停止服务,这是一个很重的机制。我确实在这里创建了一个可工作的kotlin示例应用程序,以防对您有所帮助。此示例演示如何使用
MutableLiveData
共享中心类中的信标检测数据,该中心类正在对您的活动进行检测。本例中的中心类是android应用程序类,但它也可以是任何具有长生命周期的单例。关键是它会调用bind(...)
一旦它启动,就不会再调用它,直到你完成信标的工作。