在没有配对的情况下在android中指定链接密钥

C. *_*tin 3 android bluetooth

我试图确定在android中是否有办法将特定链接密钥与已经存在的远程蓝牙设备实例相关联.

基本上我想要做的是创建与不可发现的蓝牙设备的连接,而无需通过配对或重新配对程序.

我无法在标准配对过程中与设备建立链接密钥,因为我正在使用自定义专有配对机制.我更愿意在不使用本机代码的情况下完成此任务,但如果必须,我会这样做.

hem*_*ire 16

我需要自己解决这个问题.关键的一步是找到这个代码,它告诉我将android.bluetooth包添加到我的项目中,并添加文件IBluetooth.aidl和IBluetoothCallback.aidl(你可以在链接中找到).

实例化IBluetooth对象后,您可以访问BluetoothService类,并可以使用IBluetooth.aidl中的任何方法.我感兴趣的方法是

setPin(String address, byte[] pin)
Run Code Online (Sandbox Code Playgroud)

使用它的问题是其他蓝牙代码期望已经调用配对对话框,并在BluetoothEventLoop类的HashMap中跟踪它.如果您尝试在未启动配对请求的情况下调用setPin(),您将看到如下错误:

setPin(<address>) called but no native data available, ignoring. Maybe the PasskeyAgent Request was cancelled by the remote device or by bluez.
Run Code Online (Sandbox Code Playgroud)

所以我的解决方法(使用Chat示例)启动连接线程以启动配对请求,然后休眠500毫秒以确保线程已启动,然后调用setPin().

  • 我能够在不创建助手的情况下完成此操作,我使用了byte [] ar = new byte [] {0x31,0x32,0x33,0x34};方法m2 = btDevice.getClass().getMethod("setPin",new类[] {Array.newInstance(byte.class,4).getClass()}); m2.invoke(btDevice,ar); (5认同)