简体   繁体   English

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

[英]Is there a way for automatic/programatic pairing of 2 bluetooth devices in android?

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. 例如,它可以存储和使用相同的PIN配对请求,我只是不知道如何在我的applciation中以编程方式管理此请求,如何在收到配对请求时自动设置和发送PIN以及如何发起这样的请求使用预定义的PIN码。

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? 就像我们说这些设备的MAC地址列表一样,我可以使用它们与这些设备生成绑定,以便它们在我的设备上显示配对?

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. 你不能真的这样做,我强烈建议不要试图破坏它。

Reflecting the setPin method allowed me to send the pin automatically to the other device. 反映setPin方法允许我自动将引脚发送到其他设备。 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. 虽然我无法摆脱对话框,它只是停留在屏幕上,我不知道如何关闭它(以编程方式)并继续绑定过程,因为此对话框是从connect()内部调用的,这是一种阻塞方法。 I am not giving up on it yet though :) 虽然我还没有放弃它:)

With Bluetooth version 2.1 and above there is a method of pairing called the 'just works' association model. 对于蓝牙版本2.1及更高版本,有一种称为“正常工作”关联模型的配对方法。 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. 但是,这将提供安全的加密链接,而无需交换密码或验证设备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: 不同平台上的API可能不同,但基础HCI消息要求您指明您的设备(或其中一个设备)具有以下IO功能:

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. 如果您可以找到配置它的API,那么安全简单配对的“正常工作”关联模式将用于配对。

The next step is to store the link keys eg bonding. 下一步是存储链接键,例如键合。 Many devices eg 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. 许多设备(例如移动电话)仍将为用户创建一个对话框,询问他们是否想要“记住此设备”,因为用户授权是由BT规范指定的......但这是另一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM