问题描述
我有一个应用程序在那里我编程控制蓝牙配对和解除配对。我可以连接并解除配对前配对之后。我需要做的原因是具体到我的应用程序,而不是在我的问题的范围。
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.
基本上,我做的是:
- 获取参考
IB
到IBluetooth
在this回答 - 注册一个BroadcastReceiver为
android.bluetooth.device.action.PAIRING_REQUEST
- 呼叫
ib.createBond(地址)
- 在等待的BroadcastReceiver触发
- 在转换用户PIN与<一个字节href="http://www.google.com/$c$csearch#uX1GffpyOZk/core/java/android/bluetooth/BluetoothDevice.java&q=convertPinToBytes&type=cs">convertPinToBytes()
- 呼叫
ib.setPin(地址,pinBytes)
从内部的BroadcastReceiver
- 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()
。我真的想为通知到任何一个)不会出现在所有的,或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);
这工作对我来说两款Android设备之间使用RFCOMM,但我不输入任何个人识别码
This worked for me between two Android devices using RFCOMM but I'm not entering any PINs
这篇关于如何避免或者解聘Android的蓝牙配对通知,当我在做方案的配对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!