且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何增加Android中BLE设备的扫描周期?

更新时间:2023-01-24 08:14:22

重要的是要了解Android不支持本地iBeacon。 Android leScanCallback 方法根本不等同于iOS didRangeBeacons 方法。

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.

leScanCallback 方法只是在您每次看到来自蓝牙设备的广告包(带有仅在第一次看到其Mac地址之前,便在广告中设置的可连接位被给予回调,直到您停止并重新开始扫描为止。除非您停止并重新启动计时器扫描,否则没有扫描时间,并且在数据包到达时会收到回调。这可能是一秒钟很多次。

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.

编写开放源代码时 Android iBeacon库,我必须从头开始构建所有功能,以进行 didRangeBeaconsInRegion 回调,该回调与iOS中的等效。为此,该库将停止并重新开始大约每秒扫描一次,并缓冲该周期中看到的所有iBeacon列表,仅在该周期结束时调用带有可见iBeacon列表的回调。

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.