Android beacon library oscillates between on and off with a fast pace
Android beacon library oscillates between on and off with a fast pace
I used the android beacon library to do the following action:
I switched on and off with a fast pace. On and off, on and off, and so on for 8-9 times.
However, the beacon then lost the signal for about 10 seconds and then the signal started to be received again.
Also, I tried an Android API function, "lescan", which resulted in the same situation.
Does anyone know why this happens?
MY testing device is:
Samsung S6 7.0
override fun onResume()
beaconManager = BeaconManager.getInstanceForApplication(this)
beaconManager.getBeaconParsers().add(BeaconParser().
setBeaconLayout(IBEACON_LAYOUT))
beaconManager.getBeaconParsers().add(BeaconParser().
setBeaconLayout(EDDYSTONE_UID_LAYOUT))
beaconManager.getBeaconParsers().add(BeaconParser().
setBeaconLayout(EDDYSTONE_URL_LAYOUT))
beaconManager.getBeaconParsers().add(BeaconParser().
setBeaconLayout(EDDYSTONE_TLM_LAYOUT))
beaconManager.bind(this)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
val permissions = ArrayList<String>()
if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)) permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION)
if (permissions.size != 0)
ActivityCompat.requestPermissions(this, permissions.toTypedArray(), 100)
override fun onBeaconServiceConnect()
beaconManager.addRangeNotifier beacons,region ->
Log.d("addRangeNotifier",beacons.size.toString())
try
beaconManager.startRangingBeaconsInRegion(Region("com.gigabyte.testkotlin", null, null, null))
catch (e: RemoteException)
e.printStackTrace()
override fun onPause()
super.onPause()
beaconManager.unbind(this)
I turned scanning on and off for 8-9 times in a fast pace, approximately in 10 seconds.
– Jerry Lin
Sep 4 at 1:30
1 Answer
1
It's hard to say exactly what you are witnessing without seeing exact code to reproduce, but turning scanning on and off quickly is not necessarily a problem on all devices.
By default, the Android Beacon Library uses a foreground scan period of 1100 ms and a between scan period of 0ms, so it effectively turns scanning on and off 9 times in just over 10 seconds -- similar to what you describe.
I have never noticed these symptoms in normal use of the library on Samsung devices or the Huawei P9, so something else must be triggering this behaviour in your test case.
EDIT: The posted code indicates that the activity itself is what is started and stopped rapidly, and because it binds and unbinds to the beaconManager as it starts and stops, it also starts and stops the Android service that scans for beacons. These are heavy weight data structures that are not designed to be started and stopped rapidly. Short answer: don't do this. If you really need to start and stop your activity rapidly, bind to the beaconManager outside the activity lifcycle, perhaps only one at app startup in the onCreate method of a custom Android Application class.
This's part of my Activity,try to quick Resume Pause keep 8-9 times then you will fine out
– Jerry Lin
Sep 5 at 9:05
Ok,if I do that like you said in Application onCreate what lifcycle should stop if don't stop it will power consumption, I tried this before
– Jerry Lin
Sep 6 at 1:32
You can use BackgroundPowerSaver, which will scale back scans when all activities are in the background. If you really don't want background scans at all, just set
beaconManager.setBackgroundBetweeenScanPeriod(999999999), or some other very large number so they effectively never happen.– davidgyoung
Sep 6 at 11:26
beaconManager.setBackgroundBetweeenScanPeriod(999999999)
Thanks for contributing an answer to Stack Overflow!
But avoid …
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:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
When you say "switched on and off", do you mean you turn bluetooth on and off or you turn scanning on and off? How long a period do you do this -- 8 or 9 times per second or over a longer period like 60 seconds?
– davidgyoung
Sep 2 at 17:05