当它们在范围内时自动查找已配对的蓝牙设备 [英] Find already paired bluetooth devices automatically, when they are in range

查看:47
本文介绍了当它们在范围内时自动查找已配对的蓝牙设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是蓝牙专家,想知道有哪些方法可以在已配对的蓝牙设备彼此相距范围内时自动找到它们.

I am no Bluetooth specialist and wondering what possibilities are available to find already paired Bluetooth devices automatically when they are range of each other.

背景:在我们的例子中,Android 应用程序需要通过蓝牙 (Rfcomm) 连接到专用配件.两个设备彼此已知(它们已配对).Android 应用程序注册一个广播接收器.在应用程序启动期间,应用程序会启动发现以找到专用配件.如果配件在范围内,则一切正常.

Background: In our case an Android application needs to connect to a dedicated accessory via Bluetooth (Rfcomm). Both devices are known to each other (they are paired). The Android application registers a broadcast receiver. During the startup of the application, the app initiates a discovery to find the dedicated accessory. If the accessory is in range everything works great.

问题:用户在专用附件的范围之外启动应用程序.Android 应用程序尝试发现附件但没有成功.然后用户进入蓝牙配件的范围.广播接收器不会收到有关现在在范围内的附件的通知.

Problem: The user starts the application outside the range of the dedicated accessory. The Android application tries to discover the accessory without success. Then the user goes into the range of the Bluetooth accessory. The broadcast receiver won’t get notified about the accessory that is in range now.

相似的主题/可能的解决方案在 stackoverflow 上已经提出了类似的问题(例如 在范围内时自动连接到蓝牙设备).但是不断尝试发现范围内的蓝牙设备并不是我想要的,因为这会导致 Android 设备的电池消耗过多.

Similar Thread / Possible Solutions Similar questions were already asked on stackoverflow (e.g. autoconnect to bluetooth device when in range). But continuously trying to discover Bluetooth devices in range isn't what I am looking for because this would cause too much battery drain of the Android device.

另一种解决方案是尝试在 Activity 的 onResume 方法中连接到配对设备.这可行,但缺点是应用程序无法在后台运行.因此,用户必须至少将应用程序带到前台一次才能启动连接.

Another solution would be to try to connect to the paired device in the onResume method of the Activity. This would work but has the disadvantage that the application can’t run in the background. So the user had to bring the application at least once to the foreground to initiate the connection.

我想到的第三个想法是在 Android 应用程序中也实现一个服务器套接字.当 Android 应用程序启动并且发现没有成功完成时,Android 应用程序可以创建服务器套接字并侦听附件的传入通知.这在某些情况下会有所帮助(例如,用户启动他的应用程序、接近附件、激活附件并且附件在启动时通知应用程序它现在在范围内).但这仍然不是 100% 的解决方案,因为两个设备都可以在彼此的范围之外启动.此外,还必须实现附加功能(Android 设备中的蓝牙服务器套接字……).

A third idea I thought about is to implement a server socket into the Android application too. When the android application is started and the discovery finished without success, the Android application could create server socket and to listen to incoming notifications of the accessory. This would help in some scenarios (e.g. the user starts his application, approaches the accessory, activates the accessory and the accessory notifies the application on startup that it is in range now). But this is still no 100% solution because both devices can start outside the range of each other. Also it would be mandatory to implement additional functionality (Bluetooth server socket in the Android device…).

所以我想知道是否存在更好的解决方案.我正在寻找一种不需要额外 ServerSockets 的解决方案,并且我总是收到通知,表明两个已经配对的设备在彼此的范围内:-)

So I am wondering if better solutions exist. I am looking for a solution where no additional ServerSockets are required and I always get the notification that the two already paired devices are in range of each other :-)

感谢您的帮助!

推荐答案

第一次连接设备后,将mac地址保存在本地列表中.断开连接时,使用 connectGatt 并将 autoconnect 设置为 true 以在您处于范围内时自动重新连接.

After connecting the device for the first time, keep the mac address in a local list. On disconnect, use connectGatt with autoconnect set to true to automatically re-connect when you are in range.

这篇关于当它们在范围内时自动查找已配对的蓝牙设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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