4

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.

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.

4

2 回答 2

1

通过反射调用隐藏方法“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());
    }
}   

此外,这个答案有更多细节。安卓蓝牙设置功能

于 2013-08-08T18:49:22.903 回答
0

我认为此链接将帮助您找到所需的内容。

我想更多的是“当您启动与蓝牙 API 的加密连接时会自动执行配对”。链接的一部分。我还没有尝试过,但我认为自动配对意味着没有用户输入。

于 2013-08-07T17:22:46.067 回答