如何增加Android中BLE设备的扫描周期? [英] How to increase the scan period for BLE devices in Android?

查看:837
本文介绍了如何增加Android中BLE设备的扫描周期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实施信标扫描程序,并且我希望android BLE服务的行为类似于iOS的 didRangeBeacons方法,即应每秒调用一次。但是在android中没有这样的方法。但是在android中,有一个 leScanCallback方法,它以不到一秒钟的扫描时间被频繁调用。因此,有什么方法可以实现leScanCallback方法中的功能并将其扫描周期间隔增加至1秒,从而使其行为类似于iOS的 didRangeBeacons方法。

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.

*在Android中连续扫描信标并增加其扫描间隔还会是不好的编程方法吗?

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

预先感谢

推荐答案

重要的是要了解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.

这篇关于如何增加Android中BLE设备的扫描周期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆