Howt to connect two devices via bluetooth sending the pairng code by parameter? JAVA-Android

前端 未结 2 1980
花落未央
花落未央 2020-12-18 09:23

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

相关标签:
2条回答
  • 2020-12-18 09:48

    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.

    0 讨论(0)
  • 2020-12-18 09:50

    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

    0 讨论(0)
提交回复
热议问题