Android AltBeacon background and foreground together
up vote
0
down vote
favorite
Good day. In my application i want to use background and foreground beacons scan. For this task i used AltBeacon library. But after device reboot background scan not starts.
My algorith looks like:
When application started, start monitor and attach lifecycle listener
for detect when activity dies.When activity dies - start background scan.
When activity goes back to front - stop backgroun and run foreground.
What i have now:
At first - foreground "service" (not really service, just class)
class BeaconForegroundService(private val application: Application) : BeaconConsumer {
private var beaconManager: BeaconManager? = null
companion object {
private const val REGION_UID = "region_uid"
}
override fun getApplicationContext(): Context {
return application.applicationContext
}
override fun unbindService(p0: ServiceConnection) {
application.unbindService(p0)
}
override fun bindService(p0: Intent, p1: ServiceConnection, p2: Int): Boolean {
return application.bindService(p0, p1, p2)
}
override fun onBeaconServiceConnect() {
beaconManager!!.beaconParsers.clear()
beaconManager!!.beaconParsers.add(AltBeaconParser())
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.URI_BEACON_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_TLM_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_URL_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"))
beaconManager!!.backgroundMode = false
beaconManager!!.addMonitorNotifier(object : MonitorNotifier {
override fun didDetermineStateForRegion(p0: Int, p1: Region?) {
Timber.d("FGBeacon: didDetermineStateForRegion INSIDE = ${p0 == MonitorNotifier.INSIDE}")
}
override fun didEnterRegion(p0: Region?) {
Timber.d("FGBeacon: didEnterRegion")
}
override fun didExitRegion(p0: Region?) {
Timber.d("FGBeacon: didExitRegion")
}
})
beaconManager!!.addRangeNotifier { mutableCollection: MutableCollection<Beacon>, region: Region ->
Timber.d("FGBeacon $mutableCollection")
}
beaconManager!!.startMonitoringBeaconsInRegion(Region(REGION_UID, null, null, null))
beaconManager!!.startRangingBeaconsInRegion(Region(REGION_UID, null, null, null))
}
fun startScan() {
beaconManager = BeaconManager.getInstanceForApplication(application)
beaconManager!!.bind(this)
}
fun stopScan() {
beaconManager?.unbind(this)
}
}
Background scan inside application class:
class MyApplication : Application(), BootstrapNotifier {
// Fields
private var beaconManager: BeaconManager? = null
private var backgroundPowerSaver: BackgroundPowerSaver ?= null
private var regionBootstrap : RegionBootstrap ?= null
private var foregroundService: BeaconForegroundService ?= null
val REGION_UID = "region_uid"
//Method for start background scan here
fun startScan() {
beaconManager = BeaconManager.getInstanceForApplication(this)
beaconManager!!.beaconParsers.clear()
beaconManager!!.beaconParsers.add(AltBeaconParser())
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.URI_BEACON_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_TLM_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_URL_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"))
val notificationBuilder = Notification.Builder(this)
notificationBuilder.setSmallIcon(R.drawable.ic_launcher_foreground)
notificationBuilder.setContentTitle("Scanning for beacons")
val intent = Intent(this, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(this, MainActivity.BEACON_REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT)
notificationBuilder.setContentIntent(pendingIntent)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel("MyChannelId", "NotyName", NotificationManager.IMPORTANCE_DEFAULT)
channel.description = "NotyDesc"
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
notificationBuilder.setChannelId(channel.id)
}
val region = Region(REGION_UID, null, null, null)
beaconManager!!.enableForegroundServiceScanning(notificationBuilder.build(), 456)
beaconManager!!.setEnableScheduledScanJobs(false)
beaconManager!!.backgroundBetweenScanPeriod = 0
beaconManager!!.backgroundScanPeriod = TimeUnit.SECONDS.toMillis(5)
beaconManager!!.backgroundMode = true
regionBootstrap = RegionBootstrap(this, region)
backgroundPowerSaver = BackgroundPowerSaver(this)
beaconManager!!.addRangeNotifier { mutableCollection: MutableCollection<Beacon>, r: Region ->
Timber.d("PBeacon $mutableCollection")
}
}
//Disable background scan if application start in foreground
fun disableMonitoring() {
regionBootstrap?.disable()
regionBootstrap = null
beaconManager?.removeAllRangeNotifiers()
}
override fun didDetermineStateForRegion(p0: Int, p1: Region?) {
Timber.d("PBeacon: StateForRegion INSIDE = ${p0 == MonitorNotifier.INSIDE}")
}
//Start ranging if we inside region
override fun didEnterRegion(p0: Region?) {
Timber.d("PBeacon: I see a beacon first time")
beaconManager!!.startRangingBeaconsInRegion(Region(REGION_UID, null, null, null))
}
//Stop ranging if we move out from region
override fun didExitRegion(p0: Region?) {
Timber.d("PBeacon : I no longer see a beacon.")
beaconManager!!.stopRangingBeaconsInRegion(Region(REGION_UID, null, null, null))
}
And finally - registered listener for detect foreground/background app state changes (inside onCreate of application)
registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
//Stop background scan and start foreground
override fun onActivityStarted(activity: Activity?) {
disableMonitoring()
foregroundService = BeaconForegroundService(this@MyApplication)
foregroundService!!.startScan()
}
//Stop foreground scan and start background
override fun onActivityStopped(activity: Activity?) {
foregroundService?.stopScan()
foregroundService = null
startScan()
}
})
Logs here:
Logs when application starting in foreground (looks fine)
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: m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25
D/BeaconParser: Parsing beacon layout: s:0-1=fed8,m:2-2=00,p:3-3:-41,i:4-21v
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-21v
D/BeaconParser: Parsing beacon layout: m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25
....
BeaconForegroundService$onBeaconServiceConnect: [id1: b5b182c7-eab1-4988-aa99-b5c1517008d9 id2: 1 id3: 57197]
Close app (background mode?):
Processing pdu type FF: 0201061aff4c000215b5b182c7eab14988aa99b5c1517008d90001df6dc50d0961626561636f6e5f36444446000000000000000000000000000000000000 with startIndex: 5, endIndex: 29
D/BeaconParser: This is not a matching Beacon advertisement. Was expecting aa fe at offset 5 and 20 at offset 7. The bytes I see are: 0201061aff4c000215b5b182c7eab14988aa99b5c1517008d90001df6dc50d0961626561636f6e5f36444446000000000000000000000000000000000000
D/BeaconParser: Ignoring pdu type 01
...
D/BeaconParser: Processing pdu type FF: 0201061aff4c000215b5b182c7eab14988aa99b5c1517008d90001df6dc50d0961626561636f6e5f36444446000000000000000000000000000000000000 with startIndex: 5, endIndex: 29
D/BeaconParser: This is a recognized beacon advertisement -- 02 15 seen
D/BeaconParser: Bytes are: 0201061aff4c000215b5b182c7eab14988aa99b5c1517008d90001df6dc50d0961626561636f6e5f36444446000000000000000000000000000000000000
D/ScanHelper: Beacon packet detected for: id1: b5b182c7-eab1-4988-aa99-b5c1517008d9 id2: 1 id3: 57197 with rssi -46
D/RangeState: adding id1: b5b182c7-eab1-4988-aa99-b5c1517008d9 id2: 1 id3: 57197 to existing range for: org.altbeacon.beacon.service.RangedBeacon@332c859
After reboot:
I/BeaconManager: BeaconManager started up on pid 7407 named 'com.myapp' for application package 'com.myapp'. isMainProcess=true
D/BeaconParser: Parsing beacon layout: m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25
And that's all. No detections, waiting (20+ minutes)
So
- Is it bug of library/device or i doing something wrong?
- Can we use background scan without notification?
- Whan we start application again (to foreground) notification refreshed. How can i avoid this?
Big thanks
android bluetooth-lowenergy ibeacon beacon altbeacon
|
show 1 more comment
up vote
0
down vote
favorite
Good day. In my application i want to use background and foreground beacons scan. For this task i used AltBeacon library. But after device reboot background scan not starts.
My algorith looks like:
When application started, start monitor and attach lifecycle listener
for detect when activity dies.When activity dies - start background scan.
When activity goes back to front - stop backgroun and run foreground.
What i have now:
At first - foreground "service" (not really service, just class)
class BeaconForegroundService(private val application: Application) : BeaconConsumer {
private var beaconManager: BeaconManager? = null
companion object {
private const val REGION_UID = "region_uid"
}
override fun getApplicationContext(): Context {
return application.applicationContext
}
override fun unbindService(p0: ServiceConnection) {
application.unbindService(p0)
}
override fun bindService(p0: Intent, p1: ServiceConnection, p2: Int): Boolean {
return application.bindService(p0, p1, p2)
}
override fun onBeaconServiceConnect() {
beaconManager!!.beaconParsers.clear()
beaconManager!!.beaconParsers.add(AltBeaconParser())
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.URI_BEACON_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_TLM_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_URL_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"))
beaconManager!!.backgroundMode = false
beaconManager!!.addMonitorNotifier(object : MonitorNotifier {
override fun didDetermineStateForRegion(p0: Int, p1: Region?) {
Timber.d("FGBeacon: didDetermineStateForRegion INSIDE = ${p0 == MonitorNotifier.INSIDE}")
}
override fun didEnterRegion(p0: Region?) {
Timber.d("FGBeacon: didEnterRegion")
}
override fun didExitRegion(p0: Region?) {
Timber.d("FGBeacon: didExitRegion")
}
})
beaconManager!!.addRangeNotifier { mutableCollection: MutableCollection<Beacon>, region: Region ->
Timber.d("FGBeacon $mutableCollection")
}
beaconManager!!.startMonitoringBeaconsInRegion(Region(REGION_UID, null, null, null))
beaconManager!!.startRangingBeaconsInRegion(Region(REGION_UID, null, null, null))
}
fun startScan() {
beaconManager = BeaconManager.getInstanceForApplication(application)
beaconManager!!.bind(this)
}
fun stopScan() {
beaconManager?.unbind(this)
}
}
Background scan inside application class:
class MyApplication : Application(), BootstrapNotifier {
// Fields
private var beaconManager: BeaconManager? = null
private var backgroundPowerSaver: BackgroundPowerSaver ?= null
private var regionBootstrap : RegionBootstrap ?= null
private var foregroundService: BeaconForegroundService ?= null
val REGION_UID = "region_uid"
//Method for start background scan here
fun startScan() {
beaconManager = BeaconManager.getInstanceForApplication(this)
beaconManager!!.beaconParsers.clear()
beaconManager!!.beaconParsers.add(AltBeaconParser())
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.URI_BEACON_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_TLM_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_URL_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"))
val notificationBuilder = Notification.Builder(this)
notificationBuilder.setSmallIcon(R.drawable.ic_launcher_foreground)
notificationBuilder.setContentTitle("Scanning for beacons")
val intent = Intent(this, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(this, MainActivity.BEACON_REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT)
notificationBuilder.setContentIntent(pendingIntent)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel("MyChannelId", "NotyName", NotificationManager.IMPORTANCE_DEFAULT)
channel.description = "NotyDesc"
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
notificationBuilder.setChannelId(channel.id)
}
val region = Region(REGION_UID, null, null, null)
beaconManager!!.enableForegroundServiceScanning(notificationBuilder.build(), 456)
beaconManager!!.setEnableScheduledScanJobs(false)
beaconManager!!.backgroundBetweenScanPeriod = 0
beaconManager!!.backgroundScanPeriod = TimeUnit.SECONDS.toMillis(5)
beaconManager!!.backgroundMode = true
regionBootstrap = RegionBootstrap(this, region)
backgroundPowerSaver = BackgroundPowerSaver(this)
beaconManager!!.addRangeNotifier { mutableCollection: MutableCollection<Beacon>, r: Region ->
Timber.d("PBeacon $mutableCollection")
}
}
//Disable background scan if application start in foreground
fun disableMonitoring() {
regionBootstrap?.disable()
regionBootstrap = null
beaconManager?.removeAllRangeNotifiers()
}
override fun didDetermineStateForRegion(p0: Int, p1: Region?) {
Timber.d("PBeacon: StateForRegion INSIDE = ${p0 == MonitorNotifier.INSIDE}")
}
//Start ranging if we inside region
override fun didEnterRegion(p0: Region?) {
Timber.d("PBeacon: I see a beacon first time")
beaconManager!!.startRangingBeaconsInRegion(Region(REGION_UID, null, null, null))
}
//Stop ranging if we move out from region
override fun didExitRegion(p0: Region?) {
Timber.d("PBeacon : I no longer see a beacon.")
beaconManager!!.stopRangingBeaconsInRegion(Region(REGION_UID, null, null, null))
}
And finally - registered listener for detect foreground/background app state changes (inside onCreate of application)
registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
//Stop background scan and start foreground
override fun onActivityStarted(activity: Activity?) {
disableMonitoring()
foregroundService = BeaconForegroundService(this@MyApplication)
foregroundService!!.startScan()
}
//Stop foreground scan and start background
override fun onActivityStopped(activity: Activity?) {
foregroundService?.stopScan()
foregroundService = null
startScan()
}
})
Logs here:
Logs when application starting in foreground (looks fine)
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: m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25
D/BeaconParser: Parsing beacon layout: s:0-1=fed8,m:2-2=00,p:3-3:-41,i:4-21v
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-21v
D/BeaconParser: Parsing beacon layout: m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25
....
BeaconForegroundService$onBeaconServiceConnect: [id1: b5b182c7-eab1-4988-aa99-b5c1517008d9 id2: 1 id3: 57197]
Close app (background mode?):
Processing pdu type FF: 0201061aff4c000215b5b182c7eab14988aa99b5c1517008d90001df6dc50d0961626561636f6e5f36444446000000000000000000000000000000000000 with startIndex: 5, endIndex: 29
D/BeaconParser: This is not a matching Beacon advertisement. Was expecting aa fe at offset 5 and 20 at offset 7. The bytes I see are: 0201061aff4c000215b5b182c7eab14988aa99b5c1517008d90001df6dc50d0961626561636f6e5f36444446000000000000000000000000000000000000
D/BeaconParser: Ignoring pdu type 01
...
D/BeaconParser: Processing pdu type FF: 0201061aff4c000215b5b182c7eab14988aa99b5c1517008d90001df6dc50d0961626561636f6e5f36444446000000000000000000000000000000000000 with startIndex: 5, endIndex: 29
D/BeaconParser: This is a recognized beacon advertisement -- 02 15 seen
D/BeaconParser: Bytes are: 0201061aff4c000215b5b182c7eab14988aa99b5c1517008d90001df6dc50d0961626561636f6e5f36444446000000000000000000000000000000000000
D/ScanHelper: Beacon packet detected for: id1: b5b182c7-eab1-4988-aa99-b5c1517008d9 id2: 1 id3: 57197 with rssi -46
D/RangeState: adding id1: b5b182c7-eab1-4988-aa99-b5c1517008d9 id2: 1 id3: 57197 to existing range for: org.altbeacon.beacon.service.RangedBeacon@332c859
After reboot:
I/BeaconManager: BeaconManager started up on pid 7407 named 'com.myapp' for application package 'com.myapp'. isMainProcess=true
D/BeaconParser: Parsing beacon layout: m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25
And that's all. No detections, waiting (20+ minutes)
So
- Is it bug of library/device or i doing something wrong?
- Can we use background scan without notification?
- Whan we start application again (to foreground) notification refreshed. How can i avoid this?
Big thanks
android bluetooth-lowenergy ibeacon beacon altbeacon
1
Try moving the startRanginfBeaconsInRegion() call inside the didDetermineStateForRegion method. After restart, the region state is persisted, so you will not get a new call to didEnterRegion if you were already inside when the app last was running. The didDetermineState callback is always made on restart so it is a good place to trigger starting ranging..
– davidgyoung
Nov 22 at 17:42
1
Also, make sure the startScan() call is triggered in the Application class' onCreate method, and verify by debug lines that it gets called.
– davidgyoung
Nov 23 at 0:04
1
If you are using Android 8+, make sure you have library version 2.15.2 or higher (the version shows up in the logs) otherwise starting a foreground service in the background won't work. See: github.com/AltBeacon/android-beacon-library/pull/746
– davidgyoung
Nov 23 at 0:23
@davidgyoung Thanks for the reply. Unfortunately, not working. Lib version - 2.15.2 onCreate of application not called. I move startRangingBeaconsInRegion inside didDetermineStateForRegion callback method, but after reboot still not started.
– Yuriy Aizenberg
Nov 23 at 15:22
@davidgyoung So i took code from example (github.com/AltBeacon/android-beacon-library-reference/blob/…) and now scanner work after reboot! But no beacons found :( I see that didDetermineStateForRegion always has first arg = 0, it's "OUTSIDE". D/BeaconService: Currently monitoring 1 regions. I/BeaconService: stop ranging received D/BeaconService: Currently ranging 0 regions.
– Yuriy Aizenberg
Nov 23 at 16:51
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Good day. In my application i want to use background and foreground beacons scan. For this task i used AltBeacon library. But after device reboot background scan not starts.
My algorith looks like:
When application started, start monitor and attach lifecycle listener
for detect when activity dies.When activity dies - start background scan.
When activity goes back to front - stop backgroun and run foreground.
What i have now:
At first - foreground "service" (not really service, just class)
class BeaconForegroundService(private val application: Application) : BeaconConsumer {
private var beaconManager: BeaconManager? = null
companion object {
private const val REGION_UID = "region_uid"
}
override fun getApplicationContext(): Context {
return application.applicationContext
}
override fun unbindService(p0: ServiceConnection) {
application.unbindService(p0)
}
override fun bindService(p0: Intent, p1: ServiceConnection, p2: Int): Boolean {
return application.bindService(p0, p1, p2)
}
override fun onBeaconServiceConnect() {
beaconManager!!.beaconParsers.clear()
beaconManager!!.beaconParsers.add(AltBeaconParser())
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.URI_BEACON_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_TLM_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_URL_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"))
beaconManager!!.backgroundMode = false
beaconManager!!.addMonitorNotifier(object : MonitorNotifier {
override fun didDetermineStateForRegion(p0: Int, p1: Region?) {
Timber.d("FGBeacon: didDetermineStateForRegion INSIDE = ${p0 == MonitorNotifier.INSIDE}")
}
override fun didEnterRegion(p0: Region?) {
Timber.d("FGBeacon: didEnterRegion")
}
override fun didExitRegion(p0: Region?) {
Timber.d("FGBeacon: didExitRegion")
}
})
beaconManager!!.addRangeNotifier { mutableCollection: MutableCollection<Beacon>, region: Region ->
Timber.d("FGBeacon $mutableCollection")
}
beaconManager!!.startMonitoringBeaconsInRegion(Region(REGION_UID, null, null, null))
beaconManager!!.startRangingBeaconsInRegion(Region(REGION_UID, null, null, null))
}
fun startScan() {
beaconManager = BeaconManager.getInstanceForApplication(application)
beaconManager!!.bind(this)
}
fun stopScan() {
beaconManager?.unbind(this)
}
}
Background scan inside application class:
class MyApplication : Application(), BootstrapNotifier {
// Fields
private var beaconManager: BeaconManager? = null
private var backgroundPowerSaver: BackgroundPowerSaver ?= null
private var regionBootstrap : RegionBootstrap ?= null
private var foregroundService: BeaconForegroundService ?= null
val REGION_UID = "region_uid"
//Method for start background scan here
fun startScan() {
beaconManager = BeaconManager.getInstanceForApplication(this)
beaconManager!!.beaconParsers.clear()
beaconManager!!.beaconParsers.add(AltBeaconParser())
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.URI_BEACON_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_TLM_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_URL_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"))
val notificationBuilder = Notification.Builder(this)
notificationBuilder.setSmallIcon(R.drawable.ic_launcher_foreground)
notificationBuilder.setContentTitle("Scanning for beacons")
val intent = Intent(this, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(this, MainActivity.BEACON_REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT)
notificationBuilder.setContentIntent(pendingIntent)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel("MyChannelId", "NotyName", NotificationManager.IMPORTANCE_DEFAULT)
channel.description = "NotyDesc"
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
notificationBuilder.setChannelId(channel.id)
}
val region = Region(REGION_UID, null, null, null)
beaconManager!!.enableForegroundServiceScanning(notificationBuilder.build(), 456)
beaconManager!!.setEnableScheduledScanJobs(false)
beaconManager!!.backgroundBetweenScanPeriod = 0
beaconManager!!.backgroundScanPeriod = TimeUnit.SECONDS.toMillis(5)
beaconManager!!.backgroundMode = true
regionBootstrap = RegionBootstrap(this, region)
backgroundPowerSaver = BackgroundPowerSaver(this)
beaconManager!!.addRangeNotifier { mutableCollection: MutableCollection<Beacon>, r: Region ->
Timber.d("PBeacon $mutableCollection")
}
}
//Disable background scan if application start in foreground
fun disableMonitoring() {
regionBootstrap?.disable()
regionBootstrap = null
beaconManager?.removeAllRangeNotifiers()
}
override fun didDetermineStateForRegion(p0: Int, p1: Region?) {
Timber.d("PBeacon: StateForRegion INSIDE = ${p0 == MonitorNotifier.INSIDE}")
}
//Start ranging if we inside region
override fun didEnterRegion(p0: Region?) {
Timber.d("PBeacon: I see a beacon first time")
beaconManager!!.startRangingBeaconsInRegion(Region(REGION_UID, null, null, null))
}
//Stop ranging if we move out from region
override fun didExitRegion(p0: Region?) {
Timber.d("PBeacon : I no longer see a beacon.")
beaconManager!!.stopRangingBeaconsInRegion(Region(REGION_UID, null, null, null))
}
And finally - registered listener for detect foreground/background app state changes (inside onCreate of application)
registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
//Stop background scan and start foreground
override fun onActivityStarted(activity: Activity?) {
disableMonitoring()
foregroundService = BeaconForegroundService(this@MyApplication)
foregroundService!!.startScan()
}
//Stop foreground scan and start background
override fun onActivityStopped(activity: Activity?) {
foregroundService?.stopScan()
foregroundService = null
startScan()
}
})
Logs here:
Logs when application starting in foreground (looks fine)
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: m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25
D/BeaconParser: Parsing beacon layout: s:0-1=fed8,m:2-2=00,p:3-3:-41,i:4-21v
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-21v
D/BeaconParser: Parsing beacon layout: m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25
....
BeaconForegroundService$onBeaconServiceConnect: [id1: b5b182c7-eab1-4988-aa99-b5c1517008d9 id2: 1 id3: 57197]
Close app (background mode?):
Processing pdu type FF: 0201061aff4c000215b5b182c7eab14988aa99b5c1517008d90001df6dc50d0961626561636f6e5f36444446000000000000000000000000000000000000 with startIndex: 5, endIndex: 29
D/BeaconParser: This is not a matching Beacon advertisement. Was expecting aa fe at offset 5 and 20 at offset 7. The bytes I see are: 0201061aff4c000215b5b182c7eab14988aa99b5c1517008d90001df6dc50d0961626561636f6e5f36444446000000000000000000000000000000000000
D/BeaconParser: Ignoring pdu type 01
...
D/BeaconParser: Processing pdu type FF: 0201061aff4c000215b5b182c7eab14988aa99b5c1517008d90001df6dc50d0961626561636f6e5f36444446000000000000000000000000000000000000 with startIndex: 5, endIndex: 29
D/BeaconParser: This is a recognized beacon advertisement -- 02 15 seen
D/BeaconParser: Bytes are: 0201061aff4c000215b5b182c7eab14988aa99b5c1517008d90001df6dc50d0961626561636f6e5f36444446000000000000000000000000000000000000
D/ScanHelper: Beacon packet detected for: id1: b5b182c7-eab1-4988-aa99-b5c1517008d9 id2: 1 id3: 57197 with rssi -46
D/RangeState: adding id1: b5b182c7-eab1-4988-aa99-b5c1517008d9 id2: 1 id3: 57197 to existing range for: org.altbeacon.beacon.service.RangedBeacon@332c859
After reboot:
I/BeaconManager: BeaconManager started up on pid 7407 named 'com.myapp' for application package 'com.myapp'. isMainProcess=true
D/BeaconParser: Parsing beacon layout: m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25
And that's all. No detections, waiting (20+ minutes)
So
- Is it bug of library/device or i doing something wrong?
- Can we use background scan without notification?
- Whan we start application again (to foreground) notification refreshed. How can i avoid this?
Big thanks
android bluetooth-lowenergy ibeacon beacon altbeacon
Good day. In my application i want to use background and foreground beacons scan. For this task i used AltBeacon library. But after device reboot background scan not starts.
My algorith looks like:
When application started, start monitor and attach lifecycle listener
for detect when activity dies.When activity dies - start background scan.
When activity goes back to front - stop backgroun and run foreground.
What i have now:
At first - foreground "service" (not really service, just class)
class BeaconForegroundService(private val application: Application) : BeaconConsumer {
private var beaconManager: BeaconManager? = null
companion object {
private const val REGION_UID = "region_uid"
}
override fun getApplicationContext(): Context {
return application.applicationContext
}
override fun unbindService(p0: ServiceConnection) {
application.unbindService(p0)
}
override fun bindService(p0: Intent, p1: ServiceConnection, p2: Int): Boolean {
return application.bindService(p0, p1, p2)
}
override fun onBeaconServiceConnect() {
beaconManager!!.beaconParsers.clear()
beaconManager!!.beaconParsers.add(AltBeaconParser())
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.URI_BEACON_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_TLM_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_URL_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"))
beaconManager!!.backgroundMode = false
beaconManager!!.addMonitorNotifier(object : MonitorNotifier {
override fun didDetermineStateForRegion(p0: Int, p1: Region?) {
Timber.d("FGBeacon: didDetermineStateForRegion INSIDE = ${p0 == MonitorNotifier.INSIDE}")
}
override fun didEnterRegion(p0: Region?) {
Timber.d("FGBeacon: didEnterRegion")
}
override fun didExitRegion(p0: Region?) {
Timber.d("FGBeacon: didExitRegion")
}
})
beaconManager!!.addRangeNotifier { mutableCollection: MutableCollection<Beacon>, region: Region ->
Timber.d("FGBeacon $mutableCollection")
}
beaconManager!!.startMonitoringBeaconsInRegion(Region(REGION_UID, null, null, null))
beaconManager!!.startRangingBeaconsInRegion(Region(REGION_UID, null, null, null))
}
fun startScan() {
beaconManager = BeaconManager.getInstanceForApplication(application)
beaconManager!!.bind(this)
}
fun stopScan() {
beaconManager?.unbind(this)
}
}
Background scan inside application class:
class MyApplication : Application(), BootstrapNotifier {
// Fields
private var beaconManager: BeaconManager? = null
private var backgroundPowerSaver: BackgroundPowerSaver ?= null
private var regionBootstrap : RegionBootstrap ?= null
private var foregroundService: BeaconForegroundService ?= null
val REGION_UID = "region_uid"
//Method for start background scan here
fun startScan() {
beaconManager = BeaconManager.getInstanceForApplication(this)
beaconManager!!.beaconParsers.clear()
beaconManager!!.beaconParsers.add(AltBeaconParser())
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.URI_BEACON_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_TLM_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_URL_LAYOUT))
beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"))
val notificationBuilder = Notification.Builder(this)
notificationBuilder.setSmallIcon(R.drawable.ic_launcher_foreground)
notificationBuilder.setContentTitle("Scanning for beacons")
val intent = Intent(this, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(this, MainActivity.BEACON_REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT)
notificationBuilder.setContentIntent(pendingIntent)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel("MyChannelId", "NotyName", NotificationManager.IMPORTANCE_DEFAULT)
channel.description = "NotyDesc"
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
notificationBuilder.setChannelId(channel.id)
}
val region = Region(REGION_UID, null, null, null)
beaconManager!!.enableForegroundServiceScanning(notificationBuilder.build(), 456)
beaconManager!!.setEnableScheduledScanJobs(false)
beaconManager!!.backgroundBetweenScanPeriod = 0
beaconManager!!.backgroundScanPeriod = TimeUnit.SECONDS.toMillis(5)
beaconManager!!.backgroundMode = true
regionBootstrap = RegionBootstrap(this, region)
backgroundPowerSaver = BackgroundPowerSaver(this)
beaconManager!!.addRangeNotifier { mutableCollection: MutableCollection<Beacon>, r: Region ->
Timber.d("PBeacon $mutableCollection")
}
}
//Disable background scan if application start in foreground
fun disableMonitoring() {
regionBootstrap?.disable()
regionBootstrap = null
beaconManager?.removeAllRangeNotifiers()
}
override fun didDetermineStateForRegion(p0: Int, p1: Region?) {
Timber.d("PBeacon: StateForRegion INSIDE = ${p0 == MonitorNotifier.INSIDE}")
}
//Start ranging if we inside region
override fun didEnterRegion(p0: Region?) {
Timber.d("PBeacon: I see a beacon first time")
beaconManager!!.startRangingBeaconsInRegion(Region(REGION_UID, null, null, null))
}
//Stop ranging if we move out from region
override fun didExitRegion(p0: Region?) {
Timber.d("PBeacon : I no longer see a beacon.")
beaconManager!!.stopRangingBeaconsInRegion(Region(REGION_UID, null, null, null))
}
And finally - registered listener for detect foreground/background app state changes (inside onCreate of application)
registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
//Stop background scan and start foreground
override fun onActivityStarted(activity: Activity?) {
disableMonitoring()
foregroundService = BeaconForegroundService(this@MyApplication)
foregroundService!!.startScan()
}
//Stop foreground scan and start background
override fun onActivityStopped(activity: Activity?) {
foregroundService?.stopScan()
foregroundService = null
startScan()
}
})
Logs here:
Logs when application starting in foreground (looks fine)
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: m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25
D/BeaconParser: Parsing beacon layout: s:0-1=fed8,m:2-2=00,p:3-3:-41,i:4-21v
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-21v
D/BeaconParser: Parsing beacon layout: m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25
....
BeaconForegroundService$onBeaconServiceConnect: [id1: b5b182c7-eab1-4988-aa99-b5c1517008d9 id2: 1 id3: 57197]
Close app (background mode?):
Processing pdu type FF: 0201061aff4c000215b5b182c7eab14988aa99b5c1517008d90001df6dc50d0961626561636f6e5f36444446000000000000000000000000000000000000 with startIndex: 5, endIndex: 29
D/BeaconParser: This is not a matching Beacon advertisement. Was expecting aa fe at offset 5 and 20 at offset 7. The bytes I see are: 0201061aff4c000215b5b182c7eab14988aa99b5c1517008d90001df6dc50d0961626561636f6e5f36444446000000000000000000000000000000000000
D/BeaconParser: Ignoring pdu type 01
...
D/BeaconParser: Processing pdu type FF: 0201061aff4c000215b5b182c7eab14988aa99b5c1517008d90001df6dc50d0961626561636f6e5f36444446000000000000000000000000000000000000 with startIndex: 5, endIndex: 29
D/BeaconParser: This is a recognized beacon advertisement -- 02 15 seen
D/BeaconParser: Bytes are: 0201061aff4c000215b5b182c7eab14988aa99b5c1517008d90001df6dc50d0961626561636f6e5f36444446000000000000000000000000000000000000
D/ScanHelper: Beacon packet detected for: id1: b5b182c7-eab1-4988-aa99-b5c1517008d9 id2: 1 id3: 57197 with rssi -46
D/RangeState: adding id1: b5b182c7-eab1-4988-aa99-b5c1517008d9 id2: 1 id3: 57197 to existing range for: org.altbeacon.beacon.service.RangedBeacon@332c859
After reboot:
I/BeaconManager: BeaconManager started up on pid 7407 named 'com.myapp' for application package 'com.myapp'. isMainProcess=true
D/BeaconParser: Parsing beacon layout: m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25
And that's all. No detections, waiting (20+ minutes)
So
- Is it bug of library/device or i doing something wrong?
- Can we use background scan without notification?
- Whan we start application again (to foreground) notification refreshed. How can i avoid this?
Big thanks
android bluetooth-lowenergy ibeacon beacon altbeacon
android bluetooth-lowenergy ibeacon beacon altbeacon
asked Nov 22 at 16:34
Yuriy Aizenberg
161421
161421
1
Try moving the startRanginfBeaconsInRegion() call inside the didDetermineStateForRegion method. After restart, the region state is persisted, so you will not get a new call to didEnterRegion if you were already inside when the app last was running. The didDetermineState callback is always made on restart so it is a good place to trigger starting ranging..
– davidgyoung
Nov 22 at 17:42
1
Also, make sure the startScan() call is triggered in the Application class' onCreate method, and verify by debug lines that it gets called.
– davidgyoung
Nov 23 at 0:04
1
If you are using Android 8+, make sure you have library version 2.15.2 or higher (the version shows up in the logs) otherwise starting a foreground service in the background won't work. See: github.com/AltBeacon/android-beacon-library/pull/746
– davidgyoung
Nov 23 at 0:23
@davidgyoung Thanks for the reply. Unfortunately, not working. Lib version - 2.15.2 onCreate of application not called. I move startRangingBeaconsInRegion inside didDetermineStateForRegion callback method, but after reboot still not started.
– Yuriy Aizenberg
Nov 23 at 15:22
@davidgyoung So i took code from example (github.com/AltBeacon/android-beacon-library-reference/blob/…) and now scanner work after reboot! But no beacons found :( I see that didDetermineStateForRegion always has first arg = 0, it's "OUTSIDE". D/BeaconService: Currently monitoring 1 regions. I/BeaconService: stop ranging received D/BeaconService: Currently ranging 0 regions.
– Yuriy Aizenberg
Nov 23 at 16:51
|
show 1 more comment
1
Try moving the startRanginfBeaconsInRegion() call inside the didDetermineStateForRegion method. After restart, the region state is persisted, so you will not get a new call to didEnterRegion if you were already inside when the app last was running. The didDetermineState callback is always made on restart so it is a good place to trigger starting ranging..
– davidgyoung
Nov 22 at 17:42
1
Also, make sure the startScan() call is triggered in the Application class' onCreate method, and verify by debug lines that it gets called.
– davidgyoung
Nov 23 at 0:04
1
If you are using Android 8+, make sure you have library version 2.15.2 or higher (the version shows up in the logs) otherwise starting a foreground service in the background won't work. See: github.com/AltBeacon/android-beacon-library/pull/746
– davidgyoung
Nov 23 at 0:23
@davidgyoung Thanks for the reply. Unfortunately, not working. Lib version - 2.15.2 onCreate of application not called. I move startRangingBeaconsInRegion inside didDetermineStateForRegion callback method, but after reboot still not started.
– Yuriy Aizenberg
Nov 23 at 15:22
@davidgyoung So i took code from example (github.com/AltBeacon/android-beacon-library-reference/blob/…) and now scanner work after reboot! But no beacons found :( I see that didDetermineStateForRegion always has first arg = 0, it's "OUTSIDE". D/BeaconService: Currently monitoring 1 regions. I/BeaconService: stop ranging received D/BeaconService: Currently ranging 0 regions.
– Yuriy Aizenberg
Nov 23 at 16:51
1
1
Try moving the startRanginfBeaconsInRegion() call inside the didDetermineStateForRegion method. After restart, the region state is persisted, so you will not get a new call to didEnterRegion if you were already inside when the app last was running. The didDetermineState callback is always made on restart so it is a good place to trigger starting ranging..
– davidgyoung
Nov 22 at 17:42
Try moving the startRanginfBeaconsInRegion() call inside the didDetermineStateForRegion method. After restart, the region state is persisted, so you will not get a new call to didEnterRegion if you were already inside when the app last was running. The didDetermineState callback is always made on restart so it is a good place to trigger starting ranging..
– davidgyoung
Nov 22 at 17:42
1
1
Also, make sure the startScan() call is triggered in the Application class' onCreate method, and verify by debug lines that it gets called.
– davidgyoung
Nov 23 at 0:04
Also, make sure the startScan() call is triggered in the Application class' onCreate method, and verify by debug lines that it gets called.
– davidgyoung
Nov 23 at 0:04
1
1
If you are using Android 8+, make sure you have library version 2.15.2 or higher (the version shows up in the logs) otherwise starting a foreground service in the background won't work. See: github.com/AltBeacon/android-beacon-library/pull/746
– davidgyoung
Nov 23 at 0:23
If you are using Android 8+, make sure you have library version 2.15.2 or higher (the version shows up in the logs) otherwise starting a foreground service in the background won't work. See: github.com/AltBeacon/android-beacon-library/pull/746
– davidgyoung
Nov 23 at 0:23
@davidgyoung Thanks for the reply. Unfortunately, not working. Lib version - 2.15.2 onCreate of application not called. I move startRangingBeaconsInRegion inside didDetermineStateForRegion callback method, but after reboot still not started.
– Yuriy Aizenberg
Nov 23 at 15:22
@davidgyoung Thanks for the reply. Unfortunately, not working. Lib version - 2.15.2 onCreate of application not called. I move startRangingBeaconsInRegion inside didDetermineStateForRegion callback method, but after reboot still not started.
– Yuriy Aizenberg
Nov 23 at 15:22
@davidgyoung So i took code from example (github.com/AltBeacon/android-beacon-library-reference/blob/…) and now scanner work after reboot! But no beacons found :( I see that didDetermineStateForRegion always has first arg = 0, it's "OUTSIDE". D/BeaconService: Currently monitoring 1 regions. I/BeaconService: stop ranging received D/BeaconService: Currently ranging 0 regions.
– Yuriy Aizenberg
Nov 23 at 16:51
@davidgyoung So i took code from example (github.com/AltBeacon/android-beacon-library-reference/blob/…) and now scanner work after reboot! But no beacons found :( I see that didDetermineStateForRegion always has first arg = 0, it's "OUTSIDE". D/BeaconService: Currently monitoring 1 regions. I/BeaconService: stop ranging received D/BeaconService: Currently ranging 0 regions.
– Yuriy Aizenberg
Nov 23 at 16:51
|
show 1 more comment
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435116%2fandroid-altbeacon-background-and-foreground-together%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435116%2fandroid-altbeacon-background-and-foreground-together%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Try moving the startRanginfBeaconsInRegion() call inside the didDetermineStateForRegion method. After restart, the region state is persisted, so you will not get a new call to didEnterRegion if you were already inside when the app last was running. The didDetermineState callback is always made on restart so it is a good place to trigger starting ranging..
– davidgyoung
Nov 22 at 17:42
1
Also, make sure the startScan() call is triggered in the Application class' onCreate method, and verify by debug lines that it gets called.
– davidgyoung
Nov 23 at 0:04
1
If you are using Android 8+, make sure you have library version 2.15.2 or higher (the version shows up in the logs) otherwise starting a foreground service in the background won't work. See: github.com/AltBeacon/android-beacon-library/pull/746
– davidgyoung
Nov 23 at 0:23
@davidgyoung Thanks for the reply. Unfortunately, not working. Lib version - 2.15.2 onCreate of application not called. I move startRangingBeaconsInRegion inside didDetermineStateForRegion callback method, but after reboot still not started.
– Yuriy Aizenberg
Nov 23 at 15:22
@davidgyoung So i took code from example (github.com/AltBeacon/android-beacon-library-reference/blob/…) and now scanner work after reboot! But no beacons found :( I see that didDetermineStateForRegion always has first arg = 0, it's "OUTSIDE". D/BeaconService: Currently monitoring 1 regions. I/BeaconService: stop ranging received D/BeaconService: Currently ranging 0 regions.
– Yuriy Aizenberg
Nov 23 at 16:51