How to increase the scan period for BLE devices in Android?
Asked Answered
W

1

8

I was trying to implement beacon scanning program, and i want the android BLE services to behave similar to iOS "didRangeBeacons" method,i.e, it should get called every one second. But in android there is no such method. But in android there is "leScanCallback" method that gets called very frequently with a scan period of less than a second. So is there any way that i can implement my functionality in leScanCallback method and increase its scan period interval to 1 second, so that it behaves similar to iOS's "didRangeBeacons" method.

*Also will it be the bad programming to continually scan beacon and increasing its scan interval in android?

Thanks in advance

Wilonah answered 12/6, 2014 at 9:25 Comment(0)
M
12

It is important to understand that there is no native iBeacon support in Android. The Android leScanCallback method is not at all an equivalent of the iOS didRangeBeacons method.

The leScanCallback method simply gives you a callback every time an advertising packet from a Bluetooth device is seen (devices with the connectable bit set in the advertisement are only given a callback the first time its Mac address is seen until you stop and restart scanning). Unless you stop and restart scanning on a timer, there is no scan period, and you get callbacks as the packets arrive. This can be many times a second.

When writing the open source Android iBeacon Library, I had to build all the functionality from scratch to make a didRangeBeaconsInRegion callback that was the equivalent to what is in iOS. To do this, the library stops and restarts scanning about once a second and buffers the list of all iBeacons seen in the cycle, only calling the callback with the list of visible iBeacons at the end of the cycle. There are lots of other complexities not discussed here.

The code is free for you to review and modify, so I encourage you to do so if you want to roll your own.

Midwest answered 12/6, 2014 at 11:52 Comment(10)
Thanks David. I was reviewing the code written by you. Its really nice work. And now I got the logic behind scanning. :)Wilonah
If we want to scan only when the application is in foreground and not in background.What will be better?Scanning continously or scanning in an interval(of say 1 sec)Overstudy
You must scan at an interval. Many Android devices have a bluetooth hardware layer that only provides only a single detection callback per detected hardware device per scan. Stopping and restarting is needed to get a second callback for the same hardware device.Midwest
If we are not scanning in the background should we use "BluetoothCrashResolver"?as the lib says "It is rare for most users but can be problematic for those with apps scanning for Bluetooth LE devices in the background".Overstudy
The same crashes cash happen for foreground only apps, yes, they are just less frequent.Midwest
@Midwest Is it possible somehow to increase this interval?Schizophyceous
Yes, the Android Beacon Library lets you configure the scan interval with with beaconManager.setForgroundScanPeriod(...) and beaconManager.setBackgroundScanPeriod(...) However, if you make this interval shorter than the 1.1 second default, you make it more likely that you will miss detections because you will increase the likelihood of stopping and restarting scanning in the middle of receiving a beacon packet, which will cause the receiver to miss it.Midwest
@Midwest If we stop and restart scanning on a timer, as there is no scan period, Is there any chances where we miss any advertising of BLE ?? or this stop and restart will happen immediately ? Please let me knowInfrared
There will be a intentional time lag of 1 second in every callback, by stopping and restarting the service. So if any advertising is done in say 100 ms, you will most probably find the advertising in 1000 ms. I hope that makes sense.Wilonah
Whenever scanning is off you might miss bluetooth packets, yes. The best strategy is to keep the scan interval at least las long as your beacon's advertising rate.Midwest

© 2022 - 2024 — McMap. All rights reserved.