1

我想在没有用户交互的情况下以编程方式配对蓝牙设备。所以我尝试了setPin()setPasskey()方法,但是这两种方法总是返回 false。对于这个问题,我从这里得到了帮助。

我使用的代码:

private void PairDevice(BluetoothDevice pDevice, String pin)
{
    try
    {   
        Log.d("pairDevice()", "Start Pairing...");

        Method pairMethod = pDevice.getClass().getMethod("setPin", new Class[] {byte[].class});
        Boolean lReturn = (Boolean) pairMethod.invoke(pDevice, pin.getBytes("UTF8"));
        Log.e("pairDevice",""+lReturn);

        if(lReturn.booleanValue())
        {
            Log.d("pairDevice()", "Pairing Finished...");

            Method bondMethod = pDevice.getClass().getMethod("createBond");
            bondMethod.invoke(pDevice);
            Log.d("pairDevice()", "createBond Finished...");
        }           
    }
    catch(Exception ex)
    {
        Log.e("pairDevice()", ex.getMessage());
    }
} 

我究竟做错了什么?还有另一种无需用户交互即可与蓝牙设备配对的方法吗?

4

0 回答 0