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

*爱你&永不变心* 提交于 2019-12-03 07:32:36

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!