如何在该区域获取蓝牙配对设备
我想获取该区域中所有设备的列表。
为此,我使用 getBondedDevices() 请求绑定设备,然后进行发现。但这给了我所有绑定设备(在该区域或不在该区域)和可发现的设备的列表。如果我只是发现(不使用 getBondedDevices()),我不会获得该区域的绑定设备。
因此,我想获取包含绑定设备(但仅限于该区域中的设备)和可发现设备的列表。
感谢您的帮助
I want to get a list of all the devices in the area.
For that i'm requesting the bonded devices with getBondedDevices() and then making a discovery. But this gives me the list of all bonded devices (in the area or not) and the discoverable ones. And if I just make the discovery (without using getBondedDevices()) I don't get the bonded devices in the area.
So I want to get a list with the bonded devices (but only those in the area) and the discoverable devices.
Thanks for the help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我解决这个问题的方法是尝试连接到设备。只要相关设备实际上正在侦听连接,它似乎就可以工作。如果您不知道您正在使用什么类型的设备,则必须扫描它提供的可用服务。这有点奇怪,因为似乎只能使用反射来调用 getUuids。请参阅下面的代码。
The way I solved this was to try and connect to the device. It seems to work as long as the device in question is actually listening for a connection. If you don't know what type of device you're dealing with you have to scan for the available services it provides. This is a bit weird since it seems you can only call the getUuids by using reflection. See code below.
没有直接的 API,您可以比较 discovery 和 getBondedDevices 返回的常见设备,以查找附近且可见/可发现的绑定设备。
There are no direct API for that, you can compare the common devices which and returned by discovery and getBondedDevices to find bonded devices in vicinity and that are visible/discoverable.
我拥有的蓝牙设备一旦绑定就无法被发现,这就是为什么发现不会列出它们。这是设备的本质(如果您已经在使用该设备,其他人就无法使用该设备),而不是 Android API 的限制。
因此,获取该区域中设备的完整列表的唯一方法可能是尝试打开与每个绑定设备的连接以查看它是否在该区域中,并将该子集与可发现列表合并。这是一种间接的方式,但我找不到其他方式。
我认为其他软件处理此问题的方式是将绑定列表和可发现列表分开,并让用户确定他们关心哪些绑定设备(通过个人知识已知在范围内)。
The Bluetooth devices I have aren't discoverable once they're bonded, which is why the discovery doesn't list them. This is the nature of the devices (no one else can use a device if you're already using it), not a limitation of the Android API.
So probably the only way to get a complete list of devices in the area is to try opening a connection to each bonded device to see if it's in the area, and merge that subset with the discoverable list. It's an indirect way, but I couldn't find another way.
I think the way other software deals with this is to keep the bonded and discoverable lists separate and make the user determine which bonded devices they care about (known to be in range through personal knowledge).