自动查找已配对的蓝牙设备,当他们在范围蓝牙、范围、设备

2023-09-05 08:40:59 作者:屿

我没有蓝牙专家,想知道什么可行的办法时,他们是彼此的范围内自动查找已配对的蓝牙设备。

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.

类似的主题/可能的解决方法 类似的问题已经问计算器上(例如:autoconnect蓝牙设备的范围时)。 但是,不断试图发现在范围内的蓝牙设备是不是我所期待的,因为这将导致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.

另一种解决办法是将尝试连接到配对的设备在活动的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 :-)

感谢您的帮助!

推荐答案

不是一个完整的解决方案,但也许这是足以让你的应用程序,以轮询accesory的presence每当画面是否已经开启?在这种情况下,这可能会有所帮助:唤醒/休眠启动活动在Android的

Not a full solution, but maybe it's sufficient for your app to poll the accesory's presence whenever the screen is turned on? In that case, this may be helpful: Start Activity on wake up/sleep in Android