在没有任何密钥弹出窗口的情况下配对两个android蓝牙设备

在没有任何密钥弹出窗口的情况下配对两个android蓝牙设备,android,bluetooth,Android,Bluetooth,我想配对两个android蓝牙设备(Kitkat),而不需要弹出任何密钥交换窗口。我在广播接收器中尝试了setpin()和cancelPairingUserInput()方法,以便使用反射来配对请求意图,但没有得到任何结果。有人能帮我吗 if(BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)){ BluetoothDevice localBluetoothDevice = (BluetoothDevice

我想配对两个android蓝牙设备(Kitkat),而不需要弹出任何密钥交换窗口。我在广播接收器中尝试了setpin()和cancelPairingUserInput()方法,以便使用反射来配对请求意图,但没有得到任何结果。有人能帮我吗

 if(BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)){

               BluetoothDevice localBluetoothDevice = (BluetoothDevice)intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
               try {

                   Log.d("setPin()", "Try to set the PIN");
                   Method m = localBluetoothDevice.getClass().getMethod("setPin", byte[].class);
                   m.invoke(localBluetoothDevice, ByteBuffer.allocate(4).putInt(1234).array());
                   Log.d("setPin()", "Success to add the PIN.");
                 } catch (Exception e) {
                   Log.e("setPin()", e.getMessage());
                 }
               Class localClass = localBluetoothDevice.getClass();
               Class[] arrayOfClass = new Class[0];
               try {
                   localClass.getMethod("setPairingConfirmation", boolean.class).invoke(localBluetoothDevice, true);
                localClass.getMethod("cancelPairingUserInput", arrayOfClass).invoke(localBluetoothDevice, null);
            } catch (IllegalAccessException | IllegalArgumentException
                    | InvocationTargetException | NoSuchMethodException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           }

您是否尝试通过反射调用createBond()

这对我来说很有效,设备是蓝牙设备:

Method m = device.getClass().getMethod("createBond", (Class[]) null);
m.invoke(device, (Object[]) null);
  • 从指定的目的处获取设备和PIN(或配对密钥)
  • 如果给定的引脚不是-1,请在设备中设置它
  • 调用设备的.setPairingConfirmation()方法
  • 我在.bluetoothEventReceived()回调方法中的代码(实现此目的)如下所示:

    private void bluetoothEventRecieved(Context context, Intent intent)
    {
        if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            int pin = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_KEY, -1);
            if (pin != -1) {
                byte[] pinBytes = String.format(Locale.US, "%04d", pin).getBytes();
                device.setPin(pinBytes);
            }
            device.setPairingConfirmation(true);
        }
    }
    

    添加一些到目前为止您已经尝试过的代码。@Ali:添加的代码段没有任何人回答..:(你找到解决办法了吗?我也有同样的问题…)(!!!!