如何在没有用户交互的情况下以编程方式配对蓝牙设备?

G d*_*oid 2 android bluetooth

我知道有很多这样的问题,但没有一个能够解决我的问题.我正在尝试在两个设备之间以编程方式实现蓝牙配对.我能够配对设备,但问题是它需要用户按下我不想在我的应用程序中的配对按钮.我希望它们在没有任何用户交互的情况下连接,或者如果不可能,它应该只要求一个设备按下配对按钮并且应该配对.我有一个三星的电视,在一个工作方式,当我尝试在Android手机的蓝牙与it..A对引脚连接请求,我的手机上弹出,当我打一双它被配对不会在电视上显示的任何东西他们俩都配对了.我需要在我的应用中实现这一点.我已经尝试过这段代码,但它没有取密码并连接它仍然要求输入密码.

public void setBluetoothPairingPin(BluetoothDevice device)
{
    byte[] pinBytes = convertPinToBytes("0000");
    try {
          Log.d(TAG, "Try to set the PIN");
          Method m = device.getClass().getMethod("setPin", byte[].class);
          m.invoke(device, pinBytes);
          Log.d(TAG, "Success to add the PIN.");
          try {
                device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true);
                Log.d(TAG, "Success to setPairingConfirmation.");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.e(TAG, e.getMessage());
                e.printStackTrace();
            } 
        } catch (Exception e) {
          Log.e(TAG, e.getMessage());
          e.printStackTrace();
        }
}
Run Code Online (Sandbox Code Playgroud)

我已经找了近一个星期的解决方案.实现上述目标的任何想法/建议将非常感激.

San*_*ral 5

你应该试试这个......!

private void pairDevice(BluetoothDevice device) {
            try {
                Method method = device.getClass().getMethod("createBond", (Class[]) null);
                method.invoke(device, (Object[]) null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
Run Code Online (Sandbox Code Playgroud)

mAdapter.setData(mDeviceList);
        mAdapter.setListener(new DeviceListAdapter.OnPairButtonClickListener() {            
            @Override
            public void onPairButtonClick(int position) {               
                BluetoothDevice device = mDeviceList.get(position);

                if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
                    unpairDevice(device);
                } else {
                    showToast("Pairing...");
                    pairDevice(device);//here is calling
                }
            }
        });
Run Code Online (Sandbox Code Playgroud)