如何在 Android 上定期扫描蓝牙设备
嗨,这可能听起来是一个愚蠢的问题。但我无法找到任何答案,因此在这里发帖。
我正在构建一个室内应用程序,它连续扫描位于商场或图书馆等地方不同位置的蓝牙适配器。当我手里拿着 Android 手机在商场里移动时,我应该能够找到我可以连接的最近的适配器(愚蠢的想法,但我想用这个做点别的事情)。为此,我应该能够连续扫描蓝牙设备。
请有人告诉我如何让 android 定期扫描可用的蓝牙设备。
Hi this may sound as a stupid question.But I was unable to find any answers for this, thus posting here.
I am building an indoor application which continuously scans the bluetooth dongles located at different locations in a place like a mall or library.As I move in the mall with android phone in my hand I should be able to get the nearest dongle which I can connect to(Stupid Idea but I want to do something else with this).For this I should be able to continuously scan for the bluetooth devices.
Please can someone tell me how do I make android scan the available Bluetooth devices periodically.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想这很简单,但以前我并没有想到。答案如下,
因此我们应该在
ACTION_DISCOVERY_FINISHED
上再次开始发现,它将每 12 秒连续扫描一次设备。I guess this was so simple but didnt strike me before. Here is the answer,
Thus we should start discovery again on
ACTION_DISCOVERY_FINISHED
which will continuously scan for devices every 12 seconds.这是获取所有活动蓝牙设备[配对或未配对]列表的代码
我从API演示中得到了这个想法,我希望这对所有开发人员都有用。
Here is the Code to get the list of all Active Bluetooth Devices[Paired or non-paired]
I get the idea from API demo, i hope this could be useful for all developers.
正如文档中所述,设备发现是一个高级过程,会直接降低性能您与其他设备之间的任何联系的表现。
考虑到这一点(省略错误处理):
As stated in the documentation device discovery is a lofty process that will directly degrade the performance of any bonds you have with other devices.
With this in mind (error handling omitted):
您的代码片段应该可以工作 - 只是它在功耗方面会很昂贵,并且肯定会在几个小时内杀死您的设备/手机。
startDiscovery() 执行一个 2 步过程,通过查询找到设备,然后进行名称发现,即分页并连接到设备,
不幸的是 Android 没有提供任何 API 来控制此操作并且仅执行查询,也有一个大多数蓝牙芯片组定义并支持定期查询功能,Android 目前没有任何 API 可以进行定期查询,这可能更适合您的使用案例。
Your code snippet should work - Only that it will be expensive in terms of power draw and will certainly kill your device/phone in a few hours.
The startDiscovery() does a 2 step process, finding the device by inquiring for it and followed by a name discovery ie paging and connecting to the device,
Unfortunately Android does not provide any API to control this and do only inquiry , also there is a periodic inquiry feature defined and supported by most Bluetooth chipsets, Android currently does not have any API to do periodic inquiry which could have been more ideal for your use case.