对于Android,我已经写了一些代码与服务,执行后,服务寻找我的BT设备,并调用device.createRfcommSocketToServiceRecord为它找到的配对设备(现在,只有一个)。这很好用。
为了测试的目的,当服务运行时,我手动解除配对,然后再次配对。当我这样做时,我得到了配对和连接,但随后我收到了ACTION_ACL_DISCONNECTED。
简而言之,是这样的。
当我配对时,我得到以下信息
I/MainService﹕ Entering Service bcrcvrBluetooth bcReceiver: Action = android.bluetooth.device.action.BOND_STATE_CHANGED
I/MainService﹕ Service bcrcvrBluetooth: action = BluetoothDevice.ACTION_BOND_STATE_CHANGED
I/MainService﹕ Prev State: BluetoothDevice.BOND_NONE
I/MainService﹕ New State: BluetoothDevice.BOND_BONDING
然后是输入引脚的时间 之后,我看到以下内容。
I/MainService﹕ Entering Service bcrcvrBluetooth bcReceiver: Action = android.bluetooth.device.action.ACL_CONNECTED
I/MainService﹕ Service bcrcvrBluetooth: action = BluetoothDevice.ACTION_ACL_CONNECTED
I/BTDevice﹕ Creating socket
I/BTDevice﹕ Device socket created
I/MainService﹕ Entering Service bcrcvrBluetooth bcReceiver: Action = android.bluetooth.device.action.BOND_STATE_CHANGED
I/MainService﹕ Service bcrcvrBluetooth: action = BluetoothDevice.ACTION_BOND_STATE_CHANGED
I/MainService﹕ Prev State: BluetoothDevice.BOND_BONDING
I/MainService﹕ New State: BluetoothDevice.BOND_BONDED
I/MainService﹕ Device Paired.
I/MainService﹕ Entering Service bcrcvrBluetooth bcReceiver: Action = android.bluetooth.device.action.ACL_DISCONNECTED
I/MainService﹕ Service bcrcvrBluetooth: action = BluetoothDevice.ACTION_ACL_DISCONNECTED
理想情况下,我可以加入一些代码来帮助你,但是,这些代码分布在多个类,线程,等等... ... 简而言之,一切看起来都很好,直到我因为一些未知的原因收到ACL_DISCONNECTED。这让我的代码陷入了处理一个已经被移除的设备的情况。如果我能缩小搜索范围,我很乐意发布一些代码。
为什么我在配对后会收到ACL_DISCONNECTED?
EDIT:
对于其他可能会犯我同样愚蠢错误的人来说. 事实证明,在我收到ACTION_BOND_STATE_CHANGED与BOND_BONDED之前,我正在尝试创建BT插座。这似乎导致了一个ACTION_ACL_DISCONNECTED。我本以为在这种情况下会出现错误,但我想没有。问题解决了
还有一个评论。你所描述的不仅对RFCOMM有效,而且对A2DP也有效。注意,方法createBond()并不总是使债券和配置文件连接(它取决于硬件,我已经测试了不同)
所以,正确的方法是调用createBond(),等待BOND_BONDED事件,然后与A2DP代理进行连接。在A2DP_CONNECTED事件发生后,conction就可靠了。