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

前端 未结 2 1979
花落未央
花落未央 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: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

提交回复
热议问题