在进行程序化配对时,如何避免或取消Android的蓝牙配对通知?

Joe*_*l F 43 android bluetooth

我有一个应用程序,我以编程方式控制蓝牙配对和取消配对.我可以在连接前配对,然后取消配对.我需要这样做的原因是我的申请特定而不是我的问题范围.

基本上我正在做的是:

  1. 得到一个参考ib,以IBluetooth在对象描述这个答案
  2. 注册BroadcastReceiver android.bluetooth.device.action.PAIRING_REQUEST
  3. 呼叫 ib.createBond(address)
  4. 等待BroadcastReceiver触发
  5. 使用convertPinToBytes()将用户引脚转换为字节
  6. ib.setPin(address, pinBytes)从BroadcastReceiver内部呼叫

无论如何,这种方法效果很好,除了当我进行配对时,我在状态栏中收到通知,要求用户输入PIN以完成配对.但这实际上是不必要的,因为当用户看到这个时,我的应用已经使用过setPin().我真的很喜欢这个通知要么a)根本不出现,要么b)以某种方式自动被解雇.

我意识到这可能是不可能的,但我想如果有人有创意,我会问.

小智 14

尝试先在中设置确认 PAIRING_REQUEST

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

device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true);
device.getClass().getMethod("cancelPairingUserInput").invoke(device);
Run Code Online (Sandbox Code Playgroud)

这对我使用RFCOMM的两个Android设备之间有效,但我没有输入任何PIN


Eri*_*ler 11

由于Android API 19 Google将这些方法切换为公共方法,因此不再需要Reflection.:)


小智 1

在 PAIRING_REQUEST 通知事件中执行此操作:

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

Class localClass = localBluetoothDevice.getClass();
Class[] arrayOfClass = new Class[0];
localClass.getMethod("cancelPairingUserInput", arrayOfClass).invoke(paramBluetoothDevice, null)).booleanValue();
Run Code Online (Sandbox Code Playgroud)

但你必须告诉我如何在用户不输入密码/PIN 的情况下配对远程设备?当然,您知道尝试与您的设备配对的远程设备的 PIN,但您如何将该 PIN 提供给远程设备。