I want to pair a BluetoothDevice programmatically without user interaction. So I tried the setPin() and the setPasskey() methods, but both methods returned false always. For this problem I have taken help from here.
The code that I used:
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());
}
}
What am I doing wrong? Is there another way to pair with a BluetoothDevice without the need for user interaction?