如何通过蓝牙通过参数发送配对码来连接两个设备?JAVA-Android [英] Howt to connect two devices via bluetooth sending the pairng code by parameter? JAVA-Android
问题描述
我正在尝试通过蓝牙连接两台设备.我已经能够做到,但是当连接开始时,操作系统要求我提供配对代码.
I'm trying to connect via bluetooth two devices. I've been able to do it, but when the connections starts the OS ask me to provide the pairing code.
我想做的是以编程方式提供该代码.有没有办法在不要求用户插入的情况下连接这些设备并发送配对代码?
What I want to do is give that code programatically. Is there a way to connect those devices and send that pairing code without asking the user to insert it?
注意:我有配对代码,我只是不想让用户插入它,相反,应用程序会从它的保存位置获取并使用它.
Note: I do have the pairing code, I just don't want the user to insert it, instead the app will take it from where it is saved and use it.
注意_2:必须使用配对码.因此,连接 createInsecureRfcommSocketToServiceRecord() 或不使用配对代码的类似方法不是一种选择.
Note_2: The pairing code must be used. So, connecting with createInsecureRfcommSocketToServiceRecord() or something like it that does not use the Pairing Code is not an option.
推荐答案
通过反射调用隐藏方法setPin(byte[])"是解决方案.我分享代码.
Calling by reflection the hidden method "setPin(byte[])" was the solution.I share the code.
private void PairDevice(BluetoothDevice pDevice, String pin)
{
try
{
Log.d("pairDevice()", "Start Pairing...");
Method pairMethod = pDevice.getClass().getMethod("setPin", byte[].class);
Boolean lReturn = (Boolean) pairMethod.invoke(pDevice, pin.getBytes("UTF8"));
if(lReturn.booleanValue())
{
Log.d("pairDevice()", "Pairing Finished...");
Method bondMethod = pDevice.getClass().getMethod("createBond");
bondMethod.invoke(pDevice);
}
}
catch(Exception ex)
{
Log.e("pairDevice()", ex.getMessage());
}
}
另外,这个答案有更多细节.安卓蓝牙设置功能
Also, this answer with more details. Android bluetooth setpin function
这篇关于如何通过蓝牙通过参数发送配对码来连接两个设备?JAVA-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!