简体   繁体   English

如何通过蓝牙连接两个设备按参数发送配对代码? 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. 注意:我确实有配对代码,我只是不希望用户插入它,而应用程序将从保存的地方获取它并使用它。

Note_2: The pairing code must be used. 注意2:必须使用配对码。 So, connecting with createInsecureRfcommSocketToServiceRecord() or something like it that does not use the Pairing Code is not an option. 因此,使用createInsecureRfcommSocketToServiceRecord()或不使用配对代码的类似连接不是一种选择。

Calling by reflection the hidden method "setPin(byte[])" was the solution.I share the code. 通过反射调用隐藏的方法“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());
    }
}   

Also, this answer with more details. 此外,这个答案有更多细节。 Android bluetooth setpin function Android蓝牙设置功能

I think this link will help you find what you need. 我认为此链接将帮助您找到所需内容。

I was thinking more of the "Pairing is automatically performed when you initiate an encrypted connection with the Bluetooth APIs." 我更多地考虑“当您使用蓝牙API启动加密连接时自动执行配对”。 part of the link. 部分链接。 I haven't tried it but was thinking that pairing automatically means no user input. 我没有尝试过,但认为自动配对意味着没有用户输入。

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

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