在进行程序化配对时,如何避免或关闭 Android 的蓝牙配对通知? [英] How can I avoid or dismiss Android's Bluetooth pairing notification when I am doing programmatic pairing?
问题描述
我有一个应用程序,我可以在其中以编程方式控制蓝牙配对和取消配对.我可以在连接前配对并在连接后取消配对.我需要这样做的原因特定于我的应用程序,而不是我的问题范围.
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.
基本上我正在做的是:
- 获取对
IBluetooth
对象的引用ib
,如这个答案 - 为
android.bluetooth.device.action.PAIRING_REQUEST
注册一个广播接收器 - 调用
ib.createBond(address)
- 等待 BroadcastReceiver 触发
- 使用 convertPinToBytes()
- 从 BroadcastReceiver 中调用
ib.setPin(address, pinBytes)
- Get a reference
ib
toIBluetooth
object as described in this answer - Register a BroadcastReceiver for
android.bluetooth.device.action.PAIRING_REQUEST
- Call
ib.createBond(address)
- Wait for BroadcastReceiver to trigger
- Convert user pin into bytes with convertPinToBytes()
- Call
ib.setPin(address, pinBytes)
from within BroadcastReceiver
无论如何,这种方法非常有效,除了当我进行配对时,我会在状态栏中收到一条通知,要求用户输入 PIN 以完成配对.但这实际上是不必要的,因为当用户看到这个时,我的应用程序已经使用了 setPin()
.我真的希望该通知 a) 根本不出现,或者 b) 以某种方式自动关闭.
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.
推荐答案
尝试在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 码
This worked for me between two Android devices using RFCOMM but I'm not entering any PINs
这篇关于在进行程序化配对时,如何避免或关闭 Android 的蓝牙配对通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!