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.

Nick L Scott
  • 712
  • 1
  • 8
  • 21

2 Answers2

1

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

Community
  • 1
  • 1
Nick L Scott
  • 712
  • 1
  • 8
  • 21
0

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." part of the link. I haven't tried it but was thinking that pairing automatically means no user input.

Ithamar Diaz
  • 114
  • 5
  • I read that before to create the connection feature, but it uses the "ask the user the pairing code" way. – Nick L Scott Aug 07 '13 at 17:41
  • I added a comment as to why I linked that particular page. – Ithamar Diaz Aug 07 '13 at 19:51
  • Actually I have to use the pairing code. Let me give some background. When the app tries to connect, it will launch a QRCode reader, the QRCode that will be read represents the pairing code. The connection must be created with that pairing code. – Nick L Scott Aug 07 '13 at 20:14