当我进行编程配对时,如何避免或忽略 Android 的蓝牙配对通知?

发布于 2024-12-03 19:22:56 字数 936 浏览 3 评论 0原文

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

基本上我正在做的是:

  1. 获取对 IBluetooth 对象的引用 ib ,如 这个答案
  2. android.bluetooth.device.action.PAIRING_REQUEST 调用注册一个
  3. BroadcastReceiver ib.createBond(address)
  4. 等待 BroadcastReceiver 触发 使用
  5. convertPinToBytes()
  6. 调用来自 BroadcastReceiver 的 ib.setPin(address, pinBytes)

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

我意识到这甚至可能是不可能的,但我想我会问,以防有人有创意。

I have an app where I am programmatically controlling Bluetooth pairing and unpairing. I can pair before connection and unpair afterwards. The reason I need to do this is specific to my application and not in the scope of my question.

Basically what I am doing is:

  1. Get a reference ib to IBluetooth object as described in this answer
  2. Register a BroadcastReceiver for android.bluetooth.device.action.PAIRING_REQUEST
  3. Call ib.createBond(address)
  4. Wait for BroadcastReceiver to trigger
  5. Convert user pin into bytes with convertPinToBytes()
  6. Call ib.setPin(address, pinBytes) from within BroadcastReceiver

Anyways, this approach works great, except for the fact that when I do the pairing, I get a notification in the Status bar requesting that the user enter a PIN to complete the pairing. But this is in fact unnecessary, because by the time the user sees this, my app has already used setPin(). I'd really like for that notification to either a) not appear at all, or b) be dismissed automatically somehow.

I realize this may not even be possible, but I thought I would ask in case someone has a creative idea.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瘫痪情歌 2024-12-10 19:22:56

尝试首先在 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);

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

Try setting the confirmation first in the 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);

This worked for me between two Android devices using RFCOMM but I'm not entering any PINs

上课铃就是安魂曲 2024-12-10 19:22:56

从 Android API 19 开始,Google 将这些方法切换为公共方法,因此不再需要反射。 :)

Since Android API 19 Google switched these Methods to public Methods, so there is no need for Reflection any more. :)

感情旳空白 2024-12-10 19:22:56

在 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();

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

Do this in the PAIRING_REQUEST notification event:

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();

But you gotta tell me how did you pair your remote device without the user to enter Passkey/PIN? off course, you know the PIN for the remote device which is trying to pair to your device but how did you provide that PIN to the remote device.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文