有没有办法在 Android 中自动/编程配对 2 个蓝牙设备?

发布于 2024-12-16 15:34:28 字数 344 浏览 1 评论 0原文

我正在开发一个通过蓝牙连接设备并交换消息的应用程序。它适用于配对的设备,但我也希望它能够自动配对未配对的设备。例如,它可以存储和使用相同的 PIN 来进行配对请求,我只是不知道如何在我的应用程序中以编程方式管理此请求,如何在收到配对请求时自动设置和发送 PIN 以及如何发起此类请求使用预定义的 PIN 码。

任何片段或想法将不胜感激! :)

编辑: 我知道它有风险,我正在为紧急情况开发此应用程序,仅在除了蓝牙之外没有其他方法可用的情况下。是否还有一种方法可以在不连接设备的情况下提前与设备配对?比如说,有这些设备的 MAC 地址列表,我可以使用它们来生成与该设备的绑定,以便它们在我的设备上显示为配对?

I am developing an application which connects devices over bluetooth and exchanges messages. It works fine for paired devices, but I would also like it to automatically pair devices that are not paired. Like for example it could store and use the same PIN for pairing requests, I just dont know how to manage this request programatically in my applciation, how to automatically set and send the PIN when you get a pairing request and how to initiate such a request with the predefined PIN code.

Any snippets or thoughts would be highly appreciated! :)

EDIT:
I know its risky, I am developing this app for emergency situations only where no other means but bluetooth is available. Also is there maybe a way of premature pairing with devices without even connecting to them? Like lets say there is a list of MAC Addresses of those devices and I can use them to generate a bond with that devices so that they appear paired on my device?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓬勃野心 2024-12-23 15:34:28

你不能这样做。做你想做的事会带来巨大的安全风险。想想看,我的设备只是在您的蓝牙范围内的任何地方,现在我可以在您不知情的情况下向您发送我想要的任何内容?你不能真正做到这一点,我强烈建议不要尝试颠覆它。

You can't do this. To do what you want to do would create a huge security risk. Think about it, my device just comes anywhere in bluetooth range of yours and now I can send you anything I want without you knowing? You can't really do this and I highly recommend not trying to subvert it.

蓝眸 2024-12-23 15:34:28

反映 setPin 方法允许我自动将 pin 发送到其他设备。我必须在侦听配对请求的广播接收器中实现它。虽然我无法摆脱该对话框,但它只是卡在屏幕上,而且我不知道如何关闭它(以编程方式)并继续绑定过程,因为该对话框是从 connect() 内部调用的,这是一种阻塞方法。不过我还没有放弃:)

Reflecting the setPin method allowed me to send the pin automatically to the other device. I had to implement it in a broadcast receiver that is listening for pairing requests. Although I cant get rid of the dialog it just stucks there on the screen and I dont know how to close it (programatically) and continue the bonding procedure since this dialog is called from inside connect() which is a blocking method. I am not giving up on it yet though :)

蝶舞 2024-12-23 15:34:28

对于蓝牙 2.1 及更高版本,有一种称为“正常工作”关联模型的配对方法。这是安全性最低的配对方法,并且无法防止中间人攻击。

然而,这将提供安全、加密的链接,无需交换 PIN 码或验证设备 ID。

不同平台上的 API 可能有所不同,但底层 HCI 消息要求您指示您的设备(或其中一个设备)具有以下 IO 功能:

No Keyboard, No Display. 

正如您可能已经猜到的,这是一种非常适合
使用蓝牙的简单设备,例如扬声器或耳机。

如果您可以找到进行配置的 API,则将使用安全简单配对的“正常工作”关联模式进行配对。

下一步是存储链接密钥,例如绑定。许多设备(例如移动电话)仍会向用户创建一个对话框,询问他们是否要“记住此设备”,因为用户授权是由 BT 规范指定的……但这是另一个问题。

With Bluetooth version 2.1 and above there is a method of pairing called the 'just works' association model. This is the lowest security method of pairing and has no protection against man-in-the-middle attacks.

However, this will provide a secure, encrypted link without the need to exchange pin numbers or verify device ID.

The API on different platforms may differ but the underlying HCI messages require that you indicate that your device (or one of the devices) has the following IO capabilities:

No Keyboard, No Display. 

As you might have guessed, this is a mode for very
simple devices that use Bluetooth, such as a speaker or headphones.

If you can find the API to configure that, then the 'just works' association mode of Secure Simple Pairing will be used for pairing.

The next step is to store the link keys e.g. bonding. Many devices e.g. mobile phones, will still create a dialogue box to the user to ask if they want to 'remember this device', as user authorisation is specified by the BT specifications... but that's another problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文