我正在尝试通过蓝牙连接两个设备。我已经能够做到,但是当连接开始时,操作系统要求我提供配对代码。
我想做的是以编程方式提供该代码。有没有办法在不要求用户插入的情况下连接这些设备并发送配对代码?
注意:我确实有配对代码,我只是不希望用户插入它,而是应用程序将从它的保存位置获取它并使用它。
注意_2:必须使用配对码。因此,连接 createInsecureRfcommSocketToServiceRecord() 或不使用配对代码的类似方法不是一种选择。
最佳答案
通过反射调用隐藏方法“setPin(byte[])”是解决方案。我分享了代码。
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());
}
}
此外,这个答案有更多细节。 Android bluetooth setpin function
关于java - 如何通过蓝牙连接两个设备通过参数发送配对码? JAVA-Android,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18109479/